codeborne / klite

Kotlin async http server framework with coroutines and zero deps
MIT License
105 stars 19 forks source link

Stopping server, upgrading prod. server, auto-reload #52

Open webleaf opened 8 months ago

webleaf commented 8 months ago

I have a few questions:

  1. Stopping server. I expect ability to correctly stop server. I mean, after server get stop signal, it should stop accepting and processing new requests (responding to them with 503), but shut down only after processing the ones already accepted. How does this work in klite?
  2. How properly upgrade server in production?
  3. How to automatically restart the server if it crashes in production? Watchdog?
  4. Jooby and Ktor have hot-reload/auto-reload feature in development mode. How development workflow looks with klite?
angryziber commented 8 months ago
  1. If you look at implementation of Server.start() method - it registers a JVM shutdown hook to do a graceful shutdown, which is actually implemented by jdk.httpserver itself - it closes the port, so that proxies/load balancers know that it doesn't accept any more requests, but finishes the currently running ones for up to 3 seconds (you can specify your value as a parameter). There are no 503 responses, as these may be propagated to the end-user.

  2. Docker/Heroku/Google Cloud Run/Fly.io - all do this correctly. In general, they will start a new container, wait for it to open http port and then redirect traffic there. This should work the same no matter which framework do you use.

  3. Docker and all of the the above would do it automatically for you.

  4. Klite doesn't have any magic built it, but it restarts in less than 1 second, so you just change your code/run tests and then restart the server when ready. JVM hot reload works for changed code inside of functions, but not signatures, like usual. Usually classloader auto-reload hacks are needed if your server starts slowly.

In general, Klite is built with 12-factor apps principles in mind regarding deployment.