-
Notifications
You must be signed in to change notification settings - Fork 128
/
client.go
390 lines (340 loc) 路 10.5 KB
/
client.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package core
import (
"context"
"errors"
"io"
"net"
"strings"
"sync"
"time"
"github.com/lucas-clemente/quic-go"
"github.com/yomorun/yomo/core/auth"
"github.com/yomorun/yomo/core/frame"
"github.com/yomorun/yomo/core/log"
"github.com/yomorun/yomo/core/yerr"
"github.com/yomorun/yomo/pkg/id"
"github.com/yomorun/yomo/pkg/logger"
pkgtls "github.com/yomorun/yomo/pkg/tls"
)
// ClientOption YoMo client options
type ClientOption func(*ClientOptions)
// Client is the abstraction of a YoMo-Client. a YoMo-Client can be
// Source, Upstream Zipper or StreamFunction.
type Client struct {
name string // name of the client
clientID string // id of the client
clientType ClientType // type of the connection
conn quic.Connection // quic connection
fs *FrameStream // yomo abstract stream
state ConnState // state of the connection
processor func(*frame.DataFrame) // function to invoke when data arrived
receiver func(*frame.BackflowFrame) // function to invoke when data is processed
errorfn func(error) // function to invoke when error occured
closefn func() // function to invoke when client closed
addr string // the address of server connected to
mu sync.Mutex
opts ClientOptions
localAddr string // client local addr, it will be changed on reconnect
logger log.Logger
errc chan error
}
// NewClient creates a new YoMo-Client.
func NewClient(appName string, connType ClientType, opts ...ClientOption) *Client {
c := &Client{
name: appName,
clientID: id.New(),
clientType: connType,
state: ConnStateReady,
opts: ClientOptions{},
errc: make(chan error),
}
c.Init(opts...)
once.Do(func() {
c.init()
})
return c
}
// Init the options.
func (c *Client) Init(opts ...ClientOption) error {
for _, o := range opts {
o(&c.opts)
}
return c.initOptions()
}
// Connect connects to YoMo-Zipper.
func (c *Client) Connect(ctx context.Context, addr string) error {
// TODO: refactor this later as a Connection Manager
// reconnect
// for download zipper
// If you do not check for errors, the connection will be automatically reconnected
go c.reconnect(ctx, addr)
// connect
return c.connect(ctx, addr)
}
func (c *Client) connect(ctx context.Context, addr string) error {
c.mu.Lock()
defer c.mu.Unlock()
if c.state != ConnStateReady && c.state != ConnStateDisconnected {
return nil
}
c.addr = addr
c.state = ConnStateConnecting
// create quic connection
conn, err := quic.DialAddrContext(ctx, addr, c.opts.TLSConfig, c.opts.QuicConfig)
if err != nil {
c.state = ConnStateDisconnected
return err
}
c.conn = conn
// quic stream
stream, err := conn.OpenStreamSync(ctx)
if err != nil {
c.state = ConnStateDisconnected
return err
}
c.fs = NewFrameStream(stream)
// send handshake
handshake := frame.NewHandshakeFrame(
c.name,
c.clientID,
byte(c.clientType),
c.opts.ObserveDataTags,
c.opts.Credential.Name(),
c.opts.Credential.Payload(),
)
if _, err := c.fs.WriteFrame(handshake); err != nil {
c.state = ConnStateDisconnected
return err
}
c.state = ConnStateConnected
c.localAddr = c.conn.LocalAddr().String()
c.logger.Printf("%s鉂わ笍 [%s][%s](%s) is connected to YoMo-Zipper %s", ClientLogPrefix, c.name, c.clientID, c.localAddr, addr)
// receiving frames
go func() {
closeConn, closeClient, err := c.handleFrame()
c.logger.Debugf("%shandleFrame: %v, %v, %T, %v", ClientLogPrefix, closeConn, closeClient, err, err)
c.mu.Lock()
defer c.mu.Unlock()
if c.state == ConnStateClosed {
return
}
c.state = ConnStateDisconnected
c.errc <- err
stream.Close()
if closeConn {
code := yerr.ErrorCodeClientAbort
if e, ok := err.(yerr.YomoError); ok {
code = e.ErrorCode()
}
c.conn.CloseWithError(code.To(), err.Error())
}
if closeClient {
c.close()
}
}()
return nil
}
// handleFrame handles the logic when receiving frame from server.
func (c *Client) handleFrame() (bool, bool, error) {
for {
// this will block until a frame is received
f, err := c.fs.ReadFrame()
if err != nil {
if err == io.EOF {
return true, false, err
} else if strings.HasPrefix(err.Error(), "unknown frame type") {
c.logger.Warnf("%s%v", ClientLogPrefix, err)
continue
} else if e, ok := err.(*quic.IdleTimeoutError); ok {
return false, false, e
} else if e, ok := err.(*quic.ApplicationError); ok {
return false, e.ErrorCode == yerr.ErrorCodeGoaway.To() || e.ErrorCode == yerr.ErrorCodeRejected.To(), e
} else if errors.Is(err, net.ErrClosed) {
return false, false, err
}
return true, false, err
}
// read frame
// first, get frame type
frameType := f.Type()
c.logger.Debugf("%stype=%s, frame=%# x", ClientLogPrefix, frameType, frame.Shortly(f.Encode()))
switch frameType {
case frame.TagOfRejectedFrame:
if v, ok := f.(*frame.RejectedFrame); ok {
return true, true, errors.New(v.Message())
}
case frame.TagOfGoawayFrame:
if v, ok := f.(*frame.GoawayFrame); ok {
return true, true, errors.New(v.Message())
}
case frame.TagOfDataFrame: // DataFrame carries user's data
if v, ok := f.(*frame.DataFrame); ok {
c.logger.Debugf("%sreceive DataFrame, tag=%#x, tid=%s, carry=%# x", ClientLogPrefix, v.GetDataTag(), v.TransactionID(), v.GetCarriage())
if c.processor == nil {
c.logger.Warnf("%sprocessor is nil", ClientLogPrefix)
} else {
c.processor(v)
}
}
case frame.TagOfBackflowFrame:
if v, ok := f.(*frame.BackflowFrame); ok {
c.logger.Debugf("%sreceive BackflowFrame, tag=%#x, carry=%# x", ClientLogPrefix, v.GetDataTag(), v.GetCarriage())
if c.receiver == nil {
c.logger.Warnf("%sreceiver is nil", ClientLogPrefix)
} else {
c.receiver(v)
}
}
default:
c.logger.Warnf("%sunknown or unsupported frame %#x", ClientLogPrefix, frameType)
}
}
}
// Close the client.
func (c *Client) Close() error {
c.mu.Lock()
defer c.mu.Unlock()
if c.state == ConnStateClosed {
return nil
}
if c.conn != nil {
c.conn.CloseWithError(yerr.ErrorCodeClientAbort.To(), "client ask to close")
}
return c.close()
}
func (c *Client) close() error {
c.logger.Printf("%s馃挃 close the connection, name:%s, id:%s, addr:%s", ClientLogPrefix, c.name, c.clientID, c.addr)
// close error channel so that close handler function will be called
close(c.errc)
c.state = ConnStateClosed
return nil
}
// WriteFrame writes a frame to the connection, gurantee threadsafe.
func (c *Client) WriteFrame(frm frame.Frame) error {
c.logger.Debugf("%s[%s](%s)@%s WriteFrame() will write frame: %s", ClientLogPrefix, c.name, c.localAddr, c.state, frm.Type())
if c.state != ConnStateConnected {
return errors.New("client connection isn't connected")
}
if _, err := c.fs.WriteFrame(frm); err != nil {
return err
}
return nil
}
// SetDataFrameObserver sets the data frame handler.
func (c *Client) SetDataFrameObserver(fn func(*frame.DataFrame)) {
c.processor = fn
c.logger.Debugf("%sSetDataFrameObserver(%v)", ClientLogPrefix, c.processor)
}
// SetBackflowFrameObserver sets the backflow frame handler.
func (c *Client) SetBackflowFrameObserver(fn func(*frame.BackflowFrame)) {
c.receiver = fn
c.logger.Debugf("%sSetBackflowFrameObserver(%v)", ClientLogPrefix, c.receiver)
}
// reconnect the connection between client and server.
func (c *Client) reconnect(ctx context.Context, addr string) {
t := time.NewTicker(1 * time.Second)
defer t.Stop()
for {
select {
case <-ctx.Done():
c.logger.Debugf("%s[%s](%s) context.Done()", ClientLogPrefix, c.name, c.localAddr)
return
case err, ok := <-c.errc:
if c.errorfn != nil && err != nil {
c.errorfn(err)
}
if !ok && c.closefn != nil {
c.closefn()
return
}
case <-t.C:
if c.state == ConnStateDisconnected {
c.logger.Printf("%s[%s][%s](%s) is reconnecting to YoMo-Zipper %s...", ClientLogPrefix, c.name, c.clientID, c.localAddr, addr)
err := c.connect(ctx, addr)
if err != nil {
c.logger.Errorf("%s[%s][%s](%s) reconnect error:%v", ClientLogPrefix, c.name, c.clientID, c.localAddr, err)
}
}
}
}
}
func (c *Client) init() {
// // tracing
// _, _, err := tracing.NewTracerProvider(c.name)
// if err != nil {
// logger.Errorf("tracing: %v", err)
// }
}
// ServerAddr returns the address of the server.
func (c *Client) ServerAddr() string {
return c.addr
}
// initOptions init options defaults
func (c *Client) initOptions() error {
// logger
if c.logger == nil {
if c.opts.Logger != nil {
c.logger = c.opts.Logger
} else {
c.logger = logger.Default()
}
}
// observe tag list
if c.opts.ObserveDataTags == nil {
c.opts.ObserveDataTags = make([]byte, 0)
}
// credential
if c.opts.Credential == nil {
c.opts.Credential = auth.NewCredential("")
}
// tls config
if c.opts.TLSConfig == nil {
tc, err := pkgtls.CreateClientTLSConfig()
if err != nil {
c.logger.Errorf("%sCreateClientTLSConfig: %v", ClientLogPrefix, err)
return err
}
c.opts.TLSConfig = tc
}
// quic config
if c.opts.QuicConfig == nil {
c.opts.QuicConfig = &quic.Config{
Versions: []quic.VersionNumber{quic.Version1, quic.VersionDraft29},
MaxIdleTimeout: time.Second * 40,
KeepAlivePeriod: time.Second * 20,
MaxIncomingStreams: 1000,
MaxIncomingUniStreams: 1000,
HandshakeIdleTimeout: time.Second * 3,
InitialStreamReceiveWindow: 1024 * 1024 * 2,
InitialConnectionReceiveWindow: 1024 * 1024 * 2,
TokenStore: quic.NewLRUTokenStore(10, 5),
// DisablePathMTUDiscovery: true,
}
}
// credential
if c.opts.Credential != nil {
c.logger.Printf("%suse credential: [%s]", ClientLogPrefix, c.opts.Credential.Name())
}
return nil
}
// SetObserveDataTags set the data tag list that will be observed.
// Deprecated: use yomo.WithObserveDataTags instead
func (c *Client) SetObserveDataTags(tag ...byte) {
c.opts.ObserveDataTags = append(c.opts.ObserveDataTags, tag...)
}
// Logger get client's logger instance, you can customize this using `yomo.WithLogger`
func (c *Client) Logger() log.Logger {
return c.logger
}
// SetErrorHandler set error handler
func (c *Client) SetErrorHandler(fn func(err error)) {
c.errorfn = fn
}
// SetCloseHandler set close handler
func (c *Client) SetCloseHandler(fn func()) {
c.closefn = fn
}
// ClientID return the client ID
func (c *Client) ClientID() string {
return c.clientID
}