-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.go
48 lines (40 loc) · 1009 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
package pool
import (
"net"
"sync"
)
type (
// worker represents struct for worker used by pool manager
worker struct {
handler workerHandler
mux *sync.Mutex
state int
kill chan bool
}
// workerHandler represents interface for pool worker method
workerHandler func(worker *worker, conns chan net.Conn)
)
const (
// WorkerStateInit is set during initialization
WorkerStateInit = iota
// WorkerStateBusy is set when worker is currenlty processing data
WorkerStateBusy
// WorkerStateIdle is set when worker finished processing data and is waiting
WorkerStateIdle
// WorkerStateKilled is set when worker got signal to kill
WorkerStateKilled
)
// kill sends signal for worker to kill
func (w *worker) shutdown() {
w.kill <- true
}
// setState handles state transformations for pool worker
func (w *worker) setState(state int) {
w.mux.Lock()
defer w.mux.Unlock()
w.state = state
}
// state returns current worker state
func (w *worker) getState() int {
return w.state
}