11package discordgo
22
33import (
4+ "encoding/json"
45 "net/http"
56 "strconv"
67 "strings"
@@ -236,7 +237,21 @@ func (b *Bucket) Release(headers http.Header, lockCounter int64) error {
236237 return err
237238 }
238239
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 )
240255
241256 // Lock either this single bucket or all buckets
242257 global := headers .Get ("X-RateLimit-Global" )
@@ -251,7 +266,13 @@ func (b *Bucket) Release(headers http.Header, lockCounter int64) error {
251266 return err
252267 }
253268
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 )
255276 }
256277
257278 // Udpate remaining if header is present
@@ -266,3 +287,8 @@ func (b *Bucket) Release(headers http.Header, lockCounter int64) error {
266287
267288 return nil
268289}
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