From 17223c121521a9eb3f6b6604a67c0fe1fb074466 Mon Sep 17 00:00:00 2001 From: Yaacov Akiba Slama Date: Wed, 27 Jun 2018 03:27:22 +0300 Subject: [PATCH] peer: add write timeout within writeMessage In this commit, we add a timeout within the writeMessage method when we go to write to the socket. We do this as otherwise, if the other peer is blocked for some reason, we'll never actually unblock ourselves, which may cause issues in other sub-systems waiting on this write call. For now, we use a value of 10 seconds, and will adjust in the future if we deem this time period too short. --- peer.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/peer.go b/peer.go index 82b077f6..8c9afc5e 100644 --- a/peer.go +++ b/peer.go @@ -40,6 +40,9 @@ const ( // idleTimeout is the duration of inactivity before we time out a peer. idleTimeout = 5 * time.Minute + // writeMessageTimeout is the timeout used when writing a message to peer. + writeMessageTimeout = 10 * time.Second + // outgoingQueueLen is the buffer size of the channel which houses // messages to be sent across the wire, requested by objects outside // this struct. @@ -1249,7 +1252,7 @@ func (p *peer) writeMessage(msg lnwire.Message) error { n, err := lnwire.WriteMessage(b, msg, 0) atomic.AddUint64(&p.bytesSent, uint64(n)) - // TODO(roasbeef): add write deadline? + p.conn.SetWriteDeadline(time.Now().Add(writeMessageTimeout)) // Finally, write the message itself in a single swoop. _, err = p.conn.Write(b.Bytes())