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

Method acquireLock

transaction.go:269–285  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

267// - *redlock.MultiLocker: A pointer to the acquired MultiLocker if successful.
268// - error: An error if the locks could not be acquired.
269func (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.

Callers 1

executeWithLockMethod · 0.95

Calls 4

KeysMethod · 0.95
GenerateUUIDWithSuffixFunction · 0.92
StartMethod · 0.45

Tested by

no test coverage detected