From 4b555dba99157bc32a3f3293cf37eb8acc58f89a Mon Sep 17 00:00:00 2001 From: Brian Waldon Date: Thu, 6 Nov 2014 10:53:20 -0800 Subject: [PATCH] client: add SyncableHTTPClient.Endpoints --- client/http.go | 17 ++++++++++++----- client/http_test.go | 14 +++++++------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/client/http.go b/client/http.go index 7dfecc8c2..73649ef59 100644 --- a/client/http.go +++ b/client/http.go @@ -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) diff --git a/client/http_test.go b/client/http_test.go index c3cc1c9d5..a1ebca4c1 100644 --- a/client/http_test.go +++ b/client/http_test.go @@ -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}}, },