Lock attempts to acquire all locks in deterministic order. If any lock acquisition fails, all previously acquired locks are released (rollback). Parameters: - ctx: The context for managing the lock request lifecycle. - timeout: The time-to-live (TTL) for each lock. Returns an error if any lock could
(ctx context.Context, timeout time.Duration)
| 214 | // - timeout: The time-to-live (TTL) for each lock. |
| 215 | // Returns an error if any lock could not be acquired. |
| 216 | func (m *MultiLocker) Lock(ctx context.Context, timeout time.Duration) error { |
| 217 | acquiredCount := 0 |
| 218 | |
| 219 | for _, locker := range m.lockers { |
| 220 | err := locker.Lock(ctx, timeout) |
| 221 | if err != nil { |
| 222 | // Rollback: release all previously acquired locks in reverse order |
| 223 | m.rollback(ctx, acquiredCount) |
| 224 | return fmt.Errorf("failed to acquire lock for key %s: %w", locker.key, err) |
| 225 | } |
| 226 | acquiredCount++ |
| 227 | } |
| 228 | |
| 229 | return nil |
| 230 | } |
| 231 | |
| 232 | // WaitLock tries to acquire all locks within a specified waiting period. |
| 233 | // It will attempt to acquire locks with exponential backoff if any lock is held. |