GODT-1367: use waitgroup instead of channel in pool/pchan

This commit is contained in:
James Houlahan
2021-10-05 15:03:24 +02:00
committed by Jakub
parent 8ea610c625
commit bc21bb1d8d
2 changed files with 17 additions and 17 deletions

View File

@ -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()
}