Blog

SuspendApp 0.4.0 is Now Available

26 Jan, 2023
Xebia Background Header Wave

We are excited to announce the release of SuspendApp 0.4.0, a powerful way of reasoning about resource-safe applications using Structured Concurrency.

This release introduces a new integration module for Ktor. We encourage all Kotlin users to upgrade to the latest version of SuspendApp to take advantage of the new Ktor module and provide feedback. Thank you to the community for your continued support and contributions. Below, we summarize the most significant changes. For the full changelog, check out our Github page.

SuspendApp Ktor

The SuspendApp Ktor module offers an integration layer between Ktor and Arrow Fx Coroutines’s Resource type, allowing you to build zero-downtime applications with Kubernetes easily.

Kubernetes is an open-source system for automating containerized applications’ deployment, scaling, and management. Often it’s used for networking applications, but it makes no assumptions about it. Therefore it requires manually taking into account some orchestration to allow our applications to gracefully drain existing connections and allow topology changes to happen before shutting down.

This is exactly what the new Ktor integration facilitates! Wrapping the existing embdeddedServer into Arrow’s Resource DSL with support for auto-reloading!

fun main() = SuspendApp {
  resourceScope {
    val engine = server(
        factory = Netty,
        port = 8080,
        host = "192.168.52.64",
        watchPaths = listOf("classes", "resources")
    ) { ping() }
    awaitCancellation()
  }
}

fun Application.ping(): Routing =
  routing {
    get("/ping") { call.respondText("pong") }
  }

The example will run a Ktor embdeddedServer and gracefully shut it down after delaying with a preWait = 30.seconds giving the topology the time to change, and then stop the Ktor engine with a grace = 500.milliseconds and timeout = 500.milliseconds period whenever a SIGTERM or SIGINT event occurs.

This technique gives an excellent alternative for controlling this behavior from Kotlin code rather than using Kubernetes preStop and /bin/sleep from yml configurations. These parameters can also easily be parameterized and controlled from environment configurations and configuration files.

This Ktor module is available for Kotlin JVM, Linux, and macOS. These are the only targets supported by Ktor embdeddedServer.

If you have any feedback, be sure to let us know in the #arrow channel on the KotlinLang Slack or the issues on the SuspendApp repository.

Xebia Functional (formerly 47 Degrees) 💙 Kotlin

We, the functional team at Xebia, are great fans of Kotlin, exploring the many possibilities it brings to the back-end scene. We’re proud maintainers of Arrow, a set of companion libraries to Kotlin’s standard library, coroutines, and compiler; and provide Kotlin training to become an expert Kotliner. If you’re interested in talking to us, you can use our contact form, or join us on the Kotlin Slack.

Simon Vergauwen
Arrow maintainer & Principal Engineer @ Xebia Functional from Antwerp, Belgium Working with Kotlin since 2015 & Fanatic OSS'er
Questions?

Get in touch with us to learn more about the subject and related solutions

Explore related posts