Skip to content

Commit

Permalink
Remove the ready field from token which means less locking
Browse files Browse the repository at this point in the history
The ready field was actually redundant and setting it was causing us
to take mutexes which would cause deadlocking.
  • Loading branch information
Al committed Mar 6, 2019
1 parent ade143f commit ba971f1
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions token.go
Expand Up @@ -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
Expand All @@ -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() {
Expand Down

0 comments on commit ba971f1

Please sign in to comment.