forked from panjf2000/gnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine_windows.go
172 lines (144 loc) · 4.16 KB
/
engine_windows.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
// Copyright (c) 2023 The Gnet Authors. All rights reserved.
//
// 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.
package gnet
import (
"context"
"errors"
"runtime"
"sync"
"sync/atomic"
"golang.org/x/sync/errgroup"
errorx "github.com/panjf2000/gnet/v2/pkg/errors"
)
type engine struct {
ln *listener
opts *Options // options with engine
eventLoops loadBalancer // event-loops for handling events
ticker struct {
ctx context.Context
cancel context.CancelFunc
}
inShutdown int32 // whether the engine is in shutdown
beingShutdown int32 // whether the engine is being shutdown
workerPool struct {
*errgroup.Group
shutdownCtx context.Context
shutdown context.CancelFunc
once sync.Once
}
eventHandler EventHandler // user eventHandler
}
func (eng *engine) isInShutdown() bool {
return atomic.LoadInt32(&eng.inShutdown) == 1
}
// shutdown signals the engine to shut down.
func (eng *engine) shutdown(err error) {
if err != nil && !errors.Is(err, errorx.ErrEngineShutdown) {
eng.opts.Logger.Errorf("engine is being shutdown with error: %v", err)
}
eng.workerPool.shutdown()
atomic.StoreInt32(&eng.beingShutdown, 1)
}
func (eng *engine) closeEventLoops() {
eng.eventLoops.iterate(func(i int, el *eventloop) bool {
el.ch <- errorx.ErrEngineShutdown
return true
})
eng.ln.close()
}
func (eng *engine) start(numEventLoop int) error {
for i := 0; i < numEventLoop; i++ {
el := eventloop{
ch: make(chan interface{}, 1024),
idx: i,
eng: eng,
connections: make(map[*conn]struct{}),
eventHandler: eng.eventHandler,
}
eng.eventLoops.register(&el)
eng.workerPool.Go(el.run)
if i == 0 && eng.opts.Ticker {
eng.workerPool.Go(func() error {
el.ticker(eng.ticker.ctx)
return nil
})
}
}
eng.workerPool.Go(eng.listen)
return nil
}
func (eng *engine) stop(engine Engine) error {
<-eng.workerPool.shutdownCtx.Done()
eng.eventHandler.OnShutdown(engine)
if eng.ticker.cancel != nil {
eng.ticker.cancel()
}
eng.closeEventLoops()
if err := eng.workerPool.Wait(); err != nil && !errors.Is(err, errorx.ErrEngineShutdown) {
eng.opts.Logger.Errorf("engine shutdown error: %v", err)
}
atomic.StoreInt32(&eng.inShutdown, 1)
return nil
}
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
}
shutdownCtx, shutdown := context.WithCancel(context.Background())
eng := engine{
opts: options,
eventHandler: eventHandler,
ln: listener,
workerPool: struct {
*errgroup.Group
shutdownCtx context.Context
shutdown context.CancelFunc
once sync.Once
}{&errgroup.Group{}, shutdownCtx, shutdown, sync.Once{}},
}
switch options.LB {
case RoundRobin:
eng.eventLoops = new(roundRobinLoadBalancer)
case LeastConnections:
eng.eventLoops = new(leastConnectionsLoadBalancer)
case SourceAddrHash:
eng.eventLoops = new(sourceAddrHashLoadBalancer)
}
if options.Ticker {
eng.ticker.ctx, eng.ticker.cancel = context.WithCancel(context.Background())
}
engine := Engine{eng: &eng}
switch eventHandler.OnBoot(engine) {
case None:
case Shutdown:
return nil
}
if err := eng.start(numEventLoop); err != nil {
eng.opts.Logger.Errorf("gnet engine is stopping with error: %v", err)
return err
}
defer eng.stop(engine) //nolint:errcheck
allEngines.Store(protoAddr, &eng)
return nil
}
/*
func (eng *engine) sendCmd(_ *asyncCmd, _ bool) error {
return errorx.ErrUnsupportedOp
}
*/