@@ -2,13 +2,14 @@ package io.github.freya022.botcommands.api.core.requests
2
2
3
3
import io.github.bucket4j.Bandwidth
4
4
import io.github.bucket4j.Bucket
5
+ import io.github.freya022.botcommands.api.core.JDAService
5
6
import io.github.freya022.botcommands.api.core.errorNull
6
7
import io.github.freya022.botcommands.internal.core.exceptions.getDiagnosticVersions
7
8
import io.github.oshai.kotlinlogging.KotlinLogging
8
9
import net.dv8tion.jda.api.requests.RestRateLimiter
9
10
import net.dv8tion.jda.api.requests.Route
10
11
import net.dv8tion.jda.api.sharding.DefaultShardManagerBuilder
11
- import java.util.PriorityQueue
12
+ import java.util.*
12
13
import java.util.concurrent.Executors
13
14
import java.util.concurrent.locks.ReentrantLock
14
15
import kotlin.concurrent.thread
@@ -19,7 +20,7 @@ import kotlin.time.toJavaDuration
19
20
private val logger = KotlinLogging .logger { }
20
21
21
22
/* *
22
- * An implementation of [RestRateLimiter] which handles the global rate limit (50/s) ,
23
+ * An implementation of [RestRateLimiter] which handles the global rate limit,
23
24
* and in which low-priority requests are queued last to the [delegate] (such as application command updates).
24
25
*
25
26
* 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 { }
32
33
*
33
34
* ### Usage
34
35
*
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).
36
38
*
37
39
* **Example:**
38
40
* ```kt
@@ -42,8 +44,14 @@ private val logger = KotlinLogging.logger { }
42
44
* }
43
45
* setRestConfig(restConfig)
44
46
* ```
47
+ *
48
+ * @param tokens The maximum amount of permitted requests per second
49
+ * @param delegate The [RestRateLimiter] to use when submitting a request
45
50
*/
46
- class PriorityGlobalRestRateLimiter (private val delegate : RestRateLimiter ) : RestRateLimiter {
51
+ class PriorityGlobalRestRateLimiter (
52
+ tokens : Long ,
53
+ private val delegate : RestRateLimiter
54
+ ) : RestRateLimiter {
47
55
private class PriorityWork (val task : RestRateLimiter .Work ) : Comparable<PriorityWork> {
48
56
override fun compareTo (other : PriorityWork ): Int {
49
57
return task.priority.compareTo(other.task.priority)
@@ -62,12 +70,12 @@ class PriorityGlobalRestRateLimiter(private val delegate: RestRateLimiter) : Res
62
70
}
63
71
}
64
72
65
- // 50 /s
73
+ // {tokens} /s
66
74
private val bucket = Bucket .builder()
67
75
.addLimit(
68
76
Bandwidth .builder()
69
- .capacity(50 )
70
- .refillIntervally(50 , 1 .seconds.toJavaDuration())
77
+ .capacity(tokens )
78
+ .refillIntervally(tokens , 1 .seconds.toJavaDuration())
71
79
.build()
72
80
)
73
81
.build()
0 commit comments