GODT-1367: Use sync.Once to only close pool jobs once

This commit is contained in:
James Houlahan
2021-10-04 08:22:40 +02:00
committed by Jakub
parent 10da4f284c
commit 8ea610c625
2 changed files with 9 additions and 15 deletions

View File

@ -26,6 +26,7 @@ type PChan struct {
lock sync.Mutex
items []*Item
ready, done chan struct{}
once sync.Once
}
type Item struct {
@ -82,13 +83,7 @@ func (ch *PChan) Pop() (interface{}, int, bool) {
}
func (ch *PChan) Close() {
select {
case <-ch.done:
return
default:
close(ch.done)
}
ch.once.Do(func() { close(ch.done) })
}
func (ch *PChan) push(val interface{}, prio int) *Item {

View File

@ -17,7 +17,11 @@
package pool
import "github.com/ProtonMail/proton-bridge/pkg/pchan"
import (
"sync"
"github.com/ProtonMail/proton-bridge/pkg/pchan"
)
type WorkFunc func(interface{}, int) (interface{}, error)
@ -74,6 +78,7 @@ type Job struct {
item *pchan.Item
ready, done chan struct{}
once sync.Once
}
func newJob(req interface{}) *Job {
@ -115,13 +120,7 @@ func (job *Job) setItem(item *pchan.Item) {
}
func (job *Job) markDone() {
select {
case <-job.done:
return
default:
close(job.done)
}
job.once.Do(func() { close(job.done) })
}
func (job *Job) waitDone() {