Skip to content

Commit beb491b

Browse files
committed
Use Dispatchers.Unconfined for Coroutines
As of Coroutines 1.2.0-alpha, Dispatchers.Unconfined is a stable API so we can leverage it in order to get better performances in our Reactive to Coroutines bridge. See gh-19975
1 parent 3387d3e commit beb491b

File tree

4 files changed

+14
-7
lines changed

4 files changed

+14
-7
lines changed

Diff for: spring-core-coroutines/src/main/kotlin/org/springframework/core/CoroutinesUtils.kt

+7-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.springframework.core
1919

2020
import kotlinx.coroutines.Deferred
21+
import kotlinx.coroutines.Dispatchers
2122
import kotlinx.coroutines.GlobalScope
2223
import kotlinx.coroutines.async
2324
import kotlinx.coroutines.reactive.awaitFirstOrNull
@@ -36,15 +37,17 @@ import kotlin.reflect.jvm.kotlinFunction
3637
* @author Sebastien Deleuze
3738
* @since 5.2
3839
*/
39-
internal fun <T: Any> deferredToMono(source: Deferred<T>) = GlobalScope.mono { source.await() }
40+
internal fun <T: Any> deferredToMono(source: Deferred<T>) =
41+
GlobalScope.mono(Dispatchers.Unconfined) { source.await() }
4042

4143
/**
4244
* Convert a [Mono] instance to a [Deferred] one.
4345
*
4446
* @author Sebastien Deleuze
4547
* @since 5.2
4648
*/
47-
internal fun <T: Any> monoToDeferred(source: Mono<T>) = GlobalScope.async { source.awaitFirstOrNull() }
49+
internal fun <T: Any> monoToDeferred(source: Mono<T>) =
50+
GlobalScope.async(Dispatchers.Unconfined) { source.awaitFirstOrNull() }
4851

4952
/**
5053
* Invoke an handler method converting suspending method to [Mono] if necessary.
@@ -55,7 +58,8 @@ internal fun <T: Any> monoToDeferred(source: Mono<T>) = GlobalScope.async { sour
5558
internal fun invokeHandlerMethod(method: Method, bean: Any, vararg args: Any?): Any? {
5659
val function = method.kotlinFunction!!
5760
return if (function.isSuspend) {
58-
GlobalScope.mono { function.callSuspend(bean, *args.sliceArray(0..(args.size-2)))
61+
GlobalScope.mono(Dispatchers.Unconfined) {
62+
function.callSuspend(bean, *args.sliceArray(0..(args.size-2)))
5963
.let { if (it == Unit) null else it} }
6064
.onErrorMap(InvocationTargetException::class) { it.targetException }
6165
}

Diff for: spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/client/WebClientExtensions.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.web.reactive.function.client
1818

19+
import kotlinx.coroutines.Dispatchers
1920
import kotlinx.coroutines.GlobalScope
2021
import kotlinx.coroutines.reactive.awaitSingle
2122
import kotlinx.coroutines.reactor.mono
@@ -77,7 +78,7 @@ suspend fun WebClient.RequestHeadersSpec<out WebClient.RequestHeadersSpec<*>>.aw
7778
* @since 5.2
7879
*/
7980
inline fun <reified T: Any> WebClient.RequestBodySpec.body(crossinline supplier: suspend () -> T)
80-
= body(GlobalScope.mono { supplier.invoke() })
81+
= body(GlobalScope.mono(Dispatchers.Unconfined) { supplier.invoke() })
8182

8283
/**
8384
* Coroutines variant of [WebClient.ResponseSpec.bodyToMono].

Diff for: spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/CoRouterFunctionDsl.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.web.reactive.function.server
1818

19+
import kotlinx.coroutines.Dispatchers
1920
import kotlinx.coroutines.GlobalScope
2021
import kotlinx.coroutines.reactor.mono
2122
import org.springframework.core.io.Resource
@@ -389,7 +390,7 @@ open class CoRouterFunctionDsl(private val init: (CoRouterFunctionDsl.() -> Unit
389390
*/
390391
fun resources(lookupFunction: suspend (ServerRequest) -> Resource?) {
391392
builder.resources {
392-
GlobalScope.mono {
393+
GlobalScope.mono(Dispatchers.Unconfined) {
393394
lookupFunction.invoke(it)
394395
}
395396
}
@@ -404,7 +405,7 @@ open class CoRouterFunctionDsl(private val init: (CoRouterFunctionDsl.() -> Unit
404405
}
405406

406407
private fun asHandlerFunction(init: suspend (ServerRequest) -> ServerResponse) = HandlerFunction {
407-
GlobalScope.mono {
408+
GlobalScope.mono(Dispatchers.Unconfined) {
408409
init(it)
409410
}
410411
}

Diff for: spring-webflux/src/main/kotlin/org/springframework/web/reactive/server/ServerWebExchangeExtensions.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.web.reactive.server
1818

19+
import kotlinx.coroutines.Dispatchers
1920
import kotlinx.coroutines.GlobalScope
2021
import kotlinx.coroutines.reactive.awaitSingle
2122
import kotlinx.coroutines.reactor.mono
@@ -68,4 +69,4 @@ suspend fun ServerWebExchange.awaitSession(): WebSession =
6869
* @since 5.2
6970
*/
7071
fun ServerWebExchange.Builder.principal(supplier: suspend () -> Principal): ServerWebExchange.Builder
71-
= principal(GlobalScope.mono { supplier.invoke() })
72+
= principal(GlobalScope.mono(Dispatchers.Unconfined) { supplier.invoke() })

0 commit comments

Comments
 (0)