Skip to content

Instantly share code, notes, and snippets.

@morj
Created August 5, 2019 07:46
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 morj/bad8d156d392b745ffe08e0c57fbd272 to your computer and use it in GitHub Desktop.
Save morj/bad8d156d392b745ffe08e0c57fbd272 to your computer and use it in GitHub Desktop.
@file:Suppress("KDocMissingDocumentation")
import io.ktor.client.HttpClient
import io.ktor.client.engine.apache.Apache
import io.ktor.client.request.get
import kotlinx.coroutines.Runnable
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import java.util.concurrent.ThreadFactory
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicInteger
class NamedThreadFactory private constructor(
private val name: (Int) -> String, private val daemon: Boolean
) : ThreadFactory {
private val index = AtomicInteger()
constructor(prefix: String, daemon: Boolean = false) : this({ "$prefix$it" }, daemon)
override fun newThread(r: Runnable) = Thread(r, name(index.incrementAndGet())).apply { isDaemon = daemon }
}
fun main() = runBlocking {
val url =
"https://github.com/jvm-profiling-tools/async-profiler/releases/download/v1.5/async-profiler-1.5-linux-x64.tar.gz"
val httpClient = HttpClient(Apache) {
engine {
customizeClient {
setThreadFactory(NamedThreadFactory("Apache-http-client-apm-client-", daemon = true))
}
}
}
val archive = httpClient.get<ByteArray>(url)
println("got bytes: ${archive.size}, client: $httpClient")
delay(TimeUnit.HOURS.toMillis(10))
println("after 10 hours: got bytes: ${archive.size}, client: ${httpClient}")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment