// Acquire tries to lock the provided id.
// If the resource is already locked, this function will block until the existing lock is released,
// at which point we acquire the lock and return.
Acquire tries to lock the provided id.
If the resource is already locked, this function will block until the existing lock is released,
at which point we acquire the lock and return.
// Acquire tries to lock the provided id . // If the resource is already locked this function will block until the existing lock is released // at which point we acquire the lock and return .
func (l *Lock) Acquire(ctx context.Context, id string) (lockID string, err error) {
	retries := 0
	for {
		lockID, err = l.Lock(ctx, id)
		if err != lock.ErrAlreadyLocked {
			return lockID, err 
		}
		if retries >= AcquireMaxRetries {
			return "", ErrAcquireMaxRetries 
		}
		retries++
		select {
		case <-time.After(AcquirePeriod):
			continue 
		case <-l.CloserChannel:
			log.Info(ctx, "stop acquiring lock. Mongo db is being closed")
			return "", ErrMongoDbClosing 
		}
	}
}
func ( l * Lock ) Acquire ( ctx context . Context , id string ) ( lockID string , err error ) { retries := 0 for { lockID , err = l . Lock ( ctx , id ) if err != lock . ErrAlreadyLocked { return lockID , err } if retries >= AcquireMaxRetries { return " " , ErrAcquireMaxRetries } retries ++ select { case <- time . After ( AcquirePeriod ) : continue case <- l . CloserChannel : log . Info ( ctx , " " ) return " " , ErrMongoDbClosing } } }
Acquire tries to lock the provided id.
Acquire tries to lock the provided id .
// Successful or failed due to some generic error, no retry is attempted // Failed too many times // Retry // Abort because the app is closing