From 3d97ea0baecc5ee20104c0b27e9914057656a695 Mon Sep 17 00:00:00 2001 From: Tiru Srikantha Date: Fri, 5 Jun 2015 13:23:23 -0700 Subject: [PATCH] Check to see why a lock acquisition failed --- api/lock.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/api/lock.go b/api/lock.go index 4b694789c4..a76685f04c 100644 --- a/api/lock.go +++ b/api/lock.go @@ -183,11 +183,23 @@ WAIT: // Handle the case of not getting the lock if !locked { - select { - case <-time.After(DefaultLockRetryTime): + // Determine why the lock failed + qOpts.WaitIndex = 0 + pair, meta, err = kv.Get(l.opts.Key, qOpts) + if pair != nil && pair.Session != "" { + //If the session is not null, this means that a wait can safely happen + //using a long poll + qOpts.WaitIndex = meta.LastIndex goto WAIT - case <-stopCh: - return nil, nil + } else { + // If the session is empty and the lock failed to acquire, then it means + // a lock-delay is in effect and a timed wait must be used + select { + case <-time.After(DefaultLockRetryTime): + goto WAIT + case <-stopCh: + return nil, nil + } } }