Skip to content
This repository has been archived by the owner on Dec 20, 2024. It is now read-only.

Commit

Permalink
fix(client): add error handling in client
Browse files Browse the repository at this point in the history
Signed-off-by: hwdef <[email protected]>
  • Loading branch information
hwdef committed Sep 19, 2019
1 parent 63f099d commit 40c9b2e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
4 changes: 3 additions & 1 deletion client/peer_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ func (client *APIClient) PeerDelete(ctx context.Context, id string) error {
if err != nil {
return err
}
ensureCloseReader(resp)
if err := ensureCloseReader(resp); err != nil {
return err
}
return nil
}
8 changes: 6 additions & 2 deletions client/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,12 @@ func (client *APIClient) hijack(ctx context.Context, path string, query url.Valu
}

if tcpConn, ok := conn.(*net.TCPConn); ok {
tcpConn.SetKeepAlive(true)
tcpConn.SetKeepAlivePeriod(30 * time.Second)
if err := tcpConn.SetKeepAlive(true); err != nil {
return nil, nil, err
}
if err := tcpConn.SetKeepAlivePeriod(30 * time.Second); err != nil {
return nil, nil, err
}
}

clientconn := httputil.NewClientConn(conn, nil)
Expand Down
4 changes: 3 additions & 1 deletion client/task_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ func (client *APIClient) TaskDelete(ctx context.Context, id string) error {
if err != nil {
return err
}
ensureCloseReader(resp)
if err := ensureCloseReader(resp); err != nil {
return err
}
return nil
}
4 changes: 3 additions & 1 deletion client/task_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func (client *APIClient) TaskUpdate(ctx context.Context, id string, config *type
return err
}

ensureCloseReader(resp)
if err := ensureCloseReader(resp); err != nil {
return err
}
return nil
}
9 changes: 6 additions & 3 deletions client/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ func decodeBody(obj interface{}, body io.Reader) error {
return nil
}

func ensureCloseReader(resp *Response) {
func ensureCloseReader(resp *Response) error {
if resp != nil && resp.Body != nil {
defer resp.Body.Close()

// close body ReadCloser to make Transport reuse the connection
io.CopyN(ioutil.Discard, resp.Body, 512)
resp.Body.Close()
_, err := io.CopyN(ioutil.Discard, resp.Body, 512)
return err
}
return nil
}

0 comments on commit 40c9b2e

Please sign in to comment.