-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhandler_netpoll.go
157 lines (130 loc) · 3.01 KB
/
handler_netpoll.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// +build netpoll
package conn
import (
"io"
"net"
"time"
"github.com/neverhook/easygo/netpoll"
"github.com/hb-go/conn/pkg/log"
)
func (srv *Server) handleConn(conn net.Conn) (err error) {
c := getConnection(conn)
if tc, ok := conn.(*net.TCPConn); ok {
c.file, err = tc.File()
if err != nil {
return
}
}
desc, e := netpoll.HandleFile(c.file, netpoll.EventRead|netpoll.EventOneShot)
if e != nil {
err = e
c.closeAndPut()
return
}
c.desc = desc
defer func() {
if err != nil {
c.close()
c.put()
if e := poller.Stop(desc); e != nil {
log.Errorf("poller stop error: %v", e)
}
if e := desc.Close(); e != nil {
log.Errorf("poller desc close error: %v", e)
}
}
}()
rf := func() {
if c.desc == desc {
defer func() {
if !c.isClosed() {
if err := poller.Resume(desc); err != nil {
log.Errorf("poller.Resume() error: %v", err)
}
}
}()
if !c.isClosed() {
err := srv.ReadHandler(c.conn)
c.setReaded()
log.Debugf("(connId=%d) reading unlock duration: %v", c.id, time.Since(c.timestamp).String())
if err != nil {
if e, ok := err.(net.Error); ok && (e.Timeout() || e.Temporary()) {
log.Warnf("read process handle error: %v", err)
} else {
log.Errorf("read process handle error: %v", err)
c.close()
}
}
}
}
}
err = poller.Start(desc, func(event netpoll.Event) {
log.Debugf("net poll event: %v", event)
if event&netpoll.EventRead == 0 {
return
}
if c.desc != desc {
poller.Stop(desc)
desc.Close()
return
}
defer func() {
// recover from panic
if r := recover(); r != nil {
if !c.isClosed() {
c.close()
}
c.put()
poller.Stop(desc)
desc.Close()
log.Errorf("poller event panic: %v", r)
}
}()
if c.isClosed() || event&netpoll.EventReadHup == netpoll.EventReadHup {
if !c.isClosed() {
c.close()
}
c.put()
poller.Stop(desc)
desc.Close()
return
}
// read process
usePool := true
if usePool {
log.Debugf("(connId=%d) reading pre lock: %d", c.id, c.reading)
// TODO 处于Reading状态的Conn不添加新任务
if c.setReading() {
c.timestamp = time.Now()
log.Debugf("(connId=%d) reading lock: %d", c.id, c.reading)
if err := readPool.ScheduleTimeout(time.Microsecond*100, rf); err != nil {
// TODO 任务队列timeout处理
c.setReaded()
log.Debugf("(connId=%d) reading unlock", c.id)
log.Errorf("read pool schedule error: %v", err)
if err := poller.Resume(desc); err != nil {
log.Errorf("poller.Resume() error: %v", err)
}
}
} else {
log.Warnf("(connId=%d) receive read event while reading locked", c.id)
}
} else {
go func() {
if c.desc == desc {
err := srv.ReadHandler(c.conn)
if err != nil {
log.Errorf("read process handle error: %v", err)
if err == io.EOF {
c.close()
}
}
}
if err := poller.Resume(desc); err != nil {
log.Panicf("poller.Resume() error: %v", err)
}
}()
}
})
return
}