mirror of
https://github.com/golang/go.git
synced 2025-05-05 15:43:04 +00:00
Fix code that assumes that ticker.Stop closes the ticker channel
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 {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user