github.com/sandwich-go/boost@v1.3.29/ratelimiter/ratelimit.go (about) 1 package ratelimiter 2 3 import ( 4 "time" 5 6 "github.com/sandwich-go/boost/z" 7 ) 8 9 // Note: This file is inspired by: 10 // https://github.com/prashantv/go-bench/blob/master/ratelimit 11 12 // Limiter is used to rate-limit some process, possibly across goroutines. 13 // The process is expected to call Take() before every iteration, which 14 // may block to throttle the goroutine. 15 type Limiter interface { 16 // Take should block to make sure that the RPS is met. 17 Take() time.Time 18 } 19 20 // New returns a Limiter that will limit to the given RPS. 21 func New(rate int, opts ...Option) Limiter { 22 return newAtomicInt64Based(rate, opts...) 23 } 24 25 type unlimited struct{} 26 27 // NewUnlimited returns a RateLimiter that is not limited. 28 func NewUnlimited() Limiter { 29 return unlimited{} 30 } 31 32 func (unlimited) Take() time.Time { 33 return z.Now() 34 }