/
worker.go
58 lines (50 loc) · 1.17 KB
/
worker.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
// Copyright (c) 2022 The VeChainThor developers
// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html>
package node
// worker is the simple worker to asynchronously run tasks.
// It has only one background goroutine and all tasks are executed one by one.
type worker struct {
taskCh chan func() error
ackCh chan error
}
// NewWorker creates a worker.
func newWorker() *worker {
w := &worker{
taskCh: make(chan func() error, 16),
ackCh: make(chan error),
}
go w.worker()
return w
}
// Close closes the worker and stops the background goroutine.
func (w *worker) Close() {
close(w.taskCh)
<-w.ackCh
}
// Run pushes the task into the background queue.
func (w *worker) Run(task func() error) {
if task != nil {
w.taskCh <- task
}
}
// Sync ensures the last task has been executed.
func (w *worker) Sync() error {
w.taskCh <- nil
return <-w.ackCh
}
func (w *worker) worker() {
defer func() {
close(w.ackCh)
}()
var err error
for task := range w.taskCh {
if task != nil {
if err == nil {
err = task()
}
} else {
w.ackCh <- err
}
}
}