-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.go
347 lines (324 loc) · 8.03 KB
/
service.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
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
package cnet
import (
"github.com/cuckooemm/cnet/internal/netpoll"
"log"
"os"
"os/signal"
"runtime"
"sync"
)
var defaultLogger = Logger(log.New(os.Stderr, "[service] - ", log.LstdFlags))
type tcpServer struct {
ln *tcpListener
wg sync.WaitGroup // event-loop close WaitGroup
opt *TcpOption // options with server
once sync.Once // make sure only signalShutdown once
cond *sync.Cond // shutdown signaler
logger Logger // customized logger for logging info
network, localAddr string // network and local address
loop *eventTcpLoop // main loop for accepting connections
mainLoop *eventTcpLoop
eventHandler IEventCallback // user eventHandler
subLoopGroup IEventTcpLoopGroup // loops for handling events
}
type udpServer struct {
ln *udpListener
wg sync.WaitGroup // event-loop close WaitGroup
opt *UdpOption // options with server
once sync.Once // make sure only signalShutdown once
cond *sync.Cond // shutdown signaler
logger Logger // customized logger for logging info
network, localAddr string // network and local address
loop *eventUdpLoop
loopGroup []*eventUdpLoop
eventHandler IEventCallback // user eventHandler
}
// 开启服务
func (srv *tcpServer) start(core int) error {
// 内核负载均衡
if srv.opt.ReusePort {
return srv.initLoops(core)
}
return srv.initReactors(core)
}
// 等待服务关闭
func (srv *tcpServer) wait() {
go srv.signalHandler()
// Wait on a signal for shutdown
srv.waitForShutdown()
var err error
// 通知loop关闭监听
srv.subLoopGroup.iterate(func(el *eventTcpLoop) bool {
if err = el.poller.Trigger(func() error {
return ErrServerShutdown
}); err != nil {
srv.logger.Printf("failed to close %d event-loop. err : %v\n", el.idx, err)
}
return true
})
if srv.mainLoop != nil {
srv.ln.close()
if err = srv.mainLoop.poller.Trigger(func() error {
return ErrServerShutdown
}); err != nil {
srv.logger.Printf("failed to close main-loop. err : %v\n", err)
}
}
// Wait on all loops to complete reading events
srv.wg.Wait()
// Close loops and all outstanding connections
srv.subLoopGroup.iterate(func(el *eventTcpLoop) bool {
for _, c := range el.connections {
if err := el.loopCloseConn(c, nil); err != nil {
srv.logger.Printf("failed to close connection %s\n", c.remoteAddr)
}
}
return true
})
srv.closeLoops()
if srv.mainLoop != nil {
if err = srv.mainLoop.poller.Close(); err != nil {
srv.logger.Printf("failed to close main-loop poller,err : %v\n", err)
}
}
}
func (srv *udpServer) stop() {
// Wait on a signal for shutdown
srv.waitForShutdown()
var err error
// 通知loop关闭监听
for _, loop := range srv.loopGroup {
if err = loop.poller.Trigger(func() error {
return ErrServerShutdown
}); err != nil {
srv.logger.Printf("failed to close %d event-loop. err : %v\n", loop.idx, err)
}
}
// Wait on all loops to complete reading events
srv.wg.Wait()
srv.closeLoops()
}
func (srv *tcpServer) initLoops(core int) error {
for i := 0; i < core; i++ {
var (
pr *netpoll.Poller
el *eventTcpLoop
err error
)
if pr, err = netpoll.CreatePoller(); err != nil {
return err
}
el = &eventTcpLoop{
idx: i,
srv: srv,
poller: pr,
buffer: make([]byte, 0x10000), // 65536
connections: make(map[int]*conn, 16),
eventHandler: srv.eventHandler,
}
// event-loop监听同一fd 监听fd事件到达时会唤醒全部
if err = el.poller.AddRead(srv.ln.fd); err != nil {
return err
}
srv.subLoopGroup.register(el)
}
srv.startLoops()
return nil
}
func (srv *udpServer) initLoops(core int) error {
srv.wg.Add(core)
for i := 0; i < core; i++ {
var (
pr *netpoll.Poller
el *eventUdpLoop
err error
)
if pr, err = netpoll.CreatePoller(); err != nil {
return err
}
el = &eventUdpLoop{
idx: i,
srv: srv,
poller: pr,
buffer: make([]byte, 0x10000), // 65536
eventHandler: srv.eventHandler,
}
// event-loop监听同一fd 监听fd事件到达时会唤醒全部
if err = el.poller.AddRead(srv.ln.fd); err != nil {
return err
}
go func(el *eventUdpLoop) {
el.loopRun()
srv.wg.Done()
}(el)
}
return nil
}
func (srv *tcpServer) startLoops() {
srv.subLoopGroup.iterate(func(loop *eventTcpLoop) bool {
srv.wg.Add(1)
go func() {
loop.loopRun()
srv.wg.Done()
}()
return true
})
}
func (srv *tcpServer) initReactors(numEventLoop int) error {
for i := 0; i < numEventLoop; i++ {
var (
pr *netpoll.Poller
err error
)
if pr, err = netpoll.CreatePoller(); err == nil {
el := &eventTcpLoop{
idx: i,
srv: srv,
poller: pr,
buffer: make([]byte, 0x10000), // 65536
connections: make(map[int]*conn),
eventHandler: srv.eventHandler,
}
srv.subLoopGroup.register(el)
} else {
return err
}
}
// Start sub reactors.
srv.startReactors()
var (
pr *netpoll.Poller
el *eventTcpLoop
err error
)
if pr, err = netpoll.CreatePoller(); err != nil {
return err
}
el = &eventTcpLoop{
idx: -1,
poller: pr,
srv: srv,
}
if err = el.poller.AddRead(srv.ln.fd); err != nil {
return err
}
srv.mainLoop = el
srv.wg.Add(1)
go func() {
srv.activateMainReactor()
srv.wg.Done()
}()
return nil
}
func (srv *tcpServer) startReactors() {
srv.subLoopGroup.iterate(func(el *eventTcpLoop) bool {
srv.wg.Add(1)
go func() {
srv.activateSubReactor(el)
srv.wg.Done()
}()
return true
})
}
func (srv *tcpServer) closeLoops() {
srv.subLoopGroup.iterate(func(loop *eventTcpLoop) bool {
if err := loop.poller.Close(); err != nil {
srv.logger.Printf("closeLoops idx = %d error : %s\n", loop.idx, err.Error())
}
return true
})
}
func (srv *udpServer) closeLoops() {
for _, loop := range srv.loopGroup {
if err := loop.poller.Close(); err != nil {
srv.logger.Printf("closeLoops idx = %d error : %s\n", loop.idx, err.Error())
}
}
}
func (srv *tcpServer) signalShutdown() {
srv.once.Do(func() {
srv.cond.L.Lock()
srv.cond.Signal()
srv.cond.L.Unlock()
})
}
func (srv *tcpServer) waitForShutdown() {
srv.cond.L.Lock()
srv.cond.Wait()
srv.cond.L.Unlock()
}
func (srv *udpServer) signalShutdown() {
srv.once.Do(func() {
srv.cond.L.Lock()
srv.cond.Signal()
srv.cond.L.Unlock()
})
}
func (srv *udpServer) waitForShutdown() {
srv.cond.L.Lock()
srv.cond.Wait()
srv.cond.L.Unlock()
}
func startTcpService(callback IEventCallback, ln *tcpListener, opt *TcpOption) error {
var (
srv tcpServer
err error
)
if opt.MultiCore == 0 {
opt.MultiCore = runtime.NumCPU()
}
srv.opt = opt
srv.ln = ln
srv.localAddr = ln.ln.Addr().String()
srv.network = "tcp"
srv.eventHandler = callback
srv.cond = sync.NewCond(&sync.Mutex{})
srv.subLoopGroup = new(roundRobinEventLoopGroup)
srv.logger = func() Logger {
if opt.Logger == nil {
return defaultLogger
}
return opt.Logger
}()
if err = srv.start(opt.MultiCore); err != nil {
srv.closeLoops()
srv.logger.Printf("service is stop with error : %v\n", err)
return err
}
defer srv.wait()
return nil
}
func startUpdService(callback IEventCallback, ln *udpListener, opt *UdpOption) error {
var (
srv udpServer
err error
)
if opt.MultiCore == 0 {
opt.MultiCore = runtime.NumCPU()
}
srv.opt = opt
srv.ln = ln
srv.network = "udp"
srv.localAddr = ln.ln.LocalAddr().String()
srv.eventHandler = callback
srv.cond = sync.NewCond(&sync.Mutex{})
srv.logger = func() Logger {
if opt.Logger == nil {
return defaultLogger
}
return opt.Logger
}()
if err = srv.initLoops(opt.MultiCore); err != nil {
srv.closeLoops()
srv.logger.Printf("service is stop with error : %v\n", err)
return err
}
defer srv.stop()
return nil
}
func (srv *tcpServer) signalHandler() {
control := make(chan os.Signal)
signal.Notify(control, os.Interrupt, os.Kill)
<-control
srv.signalShutdown()
}