Skip to content

Commit bb035a9

Browse files
committed
Revert "refactor RateLimitUtil a bit"
This reverts commit 013de4a.
1 parent 65b754c commit bb035a9

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

src/main/kotlin/app/util/RateLimitUtil.kt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,28 @@ object RateLimitUtil {
2222
fun enableTerribleRateLimiting(app: Javalin) {
2323

2424
app.before { ctx ->
25-
ipReqCount.compute(ctx.ip()) { _, count ->
25+
ipReqCount.compute(ctx.ip(), { _, count ->
2626
when (count) {
2727
null -> 1
2828
in 0..25 -> count + 1
2929
else -> throw TerribleRateLimitException()
3030
}
31-
}
31+
})
3232
}
3333

3434
app.exception(TerribleRateLimitException::class.java) { _, ctx ->
3535
ctx.result("You can't spam this much. I'll give you a new request every five seconds.")
3636
}
3737

38-
Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate({
39-
ipReqCount.forEachKey(1) { ip ->
40-
ipReqCount.computeIfPresent(ip) { _, count -> if (count > 1) count - 1 else null }
41-
}
42-
}, 0, 5, TimeUnit.SECONDS)
38+
Executors.newSingleThreadScheduledExecutor()
39+
.scheduleAtFixedRate(decrementAllCounters, 0, 5, TimeUnit.SECONDS)
4340

4441
}
4542

43+
private val decrementAllCounters = Runnable {
44+
ipReqCount.forEachKey(1, { ip ->
45+
ipReqCount.computeIfPresent(ip, { _, count -> if (count > 1) count - 1 else null })
46+
})
47+
}
48+
4649
}

0 commit comments

Comments
 (0)