Skip to content

Latest commit

 

History

History

ktor-rate-limiting

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Ktor Rate Limiting

GitHub release (latest SemVer)

A simple library that enables Rate Limiting in Ktor. Originally hosted in LukasForst/ktor-rate-limiting repository.

Deprecation Notice

Ktor now offers their own rate limiting plugin since 2.2.0, we recommend using their implementation.

Installation

Include following in your build.gradle.kts:

implementation("dev.forst", "ktor-rate-limiting", "<latest version>")

Versions >= 1.2.0 have implementation for Ktor >= 2.0.0, use 1.1.0 if you need support for older versions of Ktor.

Usage

This is minimal implementation of the Ktor app that uses Rate Limiting:

/**
 * Minimal Ktor application with Rate Limiting enabled.
 */
fun Application.minimalExample() {
    // install feature
    install(RateLimiting) {
        registerLimit(
            // allow 10 requests
            limit = 10,
            // each 1 minute
            window = Duration.ofMinutes(1)
        ) {
            // use host as a key to determine who is who
            call.request.origin.host
        }
        // and exclude path which ends with "excluded"
        excludeRequestWhen {
            call.request.path().endsWith("excluded")
        }
    }
    // now add some routes
    routing {
        get {
            call.respondText("Hello ${call.request.origin.host}")
        }
        get("excluded") {
            call.respondText("Hello ${call.request.origin.host}")
        }
    }
}

For details see MinimalExampleApp.kt with this example application and TestMinimalExampleApp.kt which verifies that this app works as expected.