Hi, I have my Ktor server. I use the Ktor client t...
# ktor
h
Hi, I have my Ktor server. I use the Ktor client to connect to another API in the Ktor server. The documentation says:
After you finish working with the HTTP client, you need to free up the resources ... call
client.close()
Should I call the
close()
function in the Ktor server? And if so, where?
a
Should your server live as long as a client?
The
close
method on a server destroys it.
h
The client should live as long as the server. But I wonder if there won't be any leaks and is safe to not call the
close
method? :)
a
Close
on a server or client?
h
Client's
close
method. There is any server
close
method?
a
Server has the
stop
method.
If you don’t close a client and your application continues working, there will be a leak.
h
So then, should I close the client after every client's call? My client is a singleton...
a
You should close it after all calls made.
h
Not sure if I understand it right. If the server is nonstop running, there might be another call at any time.
a
I’m talking about the client only.
h
Ok, I will close the client when all calls are done and reopen the client when needed again. Is this the right way?
a
No. If a client should live as long as your application then you don’t need to close it at all. Also, you can close it in some event handler that is launched just before an application is destroyed.
💯 1
You can’t use a client instance after you close it.
h
Thank you. Please, I have the last question. Is there any callback called right before the application is shut down (in order to clear resources)?
I see it. Since 2.0.0 there are these events. https://ktor.io/docs/custom-plugins.html#handle-app-events
a
I meant your application in general, not a Ktor server application.
👍 1