Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg/httpclient: Fix connection closed while writing or reading #305

Merged
merged 1 commit into from
May 7, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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