github.com/songzhibin97/gkit@v1.2.13/restrictor/rate/rate.go (about) 1 package rate 2 3 // package rate: https://pkg.go.dev/golang.org/x/time/rate 实现 limiter 接口 4 5 import ( 6 "context" 7 "time" 8 9 "github.com/songzhibin97/gkit/restrictor" 10 "golang.org/x/time/rate" 11 ) 12 13 // NewRate 返回limiter对应的 restrictor.AllowFunc, restrictor.WaitFunc 14 func NewRate(limiter *rate.Limiter) (restrictor.AllowFunc, restrictor.WaitFunc) { 15 return func(now time.Time, n int) bool { 16 return limiter.AllowN(now, n) 17 }, func(ctx context.Context, n int) error { 18 return limiter.WaitN(ctx, n) 19 } 20 }