Rate Limit
Limit the requests sent to a Ktor server with a timeout.
Install
This package is uploaded to MavenCentral and supports JVM and all native targets as well.
repositories {
mavenCentral()
}
dependencies {
implementation("app.softwork:ratelimit:LATEST")
}
Content copied to clipboard
Usage
Simple install it in your application module or for a specific route.
val storage: Storage = // Storage implementation, required
install(RateLimit(storage = storage)) {
limit = 1000
timeout = 1.hours
host = { call ->
call.request.local.remoteHost
}
alwaysAllow { host ->
host.startsWith("foo")
}
alwaysBlock { host ->
host.endsWith("bar")
}
skip { call ->
if (call.request.local.uri == "/login") { // alternative: install at this route only
RateLimit.SkipResult.ExecuteRateLimit
} else {
RateLimit.SkipResult.SkipRateLimit
}
}
}
Content copied to clipboard
Storage
To persist the rate limiting, you need to implement a Storage
provider, which use kotlinx.datetime.Instant
. All functions are suspend
to support async IO
operations.
License
Apache 2