Tidy up peer logging (#4536)

* Tidy up peer logging
* Merge branch 'master' into peerlogs
* Merge branch 'master' into peerlogs
* Merge branch 'master' into peerlogs
This commit is contained in:
Jim McDonald
2020-01-16 09:43:10 +00:00
committed by prylabs-bulldozer[bot]
parent 2e5429c94e
commit 5349b00e19

View File

@@ -23,19 +23,19 @@ func (s *Service) AddConnectionHandler(reqFunc func(ctx context.Context, id peer
// Handle the various pre-existing conditions that will result in us not handshaking.
peerConnectionState, err := s.peers.ConnectionState(conn.RemotePeer())
if err == nil && (peerConnectionState == peers.PeerConnected || peerConnectionState == peers.PeerConnecting) {
log.WithField("currentState", peerConnectionState).Debug("Peer already active; not handshaking again")
log.WithField("currentState", peerConnectionState).WithField("reason", "already active").Debug("Ignoring connection request")
return
}
s.peers.Add(conn.RemotePeer(), conn.RemoteMultiaddr(), conn.Stat().Direction)
if len(s.peers.Active()) >= int(s.cfg.MaxPeers) {
log.Debug("We have enough peers; disconnecting")
log.WithField("reason", "at peer limit").Debug("Ignoring connection request")
if err := s.Disconnect(conn.RemotePeer()); err != nil {
log.WithError(err).Error("Unable to disconnect from peer")
}
return
}
if s.peers.IsBad(conn.RemotePeer()) {
log.Trace("Disconnecting from bad peer")
log.WithField("reason", "bad peer").Debug("Ignoring connection request")
if err := s.Disconnect(conn.RemotePeer()); err != nil {
log.WithError(err).Error("Unable to disconnect from peer")
}
@@ -92,6 +92,11 @@ func (s *Service) AddDisconnectionHandler(handler func(ctx context.Context, id p
log := log.WithField("peer", conn.RemotePeer().Pretty())
// Must be handled in a goroutine as this callback cannot be blocking.
go func() {
priorState, err := s.peers.ConnectionState(conn.RemotePeer())
if err != nil {
// Can happen if the peer has already disconnected, so...
priorState = peers.PeerDisconnected
}
s.peers.SetConnectionState(conn.RemotePeer(), peers.PeerDisconnecting)
ctx := context.Background()
if err := handler(ctx, conn.RemotePeer()); err != nil {
@@ -99,9 +104,9 @@ func (s *Service) AddDisconnectionHandler(handler func(ctx context.Context, id p
}
s.peers.SetConnectionState(conn.RemotePeer(), peers.PeerDisconnected)
s.host.ConnManager().Unprotect(conn.RemotePeer(), "protocol")
// Log good peer disconnects; bad peers can visit here frequently so do not log them.
if !s.peers.IsBad(conn.RemotePeer()) {
log.WithField("active", len(s.peers.Active())).Debug("Peer disconnected")
// Only log disconnections if we were fully connected.
if priorState == peers.PeerConnected {
log.WithField("active", len(s.peers.Active())).Info("Peer disconnected")
}
}()
},