forked from andeya/erpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
842 lines (750 loc) · 21.6 KB
/
context.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
// Copyright 2015-2018 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 (
"context"
"fmt"
"reflect"
"sync"
"time"
"github.com/henrylee2cn/goutil"
"github.com/henrylee2cn/teleport/codec"
"github.com/henrylee2cn/teleport/socket"
"github.com/henrylee2cn/teleport/utils"
)
type (
// PreCtx context method set used before reading message header.
PreCtx interface {
// Peer returns the peer.
Peer() Peer
// Session returns the session.
Session() Session
// IP returns the remote addr.
IP() string
// RealIP returns the the current real remote addr.
RealIP() string
// Swap returns custom data swap of context.
Swap() goutil.Map
// Context carries a deadline, a cancelation signal, and other values across
// API boundaries.
Context() context.Context
// Logger logger interface
Logger
}
// WriteCtx context method set for writing message.
WriteCtx interface {
PreCtx
// Output returns writed message.
Output() Message
// Rerror returns the handle error.
Rerror() *Rerror
}
// inputCtx common context method set.
inputCtx interface {
PreCtx
// Seq returns the input message sequence.
Seq() int32
// PeekMeta peeks the header metadata for the input message.
PeekMeta(key string) []byte
// VisitMeta calls f for each existing metadata.
//
// f must not retain references to key and value after returning.
// Make key and/or value copies if you need storing them after returning.
VisitMeta(f func(key, value []byte))
// CopyMeta returns the input message metadata copy.
CopyMeta() *utils.Args
// ServiceMethod returns the input message service method.
ServiceMethod() string
// ResetServiceMethod resets the input message service method.
ResetServiceMethod(string)
}
// ReadCtx context method set for reading message.
ReadCtx interface {
inputCtx
// Input returns readed message.
Input() Message
// Rerror returns the handle error.
Rerror() *Rerror
}
// PushCtx context method set for handling the pushed message.
// For example:
// type HomePush struct{ PushCtx }
PushCtx interface {
inputCtx
// GetBodyCodec gets the body codec type of the input message.
GetBodyCodec() byte
}
// CallCtx context method set for handling the called message.
// For example:
// type HomeCall struct{ CallCtx }
CallCtx interface {
inputCtx
// Input returns readed message.
Input() Message
// GetBodyCodec gets the body codec type of the input message.
GetBodyCodec() byte
// Output returns writed message.
Output() Message
// ReplyBodyCodec initializes and returns the reply message body codec id.
ReplyBodyCodec() byte
// SetBodyCodec sets the body codec for reply message.
SetBodyCodec(byte)
// AddMeta adds the header metadata 'key=value' for reply message.
// Multiple values for the same key may be added.
AddMeta(key, value string)
// SetMeta sets the header metadata 'key=value' for reply message.
SetMeta(key, value string)
// AddXferPipe appends transfer filter pipe of reply message.
AddXferPipe(filterID ...byte)
}
// UnknownPushCtx context method set for handling the unknown pushed message.
UnknownPushCtx interface {
inputCtx
// GetBodyCodec gets the body codec type of the input message.
GetBodyCodec() byte
// InputBodyBytes if the input body binder is []byte type, returns it, else returns nil.
InputBodyBytes() []byte
// Bind when the raw body binder is []byte type, now binds the input body to v.
Bind(v interface{}) (bodyCodec byte, err error)
}
// UnknownCallCtx context method set for handling the unknown called message.
UnknownCallCtx interface {
inputCtx
// GetBodyCodec gets the body codec type of the input message.
GetBodyCodec() byte
// InputBodyBytes if the input body binder is []byte type, returns it, else returns nil.
InputBodyBytes() []byte
// Bind when the raw body binder is []byte type, now binds the input body to v.
Bind(v interface{}) (bodyCodec byte, err error)
// SetBodyCodec sets the body codec for reply message.
SetBodyCodec(byte)
// AddMeta adds the header metadata 'key=value' for reply message.
// Multiple values for the same key may be added.
AddMeta(key, value string)
// SetMeta sets the header metadata 'key=value' for reply message.
SetMeta(key, value string)
// AddXferPipe appends transfer filter pipe of reply message.
AddXferPipe(filterID ...byte)
}
)
var (
_ Logger = new(handlerCtx)
_ PreCtx = new(handlerCtx)
_ inputCtx = new(handlerCtx)
_ WriteCtx = new(handlerCtx)
_ ReadCtx = new(handlerCtx)
_ PushCtx = new(handlerCtx)
_ CallCtx = new(handlerCtx)
_ UnknownPushCtx = new(handlerCtx)
_ UnknownCallCtx = new(handlerCtx)
)
// handlerCtx the underlying common instance of CallCtx and PushCtx.
type handlerCtx struct {
sess *session
input Message
output Message
handler *Handler
arg reflect.Value
callCmd *callCmd
swap goutil.Map
start time.Time
cost time.Duration
pluginContainer *PluginContainer
handleErr *Rerror
context context.Context
next *handlerCtx
}
var (
emptyValue = reflect.Value{}
emptyMethod = reflect.Method{}
)
// newReadHandleCtx creates a handlerCtx for one request/response or push.
func newReadHandleCtx() *handlerCtx {
c := new(handlerCtx)
c.input = socket.NewMessage()
c.input.SetNewBody(c.binding)
c.output = socket.NewMessage()
return c
}
func (c *handlerCtx) reInit(s *session) {
c.sess = s
count := s.socket.SwapLen()
c.swap = goutil.RwMap(count)
if count > 0 {
s.socket.Swap().Range(func(key, value interface{}) bool {
c.swap.Store(key, value)
return true
})
}
}
func (c *handlerCtx) clean() {
c.sess = nil
c.handler = nil
c.arg = emptyValue
c.callCmd = nil
c.swap = nil
c.cost = 0
c.pluginContainer = nil
c.handleErr = nil
c.context = nil
c.input.Reset(socket.WithNewBody(c.binding))
c.output.Reset()
}
// Peer returns the peer.
func (c *handlerCtx) Peer() Peer {
return c.sess.peer
}
// Session returns the session.
func (c *handlerCtx) Session() Session {
return c.sess
}
// Input returns readed message.
func (c *handlerCtx) Input() Message {
return c.input
}
// Output returns writed message.
func (c *handlerCtx) Output() Message {
return c.output
}
// Swap returns custom data swap of context.
func (c *handlerCtx) Swap() goutil.Map {
return c.swap
}
// Seq returns the input message sequence.
func (c *handlerCtx) Seq() int32 {
return c.input.Seq()
}
// ServiceMethod returns the input message service method.
func (c *handlerCtx) ServiceMethod() string {
return c.input.ServiceMethod()
}
// ResetServiceMethod resets the input message service method.
func (c *handlerCtx) ResetServiceMethod(serviceMethod string) {
c.input.SetServiceMethod(serviceMethod)
}
// PeekMeta peeks the header metadata for the input message.
func (c *handlerCtx) PeekMeta(key string) []byte {
return c.input.Meta().Peek(key)
}
// VisitMeta calls f for each existing metadata.
//
// f must not retain references to key and value after returning.
// Make key and/or value copies if you need storing them after returning.
func (c *handlerCtx) VisitMeta(f func(key, value []byte)) {
c.input.Meta().VisitAll(f)
}
// CopyMeta returns the input message metadata copy.
func (c *handlerCtx) CopyMeta() *utils.Args {
dst := utils.AcquireArgs()
c.input.Meta().CopyTo(dst)
return dst
}
// AddMeta adds the header metadata 'key=value' for reply message.
// Multiple values for the same key may be added.
func (c *handlerCtx) AddMeta(key, value string) {
c.output.Meta().Add(key, value)
}
// SetMeta sets the header metadata 'key=value' for reply message.
func (c *handlerCtx) SetMeta(key, value string) {
c.output.Meta().Set(key, value)
}
// GetBodyCodec gets the body codec type of the input message.
func (c *handlerCtx) GetBodyCodec() byte {
return c.input.BodyCodec()
}
// SetBodyCodec sets the body codec for reply message.
func (c *handlerCtx) SetBodyCodec(bodyCodec byte) {
c.output.SetBodyCodec(bodyCodec)
}
// AddXferPipe appends transfer filter pipe of reply message.
func (c *handlerCtx) AddXferPipe(filterID ...byte) {
c.output.XferPipe().Append(filterID...)
}
// IP returns the remote addr.
func (c *handlerCtx) IP() string {
return c.sess.RemoteAddr().String()
}
// RealIP returns the the current real remote addr.
func (c *handlerCtx) RealIP() string {
realIP := c.PeekMeta(MetaRealIP)
if len(realIP) > 0 {
return string(realIP)
}
return c.sess.RemoteAddr().String()
}
// Context carries a deadline, a cancelation signal, and other values across
// API boundaries.
func (c *handlerCtx) Context() context.Context {
if c.context == nil {
return c.input.Context()
}
return c.context
}
// setContext sets the context for timeout.
func (c *handlerCtx) setContext(ctx context.Context) {
c.context = ctx
}
// Be executed synchronously when reading message
func (c *handlerCtx) binding(header Header) (body interface{}) {
c.start = c.sess.timeNow()
c.pluginContainer = c.sess.peer.pluginContainer
switch header.Mtype() {
case TypeReply:
return c.bindReply(header)
case TypePush:
return c.bindPush(header)
case TypeCall:
return c.bindCall(header)
default:
c.handleErr = rerrCodeMtypeNotAllowed
return nil
}
}
const logFormatDisconnected = "disconnected due to unsupported message type: %d %s %s %q RECV(%s)"
// Be executed asynchronously after readed message
func (c *handlerCtx) handle() {
if c.handleErr != nil && c.handleErr.Code == CodeMtypeNotAllowed {
goto E
}
switch c.input.Mtype() {
case TypeReply:
// handles call reply
c.handleReply()
return
case TypePush:
// handles push
c.handlePush()
return
case TypeCall:
// handles and replies call
c.handleCall()
return
default:
}
E:
// if unsupported, disconnected.
rerrCodeMtypeNotAllowed.SetToMeta(c.output.Meta())
Errorf(logFormatDisconnected,
c.input.Mtype(), c.IP(), c.input.ServiceMethod(), c.input.Seq(),
messageLogBytes(c.input, c.sess.peer.printDetail))
go c.sess.Close()
}
func (c *handlerCtx) bindPush(header Header) interface{} {
c.handleErr = c.pluginContainer.postReadPushHeader(c)
if c.handleErr != nil {
return nil
}
if len(header.ServiceMethod()) == 0 {
c.handleErr = rerrBadMessage.Copy().SetReason("invalid service method for message")
return nil
}
var ok bool
c.handler, ok = c.sess.getPushHandler(header.ServiceMethod())
if !ok {
c.handleErr = rerrNotFound
return nil
}
// reset plugin container
c.pluginContainer = c.handler.pluginContainer
c.arg = c.handler.NewArgValue()
c.input.SetBody(c.arg.Interface())
c.handleErr = c.pluginContainer.preReadPushBody(c)
if c.handleErr != nil {
return nil
}
return c.input.Body()
}
// handlePush handles push.
func (c *handlerCtx) handlePush() {
if age := c.sess.ContextAge(); age > 0 {
ctxTimout, _ := context.WithTimeout(context.Background(), age)
c.setContext(ctxTimout)
}
defer func() {
if p := recover(); p != nil {
Errorf("panic:%v\n%s", p, goutil.PanicTrace(2))
}
c.cost = c.sess.timeSince(c.start)
c.sess.printAccessLog(c.RealIP(), c.cost, c.input, nil, typePushHandle)
}()
if c.handleErr == nil && c.handler != nil {
if c.pluginContainer.postReadPushBody(c) == nil {
if c.handler.isUnknown {
c.handler.unknownHandleFunc(c)
} else {
c.handler.handleFunc(c, c.arg)
}
}
}
if c.handleErr != nil {
Warnf("%s", c.handleErr.String())
}
}
func (c *handlerCtx) bindCall(header Header) interface{} {
c.handleErr = c.pluginContainer.postReadCallHeader(c)
if c.handleErr != nil {
return nil
}
if len(header.ServiceMethod()) == 0 {
c.handleErr = rerrBadMessage.Copy().SetReason("invalid service method for message")
return nil
}
var ok bool
c.handler, ok = c.sess.getCallHandler(header.ServiceMethod())
if !ok {
c.handleErr = rerrNotFound
return nil
}
// reset plugin container
c.pluginContainer = c.handler.pluginContainer
if c.handler.isUnknown {
c.input.SetBody(new([]byte))
} else {
c.arg = c.handler.NewArgValue()
c.input.SetBody(c.arg.Interface())
}
c.handleErr = c.pluginContainer.preReadCallBody(c)
if c.handleErr != nil {
return nil
}
return c.input.Body()
}
// handleCall handles and replies call.
func (c *handlerCtx) handleCall() {
var writed bool
defer func() {
if p := recover(); p != nil {
Errorf("panic:%v\n%s", p, goutil.PanicTrace(2))
if !writed {
if c.handleErr == nil {
c.handleErr = rerrInternalServerError.Copy().SetReason(fmt.Sprint(p))
}
c.writeReply(c.handleErr)
}
}
c.cost = c.sess.timeSince(c.start)
c.sess.printAccessLog(c.RealIP(), c.cost, c.input, c.output, typeCallHandle)
}()
c.output.SetMtype(TypeReply)
c.output.SetSeq(c.input.Seq())
c.output.SetServiceMethod(c.input.ServiceMethod())
c.output.XferPipe().AppendFrom(c.input.XferPipe())
if age := c.sess.ContextAge(); age > 0 {
ctxTimout, _ := context.WithTimeout(c.input.Context(), age)
c.setContext(ctxTimout)
socket.WithContext(ctxTimout)(c.output)
}
if c.handleErr == nil {
c.handleErr = NewRerrorFromMeta(c.output.Meta())
}
// handle call
if c.handleErr == nil {
c.handleErr = c.pluginContainer.postReadCallBody(c)
if c.handleErr == nil {
if c.handler.isUnknown {
c.handler.unknownHandleFunc(c)
} else {
c.handler.handleFunc(c, c.arg)
}
}
}
// reply call
c.setReplyBodyCodec(c.handleErr != nil)
c.pluginContainer.preWriteReply(c)
rerr := c.writeReply(c.handleErr)
if rerr != nil {
if c.handleErr == nil {
c.handleErr = rerr
}
if rerr != rerrConnClosed {
c.writeReply(rerrInternalServerError.Copy().SetReason(rerr.Reason))
}
return
}
writed = true
c.pluginContainer.postWriteReply(c)
}
// ReplyBodyCodec initializes and returns the reply message body codec id.
func (c *handlerCtx) ReplyBodyCodec() byte {
id := c.output.BodyCodec()
if id != codec.NilCodecID {
return id
}
id, ok := GetAcceptBodyCodec(c.input.Meta())
if ok {
if _, err := codec.Get(id); err == nil {
c.output.SetBodyCodec(id)
return id
}
}
id = c.input.BodyCodec()
c.output.SetBodyCodec(id)
return id
}
func (c *handlerCtx) setReplyBodyCodec(hasError bool) {
if hasError {
return
}
c.ReplyBodyCodec()
}
func (c *handlerCtx) writeReply(rerr *Rerror) *Rerror {
if rerr != nil {
rerr.SetToMeta(c.output.Meta())
c.output.SetBody(nil)
c.output.SetBodyCodec(codec.NilCodecID)
}
serviceMethod := c.output.ServiceMethod()
c.output.SetServiceMethod("")
_, rerr = c.sess.write(c.output)
c.output.SetServiceMethod(serviceMethod)
return rerr
}
func (c *handlerCtx) bindReply(header Header) interface{} {
_callCmd, ok := c.sess.callCmdMap.Load(header.Seq())
if !ok {
Warnf("not found call cmd: %v", c.input)
return nil
}
c.callCmd = _callCmd.(*callCmd)
// unlock: handleReply
c.callCmd.mu.Lock()
c.input.SetServiceMethod(c.callCmd.output.ServiceMethod())
c.swap = c.callCmd.swap
c.callCmd.inputBodyCodec = c.GetBodyCodec()
// if c.callCmd.inputMeta!=nil, means the callCmd is replyed.
c.callCmd.inputMeta = utils.AcquireArgs()
c.input.Meta().CopyTo(c.callCmd.inputMeta)
c.setContext(c.callCmd.output.Context())
c.input.SetBody(c.callCmd.result)
rerr := c.pluginContainer.postReadReplyHeader(c)
if rerr != nil {
c.callCmd.rerr = rerr
return nil
}
rerr = c.pluginContainer.preReadReplyBody(c)
if rerr != nil {
c.callCmd.rerr = rerr
return nil
}
return c.input.Body()
}
// handleReply handles call reply.
func (c *handlerCtx) handleReply() {
if c.callCmd == nil {
return
}
// lock: bindReply
defer c.callCmd.mu.Unlock()
defer func() {
if p := recover(); p != nil {
Errorf("panic:%v\n%s", p, goutil.PanicTrace(2))
}
c.callCmd.result = c.input.Body()
c.handleErr = c.callCmd.rerr
c.callCmd.done()
c.callCmd.cost = c.sess.timeSince(c.callCmd.start)
c.sess.printAccessLog(c.RealIP(), c.callCmd.cost, c.input, c.callCmd.output, typeCallLaunch)
}()
if c.callCmd.rerr != nil {
return
}
rerr := NewRerrorFromMeta(c.input.Meta())
if rerr == nil {
rerr = c.pluginContainer.postReadReplyBody(c)
}
c.callCmd.rerr = rerr
}
// Rerror returns the handle error.
func (c *handlerCtx) Rerror() *Rerror {
return c.handleErr
}
// InputBodyBytes if the input body binder is []byte type, returns it, else returns nil.
func (c *handlerCtx) InputBodyBytes() []byte {
b, ok := c.input.Body().(*[]byte)
if !ok {
return nil
}
return *b
}
// Bind when the raw body binder is []byte type, now binds the input body to v.
func (c *handlerCtx) Bind(v interface{}) (byte, error) {
b := c.InputBodyBytes()
if b == nil {
return codec.NilCodecID, nil
}
c.input.SetBody(v)
err := c.input.UnmarshalBody(b)
return c.input.BodyCodec(), err
}
type (
// CallCmd the command of the calling operation's response.
CallCmd interface {
// TracePeer trace back the peer.
TracePeer() (peer Peer, found bool)
// TraceSession trace back the session.
TraceSession() (sess Session, found bool)
// Context carries a deadline, a cancelation signal, and other values across
// API boundaries.
Context() context.Context
// Output returns writed message.
Output() Message
// Rerror returns the call error.
Rerror() *Rerror
// Done returns the chan that indicates whether it has been completed.
Done() <-chan struct{}
// Reply returns the call reply.
// NOTE:
// Inside, <-Done() is automatically called and blocked,
// until the call is completed!
Reply() (interface{}, *Rerror)
// InputBodyCodec gets the body codec type of the input message.
// NOTE:
// Inside, <-Done() is automatically called and blocked,
// until the call is completed!
InputBodyCodec() byte
// InputMeta returns the header metadata of input message.
// NOTE:
// Inside, <-Done() is automatically called and blocked,
// until the call is completed!
InputMeta() *utils.Args
// CostTime returns the called cost time.
// If PeerConfig.CountTime=false, always returns 0.
// NOTE:
// Inside, <-Done() is automatically called and blocked,
// until the call is completed!
CostTime() time.Duration
}
callCmd struct {
sess *session
output Message
result interface{}
rerr *Rerror
inputBodyCodec byte
inputMeta *utils.Args
start time.Time
cost time.Duration
swap goutil.Map
mu sync.Mutex
// Send itself to the public channel when call is complete.
callCmdChan chan<- CallCmd
// Strobes when call is complete.
doneChan chan struct{}
}
)
var _ WriteCtx = new(callCmd)
// TracePeer trace back the peer.
func (c *callCmd) TracePeer() (Peer, bool) {
return c.Peer(), true
}
// Peer returns the peer.
func (c *callCmd) Peer() Peer {
return c.sess.peer
}
// TraceSession trace back the session.
func (c *callCmd) TraceSession() (Session, bool) {
return c.Session(), true
}
// Session returns the session.
func (c *callCmd) Session() Session {
return c.sess
}
// IP returns the remote addr.
func (c *callCmd) IP() string {
return c.sess.RemoteAddr().String()
}
// RealIP returns the the current real remote addr.
func (c *callCmd) RealIP() string {
realIP := c.inputMeta.Peek(MetaRealIP)
if len(realIP) > 0 {
return string(realIP)
}
return c.sess.RemoteAddr().String()
}
// Swap returns custom data swap of context.
func (c *callCmd) Swap() goutil.Map {
return c.swap
}
// SwapLen returns the amount of recorded custom data of context.
func (c *callCmd) SwapLen() int {
return c.swap.Len()
}
// Output returns writed message.
func (c *callCmd) Output() Message {
return c.output
}
// Context carries a deadline, a cancelation signal, and other values across
// API boundaries.
func (c *callCmd) Context() context.Context {
return c.output.Context()
}
// Rerror returns the call error.
func (c *callCmd) Rerror() *Rerror {
return c.rerr
}
// Done returns the chan that indicates whether it has been completed.
func (c *callCmd) Done() <-chan struct{} {
return c.doneChan
}
// Reply returns the call reply.
// NOTE:
// Inside, <-Done() is automatically called and blocked,
// until the call is completed!
func (c *callCmd) Reply() (interface{}, *Rerror) {
<-c.Done()
return c.result, c.rerr
}
// InputBodyCodec gets the body codec type of the input message.
// NOTE:
// Inside, <-Done() is automatically called and blocked,
// until the call is completed!
func (c *callCmd) InputBodyCodec() byte {
<-c.Done()
return c.inputBodyCodec
}
// InputMeta returns the header metadata of input message.
// NOTE:
// Inside, <-Done() is automatically called and blocked,
// until the call is completed!
func (c *callCmd) InputMeta() *utils.Args {
<-c.Done()
return c.inputMeta
}
// CostTime returns the called cost time.
// If PeerConfig.CountTime=false, always returns 0.
// NOTE:
// Inside, <-Done() is automatically called and blocked,
// until the call is completed!
func (c *callCmd) CostTime() time.Duration {
<-c.Done()
return c.cost
}
func (c *callCmd) done() {
c.sess.callCmdMap.Delete(c.output.Seq())
c.callCmdChan <- c
close(c.doneChan)
// free count call-launch
c.sess.graceCallCmdWaitGroup.Done()
}
func (c *callCmd) cancel() {
c.sess.callCmdMap.Delete(c.output.Seq())
c.rerr = rerrConnClosed
c.callCmdChan <- c
close(c.doneChan)
// free count call-launch
c.sess.graceCallCmdWaitGroup.Done()
}
// if callCmd.inputMeta!=nil, means the callCmd is replyed.
func (c *callCmd) hasReply() bool {
return c.inputMeta != nil
}