MEDIGO/go-dlm

Tickers leak

Closed this issue · 6 comments

Hello! Thanks for opening an issue. Could you describe in more detail the problem you are facing?
Thank you

OK. I did some digging around and saw this
golang/go#17757
Makes sense. Do you have a proposal for some alternate implementation?

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.

Thanks! Would you like to send us a Pull Request? Or shall I use your code?

You may use my code.

Thanks @funny-falcon for your help. Closing this issue!