fn: resource and slot cancel and broadcast improvements (#696)

* fn: resource and slot cancel and broadcast improvements

*) Context argument does not wake up the waiters correctly upon
cancellation/timeout.
*) Avoid unnecessary broadcasts in slot and resource.

* fn: limit scope of context in resource/slot calls in agent
This commit is contained in:
Tolga Ceylan
2018-01-18 13:43:56 -08:00
committed by GitHub
parent c9e995292c
commit 2f0de2b574
6 changed files with 89 additions and 51 deletions

View File

@@ -112,25 +112,29 @@ func (a *slotQueue) ejectSlot(s *slotToken) bool {
return true
}
func (a *slotQueue) startDequeuer() (chan *slotToken, context.CancelFunc) {
ctx, cancel := context.WithCancel(context.Background())
myCancel := func() {
cancel()
a.cond.L.Lock()
a.cond.Broadcast()
a.cond.L.Unlock()
}
func (a *slotQueue) startDequeuer(ctx context.Context) chan *slotToken {
isWaiting := false
output := make(chan *slotToken)
go func() {
<-ctx.Done()
a.cond.L.Lock()
if isWaiting {
a.cond.Broadcast()
}
a.cond.L.Unlock()
}()
go func() {
for {
a.cond.L.Lock()
isWaiting = true
for len(a.slots) <= 0 && (ctx.Err() == nil) {
a.cond.Wait()
}
isWaiting = false
if ctx.Err() != nil {
a.cond.L.Unlock()
@@ -154,7 +158,7 @@ func (a *slotQueue) startDequeuer() (chan *slotToken, context.CancelFunc) {
}
}()
return output, myCancel
return output
}
func (a *slotQueue) queueSlot(slot Slot) *slotToken {