浏览代码

client: return context.Canceled error when user cancels the request

Xiang Li 10 年之前
父节点
当前提交
f38187bbdb
共有 2 个文件被更改,包括 8 次插入2 次删除
  1. 7 1
      client/client.go
  2. 1 1
      client/client_test.go

+ 7 - 1
client/client.go

@@ -239,7 +239,13 @@ func (c *httpClusterClient) Do(ctx context.Context, act httpAction) (*http.Respo
 		resp, body, err = hc.Do(ctx, action)
 		if err != nil {
 			cerr.Errors = append(cerr.Errors, err)
-			if err == context.DeadlineExceeded || err == context.Canceled {
+			// mask previous errors with canceled error if the user explicitly canceled the request
+			if err == context.Canceled {
+				return nil, nil, context.Canceled
+			}
+
+			// TODO: deal with deadline error when we improve the deadline handling.
+			if err == context.DeadlineExceeded {
 				return nil, nil, cerr
 			}
 			continue

+ 1 - 1
client/client_test.go

@@ -364,7 +364,7 @@ func TestHTTPClusterClientDo(t *testing.T) {
 				),
 				rand: rand.New(rand.NewSource(0)),
 			},
-			wantErr: &ClusterError{Errors: []error{context.Canceled}},
+			wantErr: context.Canceled,
 		},
 
 		// return err if there are no endpoints