Fix RPC connections counting

* show incomming connections count
This commit is contained in:
biospb 2022-04-09 03:17:19 +03:00
parent beb038c815
commit 8a3d30853c

View File

@ -94,10 +94,11 @@ func (s *gRPCServer) SetOnConnectedHandler(onConnectedHandler server.OnConnected
} }
func (s *gRPCServer) handleInboundConnection(ctx context.Context, stream grpcStream) error { func (s *gRPCServer) handleInboundConnection(ctx context.Context, stream grpcStream) error {
err := s.incrementInboundConnectionCountAndLimitIfRequired() connectionCount, err := s.incrementInboundConnectionCountAndLimitIfRequired()
if err != nil { if err != nil {
return err return err
} }
defer s.decrementInboundConnectionCount()
peerInfo, ok := peer.FromContext(ctx) peerInfo, ok := peer.FromContext(ctx)
if !ok { if !ok {
@ -115,23 +116,23 @@ func (s *gRPCServer) handleInboundConnection(ctx context.Context, stream grpcStr
return err 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 <-connection.stopChan
s.decrementInboundConnectionCount()
return nil return nil
} }
func (s *gRPCServer) incrementInboundConnectionCountAndLimitIfRequired() error { func (s *gRPCServer) incrementInboundConnectionCountAndLimitIfRequired() (int, error) {
s.inboundConnectionCountLock.Lock() s.inboundConnectionCountLock.Lock()
defer s.inboundConnectionCountLock.Unlock() defer s.inboundConnectionCountLock.Unlock()
if s.maxInboundConnections > 0 && s.inboundConnectionCount == s.maxInboundConnections { 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++ s.inboundConnectionCount++
return nil return s.inboundConnectionCount, nil
} }
func (s *gRPCServer) decrementInboundConnectionCount() { func (s *gRPCServer) decrementInboundConnectionCount() {