Skip to content

Instantly share code, notes, and snippets.

@darkxanter
Created May 18, 2021 06:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darkxanter/d40b994eec691d69dd931cc9e60bb935 to your computer and use it in GitHub Desktop.
Save darkxanter/d40b994eec691d69dd931cc9e60bb935 to your computer and use it in GitHub Desktop.
ktor rate limit
fun Application.module(testing: Boolean = false) {
val config = RateLimiterConfig.custom()
.limitRefreshPeriod(Duration.ofMillis(1))
.limitForPeriod(10)
.timeoutDuration(Duration.ofMillis(25))
.build()
val rateLimiterRegistry = RateLimiterRegistry.of(config)
val testRateLimiter = rateLimiterRegistry
.rateLimiter("test")
routing {
get("/") {
testRateLimiter.executeSuspendFunction {
call.respondText("HELLO WORLD!", contentType = ContentType.Text.Plain)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment