mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
parent
09b7fd4975
commit
15e9510d2c
@ -372,12 +372,7 @@ func (c *httpClusterClient) Do(ctx context.Context, act httpAction) (*http.Respo
|
|||||||
if err == context.Canceled || err == context.DeadlineExceeded {
|
if err == context.Canceled || err == context.DeadlineExceeded {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
if isOneShot {
|
} else if resp.StatusCode/100 == 5 {
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if resp.StatusCode/100 == 5 {
|
|
||||||
switch resp.StatusCode {
|
switch resp.StatusCode {
|
||||||
case http.StatusInternalServerError, http.StatusServiceUnavailable:
|
case http.StatusInternalServerError, http.StatusServiceUnavailable:
|
||||||
// TODO: make sure this is a no leader response
|
// TODO: make sure this is a no leader response
|
||||||
@ -385,10 +380,16 @@ func (c *httpClusterClient) Do(ctx context.Context, act httpAction) (*http.Respo
|
|||||||
default:
|
default:
|
||||||
cerr.Errors = append(cerr.Errors, fmt.Errorf("client: etcd member %s returns server error [%s]", eps[k].String(), http.StatusText(resp.StatusCode)))
|
cerr.Errors = append(cerr.Errors, fmt.Errorf("client: etcd member %s returns server error [%s]", eps[k].String(), http.StatusText(resp.StatusCode)))
|
||||||
}
|
}
|
||||||
if isOneShot {
|
err = cerr.Errors[0]
|
||||||
return nil, nil, cerr.Errors[0]
|
}
|
||||||
|
if err != nil {
|
||||||
|
if !isOneShot {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
continue
|
c.Lock()
|
||||||
|
c.pinned = (k + 1) % leps
|
||||||
|
c.Unlock()
|
||||||
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
if k != pinned {
|
if k != pinned {
|
||||||
c.Lock()
|
c.Lock()
|
||||||
|
@ -16,6 +16,7 @@ package client
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
@ -304,7 +305,9 @@ func TestHTTPClusterClientDo(t *testing.T) {
|
|||||||
fakeErr := errors.New("fake!")
|
fakeErr := errors.New("fake!")
|
||||||
fakeURL := url.URL{}
|
fakeURL := url.URL{}
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
client *httpClusterClient
|
client *httpClusterClient
|
||||||
|
ctx context.Context
|
||||||
|
|
||||||
wantCode int
|
wantCode int
|
||||||
wantErr error
|
wantErr error
|
||||||
wantPinned int
|
wantPinned int
|
||||||
@ -395,10 +398,30 @@ func TestHTTPClusterClientDo(t *testing.T) {
|
|||||||
wantCode: http.StatusTeapot,
|
wantCode: http.StatusTeapot,
|
||||||
wantPinned: 1,
|
wantPinned: 1,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// 500-level errors cause one shot Do to fallthrough to next endpoint
|
||||||
|
{
|
||||||
|
client: &httpClusterClient{
|
||||||
|
endpoints: []url.URL{fakeURL, fakeURL},
|
||||||
|
clientFactory: newStaticHTTPClientFactory(
|
||||||
|
[]staticHTTPResponse{
|
||||||
|
{resp: http.Response{StatusCode: http.StatusBadGateway}},
|
||||||
|
{resp: http.Response{StatusCode: http.StatusTeapot}},
|
||||||
|
},
|
||||||
|
),
|
||||||
|
rand: rand.New(rand.NewSource(0)),
|
||||||
|
},
|
||||||
|
ctx: context.WithValue(context.Background(), &oneShotCtxValue, &oneShotCtxValue),
|
||||||
|
wantErr: fmt.Errorf("client: etcd member returns server error [Bad Gateway]"),
|
||||||
|
wantPinned: 1,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, tt := range tests {
|
for i, tt := range tests {
|
||||||
resp, _, err := tt.client.Do(context.Background(), nil)
|
if tt.ctx == nil {
|
||||||
|
tt.ctx = context.Background()
|
||||||
|
}
|
||||||
|
resp, _, err := tt.client.Do(tt.ctx, nil)
|
||||||
if !reflect.DeepEqual(tt.wantErr, err) {
|
if !reflect.DeepEqual(tt.wantErr, err) {
|
||||||
t.Errorf("#%d: got err=%v, want=%v", i, err, tt.wantErr)
|
t.Errorf("#%d: got err=%v, want=%v", i, err, tt.wantErr)
|
||||||
continue
|
continue
|
||||||
@ -407,11 +430,9 @@ func TestHTTPClusterClientDo(t *testing.T) {
|
|||||||
if resp == nil {
|
if resp == nil {
|
||||||
if tt.wantCode != 0 {
|
if tt.wantCode != 0 {
|
||||||
t.Errorf("#%d: resp is nil, want=%d", i, tt.wantCode)
|
t.Errorf("#%d: resp is nil, want=%d", i, tt.wantCode)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
continue
|
} else if resp.StatusCode != tt.wantCode {
|
||||||
}
|
|
||||||
|
|
||||||
if resp.StatusCode != tt.wantCode {
|
|
||||||
t.Errorf("#%d: resp code=%d, want=%d", i, resp.StatusCode, tt.wantCode)
|
t.Errorf("#%d: resp code=%d, want=%d", i, resp.StatusCode, tt.wantCode)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user