Skip to content

Commit a4e6227

Browse files
committed
protocol: reset timeout after processing header
Fixes #75 Signed-off-by: Pires <[email protected]>
1 parent 67d28b3 commit a4e6227

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

protocol.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ func (p *Listener) Accept() (net.Conn, error) {
5454
}
5555

5656
if d := p.ReadHeaderTimeout; d != 0 {
57+
// The deadline will be reset after parsing the header.
58+
// Otherwise, future p.conn.Read() will timeout.
5759
conn.SetReadDeadline(time.Now().Add(d))
5860
}
5961

@@ -106,10 +108,12 @@ func NewConn(conn net.Conn, opts ...func(*Conn)) *Conn {
106108
func (p *Conn) Read(b []byte) (int, error) {
107109
p.once.Do(func() {
108110
p.readErr = p.readHeader()
111+
p.conn.SetReadDeadline(time.Time{})
109112
})
110113
if p.readErr != nil {
111114
return 0, p.readErr
112115
}
116+
113117
return p.bufReader.Read(b)
114118
}
115119

protocol_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func TestPassthrough(t *testing.T) {
3131
conn, err := net.Dial("tcp", pl.Addr().String())
3232
if err != nil {
3333
t.Fatalf("err: %v", err)
34-
}
34+
}
3535
defer conn.Close()
3636

3737
conn.Write([]byte("ping"))
@@ -99,7 +99,7 @@ func TestReadHeaderTimeout(t *testing.T) {
9999
recv := make([]byte, 4)
100100
_, err = conn.Read(recv)
101101

102-
if err != nil && !errors.Is(err, os.ErrDeadlineExceeded){
102+
if err != nil && !errors.Is(err, os.ErrDeadlineExceeded) {
103103
t.Fatal("should timeout")
104104
}
105105
}
@@ -113,7 +113,7 @@ func TestReadHeaderTimeoutIsReset(t *testing.T) {
113113
}
114114

115115
pl := &Listener{
116-
Listener: l,
116+
Listener: l,
117117
ReadHeaderTimeout: timeout,
118118
}
119119

0 commit comments

Comments
 (0)