Skip to content

Instantly share code, notes, and snippets.

@smallufo
Created November 8, 2019 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smallufo/da4445735c8c64a05e76d108428e72ba to your computer and use it in GitHub Desktop.
Save smallufo/da4445735c8c64a05e76d108428e72ba to your computer and use it in GitHub Desktop.
flow on...
private suspend fun method1a(longUrl: String): String {
return impls.asSequence().asFlow().flatMapMerge(impls.size) { impl ->
flow {
impl.getShortUrl(longUrl)?.also {
emit(it)
}
}.flowOn(Dispatchers.IO)
}.first()
.also { Dispatchers.IO.cancelChildren() }
}
/**
22:56:30,517 INFO DumbImpl - running : DefaultDispatcher-worker-4
22:56:30,518 INFO NullImpl - running : DefaultDispatcher-worker-1
22:56:30,517 INFO IsgdImpl - running : DefaultDispatcher-worker-5
22:56:30,517 INFO TinyImpl - running : DefaultDispatcher-worker-7
22:56:30,529 INFO TinyImpl$getShortUrl$2 - running Dispatchers.IO : DefaultDispatcher-worker-7
22:56:30,529 INFO IsgdImpl$getShortUrl$2 - running Dispatchers.IO : DefaultDispatcher-worker-5
22:56:33,233 INFO IsgdImpl$getShortUrl$2 - ISGD network call invoked
22:56:33,238 INFO IsgdImpl$getShortUrl$2 - returning https://is.gd/EuvYes
22:56:33,244 INFO UrlShorterServiceTest$testHedging$1 - result = https://is.gd/EuvYes
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment