Coder Social home page Coder Social logo

Comments (4)

segevfiner avatar segevfiner commented on May 24, 2024

Here is the workaround I had to write:

// obtainLockIndefinitly tries to obtain a new lock using a key with the given TTL, indefinitly as
// long as the retry strategy says to retry.
// May return ErrNotObtained if not successful.
//
// XXX This is a workaround around Obtain having an unsettable deadline, setting it hardcoded to
// the ttl.
func obtainLockIndefinitly(client *redislock.Client, key string, ttl time.Duration, opt *redislock.Options) (*redislock.Lock, error) {
	var opt2 redislock.Options
	if opt != nil {
		opt2 = *opt
	}

	ctx := context.Background()
	if opt != nil && opt.Context != nil {
		ctx = opt.Context
	}

	retry := redislock.NoRetry()
	if opt != nil && opt.RetryStrategy != nil {
		retry = opt.RetryStrategy
		opt2.RetryStrategy = nil
	}

	var timer *time.Timer
	for {
		lock, err := client.Obtain(key, ttl, &opt2)
		if err != nil && err != redislock.ErrNotObtained {
			return nil, err
		} else if err == nil {
			return lock, nil
		}

		backoff := retry.NextBackoff()
		if backoff < 1 {
			break
		}

		if timer == nil {
			timer = time.NewTimer(backoff)
			defer timer.Stop()
		} else {
			timer.Reset(backoff)
		}

		select {
		case <-ctx.Done():
			return nil, ctx.Err()
		case <-timer.C:
		}
	}

	return nil, redislock.ErrNotObtained
}

from redislock.

dim avatar dim commented on May 24, 2024

@segevfiner in reality you need to have some sort of overall deadline, because any lock is by nature temporary and not permanent. That being said, nothing prevents you from setting a HUGE ttl, e.g. 365*24*time.Hour. Wouldn't that solve the problem?

from redislock.

segevfiner avatar segevfiner commented on May 24, 2024

No. Because I still want to have a short TTL for the lock itself in Redis, I only want to try locking it indefinitely, but once it is locked, it should have a short TTL and the code will take care of refreshing it.

from redislock.

dim avatar dim commented on May 24, 2024

How about #22?

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.