Hey, has anyone implemented graceful shutdowns wit...
# ktor
r
Hey, has anyone implemented graceful shutdowns with Ktor? I'm looking for a way to "drain" ongoing requests before the process shuts down after being sent a
SIGTERM
. I can implement this myself, though if there's already a commonly used feature, I'd opt to not re-invent the wheel. My general idea is to set some boolean like
stopRequested
when getting a
SIGTERM
and maintain another
var active: Int
with the current number of active requests similar to https://github.com/ktorio/ktor/blob/master/ktor-features/ktor-metrics/jvm/src/io/ktor/metrics/dropwizard/DropwizardMetrics.kt#L22 so when that number hits 0 and
stopRequested
is true, only then finish the process.
The use case is that when we deploy our service to our kubernetes environment, pods of the old version get sent a
SIGTERM
and have some graceful shutdown time. Right now they die while requests are in flight and we see a dip in success rate.
Oh, we're using the Netty engine
hmm it seems that parameters on
server.stop
should suffice? we have them set to
Copy code
server.stop(gracePeriod = 10, timeout = 10, timeUnit = SECONDS)
Hm I'll go back to this. Maybe this is more of a k8s problem sending requests to the pod after the SIGTERM or maybe we have some very slow requests.
n
If there was a Kotlin Native version of Ktor Server for Linux then one would expect there is functionality to catch the signals. Highly unlikely that the Kotlin JVM version of Ktor Server would allow catching of signals.
r
yeah we use
Runtime.getRuntime().addShutdownHook
n
Is that the Runtime class from the JDK?
r
yes
we have a globally available util function:
Copy code
fun onShutdown(action: () -> Unit) =
	Runtime.getRuntime().addShutdownHook(Thread(action))