acquireLock acquires distributed locks for a transaction to ensure exclusive access to both source and destination balances. It uses a MultiLocker with deterministic ordering to prevent deadlocks when multiple transactions target the same balances. Parameters: - ctx context.Context: The context for
(ctx context.Context, sourceBalanceID, destinationBalanceID string)
| 267 | // - *redlock.MultiLocker: A pointer to the acquired MultiLocker if successful. |
| 268 | // - error: An error if the locks could not be acquired. |
| 269 | func (l *LedgerForge) acquireLock(ctx context.Context, sourceBalanceID, destinationBalanceID string) (*redlock.MultiLocker, error) { |
| 270 | ctx, span := tracer.Start(ctx, "Acquiring Lock") |
| 271 | defer span.End() |
| 272 | |
| 273 | // Create a MultiLocker with both balance IDs |
| 274 | // MultiLocker handles deduplication (if source == destination) and sorts keys lexicographically |
| 275 | locker := redlock.NewMultiLocker(l.redis, []string{sourceBalanceID, destinationBalanceID}, model.GenerateUUIDWithSuffix("loc")) |
| 276 | err := l.acquireTransactionLocker(ctx, locker) |
| 277 | if err != nil { |
| 278 | span.RecordError(err) |
| 279 | return nil, err |
| 280 | } |
| 281 | span.AddEvent("Locks acquired", trace.WithAttributes( |
| 282 | attribute.StringSlice("locked.keys", locker.Keys()), |
| 283 | )) |
| 284 | return locker, nil |
| 285 | } |
| 286 | |
| 287 | // updateTransactionDetails updates the details of a transaction, including source and destination balances and status. |
| 288 | // It starts a tracing span, creates a new transaction object with updated details, and records relevant events. |
no test coverage detected