@@ -9,19 +9,18 @@ import (
9
9
"io"
10
10
"net"
11
11
"net/http"
12
+ "os"
13
+ "os/exec"
14
+ "os/signal"
12
15
"reflect"
13
16
"regexp"
14
17
"runtime"
15
18
"strconv"
16
19
"strings"
17
20
"sync"
18
21
"sync/atomic"
19
- "time"
20
-
21
- "os"
22
- "os/exec"
23
- "os/signal"
24
22
"syscall"
23
+ "time"
25
24
26
25
"github.com/smallnest/rpcx/log"
27
26
"github.com/smallnest/rpcx/protocol"
@@ -245,7 +244,6 @@ func (s *Server) ServeListener(network string, ln net.Listener) (err error) {
245
244
// creating a new service goroutine for each.
246
245
// The service goroutines read requests and then call services to reply to them.
247
246
func (s * Server ) serveListener (ln net.Listener ) error {
248
-
249
247
var tempDelay time.Duration
250
248
251
249
s .mu .Lock ()
@@ -303,6 +301,10 @@ func (s *Server) serveListener(ln net.Listener) error {
303
301
s .activeConn [conn ] = struct {}{}
304
302
s .mu .Unlock ()
305
303
304
+ if share .Trace {
305
+ log .Debug ("server accepted an conn%c" , conn .RemoteAddr ().String ())
306
+ }
307
+
306
308
go s .serveConn (conn )
307
309
}
308
310
}
@@ -333,6 +335,10 @@ func (s *Server) serveConn(conn net.Conn) {
333
335
buf = buf [:ss ]
334
336
log .Errorf ("serving %s panic error: %s, stack:\n %s" , conn .RemoteAddr (), err , buf )
335
337
}
338
+ if share .Trace {
339
+ log .Debug ("server closed conn: %v" , conn .RemoteAddr ().String ())
340
+ }
341
+
336
342
s .mu .Lock ()
337
343
delete (s .activeConn , conn )
338
344
s .mu .Unlock ()
@@ -390,8 +396,12 @@ func (s *Server) serveConn(conn net.Conn) {
390
396
conn .SetWriteDeadline (t0 .Add (s .writeTimeout ))
391
397
}
392
398
399
+ if share .Trace {
400
+ log .Debug ("server received an request %s from conn: %v" , req , conn .RemoteAddr ().String ())
401
+ }
402
+
393
403
ctx = share .WithLocalValue (ctx , StartRequestContextKey , time .Now ().UnixNano ())
394
- var closeConn = false
404
+ closeConn : = false
395
405
if ! req .IsHeartbeat () {
396
406
err = s .auth (ctx , req )
397
407
closeConn = err != nil
@@ -446,15 +456,17 @@ func (s *Server) serveConn(conn net.Conn) {
446
456
447
457
s .Plugins .DoPreHandleRequest (ctx , req )
448
458
459
+ if share .Trace {
460
+ log .Debug ("server handle request %s from conn: %v" , req , conn .RemoteAddr ().String ())
461
+ }
449
462
res , err := s .handleRequest (ctx , req )
450
-
451
463
if err != nil {
452
464
log .Warnf ("rpcx: failed to handle request: %v" , err )
453
465
}
454
466
455
467
s .Plugins .DoPreWriteResponse (ctx , req , res , err )
456
468
if ! req .IsOneway () {
457
- if len (resMetadata ) > 0 { //copy meta in context to request
469
+ if len (resMetadata ) > 0 { // copy meta in context to request
458
470
meta := res .Metadata
459
471
if meta == nil {
460
472
res .Metadata = resMetadata
@@ -476,6 +488,10 @@ func (s *Server) serveConn(conn net.Conn) {
476
488
}
477
489
s .Plugins .DoPostWriteResponse (ctx , req , res , err )
478
490
491
+ if share .Trace {
492
+ log .Debug ("server write response %v for an request %s from conn: %v" , res , req , conn .RemoteAddr ().String ())
493
+ }
494
+
479
495
protocol .FreeMsg (req )
480
496
protocol .FreeMsg (res )
481
497
}()
@@ -549,21 +565,26 @@ func (s *Server) handleRequest(ctx context.Context, req *protocol.Message) (res
549
565
res .SetMessageType (protocol .Response )
550
566
s .serviceMapMu .RLock ()
551
567
service := s .serviceMap [serviceName ]
568
+
569
+ if share .Trace {
570
+ log .Debug ("server get service %s for an request %s" , service , req )
571
+ }
572
+
552
573
s .serviceMapMu .RUnlock ()
553
574
if service == nil {
554
575
err = errors .New ("rpcx: can't find service " + serviceName )
555
576
return handleError (res , err )
556
577
}
557
578
mtype := service .method [methodName ]
558
579
if mtype == nil {
559
- if service .function [methodName ] != nil { //check raw functions
580
+ if service .function [methodName ] != nil { // check raw functions
560
581
return s .handleRequestForFunction (ctx , req )
561
582
}
562
583
err = errors .New ("rpcx: can't find method " + methodName )
563
584
return handleError (res , err )
564
585
}
565
586
566
- var argv = argsReplyPools .Get (mtype .ArgType )
587
+ argv : = argsReplyPools .Get (mtype .ArgType )
567
588
568
589
codec := share .Codecs [req .SerializeType ()]
569
590
if codec == nil {
@@ -601,7 +622,6 @@ func (s *Server) handleRequest(ctx context.Context, req *protocol.Message) (res
601
622
argsReplyPools .Put (mtype .ReplyType , replyv )
602
623
if err != nil {
603
624
return handleError (res , err )
604
-
605
625
}
606
626
res .Payload = data
607
627
}
@@ -614,13 +634,16 @@ func (s *Server) handleRequest(ctx context.Context, req *protocol.Message) (res
614
634
argsReplyPools .Put (mtype .ReplyType , replyv )
615
635
if err != nil {
616
636
return handleError (res , err )
617
-
618
637
}
619
638
res .Payload = data
620
639
} else if replyv != nil {
621
640
argsReplyPools .Put (mtype .ReplyType , replyv )
622
641
}
623
642
643
+ if share .Trace {
644
+ log .Debug ("server called service %s for an request %s" , service , req )
645
+ }
646
+
624
647
return res , nil
625
648
}
626
649
@@ -644,7 +667,7 @@ func (s *Server) handleRequestForFunction(ctx context.Context, req *protocol.Mes
644
667
return handleError (res , err )
645
668
}
646
669
647
- var argv = argsReplyPools .Get (mtype .ArgType )
670
+ argv : = argsReplyPools .Get (mtype .ArgType )
648
671
649
672
codec := share .Codecs [req .SerializeType ()]
650
673
if codec == nil {
@@ -677,7 +700,6 @@ func (s *Server) handleRequestForFunction(ctx context.Context, req *protocol.Mes
677
700
argsReplyPools .Put (mtype .ReplyType , replyv )
678
701
if err != nil {
679
702
return handleError (res , err )
680
-
681
703
}
682
704
res .Payload = data
683
705
} else if replyv != nil {
@@ -841,7 +863,7 @@ func (s *Server) startProcess() (int, error) {
841
863
var env []string
842
864
env = append (env , os .Environ ()... )
843
865
844
- var originalWD , _ = os .Getwd ()
866
+ originalWD , _ : = os .Getwd ()
845
867
allFiles := []* os.File {os .Stdin , os .Stdout , os .Stderr }
846
868
process , err := os .StartProcess (argv0 , os .Args , & os.ProcAttr {
847
869
Dir : originalWD ,
@@ -876,7 +898,7 @@ var ip4Reg = regexp.MustCompile(`^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-
876
898
func validIP4 (ipAddress string ) bool {
877
899
ipAddress = strings .Trim (ipAddress , " " )
878
900
i := strings .LastIndex (ipAddress , ":" )
879
- ipAddress = ipAddress [:i ] //remove port
901
+ ipAddress = ipAddress [:i ] // remove port
880
902
881
903
return ip4Reg .MatchString (ipAddress )
882
904
}
0 commit comments