Skip to content

Commit 5110f80

Browse files
committed
p2p: improve read deadlines
There are now two deadlines, frameReadTimeout and payloadReadTimeout. The frame timeout is longer and allows for connections that are idle. The message timeout is still short and ensures that we don't get stuck in the middle of a message.
1 parent 170eb3a commit 5110f80

2 files changed

Lines changed: 27 additions & 15 deletions

File tree

‎p2p/message.go‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,28 @@ import (
1818
"github.com/ethereum/go-ethereum/rlp"
1919
)
2020

21+
// parameters for frameRW
22+
const (
23+
// maximum time allowed for reading a message header.
24+
// this is effectively the amount of time a connection can be idle.
25+
frameReadTimeout = 1 * time.Minute
26+
27+
// maximum time allowed for reading the payload data of a message.
28+
// this is shorter than (and distinct from) frameReadTimeout because
29+
// the connection is not considered idle while a message is transferred.
30+
// this also limits the payload size of messages to how much the connection
31+
// can transfer within the timeout.
32+
payloadReadTimeout = 5 * time.Second
33+
34+
// maximum amount of time allowed for writing a complete message.
35+
msgWriteTimeout = 5 * time.Second
36+
37+
// messages smaller than this many bytes will be read at
38+
// once before passing them to a protocol. this increases
39+
// concurrency in the processing.
40+
wholePayloadSize = 64 * 1024
41+
)
42+
2143
// Msg defines the structure of a p2p message.
2244
//
2345
// Note that a Msg can only be sent once since the Payload reader is
@@ -167,9 +189,7 @@ func makeListHeader(length uint32) []byte {
167189
func (rw *frameRW) ReadMsg() (msg Msg, err error) {
168190
<-rw.rsync // wait until bufconn is ours
169191

170-
// this read timeout applies also to the payload.
171-
// TODO: proper read timeout
172-
rw.SetReadDeadline(time.Now().Add(msgReadTimeout))
192+
rw.SetReadDeadline(time.Now().Add(frameReadTimeout))
173193

174194
// read magic and payload size
175195
start := make([]byte, 8)
@@ -193,6 +213,8 @@ func (rw *frameRW) ReadMsg() (msg Msg, err error) {
193213
}
194214
msg.Size = size - posr.p
195215

216+
rw.SetReadDeadline(time.Now().Add(payloadReadTimeout))
217+
196218
if msg.Size <= wholePayloadSize {
197219
// msg is small, read all of it and move on to the next message.
198220
pbuf := make([]byte, msg.Size)

‎p2p/peer.go‎

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,12 @@ import (
1515
"github.com/ethereum/go-ethereum/rlp"
1616
)
1717

18-
const (
19-
// maximum amount of time allowed for reading a message
20-
msgReadTimeout = 5 * time.Second
21-
// maximum amount of time allowed for writing a message
22-
msgWriteTimeout = 5 * time.Second
23-
// messages smaller than this many bytes will be read at
24-
// once before passing them to a protocol.
25-
wholePayloadSize = 64 * 1024
26-
27-
disconnectGracePeriod = 2 * time.Second
28-
)
29-
3018
const (
3119
baseProtocolVersion = 2
3220
baseProtocolLength = uint64(16)
3321
baseProtocolMaxMsgSize = 10 * 1024 * 1024
22+
23+
disconnectGracePeriod = 2 * time.Second
3424
)
3525

3626
const (

0 commit comments

Comments
 (0)