Skip to content

Instantly share code, notes, and snippets.

@smallufo
Created November 8, 2019 14:31
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/7a8273af7eef03ba58305492bcb7672a to your computer and use it in GitHub Desktop.
Save smallufo/7a8273af7eef03ba58305492bcb7672a to your computer and use it in GitHub Desktop.
replace delay with sleep
class IsgdImpl : UrlShorter {
override suspend fun getShortUrl(longUrl: String): String? {
logger.info("running : {}", Thread.currentThread().name)
val url = "https://is.gd/create.php?format=simple&url=%s".format(URLEncoder.encode(longUrl, "UTF-8"))
return withContext(Dispatchers.IO) {
logger.info("running Dispatchers.IO : {}", Thread.currentThread().name)
Request.Get(url).also {
sleep(1000)
//delay(1000)
}.execute()
.also {
logger.info("ISGD network call invoked")
}.returnContent().asString().also {
logger.info("returning {}", it)
}
}
}
}
class TinyImpl : UrlShorter {
override suspend fun getShortUrl(longUrl: String): String? {
logger.info("running : {}", Thread.currentThread().name)
val url = "http://tinyurl.com/api-create.php?url=$longUrl"
return withContext(Dispatchers.IO) {
logger.info("running Dispatchers.IO : {}", Thread.currentThread().name)
Request.Get(url).also {
sleep(5000)
//delay(5000)
}.execute()
.also {
logger.info("TINY network call invoked")
}.returnContent().asString().also {
logger.info("returning {}", it)
}
}
}
}
/**
22:26:34,726 INFO NullImpl - running : main
22:26:34,732 INFO DumbImpl - running : main
22:26:34,732 INFO IsgdImpl - running : main
22:26:34,743 INFO IsgdImpl$getShortUrl$2 - running Dispatchers.IO : DefaultDispatcher-worker-1
22:26:34,748 INFO TinyImpl - running : main
22:26:34,753 INFO TinyImpl$getShortUrl$2 - running Dispatchers.IO : DefaultDispatcher-worker-2
22:26:37,530 INFO IsgdImpl$getShortUrl$2 - ISGD network call invoked
22:26:37,535 INFO IsgdImpl$getShortUrl$2 - returning https://is.gd/EuvYes
22:26:40,114 INFO TinyImpl$getShortUrl$2 - TINY network call invoked
22:26:40,114 INFO TinyImpl$getShortUrl$2 - returning http://tinyurl.com/389lo
22:26:40,115 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