client: support reset Endpoints.

ResetEndpoints is useful when the there is a scheduled cluster
changes or when manually manage the cluster without auto-sync
enabled.
This commit is contained in:
Xiang Li 2015-12-17 18:54:48 -08:00
parent afee51eeda
commit 5587c4aa9a
2 changed files with 16 additions and 11 deletions

View File

@ -162,6 +162,11 @@ type Client interface {
// this may differ from the initial Endpoints provided in the Config. // this may differ from the initial Endpoints provided in the Config.
Endpoints() []string Endpoints() []string
// SetEndpoints sets the set of API endpoints used by Client to resolve
// HTTP requests. If the given endpoints are not valid, an error will be
// returned
SetEndpoints(eps []string) error
httpClient httpClient
} }
@ -176,7 +181,7 @@ func New(cfg Config) (Client, error) {
password: cfg.Password, password: cfg.Password,
} }
} }
if err := c.reset(cfg.Endpoints); err != nil { if err := c.SetEndpoints(cfg.Endpoints); err != nil {
return nil, err return nil, err
} }
return c, nil return c, nil
@ -219,7 +224,7 @@ type httpClusterClient struct {
rand *rand.Rand rand *rand.Rand
} }
func (c *httpClusterClient) reset(eps []string) error { func (c *httpClusterClient) SetEndpoints(eps []string) error {
if len(eps) == 0 { if len(eps) == 0 {
return ErrNoEndpoints return ErrNoEndpoints
} }
@ -341,7 +346,7 @@ func (c *httpClusterClient) Sync(ctx context.Context) error {
return nil return nil
} }
return c.reset(eps) return c.SetEndpoints(eps)
} }
func (c *httpClusterClient) AutoSync(ctx context.Context, interval time.Duration) error { func (c *httpClusterClient) AutoSync(ctx context.Context, interval time.Duration) error {

View File

@ -705,7 +705,7 @@ func TestHTTPClusterClientSync(t *testing.T) {
clientFactory: cf, clientFactory: cf,
rand: rand.New(rand.NewSource(0)), rand: rand.New(rand.NewSource(0)),
} }
err := hc.reset([]string{"http://127.0.0.1:2379"}) err := hc.SetEndpoints([]string{"http://127.0.0.1:2379"})
if err != nil { if err != nil {
t.Fatalf("unexpected error during setup: %#v", err) t.Fatalf("unexpected error during setup: %#v", err)
} }
@ -728,7 +728,7 @@ func TestHTTPClusterClientSync(t *testing.T) {
t.Fatalf("incorrect endpoints post-Sync: want=%#v got=%#v", want, got) t.Fatalf("incorrect endpoints post-Sync: want=%#v got=%#v", want, got)
} }
err = hc.reset([]string{"http://127.0.0.1:4009"}) err = hc.SetEndpoints([]string{"http://127.0.0.1:4009"})
if err != nil { if err != nil {
t.Fatalf("unexpected error during reset: %#v", err) t.Fatalf("unexpected error during reset: %#v", err)
} }
@ -749,7 +749,7 @@ func TestHTTPClusterClientSyncFail(t *testing.T) {
clientFactory: cf, clientFactory: cf,
rand: rand.New(rand.NewSource(0)), rand: rand.New(rand.NewSource(0)),
} }
err := hc.reset([]string{"http://127.0.0.1:2379"}) err := hc.SetEndpoints([]string{"http://127.0.0.1:2379"})
if err != nil { if err != nil {
t.Fatalf("unexpected error during setup: %#v", err) t.Fatalf("unexpected error during setup: %#v", err)
} }
@ -783,7 +783,7 @@ func TestHTTPClusterClientAutoSyncCancelContext(t *testing.T) {
clientFactory: cf, clientFactory: cf,
rand: rand.New(rand.NewSource(0)), rand: rand.New(rand.NewSource(0)),
} }
err := hc.reset([]string{"http://127.0.0.1:2379"}) err := hc.SetEndpoints([]string{"http://127.0.0.1:2379"})
if err != nil { if err != nil {
t.Fatalf("unexpected error during setup: %#v", err) t.Fatalf("unexpected error during setup: %#v", err)
} }
@ -805,7 +805,7 @@ func TestHTTPClusterClientAutoSyncFail(t *testing.T) {
clientFactory: cf, clientFactory: cf,
rand: rand.New(rand.NewSource(0)), rand: rand.New(rand.NewSource(0)),
} }
err := hc.reset([]string{"http://127.0.0.1:2379"}) err := hc.SetEndpoints([]string{"http://127.0.0.1:2379"})
if err != nil { if err != nil {
t.Fatalf("unexpected error during setup: %#v", err) t.Fatalf("unexpected error during setup: %#v", err)
} }
@ -838,7 +838,7 @@ func TestHTTPClusterClientSyncPinEndpoint(t *testing.T) {
clientFactory: cf, clientFactory: cf,
rand: rand.New(rand.NewSource(0)), rand: rand.New(rand.NewSource(0)),
} }
err := hc.reset([]string{"http://127.0.0.1:4003", "http://127.0.0.1:2379", "http://127.0.0.1:4001", "http://127.0.0.1:4002"}) err := hc.SetEndpoints([]string{"http://127.0.0.1:4003", "http://127.0.0.1:2379", "http://127.0.0.1:4001", "http://127.0.0.1:4002"})
if err != nil { if err != nil {
t.Fatalf("unexpected error during setup: %#v", err) t.Fatalf("unexpected error during setup: %#v", err)
} }
@ -867,7 +867,7 @@ func TestHTTPClusterClientResetFail(t *testing.T) {
for i, tt := range tests { for i, tt := range tests {
hc := &httpClusterClient{rand: rand.New(rand.NewSource(0))} hc := &httpClusterClient{rand: rand.New(rand.NewSource(0))}
err := hc.reset(tt) err := hc.SetEndpoints(tt)
if err == nil { if err == nil {
t.Errorf("#%d: expected non-nil error", i) t.Errorf("#%d: expected non-nil error", i)
} }
@ -879,7 +879,7 @@ func TestHTTPClusterClientResetPinRandom(t *testing.T) {
pinNum := 0 pinNum := 0
for i := 0; i < round; i++ { for i := 0; i < round; i++ {
hc := &httpClusterClient{rand: rand.New(rand.NewSource(int64(i)))} hc := &httpClusterClient{rand: rand.New(rand.NewSource(int64(i)))}
err := hc.reset([]string{"http://127.0.0.1:4001", "http://127.0.0.1:4002", "http://127.0.0.1:4003"}) err := hc.SetEndpoints([]string{"http://127.0.0.1:4001", "http://127.0.0.1:4002", "http://127.0.0.1:4003"})
if err != nil { if err != nil {
t.Fatalf("#%d: reset error (%v)", i, err) t.Fatalf("#%d: reset error (%v)", i, err)
} }