1
1
package discordgo
2
2
3
3
import (
4
+ "encoding/json"
4
5
"net/http"
5
6
"strconv"
6
7
"strings"
@@ -236,7 +237,21 @@ func (b *Bucket) Release(headers http.Header, lockCounter int64) error {
236
237
return err
237
238
}
238
239
239
- resetAt := time .Now ().Add (time .Duration (parsedAfter ) * time .Millisecond )
240
+ var afterD time.Duration
241
+
242
+ // This is a fix for cloudflare sending ratelimits in seconds while discord in milliseconds
243
+ // This can be removed for gateway v8
244
+ if strings .Contains (headers .Get ("via" ), "1.1 google" ) {
245
+ afterD = time .Duration (parsedAfter ) * time .Millisecond
246
+ } else {
247
+ afterD = time .Duration (parsedAfter ) * time .Second
248
+ }
249
+
250
+ if afterD > time .Second * 1000 {
251
+ logHighRatelimit (afterD , b .Key , headers )
252
+ }
253
+
254
+ resetAt := time .Now ().Add (afterD )
240
255
241
256
// Lock either this single bucket or all buckets
242
257
global := headers .Get ("X-RateLimit-Global" )
@@ -251,7 +266,13 @@ func (b *Bucket) Release(headers http.Header, lockCounter int64) error {
251
266
return err
252
267
}
253
268
254
- b .reset = time .Now ().Add (time .Millisecond * time .Duration (resetAfterParsed * 1000 ))
269
+ dur := time .Millisecond * time .Duration (resetAfterParsed * 1000 )
270
+
271
+ if dur > time .Second * 1000 {
272
+ logHighRatelimit (dur , b .Key , headers )
273
+ }
274
+
275
+ b .reset = time .Now ().Add (dur )
255
276
}
256
277
257
278
// Udpate remaining if header is present
@@ -266,3 +287,8 @@ func (b *Bucket) Release(headers http.Header, lockCounter int64) error {
266
287
267
288
return nil
268
289
}
290
+
291
+ func logHighRatelimit (limit time.Duration , bucket string , headers http.Header ) {
292
+ serialized , _ := json .Marshal (headers )
293
+ msglog (LogError , 2 , "very high ratelimit on %s: %s, headers: %s" , bucket , limit , string (serialized ))
294
+ }
0 commit comments