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

[Latte] Add tryRequestQuota for SuspendingRatelimit #14

Merged
merged 2 commits into from
Oct 13, 2024
Merged
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
37 changes: 30 additions & 7 deletions latte/src/main/java/gg/beemo/latte/util/SuspendingRatelimit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,55 @@ import kotlinx.coroutines.sync.withPermit
import kotlin.time.Duration

class SuspendingRatelimit(private val burst: Int, private val duration: Duration) {

@Volatile
private var remainingQuota: Int = burst

@Volatile
private var resetTimestamp: Long = 0

private val quotaRequestSem = Semaphore(1)

fun overrideRatelimit(remainingQuota: Int, resetTimestamp: Long) {
fun overrideRatelimit(
remainingQuota: Int,
resetTimestamp: Long,
) {
this.remainingQuota = remainingQuota
this.resetTimestamp = resetTimestamp
}

private fun calculateWaitTime(): Long {
return (resetTimestamp - System.currentTimeMillis()).coerceAtLeast(0)
}

private fun tryResetQuota() {
if (System.currentTimeMillis() >= resetTimestamp) {
remainingQuota = burst
resetTimestamp = System.currentTimeMillis() + duration.inWholeMilliseconds
}
}

suspend fun requestQuota() {
quotaRequestSem.withPermit {
if (remainingQuota <= 0) {
val waitTime = (resetTimestamp - System.currentTimeMillis()).coerceAtLeast(0)
val waitTime = calculateWaitTime()
delay(waitTime)
}
if (System.currentTimeMillis() >= resetTimestamp) {
remainingQuota = burst
resetTimestamp = System.currentTimeMillis() + duration.inWholeMilliseconds
}
tryResetQuota()

check(remainingQuota > 0)
remainingQuota--
}
}

fun tryRequestQuota(): Pair<Boolean, Long?> {
if (remainingQuota <= 0) {
val waitTime = calculateWaitTime()
return false to waitTime
}
tryResetQuota()

check(remainingQuota > 0)
remainingQuota--
Comment on lines +56 to +57
Copy link
Member

Choose a reason for hiding this comment

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

Given thas these two lines are the same in the new and the old method, I think we should also move them to the common function and perhaps rename it to something a bit more generic then, like calculateQuota. Otherwise LGTM.

Copy link
Member Author

Choose a reason for hiding this comment

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

Somehow, moving these two lines to a common function would result in the function misbehaving (failing), so yeah, I think we can just keep it like that, given that it's just two lines. What do you think? (I think it's because of the check that's failing).

return true to null
}
}
Loading