Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WebSocketEngine(WebSocket.Factory) and fix documentation #6069

Merged
merged 2 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 28 additions & 12 deletions docs/source/advanced/experimental-websockets.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,35 @@ The above uses the default retry algorithm:
* Wait until the network is available if you configured a [NetworkMonitor](network-connectivity).
* Or use exponential backoff else.

To customize the retry logic more, use `addRetryOnErrorInterceptor`:
To customize the retry logic more, use `retryOnErrorInterceptor`:

```kotlin
val apolloClient = ApolloClient.Builder()
.subscriptionNetworkTransport(
WebSocketNetworkTransport.Builder()
.serverUrl(url)
.build()
)
.addRetryOnErrorInterceptor { exception, attempt ->
// retry logic here

// return true to retry or false to terminate the Flow
true
}
.retryOnErrorInterceptor(MyRetryOnErrorInterceptor())
.build()

class MyRetryOnErrorInterceptor : ApolloInterceptor {
object RetryException : Exception()

override fun <D : Operation.Data> intercept(request: ApolloRequest<D>, chain: ApolloInterceptorChain): Flow<ApolloResponse<D>> {
var attempt = 0
return request.ensureUniqueUuid()
martinbonnin marked this conversation as resolved.
Show resolved Hide resolved
.flatMapConcat { chain.proceed(it) }.onEach {
if (request.retryOnError == true && it.exception != null && it.exception is ApolloNetworkException) {
throw RetryException
} else {
attempt = 0
}
}.retryWhen { cause, _ ->
if (cause is RetryException) {
attempt++
delay(2.0.pow(attempt).seconds)
true
} else {
// Not a RetryException, probably a programming error, pass it through
false
}
}
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private class DefaultRetryOnErrorInterceptorImpl(private val networkMonitor: Net
}

@ApolloExperimental
internal fun <D : Operation.Data> ApolloRequest<D>.ensureUniqueUuid(): Flow<ApolloRequest<D>> {
fun <D : Operation.Data> ApolloRequest<D>.ensureUniqueUuid(): Flow<ApolloRequest<D>> {
var first = true
return flow {
if (first) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,6 @@ internal class RetryOnErrorInterceptor(private val retryWhen: suspend (ApolloExc
}
}

@ApolloExperimental
internal fun ApolloClient.Builder.retryWhen(retryWhen: suspend (ApolloException, Int) -> Boolean) = apply {
retryOnErrorInterceptor(RetryOnErrorInterceptor(retryWhen))
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.apollographql.apollo.network.websocket

import com.apollographql.apollo.annotations.ApolloExperimental
import com.apollographql.apollo.api.http.HttpHeader
import com.apollographql.apollo.exception.ApolloNetworkException
import com.apollographql.apollo.network.defaultOkHttpClientBuilder
import com.apollographql.apollo.network.websocket.WebSocket
import com.apollographql.apollo.network.websocket.WebSocketListener
import okhttp3.Headers
import okhttp3.OkHttpClient
import okhttp3.Request
Expand All @@ -16,9 +15,9 @@ import java.util.concurrent.atomic.AtomicReference
import okhttp3.WebSocket as PlatformWebSocket
import okhttp3.WebSocketListener as PlatformWebSocketListener

internal class JvmWebSocketEngine(private val okHttpClient: OkHttpClient) : WebSocketEngine {
internal class JvmWebSocketEngine(private val webSocketFactory: PlatformWebSocket.Factory) : WebSocketEngine {
override fun newWebSocket(url: String, headers: List<HttpHeader>, listener: WebSocketListener): WebSocket {
return JvmWebSocket(okHttpClient, url, headers, listener)
return JvmWebSocket(webSocketFactory, url, headers, listener)
}

override fun close() {
Expand Down Expand Up @@ -107,4 +106,7 @@ internal class JvmWebSocket(
}


actual fun WebSocketEngine(): WebSocketEngine = JvmWebSocketEngine(defaultOkHttpClientBuilder.build())
actual fun WebSocketEngine(): WebSocketEngine = JvmWebSocketEngine(defaultOkHttpClientBuilder.build())

@ApolloExperimental
fun WebSocketEngine(webSocketFactory: PlatformWebSocket.Factory): WebSocketEngine = JvmWebSocketEngine(webSocketFactory)
Loading