-
Notifications
You must be signed in to change notification settings - Fork 8
/
workers.go
79 lines (67 loc) · 1.91 KB
/
workers.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
package workers
import (
"context"
"runtime"
"sync"
"sync/atomic"
"github.com/simulot/aspiratv/mylog"
)
// WorkItem is an interface to work item used b the Workers
type WorkItem func()
// Worker is a pool of workers.
type Worker struct {
stop chan bool // Close this channel to stop all workers
tokens chan int
wg sync.WaitGroup // To wait completion of all workers
logger *mylog.MyLog // True to enable logs
}
// New creates a new worker pool with NumCPU runing workers
func New(ctx context.Context, workers int, logger *mylog.MyLog) *Worker {
if workers < 1 {
workers = runtime.NumCPU()
}
w := &Worker{
stop: make(chan bool),
tokens: make(chan int, workers),
logger: logger,
}
w.logger.Debug().Printf("[WORKER] Starting %d workers", workers)
for i := 0; i < workers; i++ {
w.tokens <- i + 1
}
return w
}
// Stop stops all runing workers and wait them to finish and then leave the workerpool.
func (w *Worker) Stop(ctx context.Context) {
w.logger.Debug().Printf("[WORKER] Stoping...")
// Makes all goroutine ending
close(w.stop)
// wait the end of all job
w.logger.Debug().Printf("[WORKER] Waiting for worker to end")
w.wg.Wait()
w.logger.Debug().Printf("[WORKER] Worker pool is ended")
}
var jobID = int64(0)
// Submit a work item to the worker pool
func (w *Worker) Submit(ctx context.Context, wi func(wid, jid int)) {
id := atomic.AddInt64(&jobID, 1)
w.logger.Debug().Printf("[WORKER] Job %d is waiting...", id)
select {
case <-w.stop:
w.logger.Debug().Printf("[WORKER] Stopped, job %d discared", id)
return
case <-ctx.Done():
w.logger.Debug().Printf("[WORKER] Cancled, job %d discared", id)
return
case wid := <-w.tokens:
w.wg.Add(1)
fn := func(wid, id int) {
w.logger.Debug().Printf("[WORKER %d] Job %d is started", wid, id)
wi(wid, id)
w.logger.Debug().Printf("[WORKER %d] Job %d is done", wid, id)
w.tokens <- wid
w.wg.Done()
}
go fn(wid, int(id))
}
}