diff options
author | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2016-05-23 09:24:23 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2016-05-23 09:24:23 +0900 |
commit | 90c8306a9c09823924e1a8b867a09c370c741722 (patch) | |
tree | 45e43705617997ee5ab7b8dc74428cf5941d7856 /server | |
parent | 42ab0376359f1ee378958b7ae37eaf8fbc7a811b (diff) |
server: handle grpc Serve() error
Serve() returns when accept() failed. After that, gobgp stop listening
on grpc port (no more grpc request).
This patch makes sure that gobgp starts listening on grpc port again.
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
Diffstat (limited to 'server')
-rw-r--r-- | server/grpc_server.go | 37 |
1 files changed, 26 insertions, 11 deletions
diff --git a/server/grpc_server.go b/server/grpc_server.go index e2757d87..c2669da9 100644 --- a/server/grpc_server.go +++ b/server/grpc_server.go @@ -25,6 +25,7 @@ import ( "io" "net" "strings" + "sync" ) const ( @@ -106,20 +107,34 @@ type Server struct { } func (s *Server) Serve() error { + var wg sync.WaitGroup l := strings.Split(s.hosts, ",") - for i, host := range l { - lis, err := net.Listen("tcp", fmt.Sprintf(host)) - if err != nil { - return fmt.Errorf("failed to listen: %v", err) - } - if i == len(l)-1 { - s.grpcServer.Serve(lis) - } else { - go func() { - s.grpcServer.Serve(lis) - }() + wg.Add(len(l)) + + serve := func(host string) { + for { + defer wg.Done() + lis, err := net.Listen("tcp", fmt.Sprintf(host)) + if err != nil { + log.WithFields(log.Fields{ + "Topic": "grpc", + "Key": host, + "Error": err, + }).Warn("listen failed") + return + } + err = s.grpcServer.Serve(lis) + log.WithFields(log.Fields{ + "Topic": "grpc", + "Key": host, + "Error": err, + }).Warn("accept failed") } } + for _, host := range l { + go serve(host) + } + wg.Wait() return nil } |