Hi guys, wanted to understand the general practive...
# ktor
m
Hi guys, wanted to understand the general practive of releasing resources when a Ktor Server shutsdown, say like closing a database driver and connections or completing all the running requests, came across #arrows implementation of SuspendApp and Resources concept. But want to know how everyone else does ?
s
I am the author behind SuspendApp, and Resource so 😅 But afaik the alternative is to manually manage closing resources using
application.enviroment.monitor(ApplicationStopping)
, or
addShutdownHook { }
. Afaik Ktor-koin doesn't automatically take into account
AutoCloseable
like Spring does, so when using
koin-ktor
you still need to manually take care of this. I'm curious if there are other options,
SuspendApp
and
Resource
are more general than just Ktor though. You can also use it in CLI applications, for producing/consuming records from Kafka for example.
💯 2
👍 1
m
Understood, but, in a multi-module app where database is very far away from main module where SuspendApp in is setting up the resources, it would really hard to get access to database. Say the structure of app is as below, there is no way, I can get access to Database Module in App Module.
s
That is where
Resource
is really great 😅 It's a typed (compile-time) version of what Spring does with runtime introspection / reflection. To be honest, I rather call it
ResourceScope
, and I see it as
CoroutineScope
. That's also how I explain it in my talk,

https://youtu.be/A69_t_oEP_E?feature=shared

. I also have an example here, https://github.com/nomisRev/ktor-arrow-example/blob/main/src/main/kotlin/io/github/nomisrev/env/Dependencies.kt. So in every module you can expose
suspend fun ResourceScope.dependency(): A
for "structured resources" and then you can call these functions, similarly how you do
fun CoroutineScope.concurrency(): A
for structured concurrency. I've considered splitting
ResourceScope
completely from Arrow, so you don't have any additional dependencies coming in. So you can just use
SuspendApp
and
Resource
without anything else from Arrow on the classpath. Would that lower the threshold for people to use it? 🤔
👍 1
m
Got you, I didn't wanted to import anything from Service Module to App Module. I will give it a try
s
You need to somehow through the config, right?
Even if you were using Spring
m
Its a Ktor App, Config is imported into both modules, not the other way round, I will try to restructure
s
Ow, sorry I missread your diagram. Yes, but I was referring to Spring because I though you wanted to do something similar to
@AutoWired
where in
App
module you don't touch any code from
Service
module. Somewhere there is some code constructing
service
and
repository
because those are dependencies of
UseCase
, right? Which are probably hiding the dependencies?
m
Yeah correct, each module is dependent on its lower module. I will try to play with Config Module and change its role to handle resources to be passed to SuspendApp. Koin also has hooks provided for Application Shutdown like
Copy code
environment.monitor.subscribe(KoinApplicationStarted) {
        <http://log.info|log.info>("Koin started.")
}
But again these are dependent on
Application's context
s
Yes, I think that's pretty much the same as
application.enviroment.monitor(ApplicationStopping)
.
Also, that technique doesn't really work great with streaming events for me. https://github.com/arrow-kt/suspendapp/tree/main#suspendapp-with-kafka I mean it's possible but a bit strange.
👍 1