uber-go / ratelimit

A Go blocking leaky-bucket rate limit implementation
MIT License
4.32k stars 300 forks source link

Behavior of take in goroutine #123

Closed YusukeShimizu closed 8 months ago

YusukeShimizu commented 8 months ago

It seems that sometimes Take() is not well controlled when it is run inside a goroutine. Is this expected behavior? I have confirmed that by defining take before goroutine, the problem does not occur.

func main() {
    rl := ratelimit.New(10)
    eg := errgroup.Group{}
    for {
        eg.Go(func() error {
            rl.Take()
            time.Sleep(time.Second)
            fmt.Println(time.Now().Format("15:04:05.00"))
            return nil
        })
    }
}

// The output shows that the rate limit is not working.
// 16:34:37.16
// 16:34:37.16
// 16:34:37.54
// 16:34:37.54
// 16:34:37.54
// 16:34:37.54
// 16:34:37.54
// 16:34:37.55
// 16:34:37.65
// 16:34:37.75
YusukeShimizu commented 8 months ago

Closed due to a misunderstanding.