MCPcopy Index your code
hub / github.com/apache/casbin / tryLockWithTimeout

Function tryLockWithTimeout

transaction.go:410–435  ·  view source on GitHub ↗

tryLockWithTimeout attempts to acquire the lock within the specified timeout period.

(lock *sync.Mutex, startTime time.Time, maxWait time.Duration)

Source from the content-addressed store, hash-verified

408
409// tryLockWithTimeout attempts to acquire the lock within the specified timeout period.
410func tryLockWithTimeout(lock *sync.Mutex, startTime time.Time, maxWait time.Duration) bool {
411 // Calculate remaining wait time based on transaction start time.
412 remainingTime := maxWait - time.Since(startTime)
413 if remainingTime <= 0 {
414 return false
415 }
416
417 // Create a context with timeout for lock acquisition.
418 ctx, cancel := context.WithTimeout(context.Background(), remainingTime)
419 defer cancel()
420
421 // Use channel for timeout control.
422 done := make(chan bool, 1)
423 go func() {
424 lock.Lock()
425 done <- true
426 }()
427
428 // Wait for either lock acquisition or timeout.
429 select {
430 case <-done:
431 return true
432 case <-ctx.Done():
433 return false
434 }
435}

Callers 2

CommitMethod · 0.85
RollbackMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…