mirror of
https://github.com/ProtonMail/proton-bridge.git
synced 2025-12-10 12:46:46 +00:00
GODT-1367: use waitgroup instead of channel in pool/pchan
This commit is contained in:
@ -33,11 +33,11 @@ type Item struct {
|
||||
ch *PChan
|
||||
val interface{}
|
||||
prio int
|
||||
done chan struct{}
|
||||
done sync.WaitGroup
|
||||
}
|
||||
|
||||
func (item *Item) Wait() {
|
||||
<-item.done
|
||||
item.done.Wait()
|
||||
}
|
||||
|
||||
func (item *Item) GetPriority() int {
|
||||
@ -90,15 +90,14 @@ func (ch *PChan) push(val interface{}, prio int) *Item {
|
||||
ch.lock.Lock()
|
||||
defer ch.lock.Unlock()
|
||||
|
||||
done := make(chan struct{})
|
||||
|
||||
item := &Item{
|
||||
ch: ch,
|
||||
val: val,
|
||||
prio: prio,
|
||||
done: done,
|
||||
}
|
||||
|
||||
item.done.Add(1)
|
||||
|
||||
ch.items = append(ch.items, item)
|
||||
|
||||
return item
|
||||
@ -116,7 +115,7 @@ func (ch *PChan) pop() (interface{}, int) {
|
||||
|
||||
item, ch.items = ch.items[len(ch.items)-1], ch.items[:len(ch.items)-1]
|
||||
|
||||
defer close(item.done)
|
||||
defer item.done.Done()
|
||||
|
||||
return item.val, item.prio
|
||||
}
|
||||
|
||||
@ -77,20 +77,21 @@ type Job struct {
|
||||
|
||||
item *pchan.Item
|
||||
|
||||
ready, done chan struct{}
|
||||
ready, done sync.WaitGroup
|
||||
once sync.Once
|
||||
}
|
||||
|
||||
func newJob(req interface{}) *Job {
|
||||
return &Job{
|
||||
req: req,
|
||||
ready: make(chan struct{}),
|
||||
done: make(chan struct{}),
|
||||
}
|
||||
job := &Job{req: req}
|
||||
|
||||
job.ready.Add(1)
|
||||
job.done.Add(1)
|
||||
|
||||
return job
|
||||
}
|
||||
|
||||
func (job *Job) GetResult() (interface{}, error) {
|
||||
<-job.ready
|
||||
job.ready.Wait()
|
||||
|
||||
return job.res, job.err
|
||||
}
|
||||
@ -104,13 +105,13 @@ func (job *Job) SetPriority(prio int) {
|
||||
}
|
||||
|
||||
func (job *Job) postSuccess(res interface{}) {
|
||||
defer close(job.ready)
|
||||
defer job.ready.Done()
|
||||
|
||||
job.res = res
|
||||
}
|
||||
|
||||
func (job *Job) postFailure(err error) {
|
||||
defer close(job.ready)
|
||||
defer job.ready.Done()
|
||||
|
||||
job.err = err
|
||||
}
|
||||
@ -120,9 +121,9 @@ func (job *Job) setItem(item *pchan.Item) {
|
||||
}
|
||||
|
||||
func (job *Job) markDone() {
|
||||
job.once.Do(func() { close(job.done) })
|
||||
job.once.Do(func() { job.done.Done() })
|
||||
}
|
||||
|
||||
func (job *Job) waitDone() {
|
||||
<-job.done
|
||||
job.done.Wait()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user