MCPcopy Create free account
hub / github.com/devaccuracy/ledgerforge / ExtendLock

Method ExtendLock

internal/lock/lock.go:127–138  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

125// Returns an error if the extension fails, either because the lock expired or
126// the caller is not the lock holder.
127func (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.

Callers 2

Calls

no outgoing calls

Tested by 2