Skip to content

Commit 564379b

Browse files
committed
Add configurable amount of tokens in PriorityGlobalRestRateLimiter
1 parent 67bcb73 commit 564379b

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

src/main/kotlin/io/github/freya022/botcommands/api/core/JDAService.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,12 @@ abstract class JDAService {
283283
}
284284

285285
/**
286-
* Returns a [PriorityGlobalRestRateLimiter] using a [SequentialRestRateLimiter] as its [delegate][PriorityGlobalRestRateLimiter.delegate]
286+
* Returns a [PriorityGlobalRestRateLimiter] with 50 requests/s,
287+
* using a [SequentialRestRateLimiter] as its [delegate][PriorityGlobalRestRateLimiter.delegate].
287288
*/
288289
@JvmStatic
289290
fun getDefaultRestRateLimiter(rlConfig: RateLimitConfig): RestRateLimiter {
290-
return PriorityGlobalRestRateLimiter(SequentialRestRateLimiter(rlConfig))
291+
return PriorityGlobalRestRateLimiter(tokens = 50, SequentialRestRateLimiter(rlConfig))
291292
}
292293
}
293294
}

src/main/kotlin/io/github/freya022/botcommands/api/core/requests/PriorityGlobalRestRateLimiter.kt

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package io.github.freya022.botcommands.api.core.requests
22

33
import io.github.bucket4j.Bandwidth
44
import io.github.bucket4j.Bucket
5+
import io.github.freya022.botcommands.api.core.JDAService
56
import io.github.freya022.botcommands.api.core.errorNull
67
import io.github.freya022.botcommands.internal.core.exceptions.getDiagnosticVersions
78
import io.github.oshai.kotlinlogging.KotlinLogging
89
import net.dv8tion.jda.api.requests.RestRateLimiter
910
import net.dv8tion.jda.api.requests.Route
1011
import net.dv8tion.jda.api.sharding.DefaultShardManagerBuilder
11-
import java.util.PriorityQueue
12+
import java.util.*
1213
import java.util.concurrent.Executors
1314
import java.util.concurrent.locks.ReentrantLock
1415
import kotlin.concurrent.thread
@@ -19,7 +20,7 @@ import kotlin.time.toJavaDuration
1920
private val logger = KotlinLogging.logger { }
2021

2122
/**
22-
* An implementation of [RestRateLimiter] which handles the global rate limit (50/s),
23+
* An implementation of [RestRateLimiter] which handles the global rate limit,
2324
* and in which low-priority requests are queued last to the [delegate] (such as application command updates).
2425
*
2526
* While JDA already reads the global rate limit headers, it does not prevent doing 50+ requests on different buckets.
@@ -32,7 +33,8 @@ private val logger = KotlinLogging.logger { }
3233
*
3334
* ### Usage
3435
*
35-
* You will need to configure the RestConfig on your [DefaultShardManagerBuilder][DefaultShardManagerBuilder.setRestConfig].
36+
* You will need to configure the RestConfig on your [DefaultShardManagerBuilder][DefaultShardManagerBuilder.setRestConfig],
37+
* you don't need to do this if you used a factory from [JDAService] (light/default/create/lightSharded/defaultSharded/createSharded).
3638
*
3739
* **Example:**
3840
* ```kt
@@ -42,8 +44,14 @@ private val logger = KotlinLogging.logger { }
4244
* }
4345
* setRestConfig(restConfig)
4446
* ```
47+
*
48+
* @param tokens The maximum amount of permitted requests per second
49+
* @param delegate The [RestRateLimiter] to use when submitting a request
4550
*/
46-
class PriorityGlobalRestRateLimiter(private val delegate: RestRateLimiter) : RestRateLimiter {
51+
class PriorityGlobalRestRateLimiter(
52+
tokens: Long,
53+
private val delegate: RestRateLimiter
54+
) : RestRateLimiter {
4755
private class PriorityWork(val task: RestRateLimiter.Work) : Comparable<PriorityWork> {
4856
override fun compareTo(other: PriorityWork): Int {
4957
return task.priority.compareTo(other.task.priority)
@@ -62,12 +70,12 @@ class PriorityGlobalRestRateLimiter(private val delegate: RestRateLimiter) : Res
6270
}
6371
}
6472

65-
// 50/s
73+
// {tokens}/s
6674
private val bucket = Bucket.builder()
6775
.addLimit(
6876
Bandwidth.builder()
69-
.capacity(50)
70-
.refillIntervally(50, 1.seconds.toJavaDuration())
77+
.capacity(tokens)
78+
.refillIntervally(tokens, 1.seconds.toJavaDuration())
7179
.build()
7280
)
7381
.build()

0 commit comments

Comments
 (0)