Skip to content

Commit ba971f1

Browse files
author
Al
committed
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.
1 parent ade143f commit ba971f1

File tree

1 file changed

+11
-18
lines changed

1 file changed

+11
-18
lines changed

token.go

+11-18
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,14 @@ type tokenCompletor interface {
4949
type baseToken struct {
5050
m sync.RWMutex
5151
complete chan struct{}
52-
ready bool
5352
err error
5453
}
5554

5655
// Wait will wait indefinitely for the Token to complete, ie the Publish
5756
// to be sent and confirmed receipt from the broker
5857
func (b *baseToken) Wait() bool {
59-
b.m.Lock()
60-
defer b.m.Unlock()
61-
if !b.ready {
62-
<-b.complete
63-
b.ready = true
64-
}
65-
return b.ready
58+
<-b.complete
59+
return true
6660
}
6761

6862
// 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 {
7367
b.m.Lock()
7468
defer b.m.Unlock()
7569

76-
if !b.ready {
77-
timer := time.NewTimer(d)
78-
select {
79-
case <-b.complete:
80-
b.ready = true
81-
if !timer.Stop() {
82-
<-timer.C
83-
}
84-
case <-timer.C:
70+
timer := time.NewTimer(d)
71+
select {
72+
case <-b.complete:
73+
if !timer.Stop() {
74+
<-timer.C
8575
}
76+
return true
77+
case <-timer.C:
8678
}
87-
return b.ready
79+
80+
return false
8881
}
8982

9083
func (b *baseToken) flowComplete() {

0 commit comments

Comments
 (0)