diff options
author | FUJITA Tomonori <fujita.tomonori@gmail.com> | 2019-03-25 22:22:00 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@gmail.com> | 2019-03-26 06:39:08 +0900 |
commit | 4a7c30cb8faae7153d197977493bfd3c6f0820b9 (patch) | |
tree | 1566b0d34e455c98b52398c56b3f20d9fc9e4a96 | |
parent | 80ff3a325cbced352489fa4ea4b570e5f08062ef (diff) |
server: handle grpc listen failure as fatal error
Fixed the bug to ignore grpc listen failure.
Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
-rw-r--r-- | pkg/server/grpc_server.go | 33 |
1 files changed, 22 insertions, 11 deletions
diff --git a/pkg/server/grpc_server.go b/pkg/server/grpc_server.go index cb93c599..d4b3a09e 100644 --- a/pkg/server/grpc_server.go +++ b/pkg/server/grpc_server.go @@ -61,30 +61,41 @@ func newAPIserver(b *BgpServer, g *grpc.Server, hosts string) *server { func (s *server) serve() error { var wg sync.WaitGroup - l := strings.Split(s.hosts, ",") - wg.Add(len(l)) - - serve := func(host string) { - defer wg.Done() - lis, err := net.Listen("tcp", host) + l := []net.Listener{} + var err error + for _, host := range strings.Split(s.hosts, ",") { + var lis net.Listener + lis, err = net.Listen("tcp", host) if err != nil { log.WithFields(log.Fields{ "Topic": "grpc", "Key": host, "Error": err, }).Warn("listen failed") - return + break } - err = s.grpcServer.Serve(lis) + l = append(l, lis) + } + if err != nil { + for _, lis := range l { + lis.Close() + } + return err + } + + wg.Add(len(l)) + serve := func(lis net.Listener) { + defer wg.Done() + err := s.grpcServer.Serve(lis) log.WithFields(log.Fields{ "Topic": "grpc", - "Key": host, + "Key": lis, "Error": err, }).Warn("accept failed") } - for _, host := range l { - go serve(host) + for _, lis := range l { + go serve(lis) } wg.Wait() return nil |