This repository has been archived by the owner on Jun 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool_pipeline.go
80 lines (62 loc) · 1.62 KB
/
pool_pipeline.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Package wpool contains structs and functions for making a pool of workers.
package wpool
import (
"errors"
"sync"
"github.com/egnd/go-wpool/v2/interfaces"
)
// PipelinePool is a pool of workers with pipeline.
type PipelinePool struct {
tasks chan interfaces.Task
workers []interfaces.Worker
mxWorkers sync.Mutex
logger interfaces.Logger
}
// NewPipelinePool creates pool of workers with pipeline.
func NewPipelinePool(pipeline chan interfaces.Worker, logger interfaces.Logger) *PipelinePool {
pool := &PipelinePool{ //nolint:exhaustivestruct
tasks: make(chan interfaces.Task),
logger: logger,
}
go pool.run(pipeline)
return pool
}
func (p *PipelinePool) run(pipeline chan interfaces.Worker) {
for worker := range pipeline {
for task := range p.tasks {
if err := worker.Do(task); err != nil {
p.logger.Errorf(err, `passing task "%s" to worker`, task.GetID())
}
break //nolint:staticcheck
}
}
}
// AddWorker is registering worker in the pool.
func (p *PipelinePool) AddWorker(worker interfaces.Worker) (err error) {
p.mxWorkers.Lock()
defer p.mxWorkers.Unlock()
p.workers = append(p.workers, worker)
return
}
// AddTask is putting task into pool.
func (p *PipelinePool) AddTask(task interfaces.Task) (err error) {
defer func() {
if r := recover(); r != nil {
err = errors.New("pool is closed")
}
}()
p.tasks <- task
return
}
// Close is stopping pool and workers.
func (p *PipelinePool) Close() (err error) {
close(p.tasks)
p.mxWorkers.Lock()
defer p.mxWorkers.Unlock()
for _, worker := range p.workers {
if wErr := worker.Close(); wErr != nil {
err = wErr
}
}
return
}