From 40c9b2ec92714637f2f93b747b9dd2cfa7eaf138 Mon Sep 17 00:00:00 2001 From: hwdef Date: Tue, 17 Sep 2019 11:07:56 +0800 Subject: [PATCH] fix(client): add error handling in client Signed-off-by: hwdef --- client/peer_delete.go | 4 +++- client/request.go | 8 ++++++-- client/task_delete.go | 4 +++- client/task_update.go | 4 +++- client/utils.go | 9 ++++++--- 5 files changed, 21 insertions(+), 8 deletions(-) diff --git a/client/peer_delete.go b/client/peer_delete.go index 3383fc191..7af5742ce 100644 --- a/client/peer_delete.go +++ b/client/peer_delete.go @@ -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 } diff --git a/client/request.go b/client/request.go index e2cf03a87..e7e8b4ce6 100644 --- a/client/request.go +++ b/client/request.go @@ -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) diff --git a/client/task_delete.go b/client/task_delete.go index 4881553d4..75f2f4e16 100644 --- a/client/task_delete.go +++ b/client/task_delete.go @@ -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 } diff --git a/client/task_update.go b/client/task_update.go index 2ca4fd6aa..4b2b3fb4d 100644 --- a/client/task_update.go +++ b/client/task_update.go @@ -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 } diff --git a/client/utils.go b/client/utils.go index c8f55ba3b..4c0397545 100644 --- a/client/utils.go +++ b/client/utils.go @@ -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 }