forked from andeya/erpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
904 lines (810 loc) · 25.9 KB
/
session.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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
// Copyright 2015-2017 HenryLee. 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 tp
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net"
"sync"
"sync/atomic"
"time"
"github.com/henrylee2cn/goutil"
"github.com/henrylee2cn/goutil/coarsetime"
"github.com/henrylee2cn/teleport/codec"
"github.com/henrylee2cn/teleport/socket"
)
type (
// BaseSession a connection session with the common method set.
BaseSession interface {
// Id returns the session id.
Id() string
// Peer returns the peer.
Peer() Peer
// LocalIp returns the local peer ip.
LocalIp() string
// RemoteIp returns the remote peer ip.
RemoteIp() string
// Public returns temporary public data of session(socket).
Public() goutil.Map
// PublicLen returns the length of public data of session(socket).
PublicLen() int
}
// EarlySession a connection session that has not started reading goroutine.
EarlySession interface {
BaseSession
// SetId sets the session id.
SetId(newId string)
// Conn returns the connection.
Conn() net.Conn
// ResetConn resets the connection.
// Note:
// only reset net.Conn, but not reset socket.ProtoFunc;
// inherit the previous session id.
ResetConn(net.Conn, ...socket.ProtoFunc)
// GetProtoFunc returns the socket.ProtoFunc
GetProtoFunc() socket.ProtoFunc
// Send sends packet to peer, before the formal connection.
// Note:
// the external setting seq is invalid, the internal will be forced to set;
// does not support automatic redial after disconnection.
Send(uri string, body interface{}, rerr *Rerror, setting ...socket.PacketSetting) *Rerror
// Receive receives a packet from peer, before the formal connection.
// Note: does not support automatic redial after disconnection.
Receive(socket.NewBodyFunc, ...socket.PacketSetting) (*socket.Packet, *Rerror)
// SessionAge returns the session max age.
SessionAge() time.Duration
// ContextAge returns PULL or PUSH context max age.
ContextAge() time.Duration
// SetSessionAge sets the session max age.
SetSessionAge(duration time.Duration)
// SetContextAge sets PULL or PUSH context max age.
SetContextAge(duration time.Duration)
}
// Session a connection session.
Session interface {
BaseSession
// SetId sets the session id.
SetId(newId string)
// Close closes the session.
Close() error
// Health checks if the session is usable.
Health() bool
// AsyncPull sends a packet and receives reply asynchronously.
// If the args is []byte or *[]byte type, it can automatically fill in the body codec name.
AsyncPull(uri string, args interface{}, reply interface{}, done chan PullCmd, setting ...socket.PacketSetting)
// Pull sends a packet and receives reply.
// Note:
// If the args is []byte or *[]byte type, it can automatically fill in the body codec name;
// If the session is a client role and PeerConfig.RedialTimes>0, it is automatically re-called once after a failure.
Pull(uri string, args interface{}, reply interface{}, setting ...socket.PacketSetting) PullCmd
// Push sends a packet, but do not receives reply.
// Note:
// If the args is []byte or *[]byte type, it can automatically fill in the body codec name;
// If the session is a client role and PeerConfig.RedialTimes>0, it is automatically re-called once after a failure.
Push(uri string, args interface{}, setting ...socket.PacketSetting) *Rerror
// SessionAge returns the session max age.
SessionAge() time.Duration
// ContextAge returns PULL or PUSH context max age.
ContextAge() time.Duration
}
session struct {
peer *peer
getPullHandler, getPushHandler func(uriPath string) (*Handler, bool)
timeSince func(time.Time) time.Duration
timeNow func() time.Time
seq uint64
seqLock sync.Mutex
pullCmdMap goutil.Map
conn net.Conn
protoFuncs []socket.ProtoFunc
socket socket.Socket
status int32 // 0:ok, 1:active closed, 2:disconnect
closeLock sync.RWMutex
writeLock sync.Mutex
graceCtxWaitGroup sync.WaitGroup
gracepullCmdWaitGroup sync.WaitGroup
sessionAge time.Duration
contextAge time.Duration
sessionAgeLock sync.RWMutex
contextAgeLock sync.RWMutex
// only for client role
redialForClientFunc func() bool
redialLock sync.Mutex
}
)
var (
_ EarlySession = new(session)
_ Session = new(session)
_ BaseSession = new(session)
)
func newSession(peer *peer, conn net.Conn, protoFuncs []socket.ProtoFunc) *session {
var s = &session{
peer: peer,
getPullHandler: peer.rootRouter.getPull,
getPushHandler: peer.rootRouter.getPush,
timeSince: peer.timeSince,
timeNow: peer.timeNow,
conn: conn,
protoFuncs: protoFuncs,
socket: socket.NewSocket(conn, protoFuncs...),
pullCmdMap: goutil.AtomicMap(),
sessionAge: peer.defaultSessionAge,
contextAge: peer.defaultContextAge,
}
return s
}
// Peer returns the peer.
func (s *session) Peer() Peer {
return s.peer
}
// Id returns the session id.
func (s *session) Id() string {
return s.socket.Id()
}
// SetId sets the session id.
func (s *session) SetId(newId string) {
oldId := s.Id()
if oldId == newId {
return
}
s.socket.SetId(newId)
hub := s.peer.sessHub
hub.Set(s)
hub.Delete(oldId)
Tracef("session changes id: %s -> %s", oldId, newId)
}
// Conn returns the connection.
func (s *session) Conn() net.Conn {
return s.conn
}
// ResetConn resets the connection.
// Note:
// only reset net.Conn, but not reset socket.ProtoFunc;
// inherit the previous session id.
func (s *session) ResetConn(conn net.Conn, protoFunc ...socket.ProtoFunc) {
s.conn = conn
id := s.Id()
if len(protoFunc) > 0 {
s.socket = socket.NewSocket(conn, protoFunc...)
} else {
s.socket = socket.NewSocket(conn, s.protoFuncs...)
}
s.socket.SetId(id)
}
// GetProtoFunc returns the socket.ProtoFunc
func (s *session) GetProtoFunc() socket.ProtoFunc {
if len(s.protoFuncs) > 0 && s.protoFuncs[0] != nil {
return s.protoFuncs[0]
}
return socket.DefaultProtoFunc()
}
// RemoteIp returns the remote peer ip.
func (s *session) RemoteIp() string {
return s.socket.RemoteAddr().String()
}
// LocalIp returns the local peer ip.
func (s *session) LocalIp() string {
return s.socket.LocalAddr().String()
}
// SessionAge returns the session max age.
func (s *session) SessionAge() time.Duration {
s.sessionAgeLock.RLock()
age := s.sessionAge
s.sessionAgeLock.RUnlock()
return age
}
// SetSessionAge sets the session max age.
func (s *session) SetSessionAge(duration time.Duration) {
s.sessionAgeLock.Lock()
s.sessionAge = duration
if duration > 0 {
s.socket.SetReadDeadline(coarsetime.CeilingTimeNow().Add(duration))
} else {
s.socket.SetReadDeadline(time.Time{})
}
s.sessionAgeLock.Unlock()
}
// ContextAge returns PULL or PUSH context max age.
func (s *session) ContextAge() time.Duration {
s.contextAgeLock.RLock()
age := s.contextAge
s.contextAgeLock.RUnlock()
return age
}
// SetContextAge sets PULL or PUSH context max age.
func (s *session) SetContextAge(duration time.Duration) {
s.contextAgeLock.Lock()
s.contextAge = duration
s.contextAgeLock.Unlock()
}
// Send sends packet to peer, before the formal connection.
// Note:
// the external setting seq is invalid, the internal will be forced to set;
// does not support automatic redial after disconnection.
func (s *session) Send(uri string, body interface{}, rerr *Rerror, setting ...socket.PacketSetting) *Rerror {
output := socket.GetPacket(setting...)
s.seqLock.Lock()
output.SetSeq(s.seq)
s.seq++
s.seqLock.Unlock()
if output.BodyCodec() == codec.NilCodecId {
output.SetBodyCodec(s.peer.defaultBodyCodec)
}
if len(uri) > 0 {
output.SetUri(uri)
}
if body != nil {
output.SetBody(body)
}
if rerr != nil {
rerr.SetToMeta(output.Meta())
}
err := s.socket.WritePacket(output)
socket.PutPacket(output)
if err != nil {
rerr := rerrConnClosed.Copy()
rerr.Detail = err.Error()
return rerr
}
return nil
}
// Receive receives a packet from peer, before the formal connection.
// Note: does not support automatic redial after disconnection.
func (s *session) Receive(newBodyFunc socket.NewBodyFunc, setting ...socket.PacketSetting) (*socket.Packet, *Rerror) {
input := socket.GetPacket(setting...)
input.SetNewBody(newBodyFunc)
if readTimeout := s.SessionAge(); readTimeout > 0 {
s.socket.SetReadDeadline(coarsetime.CeilingTimeNow().Add(readTimeout))
}
if err := s.socket.ReadPacket(input); err != nil {
rerr := rerrConnClosed.Copy()
rerr.Detail = err.Error()
socket.PutPacket(input)
return nil, rerr
}
rerr := NewRerrorFromMeta(input.Meta())
return input, rerr
}
// AsyncPull sends a packet and receives reply asynchronously.
// Note:
// If the args is []byte or *[]byte type, it can automatically fill in the body codec name;
// If the session is a client role and PeerConfig.RedialTimes>0, it is automatically re-called once after a failure.
func (s *session) AsyncPull(uri string, args interface{}, reply interface{}, done chan PullCmd, setting ...socket.PacketSetting) {
if done == nil && cap(done) == 0 {
// It must arrange that done has enough buffer for the number of simultaneous
// RPCs that will be using that channel. If the channel
// is totally unbuffered, it's best not to run at all.
Panicf("*session.AsyncPull(): done channel is unbuffered")
}
s.seqLock.Lock()
seq := s.seq
s.seq++
s.seqLock.Unlock()
output := socket.NewPacket(
socket.WithSeq(seq),
socket.WithPtype(TypePull),
socket.WithUri(uri),
socket.WithBody(args),
)
for _, fn := range setting {
fn(output)
}
if output.BodyCodec() == codec.NilCodecId {
output.SetBodyCodec(s.peer.defaultBodyCodec)
}
if age := s.ContextAge(); age > 0 {
ctxTimout, _ := context.WithTimeout(output.Context(), age)
socket.WithContext(ctxTimout)(output)
}
cmd := &pullCmd{
sess: s,
output: output,
reply: reply,
doneChan: done,
start: s.peer.timeNow(),
public: goutil.RwMap(),
}
// count pull-launch
s.gracepullCmdWaitGroup.Add(1)
if s.socket.PublicLen() > 0 {
s.socket.Public().Range(func(key, value interface{}) bool {
cmd.public.Store(key, value)
return true
})
}
s.pullCmdMap.Store(seq, cmd)
defer func() {
if p := recover(); p != nil {
Errorf("panic:\n%v\n%s", p, goutil.PanicTrace(1))
}
}()
cmd.rerr = s.peer.pluginContainer.PreWritePull(cmd)
if cmd.rerr != nil {
cmd.done()
return
}
W:
cmd.rerr = s.write(output)
if cmd.rerr != nil {
if cmd.rerr == rerrConnClosed && s.redialForClient() {
s.pullCmdMap.Delete(seq)
s.seqLock.Lock()
seq = s.seq
s.seq++
s.seqLock.Unlock()
output.SetSeq(seq)
s.pullCmdMap.Store(seq, cmd)
goto W
}
cmd.done()
return
}
s.peer.pluginContainer.PostWritePull(cmd)
}
// Pull sends a packet and receives reply.
// Note:
// If the args is []byte or *[]byte type, it can automatically fill in the body codec name;
// If the session is a client role and PeerConfig.RedialTimes>0, it is automatically re-called once after a failure.
func (s *session) Pull(uri string, args interface{}, reply interface{}, setting ...socket.PacketSetting) PullCmd {
doneChan := make(chan PullCmd, 1)
s.AsyncPull(uri, args, reply, doneChan, setting...)
pullCmd := <-doneChan
close(doneChan)
return pullCmd
}
// Push sends a packet, but do not receives reply.
// Note:
// If the args is []byte or *[]byte type, it can automatically fill in the body codec name;
// If the session is a client role and PeerConfig.RedialTimes>0, it is automatically re-called once after a failure.
func (s *session) Push(uri string, args interface{}, setting ...socket.PacketSetting) *Rerror {
ctx := s.peer.getContext(s, true)
ctx.start = s.peer.timeNow()
output := ctx.output
s.seqLock.Lock()
seq := s.seq
s.seq++
s.seqLock.Unlock()
output.SetSeq(seq)
output.SetPtype(TypePush)
output.SetUri(uri)
output.SetBody(args)
for _, fn := range setting {
fn(output)
}
if output.BodyCodec() == codec.NilCodecId {
output.SetBodyCodec(s.peer.defaultBodyCodec)
}
if age := s.ContextAge(); age > 0 {
ctxTimout, _ := context.WithTimeout(output.Context(), age)
socket.WithContext(ctxTimout)(output)
}
defer func() {
if p := recover(); p != nil {
Errorf("panic when pushing:\n%v\n%s", p, goutil.PanicTrace(1))
}
s.peer.putContext(ctx, true)
}()
rerr := s.peer.pluginContainer.PreWritePush(ctx)
if rerr != nil {
return rerr
}
W:
if rerr = s.write(output); rerr != nil {
if rerr == rerrConnClosed && s.redialForClient() {
s.seqLock.Lock()
output.SetSeq(s.seq)
s.seq++
s.seqLock.Unlock()
goto W
}
return rerr
}
s.runlog(s.peer.timeSince(ctx.start), nil, output, typePushLaunch)
s.peer.pluginContainer.PostWritePush(ctx)
return nil
}
// Public returns temporary public data of session(socket).
func (s *session) Public() goutil.Map {
return s.socket.Public()
}
// PublicLen returns the length of public data of session(socket).
func (s *session) PublicLen() int {
return s.socket.PublicLen()
}
func (s *session) startReadAndHandle() {
var withContext socket.PacketSetting
if readTimeout := s.SessionAge(); readTimeout > 0 {
s.socket.SetReadDeadline(coarsetime.CeilingTimeNow().Add(readTimeout))
ctxTimout, _ := context.WithTimeout(context.Background(), readTimeout)
withContext = socket.WithContext(ctxTimout)
} else {
withContext = socket.WithContext(nil)
}
var err error
defer func() {
if p := recover(); p != nil {
err = fmt.Errorf("%v\n%s", p, goutil.PanicTrace(2))
}
s.readDisconnected(err)
}()
// read pull, pull reple or push
for s.goonRead() {
var ctx = s.peer.getContext(s, false)
withContext(ctx.input)
if s.peer.pluginContainer.PreReadHeader(ctx) != nil {
s.peer.putContext(ctx, false)
return
}
err = s.socket.ReadPacket(ctx.input)
if err != nil || !s.goonRead() {
s.peer.putContext(ctx, false)
return
}
s.graceCtxWaitGroup.Add(1)
if !Go(func() {
defer func() {
s.peer.putContext(ctx, true)
if p := recover(); p != nil {
Debugf("panic:\n%v\n%s", p, goutil.PanicTrace(1))
}
}()
ctx.handle()
}) {
s.peer.putContext(ctx, true)
}
}
}
func (s *session) write(packet *socket.Packet) *Rerror {
status := s.getStatus()
if status != statusOk &&
!(status == statusActiveClosing && packet.Ptype() == TypeReply) {
return rerrConnClosed
}
var (
rerr *Rerror
err error
ctx = packet.Context()
deadline, _ = ctx.Deadline()
)
select {
case <-ctx.Done():
err = ctx.Err()
goto ERR
default:
}
s.writeLock.Lock()
defer s.writeLock.Unlock()
select {
case <-ctx.Done():
err = ctx.Err()
goto ERR
default:
s.socket.SetWriteDeadline(deadline)
err = s.socket.WritePacket(packet)
}
if err == nil {
return nil
}
if err == io.EOF || err == socket.ErrProactivelyCloseSocket {
rerr = rerrConnClosed
if s.redialForClientFunc != nil {
// Wait for the status to change
W:
if s.isOk() {
time.Sleep(time.Millisecond)
goto W
}
}
return rerr
}
ERR:
rerr = rerrWriteFailed.Copy()
rerr.Detail = err.Error()
return rerr
}
const (
statusOk int32 = 0
statusActiveClosing int32 = 1
statusActiveClosed int32 = 2
statusPassiveClosed int32 = 3
)
// Health checks if the session is usable.
func (s *session) Health() bool {
status := s.getStatus()
if status == statusOk {
return true
}
if s.redialForClientFunc == nil {
return false
}
if status == statusPassiveClosed {
return true
}
return false
}
// isOk checks if the session is normal.
func (s *session) isOk() bool {
return atomic.LoadInt32(&s.status) == statusOk
}
func (s *session) goonRead() bool {
status := atomic.LoadInt32(&s.status)
return status == statusOk || status == statusActiveClosing
}
// IsActiveClosed returns whether the connection has been closed, and is actively closed.
func (s *session) IsActiveClosed() bool {
return atomic.LoadInt32(&s.status) == statusActiveClosed
}
func (s *session) activelyClosed() {
atomic.StoreInt32(&s.status, statusActiveClosed)
}
func (s *session) activelyClosing() {
atomic.StoreInt32(&s.status, statusActiveClosing)
}
// IsPassiveClosed returns whether the connection has been closed, and is passively closed.
func (s *session) IsPassiveClosed() bool {
return atomic.LoadInt32(&s.status) == statusPassiveClosed
}
func (s *session) passivelyClosed() {
atomic.StoreInt32(&s.status, statusPassiveClosed)
}
func (s *session) getStatus() int32 {
return atomic.LoadInt32(&s.status)
}
// Close closes the session.
func (s *session) Close() error {
s.closeLock.Lock()
defer s.closeLock.Unlock()
status := s.getStatus()
if status != statusOk {
return nil
}
s.activelyClosing()
s.peer.sessHub.Delete(s.Id())
s.graceCtxWaitGroup.Wait()
s.gracepullCmdWaitGroup.Wait()
// Notice actively closed
if !s.IsPassiveClosed() {
s.activelyClosed()
}
err := s.socket.Close()
s.peer.pluginContainer.PostDisconnect(s)
return err
}
func (s *session) readDisconnected(err error) {
status := s.getStatus()
if status == statusActiveClosed {
return
}
// Notice passively closed
s.passivelyClosed()
s.peer.sessHub.Delete(s.Id())
if err != nil && err != io.EOF && err != socket.ErrProactivelyCloseSocket {
Debugf("disconnect(%s) when reading: %s", s.RemoteIp(), err.Error())
}
s.graceCtxWaitGroup.Wait()
if s.redialForClientFunc == nil || status == statusActiveClosing {
s.pullCmdMap.Range(func(_, v interface{}) bool {
pullCmd := v.(*pullCmd)
pullCmd.cancel()
return true
})
}
if status == statusActiveClosing {
return
}
s.socket.Close()
s.peer.pluginContainer.PostDisconnect(s)
if !s.redialForClient() {
s.pullCmdMap.Range(func(_, v interface{}) bool {
pullCmd := v.(*pullCmd)
pullCmd.cancel()
return true
})
}
}
func (s *session) redialForClient() bool {
if s.redialForClientFunc == nil {
return false
}
s.redialLock.Lock()
defer s.redialLock.Unlock()
status := s.getStatus()
if status == statusOk || status == statusActiveClosed || status == statusActiveClosing {
return true
}
return s.redialForClientFunc()
}
// SessionHub sessions hub
type SessionHub struct {
// key: session id (ip, name and so on)
// value: *session
sessions goutil.Map
}
// newSessionHub creates a new sessions hub.
func newSessionHub() *SessionHub {
chub := &SessionHub{
sessions: goutil.AtomicMap(),
}
return chub
}
// Set sets a *session.
func (sh *SessionHub) Set(sess *session) {
_sess, loaded := sh.sessions.LoadOrStore(sess.Id(), sess)
if !loaded {
return
}
sh.sessions.Store(sess.Id(), sess)
if oldSess := _sess.(*session); sess != oldSess {
oldSess.Close()
}
}
// Get gets *session by id.
// If second returned arg is false, mean the *session is not found.
func (sh *SessionHub) Get(id string) (*session, bool) {
_sess, ok := sh.sessions.Load(id)
if !ok {
return nil, false
}
return _sess.(*session), true
}
// Range calls f sequentially for each id and *session present in the session hub.
// If fn returns false, stop traversing.
func (sh *SessionHub) Range(fn func(*session) bool) {
sh.sessions.Range(func(key, value interface{}) bool {
return fn(value.(*session))
})
}
// Random gets a *session randomly.
// If third returned arg is false, mean no *session is exist.
func (sh *SessionHub) Random() (*session, bool) {
_, sess, exist := sh.sessions.Random()
if !exist {
return nil, false
}
return sess.(*session), true
}
// Len returns the length of the session hub.
// Note: the count implemented using sync.Map may be inaccurate.
func (sh *SessionHub) Len() int {
return sh.sessions.Len()
}
// Delete deletes the *session for a id.
func (sh *SessionHub) Delete(id string) {
sh.sessions.Delete(id)
}
const (
typePushLaunch int8 = 1
typePushHandle int8 = 2
typePullLaunch int8 = 3
typePullHandle int8 = 4
)
func (s *session) runlog(costTime time.Duration, input, output *socket.Packet, logType int8) {
if s.peer.countTime {
var (
printFunc func(string, ...interface{})
slowStr string
)
if costTime < s.peer.slowCometDuration {
printFunc = Infof
} else {
printFunc = Warnf
slowStr = "(slow)"
}
switch logType {
case typePushLaunch:
if s.peer.printBody {
logformat := "[push-launch] remote-ip: %s | seq: %d | cost-time: %s%s | uri: %-30s |\nSEND:\n size: %d\n body[-json]: %s\n"
printFunc(logformat, s.RemoteIp(), output.Seq(), costTime, slowStr, output.Uri(), output.Size(), bodyLogBytes(output))
} else {
logformat := "[push-launch] remote-ip: %s | seq: %d | cost-time: %s%s | uri: %-30s |\nSEND:\n size: %d\n"
printFunc(logformat, s.RemoteIp(), output.Seq(), costTime, slowStr, output.Uri(), output.Size())
}
case typePushHandle:
if s.peer.printBody {
logformat := "[push-handle] remote-ip: %s | seq: %d | cost-time: %s%s | uri: %-30s |\nRECV:\n size: %d\n body[-json]: %s\n"
printFunc(logformat, s.RemoteIp(), input.Seq(), costTime, slowStr, input.Uri(), input.Size(), bodyLogBytes(input))
} else {
logformat := "[push-handle] remote-ip: %s | seq: %d | cost-time: %s%s | uri: %-30s |\nRECV:\n size: %d\n"
printFunc(logformat, s.RemoteIp(), input.Seq(), costTime, slowStr, input.Uri(), input.Size())
}
case typePullLaunch:
if s.peer.printBody {
logformat := "[pull-launch] remote-ip: %s | seq: %d | cost-time: %s%s | uri: %-30s |\nSEND:\n size: %d\n body[-json]: %s\nRECV:\n size: %d\n status: %s\n body[-json]: %s\n"
printFunc(logformat, s.RemoteIp(), output.Seq(), costTime, slowStr, output.Uri(), output.Size(), bodyLogBytes(output), input.Size(), getRerrorBytes(input.Meta()), bodyLogBytes(input))
} else {
logformat := "[pull-launch] remote-ip: %s | seq: %d | cost-time: %s%s | uri: %-30s |\nSEND:\n size: %d\nRECV:\n size: %d\n status: %s\n"
printFunc(logformat, s.RemoteIp(), output.Seq(), costTime, slowStr, output.Uri(), output.Size(), input.Size(), getRerrorBytes(input.Meta()))
}
case typePullHandle:
if s.peer.printBody {
logformat := "[pull-handle] remote-ip: %s | seq: %d | cost-time: %s%s | uri: %-30s |\nRECV:\n size: %d\n body[-json]: %s\nSEND:\n size: %d\n status: %s\n body[-json]: %s\n"
printFunc(logformat, s.RemoteIp(), input.Seq(), costTime, slowStr, input.Uri(), input.Size(), bodyLogBytes(input), output.Size(), getRerrorBytes(output.Meta()), bodyLogBytes(output))
} else {
logformat := "[pull-handle] remote-ip: %s | seq: %d | cost-time: %s%s | uri: %-30s |\nRECV:\n size: %d\nSEND:\n size: %d\n status: %s\n"
printFunc(logformat, s.RemoteIp(), input.Seq(), costTime, slowStr, input.Uri(), input.Size(), output.Size(), getRerrorBytes(output.Meta()))
}
}
} else {
switch logType {
case typePushLaunch:
if s.peer.printBody {
logformat := "[push-launch] remote-ip: %s | seq: %d | uri: %-30s |\nSEND:\n size: %d\n body[-json]: %s\n"
Infof(logformat, s.RemoteIp(), output.Seq(), output.Uri(), output.Size(), bodyLogBytes(output))
} else {
logformat := "[push-launch] remote-ip: %s | seq: %d | uri: %-30s |\nSEND:\n size: %d\n"
Infof(logformat, s.RemoteIp(), output.Seq(), output.Uri(), output.Size())
}
case typePushHandle:
if s.peer.printBody {
logformat := "[push-handle] remote-ip: %s | seq: %d | uri: %-30s |\nRECV:\n size: %d\n body[-json]: %s\n"
Infof(logformat, s.RemoteIp(), input.Seq(), input.Uri(), input.Size(), bodyLogBytes(input))
} else {
logformat := "[push-handle] remote-ip: %s | seq: %d | uri: %-30s |\nRECV:\n size: %d\n"
Infof(logformat, s.RemoteIp(), input.Seq(), input.Uri(), input.Size())
}
case typePullLaunch:
if s.peer.printBody {
logformat := "[pull-launch] remote-ip: %s | seq: %d | uri: %-30s |\nSEND:\n size: %d\n body[-json]: %s\nRECV:\n size: %d\n status: %s\n body[-json]: %s\n"
Infof(logformat, s.RemoteIp(), output.Seq(), output.Uri(), output.Size(), bodyLogBytes(output), input.Size(), getRerrorBytes(input.Meta()), bodyLogBytes(input))
} else {
logformat := "[pull-launch] remote-ip: %s | seq: %d | uri: %-30s |\nSEND:\n size: %d\nRECV:\n size: %d\n status: %s\n"
Infof(logformat, s.RemoteIp(), output.Seq(), output.Uri(), output.Size(), input.Size(), getRerrorBytes(input.Meta()))
}
case typePullHandle:
if s.peer.printBody {
logformat := "[pull-handle] remote-ip: %s | seq: %d | uri: %-30s |\nRECV:\n size: %d\n body[-json]: %s\nSEND:\n size: %d\n status: %s\n body[-json]: %s\n"
Infof(logformat, s.RemoteIp(), input.Seq(), input.Uri(), input.Size(), bodyLogBytes(input), output.Size(), getRerrorBytes(output.Meta()), bodyLogBytes(output))
} else {
logformat := "[pull-handle] remote-ip: %s | seq: %d | uri: %-30s |\nRECV:\n size: %d\nSEND:\n size: %d\n status: %s\n"
Infof(logformat, s.RemoteIp(), input.Seq(), input.Uri(), input.Size(), output.Size(), getRerrorBytes(output.Meta()))
}
}
}
}
func bodyLogBytes(packet *socket.Packet) []byte {
switch v := packet.Body().(type) {
case []byte:
if len(v) == 0 || !isJsonBody(packet) {
return v
}
buf := bytes.NewBuffer(make([]byte, 0, len(v)-1))
err := json.Indent(buf, v[1:], "", " ")
if err != nil {
return v
}
return buf.Bytes()
case *[]byte:
if len(*v) == 0 || !isJsonBody(packet) {
return *v
}
buf := bytes.NewBuffer(make([]byte, 0, len(*v)-1))
err := json.Indent(buf, (*v)[1:], "", " ")
if err != nil {
return *v
}
return buf.Bytes()
default:
b, _ := json.MarshalIndent(v, " ", " ")
return b
}
}
func isJsonBody(packet *socket.Packet) bool {
if packet != nil && packet.BodyCodec() == codec.ID_JSON {
return true
}
return false
}