https://kotlinlang.org logo
Channels
100daysofcode
100daysofkotlin
100daysofkotlin-2021
advent-of-code
aem
ai
alexa
algeria
algolialibraries
amsterdam
android
android-architecture
android-databinding
android-studio
androidgithubprojects
androidthings
androidx
androidx-xprocessing
anime
anko
announcements
apollo-kotlin
appintro
arabic
argentina
arkenv
arksemdevteam
armenia
arrow
arrow-contributors
arrow-meta
ass
atlanta
atm17
atrium
austin
australia
austria
awesome-kotlin
ballast
bangladesh
barcelona
bayarea
bazel
beepiz-libraries
belgium
benchmarks
berlin
big-data
books
boston
brazil
brikk
budapest
build
build-tools
bulgaria
bydgoszcz
cambodia
canada
carrat
carrat-dev
carrat-feed
chicago
chile
china
chucker
cincinnati-user-group
cli
clikt
cloudfoundry
cn
cobalt
code-coverage
codeforces
codemash-precompiler
codereview
codingame
codingconventions
coimbatore
collaborations
colombia
colorado
communities
competitive-programming
competitivecoding
compiler
compose
compose-android
compose-desktop
compose-hiring
compose-ios
compose-mp
compose-ui-showcase
compose-wear
compose-web
confetti
connect-audit-events
corda
cork
coroutines
couchbase
coursera
croatia
cryptography
cscenter-course-2016
cucumber-bdd
cyprus
czech
dagger
data2viz
databinding
datascience
dckotlin
debugging
decompose
decouple
denmark
deprecated
detekt
detekt-hint
dev-core
dfw
docs-revamped
dokka
domain-driven-design
doodle
dsl
dublin
dutch
eap
eclipse
ecuador
edinburgh
education
effective-kotlin
effectivekotlin
emacs
embedded-kotlin
estatik
event21-community-content
events
exposed
failgood
fb-internal-demo
feed
firebase
flow
fluid-libraries
forkhandles
forum
fosdem
fp-in-kotlin
framework-elide
freenode
french
fritz2
fuchsia
functional
funktionale
gamedev
ge-kotlin
general-advice
georgia
geospatial
german-lang
getting-started
github-workflows-kt
glance
godot-kotlin
google-io
gradle
graphic
graphkool
graphql
graphql-kotlin
graviton-browser
greece
grpc
gsoc
gui
hackathons
hacktoberfest
hamburg
hamkrest
helios
helsinki
hexagon
hibernate
hikari-cp
hire-me
hiring
hongkong
hoplite
http4k
hungary
hyderabad
image-processing
india
indonesia
inkremental
intellij
intellij-plugins
intellij-tricks
internships
introduce-yourself
io
ios
iran
israel
istanbulcoders
italian
jackson-kotlin
jadx
japanese
jasync-sql
java-to-kotlin-refactoring
javadevelopers
javafx
javalin
javascript
jdbi
jhipster-kotlin
jobsworldwide
jpa
jshdq
juul-libraries
jvm-ir-backend-feedback
jxadapter
k2-early-adopters
kaal
kafka
kakao
kalasim
kapt
karachi
karg
karlsruhe
kash_shell
kaskade
kbuild
kdbc
kgen-doc-tools
kgraphql
kinta
klaxon
klock
kloudformation
kmdc
kmm-español
kmongo
knbt
knote
koalaql
koans
kobalt
kobweb
kodein
kodex
kohesive
koin
koin-dev
komapper
kondor-json
kong
kontent
kontributors
korau
korean
korge
korim
korio
korlibs
korte
kotest
kotest-contributors
kotless
kotlick
kotlin-asia
kotlin-beam
kotlin-by-example
kotlin-csv
kotlin-data-storage
kotlin-foundation
kotlin-fuel
kotlin-in-action
kotlin-inject
kotlin-latam
kotlin-logging
kotlin-multiplatform-contest
kotlin-mumbai
kotlin-native
kotlin-pakistan
kotlin-plugin
kotlin-pune
kotlin-roadmap
kotlin-samples
kotlin-sap
kotlin-serbia
kotlin-spark
kotlin-szeged
kotlin-website
kotlinacademy
kotlinbot
kotlinconf
kotlindl
kotlinforbeginners
kotlingforbeginners
kotlinlondon
kotlinmad
kotlinprogrammers
kotlinsu
kotlintest
kotlintest-devs
kotlintlv
kotlinultimatechallenge
kotlinx-datetime
kotlinx-files
kotlinx-html
kotrix
kotson
kovenant
kprompt
kraph
krawler
kroto-plus
ksp
ktcc
ktfmt
ktlint
ktor
ktp
kubed
kug-leads
kug-torino
kvision
kweb
lambdaworld_cadiz
lanark
language-evolution
language-proposals
latvia
leakcanary
leedskotlinusergroup
lets-have-fun
libgdx
libkgd
library-development
lincheck
linkeddata
lithuania
london
losangeles
lottie
love
lychee
macedonia
machinelearningbawas
madrid
malaysia
mathematics
meetkotlin
memes
meta
metro-detroit
mexico
miami
micronaut
minnesota
minutest
mirror
mockk
moko
moldova
monsterpuzzle
montreal
moonbean
morocco
motionlayout
mpapt
mu
multiplatform
mumbai
munich
mvikotlin
mvrx
myndocs-oauth2-server
naming
navigation-architecture-component
nepal
new-mexico
new-zealand
newname
nigeria
nodejs
norway
npm-publish
nyc
oceania
ohio-kotlin-users
oldenburg
oolong
opensource
orbit-mvi
osgi
otpisani
package-search
pakistan
panamá
pattern-matching
pbandk
pdx
peru
philippines
phoenix
pinoy
pocketgitclient
polish
popkorn
portugal
practical-functional-programming
proguard
prozis-android-backup
pyhsikal
python
python-contributors
quasar
random
re
react
reaktive
realm
realworldkotlin
reductor
reduks
redux
redux-kotlin
refactoring-to-kotlin
reflect
refreshversions
reports
result
rethink
revolver
rhein-main
rocksdb
romania
room
rpi-pico
rsocket
russian
russian_feed
russian-kotlinasfirst
rx
rxjava
san-diego
science
scotland
scrcast
scrimage
script
scripting
seattle
serialization
server
sg-user-group
singapore
skia-wasm-interop-temp
skrape-it
slovak
snake
sofl-user-group
southafrica
spacemacs
spain
spanish
speaking
spek
spin
splitties
spotify-mobius
spring
spring-security
squarelibraries
stackoverflow
stacks
stayhungrystayfoolish
stdlib
stlouis
strife-discord-lib
strikt
students
stuttgart
sudan
swagger-gradle-codegen
swarm
sweden
swing
swiss-user-group
switzerland
talking-kotlin
tallinn
tampa
teamcity
tegal
tempe
tensorflow
terminal
test
testing
testtestest
texas
tgbotapi
thailand
tornadofx
touchlab-tools
training
tricity-kotlin-user-group
trójmiasto
truth
tunisia
turkey
turkiye
twitter-feed
uae
udacityindia
uk
ukrainian
uniflow
unkonf
uruguay
utah
uuid
vancouver
vankotlin
vertx
videos
vienna
vietnam
vim
vkug
vuejs
web-mpp
webassembly
webrtc
wimix_sentry
wwdc
zircon
Powered by
Title
s

smallufo

07/27/2021, 9:56 AM
Hi , I got tons of “ktor-client-apache” threads when running a server. And in fact the server is not doing anything . Is there anyway to find out where goes wrong ?
jstack 45158 | grep Ktor-client-apache | wc -l                                                                                                                         
     125      // there are 125 threads generated !
And the thread number starts from #19 to #144
jstack 45158 | grep Ktor-client-apache
                                                                                                                                 
"Ktor-client-apache" #19 daemon prio=5 os_prio=31 cpu=12.09ms elapsed=513.05s tid=0x0000000135bc4c00 nid=0x8403 runnable  [0x0000000172df2000]
"Ktor-client-apache" #20 daemon prio=5 os_prio=31 cpu=11.36ms elapsed=513.05s tid=0x0000000144e30200 nid=0x7b03 runnable  [0x0000000172ffe000]
...
...
"Ktor-client-apache" #143 daemon prio=5 os_prio=31 cpu=8.96ms elapsed=510.76s tid=0x0000000142a3d400 nid=0x1fe03 runnable  [0x0000000292bea000]
"Ktor-client-apache" #144 daemon prio=5 os_prio=31 cpu=7.24ms elapsed=510.76s tid=0x0000000142a3ce00 nid=0x21903 runnable  [0x0000000292df6000]
And I cannot figure out how it starts … :
"Ktor-client-apache" #144 daemon prio=5 os_prio=31 cpu=8.90ms elapsed=591.80s tid=0x0000000142a3ce00 nid=0x21903 runnable  [0x0000000292df6000]
   java.lang.Thread.State: RUNNABLE
	at sun.nio.ch.KQueue.poll(java.base@15.0.2/Native Method)
	at sun.nio.ch.KQueueSelectorImpl.doSelect(java.base@15.0.2/KQueueSelectorImpl.java:122)
	at sun.nio.ch.SelectorImpl.lockAndDoSelect(java.base@15.0.2/SelectorImpl.java:129)
	- locked <0x00000006c6b79ec0> (a sun.nio.ch.Util$2)
	- locked <0x00000006c6b79e68> (a sun.nio.ch.KQueueSelectorImpl)
	at sun.nio.ch.SelectorImpl.select(java.base@15.0.2/SelectorImpl.java:141)
	at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:255)
	at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:104)
	at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:591)
	at java.lang.Thread.run(java.base@15.0.2/Thread.java:832)
Is it normal ? I have many beans defining ktor like this :
@Named 
class SomeBean {
  private val ktorClient = HttpClient {
    followRedirects = false
    expectSuccess = false
  }
}
It seems there is
engine
block can override
threadsCount
, but I never invoked it. Environments :
<ktor.version>1.6.1</ktor.version>

      <dependency>
        <groupId>io.ktor</groupId>
        <artifactId>ktor-client-apache</artifactId>
        <version>${ktor.version}</version>
      </dependency>

openjdk version "15.0.2" 2021-01-19
OpenJDK Runtime Environment Zulu15.29+15-CA (build 15.0.2+7)
OpenJDK 64-Bit Server VM Zulu15.29+15-CA (build 15.0.2+7, mixed mode)
These threads are all green (RUNNING) when viewing in VisualVM , but they are not transmitting anything.
j

Joost Klitsie

07/27/2021, 12:44 PM
I guess the answer is in this message:
I have many beans defining ktor like this :
every client will start up threads
so if you want to use less resources you have to share the client
s

smallufo

07/27/2021, 2:54 PM
That’s also the reason I can think of. But is it true ? Every bean with Ktor client will default occupy 4 threads? Is it reasonable ? Why not initialize threads when needed or using some pool mechanism ?
a

Aleksei Tirman [JB]

07/28/2021, 9:54 AM
You can customize a number of threads and other options using
customizeClient
by mutating HttpAsyncClientBuilder:
val client = HttpClient(Apache) {
    engine {
        customizeClient {
            setDefaultIOReactorConfig(
                IOReactorConfig.custom()
                    .setIoThreadCount(Runtime.getRuntime().availableProcessors())
                    .build()
            )
        }
    }
}
s

smallufo

07/28/2021, 10:59 AM
Thanks @Aleksei Tirman [JB] . I tried another way , Initialize ktor centrally (in one spring
@Configuration
class) , and inject ktor’s
io.ktor.client
to each component. And it reduces the total threads from 125 to 15 . It seems each ktor initialization will consume 5 threads. Anyway , is there any drawback about this ? Thanks.
a

Aleksei Tirman [JB]

07/28/2021, 11:00 AM
There should be none.
s

smallufo

07/28/2021, 11:03 AM
If I reduce total client instance to 1 (means 5 threads , maybe) , does it mean my server (including many api beans & queries) will only be sending 5 http request at the same time ? Thanks.
a

Aleksei Tirman [JB]

07/28/2021, 11:08 AM
I don't think so. The following simple test shows that all requests are executed concurrently.
(1..50).map {
    async {
        client.get<String>("<https://httpbin.org/get>")
        println("$it done")
    }
}.awaitAll()
🙌 1
s

smallufo

07/28/2021, 6:09 PM
BTW , It seems that intelliJ cannot recognize
HttpClient(Apache)
. It reports error , so that I cannot use apache’s specific settings ( ex :
customizeClient
) . What’s the cause ? Thanks.
a

Aleksei Tirman [JB]

07/29/2021, 8:31 AM
You should use
engine
property to configure the engine's specific options. Please see https://kotlinlang.slack.com/archives/C0A974TJ9/p1627466069105900?thread_ts=1627379761.098200&amp;cid=C0A974TJ9.
s

smallufo

07/29/2021, 12:10 PM
Yes , I am copying the same code . But intelliJ just cannot recognize
HttpClient(Apache)
as below : It seems kotlin’s compiler is broken here.
a

Aleksei Tirman [JB]

07/29/2021, 12:20 PM
That's probably an IDE problem.