func (l *lock) Lock(ctx context.Context, key string) error {
	if l.locked {
		return fmt.Errorf("attempt to lock while lock held")
	}

	// attempt initial lock acquisition. we throw away the bool and prefer
	// checking l.locked bool which l.TryLock flips under mu lock
	_, err := l.TryLock(ctx, key)
	if err != nil {
		return fmt.Errorf("failed at attmpeting initial lock acquition: %v", err)
	}
	if l.locked {
		return nil
	}

	// if initial attempt failed begin retry loop.
	t := time.NewTicker(l.retry)
	defer t.Stop()

	for !l.locked {
		select {
		case <-t.C:
			_, err := l.TryLock(ctx, key)
			if err != nil {
				return fmt.Errorf("failed at attmpeting initial lock acquition: %v", err)
			}
		case <-ctx.Done():
			return fmt.Errorf("context canceled: %v", ctx.Err())
		}
	}

	return nil
}
// Lock will immediately attempt to obtain a lock with the key
// being the provided hash. on failure of initial attempt a new attempt
// will be made every l.retry interval. Once lock is acquired the
// method unblocks
Lock will immediately attempt to obtain a lock with the key
being the provided hash. on failure of initial attempt a new attempt
will be made every l.retry interval. Once lock is acquired the
method unblocks
// Lock will immediately attempt to obtain a lock with the key // being the provided hash . on failure of initial attempt a new attempt // will be made every l . retry interval . Once lock is acquired the // method unblocks
func (l *lock) Lock(ctx context.Context, key string) error {
	if l.locked {
		return fmt.Errorf("attempt to lock while lock held")
	}
	_, err := l.TryLock(ctx, key)
	if err != nil {
		return fmt.Errorf("failed at attmpeting initial lock acquition: %v", err)
	}
	if l.locked {
		return nil
	}
	t := time.NewTicker(l.retry)
	defer t.Stop()
	for !l.locked {
		select {
		case <-t.C:
			_, err := l.TryLock(ctx, key)
			if err != nil {
				return fmt.Errorf("failed at attmpeting initial lock acquition: %v", err)
			}
		case <-ctx.Done():
			return fmt.Errorf("context canceled: %v", ctx.Err())
		}
	}
	return nil
}
func ( l * lock ) Lock ( ctx context . Context , key string ) error { if l . locked { return fmt . Errorf ( " " ) } _ , err := l . TryLock ( ctx , key ) if err != nil { return fmt . Errorf ( " " , err ) } if l . locked { return nil } t := time . NewTicker ( l . retry ) defer t . Stop ( ) for ! l . locked { select { case <- t . C : _ , err := l . TryLock ( ctx , key ) if err != nil { return fmt . Errorf ( " " , err ) } case <- ctx . Done ( ) : return fmt . Errorf ( " " , ctx . Err ( ) ) } } return nil }
Lock will immediately attempt to obtain a lock with the key
being the provided hash.
Lock will immediately attempt to obtain a lock with the key being the provided hash .
// attempt initial lock acquisition. we throw away the bool and prefer // checking l.locked bool which l.TryLock flips under mu lock // if initial attempt failed begin retry loop.