matterbridge/vendor/github.com/bwmarrin/discordgo/ratelimit.go

195 lines
4.7 KiB
Go
Raw Normal View History

2017-01-28 21:45:32 +00:00
package discordgo
import (
"net/http"
"strconv"
"strings"
2017-01-28 21:45:32 +00:00
"sync"
2017-07-16 12:38:45 +00:00
"sync/atomic"
2017-01-28 21:45:32 +00:00
"time"
)
// customRateLimit holds information for defining a custom rate limit
type customRateLimit struct {
suffix string
requests int
reset time.Duration
}
2017-01-28 21:45:32 +00:00
// RateLimiter holds all ratelimit buckets
type RateLimiter struct {
sync.Mutex
global *int64
buckets map[string]*Bucket
globalRateLimit time.Duration
customRateLimits []*customRateLimit
2017-01-28 21:45:32 +00:00
}
// NewRatelimiter returns a new RateLimiter
func NewRatelimiter() *RateLimiter {
return &RateLimiter{
buckets: make(map[string]*Bucket),
2017-07-16 12:38:45 +00:00
global: new(int64),
customRateLimits: []*customRateLimit{
&customRateLimit{
suffix: "//reactions//",
requests: 1,
reset: 200 * time.Millisecond,
},
},
2017-01-28 21:45:32 +00:00
}
}
2018-02-14 21:22:35 +00:00
// GetBucket retrieves or creates a bucket
func (r *RateLimiter) GetBucket(key string) *Bucket {
2017-01-28 21:45:32 +00:00
r.Lock()
defer r.Unlock()
if bucket, ok := r.buckets[key]; ok {
return bucket
}
b := &Bucket{
2018-02-14 21:22:35 +00:00
Remaining: 1,
2017-01-28 21:45:32 +00:00
Key: key,
global: r.global,
}
// Check if there is a custom ratelimit set for this bucket ID.
for _, rl := range r.customRateLimits {
if strings.HasSuffix(b.Key, rl.suffix) {
b.customRateLimit = rl
break
}
}
2017-01-28 21:45:32 +00:00
r.buckets[key] = b
return b
}
2018-02-14 21:22:35 +00:00
// GetWaitTime returns the duration you should wait for a Bucket
func (r *RateLimiter) GetWaitTime(b *Bucket, minRemaining int) time.Duration {
2017-01-28 21:45:32 +00:00
// If we ran out of calls and the reset time is still ahead of us
// then we need to take it easy and relax a little
2018-02-14 21:22:35 +00:00
if b.Remaining < minRemaining && b.reset.After(time.Now()) {
return b.reset.Sub(time.Now())
2017-01-28 21:45:32 +00:00
}
// Check for global ratelimits
2017-07-16 12:38:45 +00:00
sleepTo := time.Unix(0, atomic.LoadInt64(r.global))
if now := time.Now(); now.Before(sleepTo) {
2018-02-14 21:22:35 +00:00
return sleepTo.Sub(now)
}
return 0
}
// LockBucket Locks until a request can be made
func (r *RateLimiter) LockBucket(bucketID string) *Bucket {
return r.LockBucketObject(r.GetBucket(bucketID))
}
// LockBucketObject Locks an already resolved bucket until a request can be made
func (r *RateLimiter) LockBucketObject(b *Bucket) *Bucket {
b.Lock()
if wait := r.GetWaitTime(b, 1); wait > 0 {
time.Sleep(wait)
2017-07-16 12:38:45 +00:00
}
2017-01-28 21:45:32 +00:00
2018-02-14 21:22:35 +00:00
b.Remaining--
2017-01-28 21:45:32 +00:00
return b
}
// Bucket represents a ratelimit bucket, each bucket gets ratelimited individually (-global ratelimits)
type Bucket struct {
sync.Mutex
Key string
2018-02-14 21:22:35 +00:00
Remaining int
2017-01-28 21:45:32 +00:00
limit int
reset time.Time
2017-07-16 12:38:45 +00:00
global *int64
lastReset time.Time
customRateLimit *customRateLimit
2018-02-14 21:22:35 +00:00
Userdata interface{}
2017-01-28 21:45:32 +00:00
}
// Release unlocks the bucket and reads the headers to update the buckets ratelimit info
// and locks up the whole thing in case if there's a global ratelimit.
func (b *Bucket) Release(headers http.Header) error {
defer b.Unlock()
// Check if the bucket uses a custom ratelimiter
if rl := b.customRateLimit; rl != nil {
if time.Now().Sub(b.lastReset) >= rl.reset {
2018-02-14 21:22:35 +00:00
b.Remaining = rl.requests - 1
b.lastReset = time.Now()
}
2018-02-14 21:22:35 +00:00
if b.Remaining < 1 {
b.reset = time.Now().Add(rl.reset)
}
return nil
}
2017-01-28 21:45:32 +00:00
if headers == nil {
return nil
}
remaining := headers.Get("X-RateLimit-Remaining")
reset := headers.Get("X-RateLimit-Reset")
global := headers.Get("X-RateLimit-Global")
retryAfter := headers.Get("Retry-After")
2017-07-16 12:38:45 +00:00
// Update global and per bucket reset time if the proper headers are available
// If global is set, then it will block all buckets until after Retry-After
// If Retry-After without global is provided it will use that for the new reset
// time since it's more accurate than X-RateLimit-Reset.
// If Retry-After after is not proided, it will update the reset time from X-RateLimit-Reset
2017-01-28 21:45:32 +00:00
if retryAfter != "" {
parsedAfter, err := strconv.ParseInt(retryAfter, 10, 64)
if err != nil {
return err
}
2017-07-16 12:38:45 +00:00
resetAt := time.Now().Add(time.Duration(parsedAfter) * time.Millisecond)
// Lock either this single bucket or all buckets
if global != "" {
atomic.StoreInt64(b.global, resetAt.UnixNano())
} else {
b.reset = resetAt
}
2017-01-28 21:45:32 +00:00
} else if reset != "" {
// Calculate the reset time by using the date header returned from discord
discordTime, err := http.ParseTime(headers.Get("Date"))
if err != nil {
return err
}
unix, err := strconv.ParseInt(reset, 10, 64)
if err != nil {
return err
}
// Calculate the time until reset and add it to the current local time
// some extra time is added because without it i still encountered 429's.
// The added amount is the lowest amount that gave no 429's
// in 1k requests
delta := time.Unix(unix, 0).Sub(discordTime) + time.Millisecond*250
b.reset = time.Now().Add(delta)
}
// Udpate remaining if header is present
if remaining != "" {
parsedRemaining, err := strconv.ParseInt(remaining, 10, 32)
if err != nil {
return err
}
2018-02-14 21:22:35 +00:00
b.Remaining = int(parsedRemaining)
2017-01-28 21:45:32 +00:00
}
return nil
}