Skip to content

Commit 15f1785

Browse files
authored
Remove CloseableBackgroundDispatcher (#6286)
* Remove CloseableBackgroundDispatcher * bump Kotlin coroutines
1 parent 5e739c2 commit 15f1785

File tree

9 files changed

+23
-83
lines changed

9 files changed

+23
-83
lines changed

gradle/libraries.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ kotlin-plugin-min = "1.9.0"
3030
kotlin-plugin = "2.0.20"
3131
kotlin-plugin-max = "2.0.20"
3232
kotlin-stdlib = "2.0.0"
33-
kotlinx-coroutines = "1.8.0"
33+
kotlinx-coroutines = "1.9.0"
3434
kotlinx-datetime = "0.5.0"
3535
kotlinx-serialization-runtime = "1.6.2"
3636
ksp = "2.0.20-1.0.24"

libraries/apollo-runtime/src/appleMain/kotlin/com/apollographql/apollo/internal/dispatchers.kt

Lines changed: 0 additions & 25 deletions
This file was deleted.

libraries/apollo-runtime/src/commonMain/kotlin/com/apollographql/apollo/internal/dispatchers.kt

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,3 @@ import okio.Closeable
55

66
internal expect val defaultDispatcher: CoroutineDispatcher
77

8-
/**
9-
* A coroutine dispatcher backed by a single thread that can continue to run in the background
10-
* until it is closed. Typically, to handle a WebSocket connection or batched HTTP queries.
11-
*/
12-
internal expect class CloseableSingleThreadDispatcher() : Closeable {
13-
val coroutineDispatcher: CoroutineDispatcher
14-
15-
override fun close()
16-
}
17-

libraries/apollo-runtime/src/commonMain/kotlin/com/apollographql/apollo/network/http/BatchingHttpInterceptor.kt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ import com.apollographql.apollo.exception.ApolloException
2020
import com.apollographql.apollo.exception.ApolloHttpException
2121
import com.apollographql.apollo.exception.DefaultApolloException
2222
import com.apollographql.apollo.exception.JsonDataException
23-
import com.apollographql.apollo.internal.CloseableSingleThreadDispatcher
2423
import kotlinx.coroutines.CompletableDeferred
2524
import kotlinx.coroutines.CoroutineScope
25+
import kotlinx.coroutines.Dispatchers
2626
import kotlinx.coroutines.async
2727
import kotlinx.coroutines.cancel
2828
import kotlinx.coroutines.delay
@@ -54,6 +54,8 @@ import kotlin.time.TimeSource.Monotonic.markNow
5454
* HTTP headers will be merged from all requests in the batch by keeping the ones that have the same name and value in all requests. Any
5555
* headers present in only some requests, or with different values in some requests will be dropped.
5656
*
57+
* Disposing [com.apollographql.apollo.network.http.BatchingHttpInterceptor] is not necessary but will cancel any pending request saving some bandwidth if needed.
58+
*
5759
* @param batchIntervalMillis the maximum time interval before a new batch is sent
5860
* @param maxBatchSize the maximum number of requests queued before a new batch is sent
5961
* @param exposeErrorBody configures whether to expose the error body in [ApolloHttpException].
@@ -69,8 +71,8 @@ class BatchingHttpInterceptor @JvmOverloads constructor(
6971
private val exposeErrorBody: Boolean = false,
7072
) : HttpInterceptor {
7173
private val startMark = markNow()
72-
private val dispatcher = CloseableSingleThreadDispatcher()
73-
private val scope = CoroutineScope(dispatcher.coroutineDispatcher)
74+
private val dispatcher = Dispatchers.Default.limitedParallelism(1)
75+
private val scope = CoroutineScope(dispatcher)
7476
private val mutex = Mutex()
7577
private var disposed = false
7678

@@ -231,11 +233,15 @@ class BatchingHttpInterceptor @JvmOverloads constructor(
231233
}
232234
}
233235

236+
/**
237+
* Cancels pending requests.
238+
*
239+
* Disposing [BatchingHttpInterceptor] is not necessary but allows reclaiming resources more rapidly.
240+
*/
234241
override fun dispose() {
235242
if (!disposed) {
236243
interceptorChain = null
237244
scope.cancel()
238-
dispatcher.close()
239245
disposed = true
240246
}
241247
}

libraries/apollo-runtime/src/commonMain/kotlin/com/apollographql/apollo/network/http/HttpInterceptor.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ interface HttpInterceptorChain {
1313

1414
interface HttpInterceptor {
1515
suspend fun intercept(request: HttpRequest, chain: HttpInterceptorChain): HttpResponse
16+
17+
// TODO: remove dispose
1618
fun dispose() {}
1719
}
1820

libraries/apollo-runtime/src/commonMain/kotlin/com/apollographql/apollo/network/ws/WebSocketNetworkTransport.kt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import com.apollographql.apollo.api.toApolloResponse
1010
import com.apollographql.apollo.exception.ApolloException
1111
import com.apollographql.apollo.exception.ApolloNetworkException
1212
import com.apollographql.apollo.exception.SubscriptionOperationException
13-
import com.apollographql.apollo.internal.CloseableSingleThreadDispatcher
1413
import com.apollographql.apollo.internal.DeferredJsonMerger
1514
import com.apollographql.apollo.internal.isDeferred
1615
import com.apollographql.apollo.internal.transformWhile
@@ -31,6 +30,7 @@ import com.apollographql.apollo.network.ws.internal.StopOperation
3130
import com.benasher44.uuid.Uuid
3231
import kotlinx.coroutines.CoroutineScope
3332
import kotlinx.coroutines.CoroutineStart
33+
import kotlinx.coroutines.Dispatchers
3434
import kotlinx.coroutines.Job
3535
import kotlinx.coroutines.channels.BufferOverflow
3636
import kotlinx.coroutines.channels.Channel
@@ -86,14 +86,12 @@ private constructor(
8686

8787
val subscriptionCount = mutableEvents.subscriptionCount
8888

89-
private val backgroundDispatcher = CloseableSingleThreadDispatcher()
90-
private val coroutineScope = CoroutineScope(backgroundDispatcher.coroutineDispatcher)
89+
private val backgroundDispatcher = Dispatchers.Default.limitedParallelism(1)
90+
private val coroutineScope = CoroutineScope(backgroundDispatcher)
9191

9292
init {
9393
coroutineScope.launch {
94-
backgroundDispatcher.use {
95-
supervise(this)
96-
}
94+
supervise(this)
9795
}
9896
}
9997

@@ -345,6 +343,12 @@ private constructor(
345343
.isLast(true)
346344
.build()
347345

346+
/**
347+
* Closes the WebSocket. Active subscriptions stop receiving events.
348+
*
349+
* This is an asynchronous operation. The WebSocket is closed shortly after this call.
350+
* Disposing [WebSocketNetworkTransport] is not necessary but allows reclaiming resources more rapidly.
351+
*/
348352
override fun dispose() {
349353
messages.trySend(Dispose)
350354
}

libraries/apollo-runtime/src/jsMain/kotlin/com/apollographql/apollo/internal/dispatchers.js.kt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,3 @@ import okio.Closeable
77
internal actual val defaultDispatcher: CoroutineDispatcher
88
get() = Dispatchers.Default
99

10-
// We can't use threads in JS, so just fallback to defaultDispatcher()
11-
internal actual class CloseableSingleThreadDispatcher : Closeable {
12-
actual val coroutineDispatcher: CoroutineDispatcher = Dispatchers.Default
13-
14-
actual override fun close() {
15-
}
16-
}

libraries/apollo-runtime/src/jvmCommonMain/kotlin/com/apollographql/apollo/internal/dispatchers.kt

Lines changed: 0 additions & 23 deletions
This file was deleted.

libraries/apollo-runtime/src/wasmJsMain/kotlin/com/apollographql/apollo/internal/dispatchers.wasmJs.kt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,3 @@ import okio.Closeable
77
internal actual val defaultDispatcher: CoroutineDispatcher
88
get() = Dispatchers.Default
99

10-
// We can't use threads in JS, so just fallback to defaultDispatcher()
11-
internal actual class CloseableSingleThreadDispatcher : Closeable {
12-
actual val coroutineDispatcher: CoroutineDispatcher = Dispatchers.Default
13-
14-
actual override fun close() {
15-
}
16-
}

0 commit comments

Comments
 (0)