-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract 'BlockingBridge' for IO operations (#1933)
- Loading branch information
Showing
13 changed files
with
102 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
save-cloud-common/src/jvmMain/kotlin/com/saveourtool/save/utils/BlockingBridge.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.saveourtool.save.utils | ||
|
||
import org.jetbrains.annotations.NonBlocking | ||
import reactor.core.publisher.Flux | ||
import reactor.core.publisher.Mono | ||
import reactor.core.scheduler.Scheduler | ||
import reactor.core.scheduler.Schedulers | ||
import reactor.kotlin.core.publisher.toMono | ||
|
||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
|
||
/** | ||
* A bridge for blocking (IO) operations | ||
* | ||
* @property ioScheduler [Scheduler] for IO operations for [Mono] and [Flux] | ||
* @property ioDispatcher [CoroutineDispatcher] for IO operations in suspend function | ||
*/ | ||
class BlockingBridge( | ||
val ioScheduler: Scheduler = Schedulers.boundedElastic(), | ||
val ioDispatcher: CoroutineDispatcher = Dispatchers.IO, | ||
) { | ||
/** | ||
* Taking from https://projectreactor.io/docs/core/release/reference/#faq.wrap-blocking | ||
* | ||
* @param supplier blocking operation like JDBC | ||
* @return [Mono] from result of blocking operation [T] | ||
* @see blockingToFlux | ||
*/ | ||
@NonBlocking | ||
fun <T : Any> blockingToMono(supplier: () -> T?): Mono<T> = supplier.toMono().subscribeOn(ioScheduler) | ||
|
||
/** | ||
* @param supplier blocking operation like JDBC | ||
* @return [Flux] from result of blocking operation [List] of [T] | ||
* @see blockingToMono | ||
*/ | ||
@NonBlocking | ||
fun <T> blockingToFlux(supplier: () -> Iterable<T>): Flux<T> = blockingToMono(supplier).flatMapIterable { it } | ||
|
||
/** | ||
* @param supplier blocking operation like JDBC | ||
* @return suspend result of blocking operation [T] | ||
* @see blockingToMono | ||
*/ | ||
@NonBlocking | ||
suspend fun <T> blockingToSuspend(supplier: () -> T): T = withContext(ioDispatcher) { | ||
supplier() | ||
} | ||
|
||
companion object { | ||
/** | ||
* A default instance of [BlockingBridge] | ||
*/ | ||
val default: BlockingBridge = BlockingBridge() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters