-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathclient_io.go
More file actions
296 lines (256 loc) · 8.15 KB
/
client_io.go
File metadata and controls
296 lines (256 loc) · 8.15 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
/*
* This file is part of Golaxy Distributed Service Development Framework.
*
* Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 of the License, or
* (at your option) any later version.
*
* Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Golaxy Distributed Service Development Framework. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright (c) 2024 pangdogs.
*/
package cli
import (
"context"
"errors"
"git.golaxy.org/core/utils/async"
"git.golaxy.org/core/utils/generic"
"git.golaxy.org/framework/net/gtp"
"git.golaxy.org/framework/net/gtp/transport"
"git.golaxy.org/framework/utils/binaryutil"
"git.golaxy.org/framework/utils/concurrent"
"go.uber.org/zap"
)
type (
DataHandler = generic.DelegateVoid1[[]byte]
EventHandler = transport.EventHandler
)
// IDataIO 数据IO
type IDataIO interface {
// Send 发送数据
Send(data []byte) error
// Listen 监听数据
Listen(ctx context.Context, handler DataHandler) error
}
// IEventIO 事件IO
type IEventIO interface {
// Send 发送事件
Send(event transport.IEvent) error
// Listen 监听事件
Listen(ctx context.Context, handler EventHandler) error
}
type _ClientIO struct {
client *Client
barrier generic.Barrier
terminated async.FutureVoid
dataChan *generic.UnboundedChannel[binaryutil.Bytes]
eventChan *generic.UnboundedChannel[transport.IEvent]
dataListeners concurrent.Listeners[DataHandler, []byte]
eventListeners concurrent.Listeners[EventHandler, transport.IEvent]
}
func (io *_ClientIO) init(client *Client) {
io.client = client
io.terminated = async.NewFutureVoid()
io.dataChan = generic.NewUnboundedChannel[binaryutil.Bytes]()
io.eventChan = generic.NewUnboundedChannel[transport.IEvent]()
}
func (io *_ClientIO) sendLoop() {
loop:
for {
select {
case <-io.client.Done():
break loop
case buff := <-io.dataChan.Out():
if err := io.client.trans.SendData(buff.Payload()); err != nil {
io.client.logger.Error("client send data failed",
zap.String("session_id", io.client.SessionId().String()),
zap.Int64("migrations", io.client.Migrations()),
zap.Error(err))
}
buff.Release()
case event := <-io.eventChan.Out():
err := transport.Retry{
Transceiver: &io.client.transceiver,
Times: io.client.options.IORetryTimes,
}.Send(io.client.transceiver.Send(event))
if err != nil {
io.client.logger.Error("client send event failed",
zap.String("session_id", io.client.SessionId().String()),
zap.Int64("migrations", io.client.Migrations()),
zap.Error(err))
}
}
}
io.barrier.Close()
io.barrier.Wait()
io.dataChan.Close()
io.eventChan.Close()
for buff := range io.dataChan.Out() {
if err := io.client.trans.SendData(buff.Payload()); err != nil {
io.client.logger.Error("client send data failed",
zap.String("session_id", io.client.SessionId().String()),
zap.Int64("migrations", io.client.Migrations()),
zap.Error(err))
}
buff.Release()
}
for event := range io.eventChan.Out() {
err := transport.Retry{
Transceiver: &io.client.transceiver,
Times: io.client.options.IORetryTimes,
}.Send(io.client.transceiver.Send(event))
if err != nil {
io.client.logger.Error("client send event failed",
zap.String("session_id", io.client.SessionId().String()),
zap.Int64("migrations", io.client.Migrations()),
zap.Error(err))
}
}
async.ReturnVoid(io.terminated)
}
func (io *_ClientIO) handlePayload(event transport.Event[*gtp.MsgPayload]) {
rejected := io.dataListeners.Broadcast(event.Msg.Data)
if rejected > 0 {
io.client.logger.Error("some listeners rejected the receive payload due to backpressure",
zap.String("session_id", io.client.SessionId().String()),
zap.Uint32("seq", event.Seq),
zap.Uint32("ack", event.Ack),
zap.Int("rejected", rejected))
}
}
func (io *_ClientIO) handleEvent(event transport.IEvent) {
rejected := io.eventListeners.Broadcast(event)
if rejected > 0 {
io.client.logger.Error("some listeners rejected the receive event due to backpressure",
zap.String("session_id", io.client.SessionId().String()),
zap.Uint32("seq", event.Seq),
zap.Uint32("ack", event.Ack),
zap.Uint8("msg_id", event.Msg.MsgId()),
zap.Int("rejected", rejected))
}
}
type _ClientDataIO _ClientIO
func (io *_ClientDataIO) Send(data []byte) error {
if !io.barrier.Join(1) {
return errors.New("cli: client data i/o is terminating")
}
defer io.barrier.Done()
io.dataChan.In() <- binaryutil.CloneBytes(true, data)
return nil
}
func (io *_ClientDataIO) Listen(ctx context.Context, handler DataHandler) error {
if handler == nil {
return errors.New("cli: handler is nil")
}
return io.addListener(ctx, handler)
}
func (io *_ClientDataIO) addListener(ctx context.Context, handler DataHandler) error {
if ctx == nil {
ctx = context.Background()
}
select {
case <-io.client.Done():
return errors.New("cli: client data i/o is terminating")
default:
}
if !io.barrier.Join(1) {
return errors.New("cli: client data i/o is terminating")
}
ctx, cancel := context.WithCancel(ctx)
go func() {
select {
case <-ctx.Done():
case <-io.client.Done():
}
cancel()
}()
listener := io.dataListeners.Add(handler, io.client.options.DataListenerInboxSize)
go func() {
defer io.barrier.Done()
for {
select {
case <-ctx.Done():
io.dataListeners.Delete(listener)
io.client.logger.Debug("delete a receive data listener", zap.String("session_id", io.client.SessionId().String()))
return
case data := <-listener.Inbox:
listener.Handler.Call(io.client.options.AutoRecover, io.client.options.ReportError, func(panicError error) bool {
if panicError != nil {
io.client.logger.Error("handle receive data panicked",
zap.String("session_id", io.client.SessionId().String()),
zap.Error(panicError))
}
return false
}, data)
}
}
}()
io.client.logger.Debug("add a receive data listener", zap.String("session_id", io.client.SessionId().String()))
return nil
}
type _ClientEventIO _ClientIO
func (io *_ClientEventIO) Send(event transport.IEvent) error {
if !io.barrier.Join(1) {
return errors.New("cli: client event i/o is terminating")
}
defer io.barrier.Done()
io.eventChan.In() <- event
return nil
}
func (io *_ClientEventIO) Listen(ctx context.Context, handler EventHandler) error {
if handler == nil {
return errors.New("cli: handler is nil")
}
return io.addListener(ctx, handler)
}
func (io *_ClientEventIO) addListener(ctx context.Context, handler EventHandler) error {
if ctx == nil {
ctx = context.Background()
}
select {
case <-io.client.Done():
return errors.New("cli: client event i/o is terminating")
default:
}
if !io.barrier.Join(1) {
return errors.New("cli: client event i/o is terminating")
}
ctx, cancel := context.WithCancel(ctx)
go func() {
select {
case <-ctx.Done():
case <-io.client.Done():
}
cancel()
}()
listener := io.eventListeners.Add(handler, io.client.options.EventListenerInboxSize)
go func() {
defer io.barrier.Done()
for {
select {
case <-ctx.Done():
io.eventListeners.Delete(listener)
io.client.logger.Debug("delete a receive event listener", zap.String("session_id", io.client.SessionId().String()))
return
case event := <-listener.Inbox:
listener.Handler.Call(io.client.options.AutoRecover, io.client.options.ReportError, func(panicError error) bool {
if panicError != nil {
io.client.logger.Error("handle receive event panicked",
zap.String("session_id", io.client.SessionId().String()),
zap.Error(panicError))
}
return false
}, event)
}
}
}()
io.client.logger.Debug("add a receive event listener", zap.String("session_id", io.client.SessionId().String()))
return nil
}