From 232419458dd668109fd9164ae44217153525c794 Mon Sep 17 00:00:00 2001 From: "renzheng.wang" Date: Wed, 16 Oct 2019 02:17:09 +0800 Subject: [PATCH] refine warden server start --- pkg/net/rpc/warden/server.go | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/pkg/net/rpc/warden/server.go b/pkg/net/rpc/warden/server.go index 9a8518674..ade2c2b8a 100644 --- a/pkg/net/rpc/warden/server.go +++ b/pkg/net/rpc/warden/server.go @@ -302,17 +302,10 @@ func (s *Server) RunUnix(file string) error { // will panic if any error happend // return server itself func (s *Server) Start() (*Server, error) { - lis, err := net.Listen(s.conf.Network, s.conf.Addr) + _, err := s.startWithAddr() if err != nil { return nil, err } - log.Info("warden: start grpc listen addr: %v", lis.Addr()) - reflection.Register(s.server) - go func() { - if err := s.Serve(lis); err != nil { - panic(err) - } - }() return s, nil } @@ -321,10 +314,18 @@ func (s *Server) Start() (*Server, error) { // return server itself and the actually listened address (if configured listen // port is zero, the os will allocate an unused port) func (s *Server) StartWithAddr() (*Server, net.Addr, error) { - lis, err := net.Listen(s.conf.Network, s.conf.Addr) + addr, err := s.startWithAddr() if err != nil { return nil, nil, err } + return s, addr, nil +} + +func (s *Server) startWithAddr() (net.Addr, error) { + lis, err := net.Listen(s.conf.Network, s.conf.Addr) + if err != nil { + return nil, err + } log.Info("warden: start grpc listen addr: %v", lis.Addr()) reflection.Register(s.server) go func() { @@ -332,7 +333,7 @@ func (s *Server) StartWithAddr() (*Server, net.Addr, error) { panic(err) } }() - return s, lis.Addr(), nil + return lis.Addr(), nil } // Serve accepts incoming connections on the listener lis, creating a new