/
worker.go
52 lines (43 loc) · 855 Bytes
/
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
package navvy
import (
"log"
"runtime"
)
// Worker is the worker for navvy.
type Worker struct {
// pool who owns this worker.
pool *Pool
// task is a job should be done.
task chan Task
}
func (w *Worker) run() {
w.pool.incRunning()
go func() {
defer func() {
p := recover()
if p == nil {
return
}
// Make sure wait_group have the right value.
w.pool.wg.Done()
w.pool.decRunning()
w.pool.workerCache.Put(w)
log.Printf("worker exits from a panic: %v\n", p)
var buf [4096]byte
n := runtime.Stack(buf[:], false)
log.Printf("worker exits from panic: %s\n", string(buf[:n]))
}()
for f := range w.task {
if f == nil {
w.pool.decRunning()
w.pool.workerCache.Put(w)
return
}
f.Run(f.Context())
w.pool.wg.Done()
if ok := w.pool.revertWorker(w); !ok {
break
}
}
}()
}