25 lines
342 B
Go
25 lines
342 B
Go
|
package discord
|
||
|
|
||
|
import "time"
|
||
|
|
||
|
type rateLimit struct {
|
||
|
rate time.Duration
|
||
|
next time.Time
|
||
|
}
|
||
|
|
||
|
func NewRateLimit(rate time.Duration) *rateLimit {
|
||
|
return &rateLimit{
|
||
|
rate: rate,
|
||
|
next: time.Now(),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (r *rateLimit) Try() bool {
|
||
|
now := time.Now()
|
||
|
if now.After(r.next) {
|
||
|
r.next = now.Add(r.rate)
|
||
|
return true
|
||
|
}
|
||
|
return false
|
||
|
}
|