bsm / redislock

Simplified distributed locking implementation using Redis
Other
1.45k stars 157 forks source link

when obtain have retry, hope the lock ttl will auto delay #74

Closed KM612 closed 8 months ago

KM612 commented 9 months ago

lock, err := locker.Obtain(ctx, fmt.Sprintf("update-ml-folder:%v", folder.ID), 12*time.Second, &redislock.Options{ RetryStrategy: redislock.LimitRetry(redislock.LinearBackoff(2*time.Second), 30), })

like this code, it will timeout after 12 second(if some one also hold the lock), I hope it will wait 60 second and when it get the lock success,also will hold the lock 12 second

dim commented 8 months ago

To do that, you need to pass a context with a custom deadline. At the moment, the TTL is used by default https://github.com/bsm/redislock/blob/main/redislock.go#L89.

Do this:

ctx, cancel = context.WithDeadline(ctx, time.Now().Add(time.Minute)) // wait for up-to 60s
defer cancel()

lock, err := locker.Obtain(ctx, ...)
KM612 commented 8 months ago

great, thank you