Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rate limit headers before returning response by default #78

Merged
merged 4 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions documentation/mkdocs/docs/ktor-server-rate-limiting/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ routing {
whiteListedHosts = setOf("trusted-host.com")
blackListedAgents = setOf("malicious-agent")
rateLimitExceededHandler = { rateLimiterResponse ->
...
respond(HttpStatusCode.TooManyRequests, rateLimiterResponse.message)
...
}
}

Expand Down Expand Up @@ -92,10 +92,10 @@ routing {

rateLimitExceededHandler = { limitedBy ->
// Respond with a 429 status and appropriate headers for rate-limited callers
respond(HttpStatusCode.TooManyRequests, "Rate limit exceeded: ${limitedBy.message}")
response.headers.append("X-RateLimit-Limit", "${limitedBy.rateLimiter.capacity}")
response.headers.append("X-RateLimit-Measured-by", limitedBy.rateLimiter.callVolumeUnit.name)
response.headers.append("X-RateLimit-Reset", "${limitedBy.resetIn.inWholeMilliseconds}")
respond(HttpStatusCode.TooManyRequests, "Rate limit exceeded: ${limitedBy.message}")
}

```
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ github.repository.name=extra-ktor-plugins
kotlin.native.cacheKind.linuxX64=none
kotlin.native.ignoreDisabledTargets=true
gradle.publish.enable.module-metadata=true
version=2.1.1
version=2.1.2
gpr.user=flaxoos
org.gradle.jvmargs=-Xmx2g -XX:MaxMetaspaceSize=2g
kotlin.mpp.applyDefaultHierarchyTemplate=false
kotlin.mpp.applyDefaultHierarchyTemplate=false
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ class RateLimitingConfiguration {
*/
var rateLimitExceededHandler: suspend ApplicationCall.(RateLimiterResponse.LimitedBy) -> Unit =
{ rateLimiterResponse ->
respond(HttpStatusCode.TooManyRequests, "$RATE_LIMIT_EXCEEDED_MESSAGE: ${rateLimiterResponse.message}")
this.response.headers.append("$X_RATE_LIMIT-Limit", "${rateLimiterResponse.rateLimiter.capacity}")
this.response.headers.append(
"$X_RATE_LIMIT-Measured-by",
rateLimiterResponse.rateLimiter.callVolumeUnit.name,
)
this.response.headers.append("$X_RATE_LIMIT-Reset", "${rateLimiterResponse.resetIn.inWholeMilliseconds}")
respond(HttpStatusCode.TooManyRequests, "$RATE_LIMIT_EXCEEDED_MESSAGE: ${rateLimiterResponse.message}")
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import io.kotest.core.spec.style.scopes.FunSpecContainerScope
import io.kotest.datatest.withData
import io.kotest.inspectors.forAll
import io.kotest.matchers.collections.shouldContain
import io.kotest.matchers.collections.shouldContainAll
import io.kotest.matchers.collections.shouldContainOnly
import io.kotest.matchers.comparables.shouldBeLessThan
import io.ktor.client.HttpClient
Expand Down Expand Up @@ -347,6 +348,7 @@ class RateLimitingPluginTest : FunSpec() {
withClue("Should be limited") {
logErrors()
map { it.status } shouldContain TooManyRequests
flatMap { it.headers.names() } shouldContainAll listOf("X-RateLimit-Limit", "X-RateLimit-Measured-by", "X-RateLimit-Reset")
}
}

Expand Down
Loading