Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: give high priority to closeChan #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 22 additions & 12 deletions worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,24 +91,34 @@ func (w *workerWrapper) run() {
// NOTE: Blocking here will prevent the worker from closing down.
w.worker.BlockUntilReady()
select {
case w.reqChan <- workRequest{
jobChan: jobChan,
retChan: retChan,
interruptFunc: w.interrupt,
}:
// give more priority to closeChan. Because select will randomly select one when multiple channel available,
// when many closeChan and reqChan come at the same time in extreme cases, giving high priority to closeChan
// to prevent closeChan to be ignored all the time
case <-w.closeChan:
return
default:
nextReq := workRequest{
jobChan: jobChan,
retChan: retChan,
interruptFunc: w.interrupt,
}

select {
case payload := <-jobChan:
result := w.worker.Process(payload)
case w.reqChan <- nextReq:
select {
case retChan <- result:
case payload := <-jobChan:
result := w.worker.Process(payload)
select {
case retChan <- result:
case <-w.interruptChan:
w.interruptChan = make(chan struct{})
}
case <-w.interruptChan:
w.interruptChan = make(chan struct{})
}
case <-w.interruptChan:
w.interruptChan = make(chan struct{})
case <-w.closeChan:
return
}
case <-w.closeChan:
return
}
}
}
Expand Down