From ba971f185f1bce82b0e8d19c74da95e37c68234a Mon Sep 17 00:00:00 2001 From: Al Date: Wed, 6 Mar 2019 09:50:27 +0000 Subject: [PATCH] Remove the ready field from token which means less locking The ready field was actually redundant and setting it was causing us to take mutexes which would cause deadlocking. --- token.go | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/token.go b/token.go index 935af512..08185533 100644 --- a/token.go +++ b/token.go @@ -49,20 +49,14 @@ type tokenCompletor interface { type baseToken struct { m sync.RWMutex complete chan struct{} - ready bool err error } // Wait will wait indefinitely for the Token to complete, ie the Publish // to be sent and confirmed receipt from the broker func (b *baseToken) Wait() bool { - b.m.Lock() - defer b.m.Unlock() - if !b.ready { - <-b.complete - b.ready = true - } - return b.ready + <-b.complete + return true } // WaitTimeout takes a time.Duration to wait for the flow associated with the @@ -73,18 +67,17 @@ func (b *baseToken) WaitTimeout(d time.Duration) bool { b.m.Lock() defer b.m.Unlock() - if !b.ready { - timer := time.NewTimer(d) - select { - case <-b.complete: - b.ready = true - if !timer.Stop() { - <-timer.C - } - case <-timer.C: + timer := time.NewTimer(d) + select { + case <-b.complete: + if !timer.Stop() { + <-timer.C } + return true + case <-timer.C: } - return b.ready + + return false } func (b *baseToken) flowComplete() {