mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
client: add SyncableHTTPClient.Endpoints
This commit is contained in:
parent
2760739ceb
commit
4b555dba99
@ -40,6 +40,7 @@ var (
|
||||
type SyncableHTTPClient interface {
|
||||
HTTPClient
|
||||
Sync(context.Context) error
|
||||
Endpoints() []string
|
||||
}
|
||||
|
||||
type HTTPClient interface {
|
||||
@ -65,7 +66,8 @@ func NewHTTPClient(tr CancelableTransport, eps []string) (SyncableHTTPClient, er
|
||||
func newHTTPClusterClient(tr CancelableTransport, eps []string) (*httpClusterClient, error) {
|
||||
c := httpClusterClient{
|
||||
transport: tr,
|
||||
endpoints: make([]HTTPClient, len(eps)),
|
||||
endpoints: eps,
|
||||
clients: make([]HTTPClient, len(eps)),
|
||||
}
|
||||
|
||||
for i, ep := range eps {
|
||||
@ -74,7 +76,7 @@ func newHTTPClusterClient(tr CancelableTransport, eps []string) (*httpClusterCli
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c.endpoints[i] = &redirectFollowingHTTPClient{
|
||||
c.clients[i] = &redirectFollowingHTTPClient{
|
||||
max: DefaultMaxRedirects,
|
||||
client: &httpClient{
|
||||
transport: tr,
|
||||
@ -88,14 +90,15 @@ func newHTTPClusterClient(tr CancelableTransport, eps []string) (*httpClusterCli
|
||||
|
||||
type httpClusterClient struct {
|
||||
transport CancelableTransport
|
||||
endpoints []HTTPClient
|
||||
endpoints []string
|
||||
clients []HTTPClient
|
||||
}
|
||||
|
||||
func (c *httpClusterClient) Do(ctx context.Context, act HTTPAction) (resp *http.Response, body []byte, err error) {
|
||||
if len(c.endpoints) == 0 {
|
||||
if len(c.clients) == 0 {
|
||||
return nil, nil, ErrNoEndpoints
|
||||
}
|
||||
for _, hc := range c.endpoints {
|
||||
for _, hc := range c.clients {
|
||||
resp, body, err = hc.Do(ctx, act)
|
||||
if err != nil {
|
||||
if err == ErrTimeout || err == ErrCanceled {
|
||||
@ -111,6 +114,10 @@ func (c *httpClusterClient) Do(ctx context.Context, act HTTPAction) (resp *http.
|
||||
return
|
||||
}
|
||||
|
||||
func (c *httpClusterClient) Endpoints() []string {
|
||||
return c.endpoints
|
||||
}
|
||||
|
||||
func (c *httpClusterClient) Sync(ctx context.Context) error {
|
||||
mAPI := NewMembersAPI(c)
|
||||
ms, err := mAPI.List(ctx)
|
||||
|
@ -193,7 +193,7 @@ func TestHTTPClusterClientDo(t *testing.T) {
|
||||
// first good response short-circuits Do
|
||||
{
|
||||
client: &httpClusterClient{
|
||||
endpoints: []HTTPClient{
|
||||
clients: []HTTPClient{
|
||||
&staticHTTPClient{resp: http.Response{StatusCode: http.StatusTeapot}},
|
||||
&staticHTTPClient{err: fakeErr},
|
||||
},
|
||||
@ -204,7 +204,7 @@ func TestHTTPClusterClientDo(t *testing.T) {
|
||||
// fall through to good endpoint if err is arbitrary
|
||||
{
|
||||
client: &httpClusterClient{
|
||||
endpoints: []HTTPClient{
|
||||
clients: []HTTPClient{
|
||||
&staticHTTPClient{err: fakeErr},
|
||||
&staticHTTPClient{resp: http.Response{StatusCode: http.StatusTeapot}},
|
||||
},
|
||||
@ -215,7 +215,7 @@ func TestHTTPClusterClientDo(t *testing.T) {
|
||||
// ErrTimeout short-circuits Do
|
||||
{
|
||||
client: &httpClusterClient{
|
||||
endpoints: []HTTPClient{
|
||||
clients: []HTTPClient{
|
||||
&staticHTTPClient{err: ErrTimeout},
|
||||
&staticHTTPClient{resp: http.Response{StatusCode: http.StatusTeapot}},
|
||||
},
|
||||
@ -226,7 +226,7 @@ func TestHTTPClusterClientDo(t *testing.T) {
|
||||
// ErrCanceled short-circuits Do
|
||||
{
|
||||
client: &httpClusterClient{
|
||||
endpoints: []HTTPClient{
|
||||
clients: []HTTPClient{
|
||||
&staticHTTPClient{err: ErrCanceled},
|
||||
&staticHTTPClient{resp: http.Response{StatusCode: http.StatusTeapot}},
|
||||
},
|
||||
@ -237,7 +237,7 @@ func TestHTTPClusterClientDo(t *testing.T) {
|
||||
// return err if there are no endpoints
|
||||
{
|
||||
client: &httpClusterClient{
|
||||
endpoints: []HTTPClient{},
|
||||
clients: []HTTPClient{},
|
||||
},
|
||||
wantErr: ErrNoEndpoints,
|
||||
},
|
||||
@ -245,7 +245,7 @@ func TestHTTPClusterClientDo(t *testing.T) {
|
||||
// return err if all endpoints return arbitrary errors
|
||||
{
|
||||
client: &httpClusterClient{
|
||||
endpoints: []HTTPClient{
|
||||
clients: []HTTPClient{
|
||||
&staticHTTPClient{err: fakeErr},
|
||||
&staticHTTPClient{err: fakeErr},
|
||||
},
|
||||
@ -256,7 +256,7 @@ func TestHTTPClusterClientDo(t *testing.T) {
|
||||
// 500-level errors cause Do to fallthrough to next endpoint
|
||||
{
|
||||
client: &httpClusterClient{
|
||||
endpoints: []HTTPClient{
|
||||
clients: []HTTPClient{
|
||||
&staticHTTPClient{resp: http.Response{StatusCode: http.StatusBadGateway}},
|
||||
&staticHTTPClient{resp: http.Response{StatusCode: http.StatusTeapot}},
|
||||
},
|
||||
|
Loading…
x
Reference in New Issue
Block a user