-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathworker_handle.go
71 lines (63 loc) · 1.4 KB
/
worker_handle.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
/**
* @Author: llh
* @Date: 2019-06-01 15:08:12
* @Last Modified by: llh
* 对 https://github.com/panjf2000/ants 进行了适当的修改以满足数据异步处理的连接池
*/
package tfg
import (
"log"
"time"
)
type WorkerHandle struct {
pool *PoolHandle
connCh chan *connWorker
recycleTime time.Time
}
type connWorker struct {
conn *conn
in []byte
n int
}
func (w *WorkerHandle) run() {
w.pool.incRunning()
go func() {
defer func() {
if p := recover(); p != nil {
w.pool.decRunning()
w.pool.workerCache.Put(w)
if w.pool.PanicHandler != nil {
w.pool.PanicHandler(p)
} else {
log.Printf("worker exits from a panic: %v", p)
}
}
}()
var remain []byte
var packet interface{}
var isFinRead, isHandle bool
var err error
for connWorker := range w.connCh {
if nil == connWorker || connWorker.conn == nil {
w.pool.decRunning()
w.pool.workerCache.Put(w)
return
}
packet, remain, isFinRead, isHandle, err = w.pool.read(connWorker.in[:connWorker.n], remain)
if isHandle {
req := w.pool.s.connManager.handleReqCache.Get().(*handleReq)
req.conn = connWorker.conn
req.packet = packet
req.err = err
w.pool.s.pool.Serve(req)
}
if isFinRead {
w.pool.s.connManager.inCache.Put(connWorker)
if ok := w.pool.revertWorker(w); !ok {
break
}
w.pool.deleteConnWorker(connWorker.conn.fd)
}
}
}()
}