2021-09-28 20:19:04 +00:00
|
|
|
package ws
|
2020-01-09 05:24:45 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/time/rate"
|
|
|
|
)
|
|
|
|
|
2022-12-04 03:19:07 +00:00
|
|
|
// 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 {
|
2022-12-04 03:19:07 +00:00
|
|
|
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
|
|
|
|
2022-12-04 03:19:07 +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
|
|
|
|
2022-12-04 03:19:07 +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
|
|
|
|
}
|
|
|
|
|
2022-12-04 03:19:07 +00:00
|
|
|
// 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)
|
|
|
|
}
|