Skip to content

Commit

Permalink
pkg/httpclient: Fix connection closed while writing or reading (#305)
Browse files Browse the repository at this point in the history
Signed-off-by: Xuanwo <github@xuanwo.io>
  • Loading branch information
Xuanwo authored May 7, 2020
1 parent e581b62 commit 6faf34e
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions pkg/httpclient/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,29 @@ type Conn struct {

// Read will read from the conn.
func (c *Conn) Read(buf []byte) (n int, err error) {
err = c.SetReadDeadline(time.Now().Add(c.readTimeout))
err = c.SetDeadline(time.Now().Add(c.readTimeout))
if err != nil {
return
}
defer func() {
// Clean read timeout so that this will not affect further read
// It's safe to ignore the returning error: even if it don’t return now, it will return via next read.
_ = c.SetReadDeadline(time.Time{})
_ = c.SetDeadline(time.Time{})
}()

return c.Conn.Read(buf)
}

// Write will write into the conn.
func (c *Conn) Write(buf []byte) (n int, err error) {
err = c.SetWriteDeadline(time.Now().Add(c.writeTimeout))
err = c.SetDeadline(time.Now().Add(c.writeTimeout))
if err != nil {
return
}
defer func() {
// Clean read timeout so that this will not affect further write
// It's safe to ignore the returning error: even if it don’t return now, it will return via next write.
_ = c.SetWriteDeadline(time.Time{})
_ = c.SetDeadline(time.Time{})
}()

return c.Conn.Write(buf)
Expand Down

1 comment on commit 6faf34e

@vercel
Copy link

@vercel vercel bot commented on 6faf34e May 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.