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.
This commit is contained in:
Yaacov Akiba Slama 2018-06-27 03:27:22 +03:00 committed by Olaoluwa Osuntokun
parent b8fecfca71
commit 17223c1215

@ -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())