ExtendLock extends the TTL of the lock if the calling instance is the lock holder. This method ensures that the lock is renewed only by the lock holder. Parameters: - ctx: The context for managing the extension request. - extension: The additional time to extend the lock by. Returns an error if the
(ctx context.Context, extension time.Duration)
| 125 | // Returns an error if the extension fails, either because the lock expired or |
| 126 | // the caller is not the lock holder. |
| 127 | func (l *Locker) ExtendLock(ctx context.Context, extension time.Duration) error { |
| 128 | // Lua script ensures atomicity: checks the value and extends the TTL if the value matches. |
| 129 | script := "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('pexpire', KEYS[1], ARGV[2]) else return 0 end" |
| 130 | result, err := l.client.Eval(ctx, script, []string{l.key}, l.value, fmt.Sprintf("%d", extension.Milliseconds())).Result() |
| 131 | if err != nil { |
| 132 | return err |
| 133 | } |
| 134 | if result == int64(0) { |
| 135 | return fmt.Errorf("lock extension failed for key %s, either lock expired or you're not the holder", l.key) |
| 136 | } |
| 137 | return nil |
| 138 | } |
| 139 | |
| 140 | // WaitLock tries to acquire the lock within a specified waiting period. |
| 141 | // It will attempt to acquire the lock with exponential backoff if the lock is held by another process. |
no outgoing calls