-
Notifications
You must be signed in to change notification settings - Fork 20
/
shard_worker.go
278 lines (232 loc) · 6.64 KB
/
shard_worker.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
// Copyright (c) 2023 Paweł Gaczyński
//
// 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 gain
import (
"fmt"
"net"
"sync/atomic"
"time"
"github.com/pawelgaczynski/gain/logger"
gainErrors "github.com/pawelgaczynski/gain/pkg/errors"
"github.com/pawelgaczynski/gain/pkg/socket"
"github.com/pawelgaczynski/giouring"
"golang.org/x/sys/unix"
)
type shardWorkerConfig struct {
readWriteWorkerConfig
tcpKeepAlive time.Duration
}
type shardWorker struct {
*acceptor
*readWriteWorkerImpl
ring *giouring.Ring
connectionManager *connectionManager
cpuAffinity bool
tcpKeepAlive time.Duration
connectionProtocol bool
accepting atomic.Bool
}
func (w *shardWorker) onAccept(cqe *giouring.CompletionQueueEvent) error {
err := w.addAcceptConnRequest()
if err != nil {
w.accepting.Store(false)
return fmt.Errorf("add accept() request error: %w", err)
}
fileDescriptor := int(cqe.Res)
w.logDebug().Int("fd", fileDescriptor).Msg("Connection accepted")
conn := w.connectionManager.getFd(fileDescriptor)
conn.fd = fileDescriptor
conn.localAddr = w.localAddr
var clientAddr net.Addr
if clientAddr, err = w.acceptor.lastClientAddr(); err == nil {
conn.remoteAddr = clientAddr
} else {
w.logError(err).Msg("get last client address failed")
}
if w.cpuAffinity {
err = unix.SetsockoptInt(fileDescriptor, unix.SOL_SOCKET, unix.SO_INCOMING_CPU, w.index()+1)
if err != nil {
return fmt.Errorf("fd: %d, setting SO_INCOMING_CPU error: %w", fileDescriptor, err)
}
}
if w.tcpKeepAlive > 0 {
err = socket.SetKeepAlivePeriod(fileDescriptor, int(w.tcpKeepAlive.Seconds()))
if err != nil {
return fmt.Errorf("fd: %d, setting tcpKeepAlive error: %w", fileDescriptor, err)
}
}
conn.setUserSpace()
w.eventHandler.OnAccept(conn)
return w.addNextRequest(conn)
}
func (w *shardWorker) stopAccept() error {
return w.syscallCloseSocket(w.fd)
}
func (w *shardWorker) activeConnections() int {
return w.connectionManager.activeConnections() - 1
}
func (w *shardWorker) handleConn(conn *connection, cqe *giouring.CompletionQueueEvent) {
var (
err error
errMsg string
)
switch conn.state {
case connAccept:
err = w.onAccept(cqe)
if err != nil {
errMsg = "accept error"
}
case connRead:
err = w.onRead(cqe, conn)
if err != nil {
errMsg = "read error"
}
case connWrite:
if !w.connectionProtocol && conn.key == 1 {
errMsg = "main socket cannot be in write mode"
break
}
n := int(cqe.Res)
conn.onKernelWrite(n)
w.logDebug().Int("fd", conn.fd).Int32("count", cqe.Res).Msg("Bytes writed")
conn.setUserSpace()
w.eventHandler.OnWrite(conn, n)
if w.sendRecvMsg {
w.connectionManager.release(conn.key)
break
}
err = w.addNextRequest(conn)
if err != nil {
errMsg = "add request error"
}
case connClose:
if cqe.UserData&closeConnFlag > 0 {
w.closeConn(conn, false, nil)
} else if cqe.UserData&writeDataFlag > 0 {
n := int(cqe.Res)
conn.onKernelWrite(n)
w.logDebug().Int("fd", conn.fd).Int32("count", cqe.Res).Msg("Bytes writed")
conn.setUserSpace()
w.eventHandler.OnWrite(conn, n)
}
default:
err = gainErrors.ErrorUnknownConnectionState(int(conn.state))
}
if err != nil {
w.logError(err).Msg(errMsg)
w.closeConn(conn, true, err)
}
}
func (w *shardWorker) initLoop() {
if w.connectionProtocol {
w.prepareHandler = func() error {
w.startedChan <- done
err := w.addAcceptConnRequest()
if err == nil {
w.accepting.Store(true)
}
return err
}
} else {
w.prepareHandler = func() error {
w.startedChan <- done
// 1 is always index for main socket
conn := w.connectionManager.get(1, w.fd)
conn.fd = w.fd
conn.initMsgHeader()
return w.addReadRequest(conn)
}
}
w.shutdownHandler = func() bool {
if w.needToShutdown() {
w.onCloseHandler()
w.markShutdownInProgress()
}
return true
}
w.loopFinisher = w.handleAsyncWritesIfEnabled
w.loopFinishCondition = func() bool {
if w.connectionManager.allClosed() || (w.connectionProtocol && !w.accepting.Load() && w.activeConnections() == 0) {
w.close()
w.notifyFinish()
return true
}
return false
}
}
func (w *shardWorker) loop(fd int) error {
w.logInfo().Int("fd", fd).Msg("Starting worker loop...")
w.fd = fd
w.initLoop()
loopErr := w.startLoop(w.index(), func(cqe *giouring.CompletionQueueEvent) error {
if exit := w.processEvent(cqe, func(cqe *giouring.CompletionQueueEvent) bool {
keyOrFd := cqe.UserData & ^allFlagsMask
if acceptReqFailedAfterStop := keyOrFd == uint64(w.fd) &&
!w.accepting.Load(); acceptReqFailedAfterStop {
return true
}
return false
}); exit {
return nil
}
key := int(cqe.UserData & ^allFlagsMask)
var connFd int
if w.connectionProtocol {
connFd = key
} else {
connFd = w.fd
}
conn := w.connectionManager.get(key, connFd)
w.handleConn(conn, cqe)
return nil
})
_ = w.stopAccept()
return loopErr
}
func (w *shardWorker) closeAllConnsAndRings() {
w.logWarn().Msg("Closing connections")
w.accepting.Store(false)
_ = w.syscallCloseSocket(w.fd)
w.connectionManager.close(func(conn *connection) bool {
err := w.addCloseConnRequest(conn)
if err != nil {
w.logError(err).Msg("Add close() connection request error")
}
return err == nil
}, w.fd)
}
func newShardWorker(
index int, localAddr net.Addr, config shardWorkerConfig, eventHandler EventHandler,
) (*shardWorker, error) {
ring, err := giouring.CreateRing(uint32(config.maxSQEntries))
if err != nil {
return nil, fmt.Errorf("creating ring error: %w", err)
}
logger := logger.NewLogger("worker", config.loggerLevel, config.prettyLogger)
connectionManager := newConnectionManager()
connectionProtocol := !config.sendRecvMsg
worker := &shardWorker{
readWriteWorkerImpl: newReadWriteWorkerImpl(
ring, index, localAddr, eventHandler, connectionManager, config.readWriteWorkerConfig, logger,
),
ring: ring,
connectionManager: connectionManager,
cpuAffinity: config.cpuAffinity,
tcpKeepAlive: config.tcpKeepAlive,
connectionProtocol: connectionProtocol,
acceptor: newAcceptor(ring, connectionManager),
}
worker.onCloseHandler = worker.closeAllConnsAndRings
return worker, nil
}