Kotlin
A concise multiplatform language developed by JetBrains
Ktor 3.4.0 Is Now Available!
Ktor 3.4.0 improves stability and addresses outstanding issues. Highlights include OpenAPI generation, Ztsd support for the compression plugin, duplex streaming for OkHttp, Structured concurrency integrations for the HTTP request lifecycle, and much more. For a detailed list of all the changes, check out the What’s new page.
Stability and performance
With its focus on stability and fixes for some long-standing issues, this release ensures Ktor is ready for another year of amazing development!
Because stability and performance remain among our top priorities heading into the new year, we want to invite all of you again to join the Ktor Early Access Program and help us test new versions and features. Start building your next project at start.ktor.io. Your suggestions and contributions are always welcome!
OpenAPI generation from code
In 3.4.0, we completed the story for OpenAPI document generation by introducing a new API for dynamically documenting endpoints that works in tandem with a new compiler plugin. Now, instead of building your Swagger frontend from a static file, the model is built at runtime from details embedded in the routing tree.
To generate your documentation, you can enable it through the Ktor Gradle plugin, then it will automatically provide details in your code via the new describe API:
/**
* Our existing plugins work with the information found in the routes.
*/
swaggerUI("/docs") {
// Customize the result with general details
info = OpenApiInfo("KChat API", "1.0.0")
}
/**
* Get a list of messages.
*
* Query parameters:
* - search [String] a search query
*/
get("/messages") {
val query = call.parameters["search"]?.parseQueryOrNull()
call.respond(messageTable.listMessages(query))
}.describe {
// This code is generated from the code above
summary = "Get a list of messages"
parameters {
query("q") {
description = "a search query"
}
}
responses {
HttpStatusCode.OK {
schema = jsonSchema<Message>()
}
}
}
Duplex streaming for OkHttp
The OkHttp client engine now supports duplex streaming, enabling clients to send request body data and receive response data simultaneously, in contrast to regular HTTP calls, where the request body must be fully sent before the response begins.
Duplex streaming is available for HTTP/2 connections and can be enabled using the new duplexStreamingEnabled property in OkHttpConfig:
val client = HttpClient(OkHttp) {
engine {
duplexStreamingEnabled = true
config {
protocols(listOf(Protocol.H2_PRIOR_KNOWLEDGE))
}
}
}
Zstd compression support
The Compression plugin now supports Ztsd via the new ktor-server-compression-zstd module. Zstd is a fast compression algorithm that offers high compression ratios, low compression times, and a configurable compression level. To set this level, call the zstd function from inside the compression block:
install(Compression) {
zstd {
compressionLevel = 3
...
}
}
HttpRequestLifecycle
The new HttpRequestLifecycle plugin allows you to cancel in-flight HTTP requests when the client disconnects, which is useful when you need to cancel a long-running or resource-intensive in-flight request. Simply install the HttpRequestLifecycle plugin and set cancelCallOnClose = true:
install(HttpRequestLifecycle) {
cancelCallOnClose = true
}
routing {
get("/long-process") {
try {
while (isActive) {
delay(10_000)
logger.info("Very important work.")
}
call.respond("Completed")
} catch (e: CancellationException) {
logger.info("Cleaning up resources.")
}
}
}
When the client disconnects, the coroutine handling the request is canceled, along with any launch or async coroutines started by it, and structured concurrency cleans all resources. This is currently only supported for the Netty and CIO engines.
🚀 Thank you!
We want to thank everyone in the community for your support and feedback, as well as for reporting issues.
Start building your next project at start.ktor.io. Your suggestions and contributions are always welcome! 🔗 Get Started With Ktor | 📢 Join the Community on Reddit and Slack
