forked from panjf2000/gnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.go
283 lines (242 loc) · 6.88 KB
/
engine.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
// Copyright (c) 2019 Andy Pan
// Copyright (c) 2018 Joshua J Baker
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build linux || freebsd || dragonfly || darwin
// +build linux freebsd dragonfly darwin
package gnet
import (
"context"
"runtime"
"sync"
"sync/atomic"
"github.com/panjf2000/gnet/v2/internal/netpoll"
"github.com/panjf2000/gnet/v2/pkg/errors"
)
type engine struct {
ln *listener // the listener for accepting new connections
lb loadBalancer // event-loops for handling events
wg sync.WaitGroup // event-loop close WaitGroup
opts *Options // options with engine
once sync.Once // make sure only signalShutdown once
cond *sync.Cond // shutdown signaler
mainLoop *eventloop // main event-loop for accepting connections
inShutdown int32 // whether the engine is in shutdown
tickerCtx context.Context // context for ticker
cancelTicker context.CancelFunc // function to stop the ticker
eventHandler EventHandler // user eventHandler
}
func (eng *engine) isInShutdown() bool {
return atomic.LoadInt32(&eng.inShutdown) == 1
}
// waitForShutdown waits for a signal to shut down.
func (eng *engine) waitForShutdown() {
eng.cond.L.Lock()
eng.cond.Wait()
eng.cond.L.Unlock()
}
// signalShutdown signals the engine to shut down.
func (eng *engine) signalShutdown() {
eng.once.Do(func() {
eng.cond.L.Lock()
eng.cond.Signal()
eng.cond.L.Unlock()
})
}
func (eng *engine) startEventLoops() {
eng.lb.iterate(func(i int, el *eventloop) bool {
eng.wg.Add(1)
go func() {
el.run(eng.opts.LockOSThread)
eng.wg.Done()
}()
return true
})
}
func (eng *engine) closeEventLoops() {
eng.lb.iterate(func(i int, el *eventloop) bool {
_ = el.poller.Close()
return true
})
}
func (eng *engine) startSubReactors() {
eng.lb.iterate(func(i int, el *eventloop) bool {
eng.wg.Add(1)
go func() {
el.activateSubReactor(eng.opts.LockOSThread)
eng.wg.Done()
}()
return true
})
}
func (eng *engine) activateEventLoops(numEventLoop int) (err error) {
network, address := eng.ln.network, eng.ln.address
ln := eng.ln
eng.ln = nil
var striker *eventloop
// Create loops locally and bind the listeners.
for i := 0; i < numEventLoop; i++ {
if i > 0 {
if ln, err = initListener(network, address, eng.opts); err != nil {
return
}
}
var p *netpoll.Poller
if p, err = netpoll.OpenPoller(); err == nil {
el := new(eventloop)
el.ln = ln
el.engine = eng
el.poller = p
el.buffer = make([]byte, eng.opts.ReadBufferCap)
el.connections = make(map[int]*conn)
el.eventHandler = eng.eventHandler
if err = el.poller.AddRead(el.ln.packPollAttachment(el.accept)); err != nil {
return
}
eng.lb.register(el)
// Start the ticker.
if el.idx == 0 && eng.opts.Ticker {
striker = el
}
} else {
return
}
}
// Start event-loops in background.
eng.startEventLoops()
go striker.ticker(eng.tickerCtx)
return
}
func (eng *engine) activateReactors(numEventLoop int) error {
for i := 0; i < numEventLoop; i++ {
if p, err := netpoll.OpenPoller(); err == nil {
el := new(eventloop)
el.ln = eng.ln
el.engine = eng
el.poller = p
el.buffer = make([]byte, eng.opts.ReadBufferCap)
el.connections = make(map[int]*conn)
el.eventHandler = eng.eventHandler
eng.lb.register(el)
} else {
return err
}
}
// Start sub reactors in background.
eng.startSubReactors()
if p, err := netpoll.OpenPoller(); err == nil {
el := new(eventloop)
el.ln = eng.ln
el.idx = -1
el.engine = eng
el.poller = p
el.eventHandler = eng.eventHandler
if err = el.poller.AddRead(eng.ln.packPollAttachment(eng.accept)); err != nil {
return err
}
eng.mainLoop = el
// Start main reactor in background.
eng.wg.Add(1)
go func() {
el.activateMainReactor(eng.opts.LockOSThread)
eng.wg.Done()
}()
} else {
return err
}
// Start the ticker.
if eng.opts.Ticker {
go eng.mainLoop.ticker(eng.tickerCtx)
}
return nil
}
func (eng *engine) start(numEventLoop int) error {
if eng.opts.ReusePort || eng.ln.network == "udp" {
return eng.activateEventLoops(numEventLoop)
}
return eng.activateReactors(numEventLoop)
}
func (eng *engine) stop(s Engine) {
// Wait on a signal for shutdown
eng.waitForShutdown()
eng.eventHandler.OnShutdown(s)
// Notify all loops to close by closing all listeners
eng.lb.iterate(func(i int, el *eventloop) bool {
err := el.poller.UrgentTrigger(func(_ interface{}) error { return errors.ErrEngineShutdown }, nil)
if err != nil {
eng.opts.Logger.Errorf("failed to call UrgentTrigger on sub event-loop when stopping engine: %v", err)
}
return true
})
if eng.mainLoop != nil {
eng.ln.close()
err := eng.mainLoop.poller.UrgentTrigger(func(_ interface{}) error { return errors.ErrEngineShutdown }, nil)
if err != nil {
eng.opts.Logger.Errorf("failed to call UrgentTrigger on main event-loop when stopping engine: %v", err)
}
}
// Wait on all loops to complete reading events
eng.wg.Wait()
eng.closeEventLoops()
if eng.mainLoop != nil {
err := eng.mainLoop.poller.Close()
if err != nil {
eng.opts.Logger.Errorf("failed to close poller when stopping engine: %v", err)
}
}
// Stop the ticker.
if eng.opts.Ticker {
eng.cancelTicker()
}
atomic.StoreInt32(&eng.inShutdown, 1)
}
func run(eventHandler EventHandler, listener *listener, options *Options, protoAddr string) error {
// Figure out the proper number of event-loops/goroutines to run.
numEventLoop := 1
if options.Multicore {
numEventLoop = runtime.NumCPU()
}
if options.NumEventLoop > 0 {
numEventLoop = options.NumEventLoop
}
eng := new(engine)
eng.opts = options
eng.eventHandler = eventHandler
eng.ln = listener
switch options.LB {
case RoundRobin:
eng.lb = new(roundRobinLoadBalancer)
case LeastConnections:
eng.lb = new(leastConnectionsLoadBalancer)
case SourceAddrHash:
eng.lb = new(sourceAddrHashLoadBalancer)
}
eng.cond = sync.NewCond(&sync.Mutex{})
if eng.opts.Ticker {
eng.tickerCtx, eng.cancelTicker = context.WithCancel(context.Background())
}
e := Engine{eng}
switch eng.eventHandler.OnBoot(e) {
case None:
case Shutdown:
return nil
}
if err := eng.start(numEventLoop); err != nil {
eng.closeEventLoops()
eng.opts.Logger.Errorf("gnet engine is stopping with error: %v", err)
return err
}
defer eng.stop(e)
allEngines.Store(protoAddr, eng)
return nil
}