From 8a3d30853c468beea35855d03ef45a3d51a847e9 Mon Sep 17 00:00:00 2001 From: biospb <42325079+biospb@users.noreply.github.com> Date: Sat, 9 Apr 2022 03:17:19 +0300 Subject: [PATCH] Fix RPC connections counting * show incomming connections count --- .../netadapter/server/grpcserver/grpc_server.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/infrastructure/network/netadapter/server/grpcserver/grpc_server.go b/infrastructure/network/netadapter/server/grpcserver/grpc_server.go index c18a47357..cb5f9d25c 100644 --- a/infrastructure/network/netadapter/server/grpcserver/grpc_server.go +++ b/infrastructure/network/netadapter/server/grpcserver/grpc_server.go @@ -94,10 +94,11 @@ func (s *gRPCServer) SetOnConnectedHandler(onConnectedHandler server.OnConnected } func (s *gRPCServer) handleInboundConnection(ctx context.Context, stream grpcStream) error { - err := s.incrementInboundConnectionCountAndLimitIfRequired() + connectionCount, err := s.incrementInboundConnectionCountAndLimitIfRequired() if err != nil { return err } + defer s.decrementInboundConnectionCount() peerInfo, ok := peer.FromContext(ctx) if !ok { @@ -115,23 +116,23 @@ func (s *gRPCServer) handleInboundConnection(ctx context.Context, stream grpcStr return err } - log.Infof("%s Incoming connection from %s", s.name, peerInfo.Addr) + log.Infof("%s Incoming connection from %s #%d", s.name, peerInfo.Addr, connectionCount) <-connection.stopChan - s.decrementInboundConnectionCount() return nil } -func (s *gRPCServer) incrementInboundConnectionCountAndLimitIfRequired() error { +func (s *gRPCServer) incrementInboundConnectionCountAndLimitIfRequired() (int, error) { s.inboundConnectionCountLock.Lock() defer s.inboundConnectionCountLock.Unlock() if s.maxInboundConnections > 0 && s.inboundConnectionCount == s.maxInboundConnections { - return errors.Errorf("limit of %d inbound connections has been exceeded", s.maxInboundConnections) + log.Warnf("Limit of %d inbound connections has been exceeded", s.maxInboundConnections) + return s.inboundConnectionCount, errors.Errorf("limit of %d inbound connections has been exceeded", s.maxInboundConnections) } s.inboundConnectionCount++ - return nil + return s.inboundConnectionCount, nil } func (s *gRPCServer) decrementInboundConnectionCount() {