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

Get rate limit count and remaining time limit. #2081

Closed
wants to merge 1 commit into from
Closed
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
14 changes: 12 additions & 2 deletions javalin/src/main/java/io/javalin/http/util/RateLimitUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,22 @@ import java.util.*
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.Executors
import java.util.concurrent.ScheduledExecutorService
import java.util.concurrent.ScheduledFuture
import java.util.concurrent.TimeUnit

object RateLimitUtil {
val limiters = ConcurrentHashMap<TimeUnit, RateLimiter>()
var keyFunction: (Context) -> String = { ip(it) + it.method() + it.matchedPath() }
val executor: ScheduledExecutorService = Executors.newSingleThreadScheduledExecutor()

private fun ip(ctx: Context) = ctx.header("X-Forwarded-For")?.split(",")?.get(0) ?: ctx.ip()
}

class RateLimiter(val timeUnit: TimeUnit) {

private var future: ScheduledFuture<*>?
private val timeUnitString = timeUnit.toString().lowercase(Locale.ROOT).removeSuffix("s")
private val keyToRequestCount = ConcurrentHashMap<String, Int>().also {
RateLimitUtil.executor.scheduleAtFixedRate({ it.clear() }, /*delay=*/0, /*period=*/1, timeUnit)
future = RateLimitUtil.executor.scheduleAtFixedRate({ it.clear() }, /*delay=*/0, /*period=*/1, timeUnit)
}

fun incrementCounter(ctx: Context, requestLimit: Int) {
Expand All @@ -38,6 +40,14 @@ class RateLimiter(val timeUnit: TimeUnit) {
}
}
}

fun getCounter(ctx: Context) : Int? {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fun getCounter(ctx: Context) : Int? {
fun counter(ctx: Context) : Int? {

return keyToRequestCount.get(RateLimitUtil.keyFunction(ctx))
}

fun getRemainder(timeUnit: TimeUnit) : Long? {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fun getRemainder(timeUnit: TimeUnit) : Long? {
fun remainder(timeUnit: TimeUnit) : Long? {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, why does it take a TimeUnit ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You will want a different time unit for the remainder. e.g. take a rate limit of 5 requests per SECOND where the remaining time before the SECOND expires is in MILLISECONDS...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps I could use a float... 0.25 left of your TimeUnit; then you would not need another TimeUnit

return future?.getDelay(timeUnit)
}
}

object NaiveRateLimit {
Expand Down