Skip to content

Commit 3cb61be

Browse files
committed
server: make use of structured logging for peer connections
1 parent a1006d8 commit 3cb61be

File tree

1 file changed

+39
-34
lines changed

1 file changed

+39
-34
lines changed

server.go

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4093,6 +4093,11 @@ func (s *server) InboundPeerConnected(conn net.Conn) {
40934093
s.peerConnected(conn, nil, true)
40944094

40954095
case nil:
4096+
ctx := btclog.WithCtx(
4097+
context.TODO(),
4098+
lnutils.LogPubKey("peer", connectedPeer.IdentityKey()),
4099+
)
4100+
40964101
// We already have a connection with the incoming peer. If the
40974102
// connection we've already established should be kept and is
40984103
// not of the same type of the new connection (inbound), then
@@ -4102,24 +4107,24 @@ func (s *server) InboundPeerConnected(conn net.Conn) {
41024107
if !connectedPeer.Inbound() &&
41034108
!shouldDropLocalConnection(localPub, nodePub) {
41044109

4105-
srvrLog.Warnf("Received inbound connection from "+
4106-
"peer %v, but already have outbound "+
4107-
"connection, dropping conn", connectedPeer)
4110+
srvrLog.WarnS(ctx, "Received inbound connection from "+
4111+
"peer, but already have outbound "+
4112+
"connection, dropping conn",
4113+
fmt.Errorf("already have outbound conn"))
41084114
conn.Close()
41094115
return
41104116
}
41114117

41124118
// Otherwise, if we should drop the connection, then we'll
41134119
// disconnect our already connected peer.
4114-
srvrLog.Debugf("Disconnecting stale connection to %v",
4115-
connectedPeer)
4120+
srvrLog.DebugS(ctx, "Disconnecting stale connection")
41164121

41174122
s.cancelConnReqs(pubStr, nil)
41184123

41194124
// Remove the current peer from the server's internal state and
41204125
// signal that the peer termination watcher does not need to
41214126
// execute for this peer.
4122-
s.removePeerUnsafe(connectedPeer)
4127+
s.removePeerUnsafe(ctx, connectedPeer)
41234128
s.ignorePeerTermination[connectedPeer] = struct{}{}
41244129
s.scheduledPeerConnection[pubStr] = func() {
41254130
s.peerConnected(conn, nil, true)
@@ -4208,6 +4213,11 @@ func (s *server) OutboundPeerConnected(connReq *connmgr.ConnReq, conn net.Conn)
42084213
s.peerConnected(conn, connReq, false)
42094214

42104215
case nil:
4216+
ctx := btclog.WithCtx(
4217+
context.TODO(),
4218+
lnutils.LogPubKey("peer", connectedPeer.IdentityKey()),
4219+
)
4220+
42114221
// We already have a connection with the incoming peer. If the
42124222
// connection we've already established should be kept and is
42134223
// not of the same type of the new connection (outbound), then
@@ -4217,9 +4227,10 @@ func (s *server) OutboundPeerConnected(connReq *connmgr.ConnReq, conn net.Conn)
42174227
if connectedPeer.Inbound() &&
42184228
shouldDropLocalConnection(localPub, nodePub) {
42194229

4220-
srvrLog.Warnf("Established outbound connection to "+
4221-
"peer %v, but already have inbound "+
4222-
"connection, dropping conn", connectedPeer)
4230+
srvrLog.WarnS(ctx, "Established outbound connection "+
4231+
"to peer, but already have inbound "+
4232+
"connection, dropping conn",
4233+
fmt.Errorf("already have inbound conn"))
42234234
if connReq != nil {
42244235
s.connMgr.Remove(connReq.ID())
42254236
}
@@ -4230,13 +4241,12 @@ func (s *server) OutboundPeerConnected(connReq *connmgr.ConnReq, conn net.Conn)
42304241
// Otherwise, _their_ connection should be dropped. So we'll
42314242
// disconnect the peer and send the now obsolete peer to the
42324243
// server for garbage collection.
4233-
srvrLog.Debugf("Disconnecting stale connection to %v",
4234-
connectedPeer)
4244+
srvrLog.DebugS(ctx, "Disconnecting stale connection")
42354245

42364246
// Remove the current peer from the server's internal state and
42374247
// signal that the peer termination watcher does not need to
42384248
// execute for this peer.
4239-
s.removePeerUnsafe(connectedPeer)
4249+
s.removePeerUnsafe(ctx, connectedPeer)
42404250
s.ignorePeerTermination[connectedPeer] = struct{}{}
42414251
s.scheduledPeerConnection[pubStr] = func() {
42424252
s.peerConnected(conn, connReq, false)
@@ -4684,16 +4694,18 @@ func (s *server) peerInitializer(p *peer.Brontide) {
46844694
func (s *server) peerTerminationWatcher(p *peer.Brontide, ready chan struct{}) {
46854695
defer s.wg.Done()
46864696

4687-
ctx := context.TODO()
4697+
ctx := btclog.WithCtx(
4698+
context.TODO(), lnutils.LogPubKey("peer", p.IdentityKey()),
4699+
)
46884700

46894701
p.WaitForDisconnect(ready)
46904702

4691-
srvrLog.Debugf("Peer %v has been disconnected", p)
4703+
srvrLog.DebugS(ctx, "Peer has been disconnected")
46924704

46934705
// If the server is exiting then we can bail out early ourselves as all
46944706
// the other sub-systems will already be shutting down.
46954707
if s.Stopped() {
4696-
srvrLog.Debugf("Server quitting, exit early for peer %v", p)
4708+
srvrLog.DebugS(ctx, "Server quitting, exit early for peer")
46974709
return
46984710
}
46994711

@@ -4728,7 +4740,7 @@ func (s *server) peerTerminationWatcher(p *peer.Brontide, ready chan struct{}) {
47284740

47294741
// If there were any notification requests for when this peer
47304742
// disconnected, we can trigger them now.
4731-
srvrLog.Debugf("Notifying that peer %v is offline", p)
4743+
srvrLog.DebugS(ctx, "Notifying that peer is offline")
47324744
pubStr := string(pubKey.SerializeCompressed())
47334745
for _, offlineChan := range s.peerDisconnectedListeners[pubStr] {
47344746
close(offlineChan)
@@ -4758,7 +4770,7 @@ func (s *server) peerTerminationWatcher(p *peer.Brontide, ready chan struct{}) {
47584770

47594771
// First, cleanup any remaining state the server has regarding the peer
47604772
// in question.
4761-
s.removePeerUnsafe(p)
4773+
s.removePeerUnsafe(ctx, p)
47624774

47634775
// Next, check to see if this is a persistent peer or not.
47644776
if _, ok := s.persistentPeers[pubStr]; !ok {
@@ -4797,18 +4809,16 @@ func (s *server) peerTerminationWatcher(p *peer.Brontide, ready chan struct{}) {
47974809
// the address used by our onion service to dial
47984810
// to lnd), so we don't have enough information
47994811
// to attempt a reconnect.
4800-
srvrLog.Debugf("Ignoring reconnection attempt "+
4801-
"to inbound peer %v without "+
4802-
"advertised address", p)
4812+
srvrLog.DebugS(ctx, "Ignoring reconnection attempt "+
4813+
"to inbound peer without advertised address")
48034814
return
48044815

48054816
// We came across an error retrieving an advertised
48064817
// address, log it, and fall back to the existing peer
48074818
// address.
48084819
default:
4809-
srvrLog.Errorf("Unable to retrieve advertised "+
4810-
"address for node %x: %v", p.PubKey(),
4811-
err)
4820+
srvrLog.ErrorS(ctx, "Unable to retrieve advertised "+
4821+
"address for peer", err)
48124822
}
48134823

48144824
// Make an easy lookup map so that we can check if an address
@@ -4850,9 +4860,9 @@ func (s *server) peerTerminationWatcher(p *peer.Brontide, ready chan struct{}) {
48504860
// call can stall for arbitrarily long if we shutdown while an
48514861
// outbound connection attempt is being made.
48524862
go func() {
4853-
srvrLog.Debugf("Scheduling connection re-establishment to "+
4854-
"persistent peer %x in %s",
4855-
p.IdentityKey().SerializeCompressed(), backoff)
4863+
srvrLog.DebugS(ctx, "Scheduling connection "+
4864+
"re-establishment to persistent peer",
4865+
"reconnecting_in", backoff)
48564866

48574867
select {
48584868
case <-time.After(backoff):
@@ -4862,9 +4872,8 @@ func (s *server) peerTerminationWatcher(p *peer.Brontide, ready chan struct{}) {
48624872
return
48634873
}
48644874

4865-
srvrLog.Debugf("Attempting to re-establish persistent "+
4866-
"connection to peer %x",
4867-
p.IdentityKey().SerializeCompressed())
4875+
srvrLog.DebugS(ctx, "Attempting to re-establish persistent "+
4876+
"connection")
48684877

48694878
s.connectToPersistentPeer(pubStr)
48704879
}()
@@ -4971,15 +4980,11 @@ func (s *server) connectToPersistentPeer(pubKeyStr string) {
49714980
// active peers.
49724981
//
49734982
// NOTE: Server mutex must be held when calling this function.
4974-
func (s *server) removePeerUnsafe(p *peer.Brontide) {
4983+
func (s *server) removePeerUnsafe(ctx context.Context, p *peer.Brontide) {
49754984
if p == nil {
49764985
return
49774986
}
49784987

4979-
ctx := btclog.WithCtx(
4980-
context.TODO(), lnutils.LogPubKey("peer", p.IdentityKey()),
4981-
)
4982-
49834988
srvrLog.DebugS(ctx, "Removing peer")
49844989

49854990
// Exit early if we have already been instructed to shutdown, the peers

0 commit comments

Comments
 (0)