Tickers leak
Closed this issue · 6 comments
eggman64 commented
Hello! Thanks for opening an issue. Could you describe in more detail the problem you are facing?
Thank you
eggman64 commented
OK. I did some digging around and saw this
golang/go#17757
Makes sense. Do you have a proposal for some alternate implementation?
funny-falcon commented
It is easy:
timeout := time.NewTimer(l.waitTime)
retry := time.NewTicker(l.retryTime)
defer retry.Stop()
for {
select {
case <-timeout.C:
return ErrCannotLock
case <-retry.C:
ok, err := l.client.SetNX(key, l.token, l.ttl).Result()
if err != nil {
timeout.Stop()
return fmt.Errorf("failed to acquire lock: %v", err)
}
if ok {
l.isHeld = true
timeout.Stop()
return nil
}
}
}
just never use time.Tick() if you are ain't gonna make infinite loop. And try to not use time.After() if there is a case to not wait until timeout.
eggman64 commented
Thanks! Would you like to send us a Pull Request? Or shall I use your code?
funny-falcon commented
You may use my code.
eggman64 commented
Thanks @funny-falcon for your help. Closing this issue!