Esempio n. 1
0
// NewClientWrapper creates a blocking side rate limiter
func NewClientWrapper(rate int) client.Wrapper {
	r := ratelimit.New(rate)

	return func(c client.Client) client.Client {
		return &clientWrapper{r, c}
	}
}
Esempio n. 2
0
// NewHandlerWrapper creates a blocking server side rate limiter
func NewHandlerWrapper(rate int) server.HandlerWrapper {
	r := ratelimit.New(rate)

	return func(h server.HandlerFunc) server.HandlerFunc {
		return func(ctx context.Context, req server.Request, rsp interface{}) error {
			r.Take()
			return h(ctx, req, rsp)
		}
	}
}
Esempio n. 3
0
func main() {
	rl := ratelimit.New(10) // per second

	prev := time.Now()
	for i := 0; i < 10; i++ {
		//每次调用前take一下就好了,有时间看看实现~
		now := rl.Take()
		fmt.Println(i, now.Sub(prev))
		prev = now
	}
}