forked from lesismal/nbio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgopher.go
More file actions
388 lines (329 loc) · 8.87 KB
/
gopher.go
File metadata and controls
388 lines (329 loc) · 8.87 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
// Copyright 2020 lesismal. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package nbio
import (
"container/heap"
"net"
"runtime/debug"
"sync"
"time"
"github.com/lesismal/nbio/logging"
)
const (
// DefaultReadBufferSize .
DefaultReadBufferSize = 1024 * 2
// DefaultMaxWriteBufferSize .
DefaultMaxWriteBufferSize = 1024 * 1024
// DefaultMinConnCacheSize .
DefaultMinConnCacheSize = 1024 * 2
)
var (
// MaxOpenFiles .
MaxOpenFiles = 1024 * 1024
)
// Config Of Gopher
type Config struct {
// Name describes your gopher name for logging, it's set to "NB" by default.
Name string
// Network is the listening protocol, used with Addrs toghter.
// tcp* supported only by now, there's no plan for other protocol such as udp,
// because it's too easy to write udp server/client.
Network string
// Addrs is the listening addr list for a nbio server.
// if it is empty, no listener created, then the Gopher is used for client by default.
Addrs []string
// NPoller represents poller goroutine num, it's set to runtime.NumCPU() by default.
NPoller int
// NListener represents poller goroutine num, it's set to runtime.NumCPU() by default.
NListener int
// Backlog represents backlog arg for syscall.Listen
Backlog int
// ReadBufferSize represents buffer size for reading, it's set to 16k by default.
ReadBufferSize int
// MinConnCacheSize represents application layer's Conn write cache buffer size when the kernel sendQ is full
MinConnCacheSize int
// MaxWriteBufferSize represents max write buffer size for Conn, it's set to 1m by default.
// if the connection's Send-Q is full and the data cached by nbio is
// more than MaxWriteBufferSize, the connection would be closed by nbio.
MaxWriteBufferSize int
// LockListener represents listener's goroutine to lock thread or not, it's set to false by default.
LockListener bool
// LockPoller represents poller's goroutine to lock thread or not, it's set to false by default.
LockPoller bool
}
// Gopher is a manager of poller
type Gopher struct {
sync.WaitGroup
mux sync.Mutex
tmux sync.Mutex
Name string
network string
addrs []string
pollerNum int
backlogSize int
readBufferSize int
maxWriteBufferSize int
minConnCacheSize int
lockListener bool
lockPoller bool
lfds []int
connsStd map[*Conn]struct{}
connsUnix []*Conn
listeners []*poller
pollers []*poller
onOpen func(c *Conn)
onClose func(c *Conn, err error)
onData func(c *Conn, data []byte)
onReadBufferAlloc func(c *Conn) []byte
onReadBufferFree func(c *Conn, buffer []byte)
onWriteBufferFree func(c *Conn, buffer []byte)
beforeRead func(c *Conn)
afterRead func(c *Conn)
beforeWrite func(c *Conn)
onStop func()
timers timerHeap
trigger *time.Timer
chTimer chan struct{}
}
// Stop pollers
func (g *Gopher) Stop() {
g.onStop()
g.trigger.Stop()
close(g.chTimer)
for _, l := range g.listeners {
l.stop()
}
for i := 0; i < g.pollerNum; i++ {
g.pollers[i].stop()
}
g.mux.Lock()
conns := g.connsStd
g.connsStd = map[*Conn]struct{}{}
connsUnix := g.connsUnix
g.mux.Unlock()
for c := range conns {
if c != nil {
c.Close()
}
}
for _, c := range connsUnix {
if c != nil {
go c.Close()
}
}
g.Wait()
logging.Info("Gopher[%v] stop", g.Name)
}
// AddConn adds conn to a poller
func (g *Gopher) AddConn(conn net.Conn) (*Conn, error) {
c, err := NBConn(conn)
if err != nil {
return nil, err
}
g.pollers[uint32(c.Hash())%uint32(g.pollerNum)].addConn(c)
return c, nil
}
// OnOpen registers callback for new connection
func (g *Gopher) OnOpen(h func(c *Conn)) {
if h == nil {
panic("invalid nil handler")
}
g.onOpen = h
}
// OnClose registers callback for disconnected
func (g *Gopher) OnClose(h func(c *Conn, err error)) {
if h == nil {
panic("invalid nil handler")
}
g.onClose = h
}
// OnData registers callback for data
func (g *Gopher) OnData(h func(c *Conn, data []byte)) {
if h == nil {
panic("invalid nil handler")
}
g.onData = h
}
// OnReadBufferAlloc registers callback for memory allocating
func (g *Gopher) OnReadBufferAlloc(h func(c *Conn) []byte) {
if h == nil {
panic("invalid nil handler")
}
g.onReadBufferAlloc = h
}
// OnReadBufferFree registers callback for memory release
func (g *Gopher) OnReadBufferFree(h func(c *Conn, b []byte)) {
if h == nil {
panic("invalid nil handler")
}
g.onReadBufferFree = h
}
// OnWriteBufferRelease registers callback for write buffer memory release
func (g *Gopher) OnWriteBufferRelease(h func(c *Conn, b []byte)) {
if h == nil {
panic("invalid nil handler")
}
g.onWriteBufferFree = h
}
// BeforeRead registers callback before syscall.Read
// the handler would be called on windows
func (g *Gopher) BeforeRead(h func(c *Conn)) {
if h == nil {
panic("invalid nil handler")
}
g.beforeRead = h
}
// AfterRead registers callback after syscall.Read
// the handler would be called on *nix
func (g *Gopher) AfterRead(h func(c *Conn)) {
if h == nil {
panic("invalid nil handler")
}
g.afterRead = h
}
// BeforeWrite registers callback befor syscall.Write and syscall.Writev
// the handler would be called on windows
func (g *Gopher) BeforeWrite(h func(c *Conn)) {
if h == nil {
panic("invalid nil handler")
}
g.beforeWrite = h
}
// OnStop registers callback before Gopher is stopped.
func (g *Gopher) OnStop(h func()) {
if h == nil {
panic("invalid nil handler")
}
g.onStop = h
}
// After used as time.After
func (g *Gopher) After(timeout time.Duration) <-chan time.Time {
c := make(chan time.Time, 1)
g.afterFunc(timeout, func() {
c <- time.Now()
})
return c
}
// AfterFunc used as time.AfterFunc
func (g *Gopher) AfterFunc(timeout time.Duration, f func()) *Timer {
ht := g.afterFunc(timeout, f)
return &Timer{htimer: ht}
}
func (g *Gopher) afterFunc(timeout time.Duration, f func()) *htimer {
g.tmux.Lock()
defer g.tmux.Unlock()
now := time.Now()
it := &htimer{
index: len(g.timers),
expire: now.Add(timeout),
f: f,
parent: g,
}
heap.Push(&g.timers, it)
if g.timers[0] == it {
g.trigger.Reset(timeout)
}
return it
}
func (g *Gopher) removeTimer(it *htimer) {
g.tmux.Lock()
defer g.tmux.Unlock()
index := it.index
if index < 0 || index >= len(g.timers) {
return
}
if g.timers[index] == it {
heap.Remove(&g.timers, index)
if len(g.timers) > 0 {
if index == 0 {
g.trigger.Reset(g.timers[0].expire.Sub(time.Now()))
}
} else {
g.trigger.Reset(timeForever)
}
}
}
// ResetTimer removes a timer
func (g *Gopher) resetTimer(it *htimer) {
g.tmux.Lock()
defer g.tmux.Unlock()
index := it.index
if index < 0 || index >= len(g.timers) {
return
}
if g.timers[index] == it {
heap.Fix(&g.timers, index)
if index == 0 || it.index == 0 {
g.trigger.Reset(g.timers[0].expire.Sub(time.Now()))
}
}
}
func (g *Gopher) timerLoop() {
defer g.Done()
logging.Debug("Gopher[%v] timer start", g.Name)
defer logging.Debug("Gopher[%v] timer stopped", g.Name)
for {
select {
case <-g.trigger.C:
for {
g.tmux.Lock()
if g.timers.Len() == 0 {
g.tmux.Unlock()
break
}
now := time.Now()
it := g.timers[0]
if now.After(it.expire) {
heap.Remove(&g.timers, it.index)
g.tmux.Unlock()
func() {
defer func() {
err := recover()
if err != nil {
logging.Error("Gopher[%v] exec timer failed: %v", g.Name, err)
debug.PrintStack()
}
}()
it.f()
}()
} else {
g.trigger.Reset(it.expire.Sub(now))
g.tmux.Unlock()
break
}
}
case <-g.chTimer:
return
}
}
}
// PollerBuffer returns Poller's buffer by Conn, can be used on linux/bsd
func (g *Gopher) PollerBuffer(c *Conn) []byte {
return g.pollers[uint32(c.Hash())%uint32(g.pollerNum)].ReadBuffer
}
func (g *Gopher) initHandlers() {
g.OnOpen(func(c *Conn) {})
g.OnClose(func(c *Conn, err error) {})
// g.OnRead(func(c *Conn, b []byte) ([]byte, error) {
// n, err := c.Read(b)
// if n > 0 {
// return b[:n], err
// }
// return nil, err
// })
g.OnData(func(c *Conn, data []byte) {})
g.OnReadBufferAlloc(g.PollerBuffer)
g.OnReadBufferFree(func(c *Conn, buffer []byte) {})
g.OnWriteBufferRelease(func(c *Conn, buffer []byte) {})
g.BeforeRead(func(c *Conn) {})
g.AfterRead(func(c *Conn) {})
g.BeforeWrite(func(c *Conn) {})
g.OnStop(func() {})
}
func (g *Gopher) borrow(c *Conn) []byte {
return g.onReadBufferAlloc(c)
}
func (g *Gopher) payback(c *Conn, buffer []byte) {
g.onReadBufferFree(c, buffer)
}