forked from topfreegames/pitaya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.go
489 lines (436 loc) · 13.9 KB
/
agent.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// Copyright (c) nano Author and TFG Co. All Rights Reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package agent
import (
"context"
gojson "encoding/json"
e "errors"
"fmt"
"net"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/topfreegames/pitaya/conn/codec"
"github.com/topfreegames/pitaya/conn/message"
"github.com/topfreegames/pitaya/conn/packet"
"github.com/topfreegames/pitaya/constants"
"github.com/topfreegames/pitaya/errors"
"github.com/topfreegames/pitaya/logger"
"github.com/topfreegames/pitaya/metrics"
"github.com/topfreegames/pitaya/protos"
"github.com/topfreegames/pitaya/serialize"
"github.com/topfreegames/pitaya/session"
"github.com/topfreegames/pitaya/tracing"
"github.com/topfreegames/pitaya/util"
"github.com/topfreegames/pitaya/util/compression"
opentracing "github.com/opentracing/opentracing-go"
)
var (
// hbd contains the heartbeat packet data
hbd []byte
// hrd contains the handshake response data
hrd []byte
once sync.Once
)
const handlerType = "handler"
type (
// Agent corresponds to a user and is used for storing raw Conn information
Agent struct {
Session *session.Session // session
appDieChan chan bool // app die channel
chDie chan struct{} // wait for close
chSend chan pendingMessage // push message queue
chStopHeartbeat chan struct{} // stop heartbeats
chStopWrite chan struct{} // stop writing messages
closeMutex sync.Mutex
conn net.Conn // low-level conn fd
decoder codec.PacketDecoder // binary decoder
encoder codec.PacketEncoder // binary encoder
heartbeatTimeout time.Duration
lastAt int64 // last heartbeat unix time stamp
messageEncoder message.Encoder
messagesBufferSize int // size of the pending messages buffer
metricsReporters []metrics.Reporter
serializer serialize.Serializer // message serializer
state int32 // current agent state
}
pendingMessage struct {
ctx context.Context
typ message.Type // message type
route string // message route (push)
mid uint // response message id (response)
payload interface{} // payload
err bool // if its an error message
}
)
// NewAgent create new agent instance
func NewAgent(
conn net.Conn,
packetDecoder codec.PacketDecoder,
packetEncoder codec.PacketEncoder,
serializer serialize.Serializer,
heartbeatTime time.Duration,
messagesBufferSize int,
dieChan chan bool,
messageEncoder message.Encoder,
metricsReporters []metrics.Reporter,
) *Agent {
// initialize heartbeat and handshake data on first user connection
once.Do(func() {
hbdEncode(heartbeatTime, packetEncoder, messageEncoder.IsCompressionEnabled(), serializer.GetName())
})
a := &Agent{
appDieChan: dieChan,
chDie: make(chan struct{}),
chSend: make(chan pendingMessage, messagesBufferSize),
chStopHeartbeat: make(chan struct{}),
chStopWrite: make(chan struct{}),
messagesBufferSize: messagesBufferSize,
conn: conn,
decoder: packetDecoder,
encoder: packetEncoder,
heartbeatTimeout: heartbeatTime,
lastAt: time.Now().Unix(),
serializer: serializer,
state: constants.StatusStart,
messageEncoder: messageEncoder,
metricsReporters: metricsReporters,
}
// binding session
s := session.New(a, true)
metrics.ReportNumberOfConnectedClients(metricsReporters, session.SessionCount)
a.Session = s
return a
}
func (a *Agent) send(m pendingMessage) (err error) {
defer func() {
if e := recover(); e != nil {
err = errors.NewError(constants.ErrBrokenPipe, errors.ErrClientClosedRequest)
}
}()
a.reportChannelSize()
a.chSend <- m
return
}
// Push implementation for session.NetworkEntity interface
func (a *Agent) Push(route string, v interface{}) error {
if a.GetStatus() == constants.StatusClosed {
return errors.NewError(constants.ErrBrokenPipe, errors.ErrClientClosedRequest)
}
switch d := v.(type) {
case []byte:
logger.Log.Debugf("Type=Push, ID=%d, UID=%d, Route=%s, Data=%dbytes",
a.Session.ID(), a.Session.UID(), route, len(d))
default:
logger.Log.Debugf("Type=Push, ID=%d, UID=%d, Route=%s, Data=%+v",
a.Session.ID(), a.Session.UID(), route, v)
}
return a.send(pendingMessage{typ: message.Push, route: route, payload: v})
}
// ResponseMID implementation for session.NetworkEntity interface
// Respond message to session
func (a *Agent) ResponseMID(ctx context.Context, mid uint, v interface{}, isError ...bool) error {
err := false
if len(isError) > 0 {
err = isError[0]
}
if a.GetStatus() == constants.StatusClosed {
err := errors.NewError(constants.ErrBrokenPipe, errors.ErrClientClosedRequest)
tracing.FinishSpan(ctx, err)
metrics.ReportTimingFromCtx(ctx, a.metricsReporters, handlerType, err)
return err
}
if mid <= 0 {
err := constants.ErrSessionOnNotify
tracing.FinishSpan(ctx, err)
metrics.ReportTimingFromCtx(ctx, a.metricsReporters, handlerType, err)
return err
}
switch d := v.(type) {
case []byte:
logger.Log.Debugf("Type=Response, ID=%d, UID=%d, MID=%d, Data=%dbytes",
a.Session.ID(), a.Session.UID(), mid, len(d))
default:
logger.Log.Infof("Type=Response, ID=%d, UID=%d, MID=%d, Data=%+v",
a.Session.ID(), a.Session.UID(), mid, v)
}
return a.send(pendingMessage{ctx: ctx, typ: message.Response, mid: mid, payload: v, err: err})
}
// Close closes the agent, cleans inner state and closes low-level connection.
// Any blocked Read or Write operations will be unblocked and return errors.
func (a *Agent) Close() error {
a.closeMutex.Lock()
defer a.closeMutex.Unlock()
if a.GetStatus() == constants.StatusClosed {
return constants.ErrCloseClosedSession
}
a.SetStatus(constants.StatusClosed)
logger.Log.Debugf("Session closed, ID=%d, UID=%s, IP=%s",
a.Session.ID(), a.Session.UID(), a.conn.RemoteAddr())
// prevent closing closed channel
select {
case <-a.chDie:
// expect
default:
close(a.chStopWrite)
close(a.chStopHeartbeat)
close(a.chDie)
onSessionClosed(a.Session)
}
metrics.ReportNumberOfConnectedClients(a.metricsReporters, session.SessionCount)
return a.conn.Close()
}
// RemoteAddr implementation for session.NetworkEntity interface
// returns the remote network address.
func (a *Agent) RemoteAddr() net.Addr {
return a.conn.RemoteAddr()
}
// String, implementation for Stringer interface
func (a *Agent) String() string {
return fmt.Sprintf("Remote=%s, LastTime=%d", a.conn.RemoteAddr().String(), atomic.LoadInt64(&a.lastAt))
}
// GetStatus gets the status
func (a *Agent) GetStatus() int32 {
return atomic.LoadInt32(&a.state)
}
// Kick sends a kick packet to a client
func (a *Agent) Kick(ctx context.Context) error {
// packet encode
p, err := a.encoder.Encode(packet.Kick, nil)
if err != nil {
return err
}
_, err = a.conn.Write(p)
return err
}
// SetLastAt sets the last at to now
func (a *Agent) SetLastAt() {
atomic.StoreInt64(&a.lastAt, time.Now().Unix())
}
// SetStatus sets the agent status
func (a *Agent) SetStatus(state int32) {
atomic.StoreInt32(&a.state, state)
}
// Handle handles the messages from and to a client
func (a *Agent) Handle() {
defer func() {
a.Close()
logger.Log.Debugf("Session handle goroutine exit, SessionID=%d, UID=%d", a.Session.ID(), a.Session.UID())
}()
go a.write()
go a.heartbeat()
select {
case <-a.chDie: // agent closed signal
return
}
}
// IPVersion returns the remote address ip version.
// net.TCPAddr and net.UDPAddr implementations of String()
// always construct result as <ip>:<port> on both
// ipv4 and ipv6. Also, to see if the ip is ipv6 they both
// check if there is a colon on the string.
// So checking if there are more than one colon here is safe.
func (a *Agent) IPVersion() string {
version := constants.IPv4
ipPort := a.RemoteAddr().String()
if strings.Count(ipPort, ":") > 1 {
version = constants.IPv6
}
return version
}
func (a *Agent) heartbeat() {
ticker := time.NewTicker(a.heartbeatTimeout)
defer func() {
ticker.Stop()
a.Close()
}()
for {
select {
case <-ticker.C:
deadline := time.Now().Add(-2 * a.heartbeatTimeout).Unix()
if atomic.LoadInt64(&a.lastAt) < deadline {
logger.Log.Debugf("Session heartbeat timeout, LastTime=%d, Deadline=%d", atomic.LoadInt64(&a.lastAt), deadline)
return
}
if _, err := a.conn.Write(hbd); err != nil {
return
}
case <-a.chDie:
return
case <-a.chStopHeartbeat:
return
}
}
}
func onSessionClosed(s *session.Session) {
defer func() {
if err := recover(); err != nil {
logger.Log.Errorf("pitaya/onSessionClosed: %v", err)
}
}()
for _, fn1 := range s.OnCloseCallbacks {
fn1()
}
for _, fn2 := range session.SessionCloseCallbacks {
fn2(s)
}
}
// SendHandshakeResponse sends a handshake response
func (a *Agent) SendHandshakeResponse() error {
_, err := a.conn.Write(hrd)
return err
}
func (a *Agent) write() {
// clean func
defer func() {
close(a.chSend)
a.Close()
}()
for {
select {
case data := <-a.chSend:
payload, err := util.SerializeOrRaw(a.serializer, data.payload)
if err != nil {
logger.Log.Errorf("Failed to serialize response: %s", err.Error())
payload, err = util.GetErrorPayload(a.serializer, err)
if err != nil {
tracing.FinishSpan(data.ctx, err)
if data.typ == message.Response {
metrics.ReportTimingFromCtx(data.ctx, a.metricsReporters, handlerType, err)
}
logger.Log.Error("cannot serialize message and respond to the client ", err.Error())
break
}
}
// construct message and encode
m := &message.Message{
Type: data.typ,
Data: payload,
Route: data.route,
ID: data.mid,
Err: data.err,
}
em, err := a.messageEncoder.Encode(m)
if err != nil {
tracing.FinishSpan(data.ctx, err)
if data.typ == message.Response {
metrics.ReportTimingFromCtx(data.ctx, a.metricsReporters, handlerType, err)
}
logger.Log.Errorf("Failed to encode message: %s", err.Error())
break
}
// packet encode
p, err := a.encoder.Encode(packet.Data, em)
if err != nil {
tracing.FinishSpan(data.ctx, err)
if data.typ == message.Response {
metrics.ReportTimingFromCtx(data.ctx, a.metricsReporters, handlerType, err)
}
logger.Log.Errorf("Failed to encode packet: %s", err.Error())
break
}
// close agent if low-level Conn broken
if _, err := a.conn.Write(p); err != nil {
tracing.FinishSpan(data.ctx, err)
if data.typ == message.Response {
metrics.ReportTimingFromCtx(data.ctx, a.metricsReporters, handlerType, err)
}
logger.Log.Errorf("Failed to write response: %s", err.Error())
return
}
var e error
tracing.FinishSpan(data.ctx, e)
if data.typ == message.Response {
var rErr error
if m.Err {
rErr = util.GetErrorFromPayload(a.serializer, payload)
}
metrics.ReportTimingFromCtx(data.ctx, a.metricsReporters, handlerType, rErr)
}
case <-a.chStopWrite:
return
}
}
}
// SendRequest sends a request to a server
func (a *Agent) SendRequest(ctx context.Context, serverID, route string, v interface{}) (*protos.Response, error) {
return nil, e.New("not implemented")
}
// AnswerWithError answers with an error
func (a *Agent) AnswerWithError(ctx context.Context, mid uint, err error) {
if ctx != nil && err != nil {
s := opentracing.SpanFromContext(ctx)
if s != nil {
tracing.LogError(s, err.Error())
}
}
p, e := util.GetErrorPayload(a.serializer, err)
if e != nil {
logger.Log.Errorf("error answering the user with an error: %s", e.Error())
return
}
e = a.Session.ResponseMID(ctx, mid, p, true)
if e != nil {
logger.Log.Errorf("error answering the user with an error: %s", e.Error())
}
}
func hbdEncode(heartbeatTimeout time.Duration, packetEncoder codec.PacketEncoder, dataCompression bool, serializerName string) {
hData := map[string]interface{}{
"code": 200,
"sys": map[string]interface{}{
"heartbeat": heartbeatTimeout.Seconds(),
"dict": message.GetDictionary(),
"serializer": serializerName,
},
}
data, err := gojson.Marshal(hData)
if err != nil {
panic(err)
}
if dataCompression {
compressedData, err := compression.DeflateData(data)
if err != nil {
panic(err)
}
if len(compressedData) < len(data) {
data = compressedData
}
}
hrd, err = packetEncoder.Encode(packet.Handshake, data)
if err != nil {
panic(err)
}
hbd, err = packetEncoder.Encode(packet.Heartbeat, nil)
if err != nil {
panic(err)
}
}
func (a *Agent) reportChannelSize() {
chSendCapacity := a.messagesBufferSize - len(a.chSend)
if chSendCapacity == 0 {
logger.Log.Warnf("chSend is at maximum capacity")
}
for _, mr := range a.metricsReporters {
if err := mr.ReportGauge(metrics.ChannelCapacity, map[string]string{"channel": "agent_chsend"}, float64(chSendCapacity)); err != nil {
logger.Log.Warnf("failed to report chSend channel capaacity: %s", err.Error())
}
}
}