Coder Social home page Coder Social logo

Comments (5)

dim avatar dim commented on May 27, 2024

Excellent point, would you mind submit a PR with a fix?

from redislock.

pierrre avatar pierrre commented on May 27, 2024

Actually, I'm not really a user of this library.
I'm implementing a similar library, which provides slightly different features (and I don't want to disturb you with these features).

I don't know how to fix the exponential backoff without breaking the backward compatibility of your library.

Here is my code.
It's not tested, because I'm still working on it.

// ExponentialRetry retries after an exponential delay.
// The delay is multiplied by two, up to the maximum value.
func ExponentialRetry(start, max time.Duration) RetryStrategy {
	return &exponentialRetry{
		d:   start,
		max: max,
	}
}

type exponentialRetry struct {
	d   time.Duration
	max time.Duration
}

func (r *exponentialRetry) Retry() (time.Duration, bool) {
	if r.d > r.max {
		return r.max, true
	}
	d := r.d
	r.d *= 2
	return d, true
}

Of course, it suffers from the same "overflow" issue, but the user will have to wait until the duration is reached, which can't happen, because the value is too high.
In your implementation the "multiply by 2" logic is always computed, even if the maximum is reached.

from redislock.

dim avatar dim commented on May 27, 2024

So the idea there is to increase backoff using 2^n rather than n*2. It's only a theoretical problem though, as 2<<55 => 72057594037927936 which is 2.2 million years. I therefore doubt it would ever overflow :)

from redislock.

pierrre avatar pierrre commented on May 27, 2024

The problem in this code is real

redislock/redislock.go

Lines 279 to 286 in 1b9b4ef

r.cnt++
if d := time.Duration(2<<r.cnt) * time.Millisecond; d < r.min {
return r.min
} else if r.max != 0 && d > r.max {
return r.max
} else {
return d
}

Let's say that r.max is equal to 1 second.
For the 53/54/55 retry you will have very high values, but it will be limited by the max value of 1 second.
For the 56th retry, you will start to have a negative value, so it will be limited by the min value.
For the 57th retry, the value will always be 0, so it will be limited by the min value.

You don't have to wait for a very long time if you want to trigger this bug, if the "max" limit is low enough.
It happens after the 56th retry.

from redislock.

dim avatar dim commented on May 27, 2024

Addressed in #8, thanks for the report

from redislock.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.