Fix code that assumes that ticker.Stop closes the ticker channel

Ian Lance Taylor 2020-04-18 13:04:36 -07:00
parent ca416e3f5b
commit 08857d1646

@ -45,16 +45,22 @@ type Payload struct {}
// BurstRateLimitCall allows burst rate limiting client calls with the // BurstRateLimitCall allows burst rate limiting client calls with the
// payloads. // payloads.
func BurstRateLimitCall(client Client, payloads []*Payload, burstLimit int) { func BurstRateLimitCall(ctx context.Context, client Client, payloads []*Payload, burstLimit int) {
ticker := time.NewTicker(rateLimit)
defer ticker.Stop()
throttle := make(chan time.Time, burstLimit) throttle := make(chan time.Time, burstLimit)
ctx, cancel := context.WithCancel(ctx)
defer cancel()
go func() { go func() {
ticker := time.NewTicker(rateLimit)
defer ticker.Stop()
for t := range ticker.C { for t := range ticker.C {
throttle <- t select {
} // for loop will complete the range after tick.Stop() closes tick.C case throttle <- t:
case <-ctx.Done():
return // exit goroutine when surrounding function returns
}
}
}() }()
for _, payload := range payloads { for _, payload := range payloads {