question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] CPU 100% Apache Ktor Http Client

See original GitHub issue

Ktor Version

1.1.3

Ktor Engine Used(client or server and name)

Apache

JVM Version, Operating System and Relevant Context

Feedback

I utilize ktor http client to communicating from service to other service, but after 10 minutes deployment my machine experience CPU 100%, any idea why ?

here’s the building block that I use,

override fun sendAsync(request: HttpRequest, responseCallback: Callback<HttpResponse>) {
    GlobalScope.launch (Dispatchers.IO + CoroutineName(key ?: "")) {
      try {
        decorateRequest(request)

        val requestUrl = URL(request.url)
        val requestBody = String(request.body.asArray(), StandardCharsets.UTF_8)
        val requestMethod = HttpMethod(request.method)
        val requestContentType = createContentType(request)

        val clientResponse = httpClient!!.request<io.ktor.client.response.HttpResponse>{
          method = requestMethod
          url(requestUrl)
          body = TextContent(requestBody, requestContentType)

          request.headers.forEach {
            if (!unsafeHeaderSet.contains(it.name)) {
              header(it.name, it.value)
            }
          }
        }

        val response = HttpResponse()
        val responseBodyString = String(clientResponse.readBytes())
        response.statusCode = clientResponse.status.value
        response.bodyEncoding = "UTF-8"
        response.body = IndirectNIOBuffer(ByteBuffer.wrap(responseBodyString.toByteArray(charset("UTF-8"))), true)

        clientResponse.headers.forEach { name, value ->
          val headerValue = value.joinToString(",")

          response.addHeader(name, headerValue)
        }

        clientResponse.close()
        responseCallback.onComplete(response)
      } catch (error: Exception) {
        responseCallback.onException(error)
      }
    }
  }

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:23 (19 by maintainers)

github_iconTop GitHub Comments

2reactions
ok2ccommented, Dec 14, 2019

@e5l @IRus TLSv13 caused us a lot grief but as far as I can say all known issues have been resolved with HttpCore 4.4.12.

1reaction
IRuscommented, Mar 1, 2020

Apache Client one of the most feature complete, and production tested clients. This issue related to the newest JDK releases and particularly TSL v1.3 support. Still, this https://github.com/ktorio/ktor/issues/1018#issuecomment-565648818 fixes issue

Read more comments on GitHub >

github_iconTop Results From Across the Web

High CPU usage by Ktor-client-apache when Package ...
Hi. After half an hour of using the environment, the CPU is at 100% load. What I got from the Activity Monitor: %CPU...
Read more >
Occasionally Ktor utilizes 100% CPU without any load after ...
Occasionally Ktor utilizes 100% CPU without any load after failed request.
Read more >
Hi I got tons of ktor client apache threads when running a s - Kotlinlang
Hi , I got tons of “ktor-client-apache” threads when running a server. And in fact the server is not doing anything . Is...
Read more >
Engines | Ktor
The Ktor HTTP client can be used on different platforms, including JVM, ... The Apache engine supports HTTP/1.1 and provides multiple ...
Read more >
Performance for my Ktor web service (Profiling?) : r/Kotlin
I tried assigning a dedicated 2k thread pool to the Java 11 HttpClient, and it's gotten to the point where CPU%/100 is fairly...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found