@@ -191,6 +191,9 @@ type peer struct {
191191 // Must only be accessed atomically
192192 lastSent , lastReceived int64
193193
194+ // lastPingSent is the milliseconds since 1970-01-01 UTC when the last ping was sent
195+ lastPingSent int64
196+
194197 // getPeerListChan signals that we should attempt to send a GetPeerList to
195198 // this peer
196199 getPeerListChan chan struct {}
@@ -613,6 +616,10 @@ func (p *peer) writeMessage(writer io.Writer, msg message.OutboundMessage) {
613616 return
614617 }
615618
619+ if msg .Op () == message .PingOp {
620+ atomic .StoreInt64 (& p .lastPingSent , p .Clock .Time ().UnixMilli ())
621+ }
622+
616623 // Write the message
617624 var buf net.Buffers = [][]byte {msgLenBytes [:], msgBytes }
618625 if _ , err := io .CopyN (writer , & buf , int64 (wrappers .IntLen + msgLen )); err != nil {
@@ -819,7 +826,23 @@ func (p *peer) getUptime() uint32 {
819826 return primaryUptimePercent
820827}
821828
822- func (* peer ) handlePong (* p2p.Pong ) {}
829+ func (p * peer ) handlePong (* p2p.Pong ) {
830+ pingSent := atomic .SwapInt64 (& p .lastPingSent , 0 )
831+ if pingSent == 0 {
832+ p .Log .Debug (malformedMessageLog ,
833+ zap .Stringer ("nodeID" , p .id ),
834+ zap .Stringer ("messageOp" , message .PongOp ),
835+ zap .String ("reason" , "received unexpected pong" ),
836+ )
837+ p .StartClose ()
838+ return
839+ }
840+
841+ elapsed := p .Clock .Time ().UnixMilli () - pingSent
842+
843+ p .Metrics .RTTCount .Inc ()
844+ p .Metrics .RTTSum .Add (float64 (elapsed ))
845+ }
823846
824847func (p * peer ) handleHandshake (msg * p2p.Handshake ) {
825848 if p .gotHandshake .Get () {
0 commit comments