Skip to content

Commit 22659a7

Browse files
committed
p2p: restore read/write timeouts
They got lost in the transition to rlpxFrameRW.
1 parent 6e7e5d5 commit 22659a7

5 files changed

Lines changed: 37 additions & 37 deletions

File tree

‎p2p/handshake.go‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const (
3737
//
3838
// The MsgReadWriter is usually layered as follows:
3939
//
40-
// lockedRW (thread-safety for ReadMsg, WriteMsg)
40+
// netWrapper (I/O timeouts, thread-safe ReadMsg, WriteMsg)
4141
// rlpxFrameRW (message encoding, encryption, authentication)
4242
// bufio.ReadWriter (buffering)
4343
// net.Conn (network I/O)
@@ -83,7 +83,6 @@ func setupInboundConn(fd net.Conn, prv *ecdsa.PrivateKey, our *protoHandshake) (
8383
}
8484

8585
// Run the protocol handshake using authenticated messages.
86-
// TODO: move buffering setup here (out of newFrameRW)
8786
rw := newRlpxFrameRW(fd, secrets)
8887
rhs, err := readProtocolHandshake(rw, our)
8988
if err != nil {
@@ -96,7 +95,7 @@ func setupInboundConn(fd net.Conn, prv *ecdsa.PrivateKey, our *protoHandshake) (
9695
if err := writeProtocolHandshake(rw, our); err != nil {
9796
return nil, fmt.Errorf("protocol write error: %v", err)
9897
}
99-
return &conn{&lockedRW{wrapped: rw}, rhs}, nil
98+
return &conn{rw, rhs}, nil
10099
}
101100

102101
func setupOutboundConn(fd net.Conn, prv *ecdsa.PrivateKey, our *protoHandshake, dial *discover.Node) (*conn, error) {
@@ -106,7 +105,6 @@ func setupOutboundConn(fd net.Conn, prv *ecdsa.PrivateKey, our *protoHandshake,
106105
}
107106

108107
// Run the protocol handshake using authenticated messages.
109-
// TODO: move buffering setup here (out of newFrameRW)
110108
rw := newRlpxFrameRW(fd, secrets)
111109
if err := writeProtocolHandshake(rw, our); err != nil {
112110
return nil, fmt.Errorf("protocol write error: %v", err)
@@ -118,7 +116,7 @@ func setupOutboundConn(fd net.Conn, prv *ecdsa.PrivateKey, our *protoHandshake,
118116
if rhs.ID != dial.ID {
119117
return nil, errors.New("dialed node id mismatch")
120118
}
121-
return &conn{&lockedRW{wrapped: rw}, rhs}, nil
119+
return &conn{rw, rhs}, nil
122120
}
123121

124122
// encHandshake contains the state of the encryption handshake.

‎p2p/message.go‎

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"io"
88
"io/ioutil"
9+
"net"
910
"sync"
1011
"sync/atomic"
1112
"time"
@@ -14,28 +15,6 @@ import (
1415
"github.com/ethereum/go-ethereum/rlp"
1516
)
1617

17-
// parameters for frameRW
18-
const (
19-
// maximum time allowed for reading a message header.
20-
// this is effectively the amount of time a connection can be idle.
21-
frameReadTimeout = 1 * time.Minute
22-
23-
// maximum time allowed for reading the payload data of a message.
24-
// this is shorter than (and distinct from) frameReadTimeout because
25-
// the connection is not considered idle while a message is transferred.
26-
// this also limits the payload size of messages to how much the connection
27-
// can transfer within the timeout.
28-
payloadReadTimeout = 5 * time.Second
29-
30-
// maximum amount of time allowed for writing a complete message.
31-
msgWriteTimeout = 5 * time.Second
32-
33-
// messages smaller than this many bytes will be read at
34-
// once before passing them to a protocol. this increases
35-
// concurrency in the processing.
36-
wholePayloadSize = 64 * 1024
37-
)
38-
3918
// Msg defines the structure of a p2p message.
4019
//
4120
// Note that a Msg can only be sent once since the Payload reader is
@@ -103,22 +82,27 @@ func EncodeMsg(w MsgWriter, code uint64, data ...interface{}) error {
10382
return w.WriteMsg(NewMsg(code, data...))
10483
}
10584

106-
// lockedRW wraps a MsgReadWriter with locks around
107-
// ReadMsg and WriteMsg.
108-
type lockedRW struct {
85+
// netWrapper wrapsa MsgReadWriter with locks around
86+
// ReadMsg/WriteMsg and applies read/write deadlines.
87+
type netWrapper struct {
10988
rmu, wmu sync.Mutex
110-
wrapped MsgReadWriter
89+
90+
rtimeout, wtimeout time.Duration
91+
conn net.Conn
92+
wrapped MsgReadWriter
11193
}
11294

113-
func (rw *lockedRW) ReadMsg() (Msg, error) {
95+
func (rw *netWrapper) ReadMsg() (Msg, error) {
11496
rw.rmu.Lock()
11597
defer rw.rmu.Unlock()
98+
rw.conn.SetReadDeadline(time.Now().Add(rw.rtimeout))
11699
return rw.wrapped.ReadMsg()
117100
}
118101

119-
func (rw *lockedRW) WriteMsg(msg Msg) error {
102+
func (rw *netWrapper) WriteMsg(msg Msg) error {
120103
rw.wmu.Lock()
121104
defer rw.wmu.Unlock()
105+
rw.conn.SetWriteDeadline(time.Now().Add(rw.wtimeout))
122106
return rw.wrapped.WriteMsg(msg)
123107
}
124108

‎p2p/peer.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ const (
2020
baseProtocolLength = uint64(16)
2121
baseProtocolMaxMsgSize = 10 * 1024 * 1024
2222

23-
disconnectGracePeriod = 2 * time.Second
2423
pingInterval = 15 * time.Second
24+
disconnectGracePeriod = 2 * time.Second
2525
)
2626

2727
const (
@@ -176,6 +176,7 @@ func (p *Peer) politeDisconnect(reason DiscReason) {
176176

177177
func (p *Peer) readLoop() error {
178178
for {
179+
p.conn.SetDeadline(time.Now().Add(frameReadTimeout))
179180
msg, err := p.rw.ReadMsg()
180181
if err != nil {
181182
return err

‎p2p/rlpx.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ var (
2121
zero16 = make([]byte, 16)
2222
)
2323

24+
// rlpxFrameRW implements a simplified version of RLPx framing.
25+
// chunked messages are not supported and all headers are equal to
26+
// zeroHeader.
27+
//
28+
// rlpxFrameRW is not safe for concurrent use from multiple goroutines.
2429
type rlpxFrameRW struct {
2530
conn io.ReadWriter
2631
enc cipher.Stream

‎p2p/server.go‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,17 @@ import (
1717
)
1818

1919
const (
20-
handshakeTimeout = 5 * time.Second
2120
defaultDialTimeout = 10 * time.Second
2221
refreshPeersInterval = 30 * time.Second
22+
23+
// total timeout for encryption handshake and protocol
24+
// handshake in both directions.
25+
handshakeTimeout = 5 * time.Second
26+
// maximum time allowed for reading a complete message.
27+
// this is effectively the amount of time a connection can be idle.
28+
frameReadTimeout = 1 * time.Minute
29+
// maximum amount of time allowed for writing a complete message.
30+
frameWriteTimeout = 5 * time.Second
2331
)
2432

2533
var srvlog = logger.NewLogger("P2P Server")
@@ -359,14 +367,18 @@ func (srv *Server) findPeers() {
359367

360368
func (srv *Server) startPeer(fd net.Conn, dest *discover.Node) {
361369
// TODO: handle/store session token
362-
// TODO: reenable deadlines
363-
// fd.SetDeadline(time.Now().Add(handshakeTimeout))
370+
fd.SetDeadline(time.Now().Add(handshakeTimeout))
364371
conn, err := srv.setupFunc(fd, srv.PrivateKey, srv.ourHandshake, dest)
365372
if err != nil {
366373
fd.Close()
367374
srvlog.Debugf("Handshake with %v failed: %v", fd.RemoteAddr(), err)
368375
return
369376
}
377+
378+
conn.MsgReadWriter = &netWrapper{
379+
wrapped: conn.MsgReadWriter,
380+
conn: fd, rtimeout: frameReadTimeout, wtimeout: frameWriteTimeout,
381+
}
370382
p := newPeer(fd, conn, srv.Protocols)
371383
if ok, reason := srv.addPeer(conn.ID, p); !ok {
372384
srvlog.DebugDetailf("Not adding %v (%v)\n", p, reason)

0 commit comments

Comments
 (0)