1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-09-28 05:08:59 +00:00
arikawa/utils/ws/throttler.go

40 lines
1.1 KiB
Go
Raw Normal View History

package ws
2020-01-09 05:24:45 +00:00
import (
"time"
"golang.org/x/time/rate"
)
// SendBurst determines the number of gateway commands that can be sent all at
// once before being throttled. The higher the burst, the slower the rate
// limiter recovers.
var SendBurst = 5
// NewSendLimiter returns a rate limiter for throttling gateway commands.
2020-01-09 05:24:45 +00:00
func NewSendLimiter() *rate.Limiter {
const perMinute = 120
return rate.NewLimiter(
// Permit r = minute / (120 - b) commands per second.
rate.Every(time.Minute/(perMinute-time.Duration(SendBurst))),
SendBurst,
)
2020-01-09 05:24:45 +00:00
}
2020-01-15 04:43:34 +00:00
// NewDialLimiter returns a rate limiter for throttling new gateway connections.
2020-01-15 04:43:34 +00:00
func NewDialLimiter() *rate.Limiter {
return rate.NewLimiter(rate.Every(5*time.Second), 1)
}
2020-01-15 07:34:18 +00:00
// NewIdentityLimiter returns a rate limiter for throttling gateway Identify
// commands.
2020-01-15 07:34:18 +00:00
func NewIdentityLimiter() *rate.Limiter {
return NewDialLimiter() // same
}
// NewGlobalIdentityLimiter returns a rate limiter for throttling global
// gateway Identify commands.
2020-01-15 07:34:18 +00:00
func NewGlobalIdentityLimiter() *rate.Limiter {
return rate.NewLimiter(rate.Every(24*time.Hour), 1000)
}