golang.org rate limiter:
rl := rate.NewLimiter(1000000, 1)
last := time.Now()
for i := 0; i < 10; i++ {
rl.Wait(context.Background())
cur := time.Now()
fmt.Println("last", cur.Sub(last))
last = cur
}
Output:
last 16.038µs
last 11.367µs
last 4.051µs
last 4.582µs
last 2.146µs
last 3.085µs
last 2.173µs
last 3.121µs
last 1.984µs
last 3.09µs
uber-go rate limiter:
rl := ratelimit.New(1000000, ratelimit.WithoutSlack)
last := time.Now()
for i := 0; i < 10; i++ {
rl.Take()
cur := time.Now()
fmt.Println("last", cur.Sub(last))
last = cur
}
Output:
last 315ns
last 1.846µs
last 1.3µs
last 1.218µs
last 1.191µs
last 1.202µs
last 1.206µs
last 1.216µs
last 1.166µs
last 1.18µs
What about the context.Background() call? You're measuring that, too!