forked from Silverfish/proton-bridge
GODT-1367: Use sync.Once to only close pool jobs once
This commit is contained in:
@ -26,6 +26,7 @@ type PChan struct {
|
|||||||
lock sync.Mutex
|
lock sync.Mutex
|
||||||
items []*Item
|
items []*Item
|
||||||
ready, done chan struct{}
|
ready, done chan struct{}
|
||||||
|
once sync.Once
|
||||||
}
|
}
|
||||||
|
|
||||||
type Item struct {
|
type Item struct {
|
||||||
@ -82,13 +83,7 @@ func (ch *PChan) Pop() (interface{}, int, bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ch *PChan) Close() {
|
func (ch *PChan) Close() {
|
||||||
select {
|
ch.once.Do(func() { close(ch.done) })
|
||||||
case <-ch.done:
|
|
||||||
return
|
|
||||||
|
|
||||||
default:
|
|
||||||
close(ch.done)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ch *PChan) push(val interface{}, prio int) *Item {
|
func (ch *PChan) push(val interface{}, prio int) *Item {
|
||||||
|
|||||||
@ -17,7 +17,11 @@
|
|||||||
|
|
||||||
package pool
|
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)
|
type WorkFunc func(interface{}, int) (interface{}, error)
|
||||||
|
|
||||||
@ -74,6 +78,7 @@ type Job struct {
|
|||||||
item *pchan.Item
|
item *pchan.Item
|
||||||
|
|
||||||
ready, done chan struct{}
|
ready, done chan struct{}
|
||||||
|
once sync.Once
|
||||||
}
|
}
|
||||||
|
|
||||||
func newJob(req interface{}) *Job {
|
func newJob(req interface{}) *Job {
|
||||||
@ -115,13 +120,7 @@ func (job *Job) setItem(item *pchan.Item) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (job *Job) markDone() {
|
func (job *Job) markDone() {
|
||||||
select {
|
job.once.Do(func() { close(job.done) })
|
||||||
case <-job.done:
|
|
||||||
return
|
|
||||||
|
|
||||||
default:
|
|
||||||
close(job.done)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (job *Job) waitDone() {
|
func (job *Job) waitDone() {
|
||||||
|
|||||||
Reference in New Issue
Block a user