Skip to content

Commit b74e2b0

Browse files
committed
Revert "Revert "refactor RateLimitUtil a bit""
This reverts commit bb035a9.
1 parent 9f6d98e commit b74e2b0

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,25 @@ 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()
39-
.scheduleAtFixedRate(decrementAllCounters, 0, 5, TimeUnit.SECONDS)
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)
4043

4144
}
4245

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-
4946
}

0 commit comments

Comments
 (0)