From 8d519ffdb85e2075b38a91fc1a95fa386e747e1b Mon Sep 17 00:00:00 2001 From: Brian Waldon Date: Thu, 30 Oct 2014 17:10:01 -0700 Subject: [PATCH] client: introduce httpClusterClient --- client/cluster.go | 52 +++++++++++++++++++++++++++++++++++++++++++++++ client/keys.go | 7 +------ client/members.go | 7 +------ 3 files changed, 54 insertions(+), 12 deletions(-) create mode 100644 client/cluster.go diff --git a/client/cluster.go b/client/cluster.go new file mode 100644 index 000000000..3fcc8ec04 --- /dev/null +++ b/client/cluster.go @@ -0,0 +1,52 @@ +/* + Copyright 2014 CoreOS, Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package client + +import ( + "net/http" + "net/url" + + "github.com/coreos/etcd/Godeps/_workspace/src/code.google.com/p/go.net/context" +) + +func newHTTPClusterClient(tr *http.Transport, eps []string) (*httpClusterClient, error) { + c := httpClusterClient{ + endpoints: make([]*httpClient, len(eps)), + } + + for i, ep := range eps { + u, err := url.Parse(ep) + if err != nil { + return nil, err + } + c.endpoints[i] = &httpClient{ + transport: tr, + endpoint: *u, + } + } + + return &c, nil +} + +type httpClusterClient struct { + endpoints []*httpClient +} + +func (c *httpClusterClient) do(ctx context.Context, act httpAction) (int, []byte, error) { + //TODO(bcwaldon): introduce retry logic so all endpoints are attempted + return c.endpoints[0].do(ctx, act) +} diff --git a/client/keys.go b/client/keys.go index 878e99ca4..4e1c5d2cf 100644 --- a/client/keys.go +++ b/client/keys.go @@ -50,16 +50,11 @@ func NewDiscoveryKeysAPI(tr *http.Transport, ep string, to time.Duration) (KeysA } func newHTTPKeysAPIWithPrefix(tr *http.Transport, ep string, to time.Duration, prefix string) (*httpKeysAPI, error) { - u, err := url.Parse(ep) + c, err := newHTTPClusterClient(tr, []string{ep}) if err != nil { return nil, err } - c := &httpClient{ - transport: tr, - endpoint: *u, - } - kAPI := httpKeysAPI{ client: c, prefix: prefix, diff --git a/client/members.go b/client/members.go index abd211a23..eb798980c 100644 --- a/client/members.go +++ b/client/members.go @@ -35,16 +35,11 @@ var ( ) func NewMembersAPI(tr *http.Transport, ep string, to time.Duration) (MembersAPI, error) { - u, err := url.Parse(ep) + c, err := newHTTPClusterClient(tr, []string{ep}) if err != nil { return nil, err } - c := &httpClient{ - transport: tr, - endpoint: *u, - } - mAPI := httpMembersAPI{ client: c, timeout: to,