-
-
Notifications
You must be signed in to change notification settings - Fork 564
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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) { | ||||||
|
@@ -38,6 +40,14 @@ class RateLimiter(val timeUnit: TimeUnit) { | |||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
fun getCounter(ctx: Context) : Int? { | ||||||
return keyToRequestCount.get(RateLimitUtil.keyFunction(ctx)) | ||||||
} | ||||||
|
||||||
fun getRemainder(timeUnit: TimeUnit) : Long? { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wait, why does it take a TimeUnit ? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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... There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.