client: KeysAPI.[R]Watch -> Watcher w/ opts struct
This commit is contained in:

committed by
Yicheng Qin

parent
bc32060b1d
commit
01fc01ec69
@ -70,8 +70,12 @@ type KeysAPI interface {
|
|||||||
Get(ctx context.Context, key string) (*Response, error)
|
Get(ctx context.Context, key string) (*Response, error)
|
||||||
RGet(ctx context.Context, key string) (*Response, error)
|
RGet(ctx context.Context, key string) (*Response, error)
|
||||||
|
|
||||||
Watch(key string, idx uint64) Watcher
|
Watcher(key string, opts WatcherOptions) Watcher
|
||||||
RWatch(key string, idx uint64) Watcher
|
}
|
||||||
|
|
||||||
|
type WatcherOptions struct {
|
||||||
|
WaitIndex uint64
|
||||||
|
Recursive bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type SetOptions struct {
|
type SetOptions struct {
|
||||||
@ -184,26 +188,14 @@ func (k *httpKeysAPI) RGet(ctx context.Context, key string) (*Response, error) {
|
|||||||
return unmarshalHTTPResponse(resp.StatusCode, resp.Header, body)
|
return unmarshalHTTPResponse(resp.StatusCode, resp.Header, body)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k *httpKeysAPI) Watch(key string, idx uint64) Watcher {
|
func (k *httpKeysAPI) Watcher(key string, opts WatcherOptions) Watcher {
|
||||||
return &httpWatcher{
|
return &httpWatcher{
|
||||||
client: k.client,
|
client: k.client,
|
||||||
nextWait: waitAction{
|
nextWait: waitAction{
|
||||||
Prefix: k.prefix,
|
Prefix: k.prefix,
|
||||||
Key: key,
|
Key: key,
|
||||||
WaitIndex: idx,
|
WaitIndex: opts.WaitIndex,
|
||||||
Recursive: false,
|
Recursive: opts.Recursive,
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (k *httpKeysAPI) RWatch(key string, idx uint64) Watcher {
|
|
||||||
return &httpWatcher{
|
|
||||||
client: k.client,
|
|
||||||
nextWait: waitAction{
|
|
||||||
Prefix: k.prefix,
|
|
||||||
Key: key,
|
|
||||||
WaitIndex: idx,
|
|
||||||
Recursive: true,
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -186,7 +186,7 @@ func (d *discovery) createSelf(contents string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ensure self appears on the server we connected to
|
// ensure self appears on the server we connected to
|
||||||
w := d.c.Watch(d.selfKey(), resp.Node.CreatedIndex)
|
w := d.c.Watcher(d.selfKey(), client.WatcherOptions{WaitIndex: resp.Node.CreatedIndex})
|
||||||
_, err = w.Next(context.Background())
|
_, err = w.Next(context.Background())
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -275,7 +275,7 @@ func (d *discovery) waitNodes(nodes client.Nodes, size int, index uint64) (clien
|
|||||||
nodes = nodes[:size]
|
nodes = nodes[:size]
|
||||||
}
|
}
|
||||||
// watch from the next index
|
// watch from the next index
|
||||||
w := d.c.RWatch(d.cluster, index+1)
|
w := d.c.Watcher(d.cluster, client.WatcherOptions{WaitIndex: index + 1, Recursive: true})
|
||||||
all := make(client.Nodes, len(nodes))
|
all := make(client.Nodes, len(nodes))
|
||||||
copy(all, nodes)
|
copy(all, nodes)
|
||||||
for _, n := range all {
|
for _, n := range all {
|
||||||
|
@ -431,11 +431,7 @@ func (c *clientWithResp) Get(ctx context.Context, key string) (*client.Response,
|
|||||||
return r, nil
|
return r, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *clientWithResp) Watch(key string, waitIndex uint64) client.Watcher {
|
func (c *clientWithResp) Watcher(key string, opts client.WatcherOptions) client.Watcher {
|
||||||
return c.w
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *clientWithResp) RWatch(key string, waitIndex uint64) client.Watcher {
|
|
||||||
return c.w
|
return c.w
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -453,11 +449,7 @@ func (c *clientWithErr) Get(ctx context.Context, key string) (*client.Response,
|
|||||||
return &client.Response{}, c.err
|
return &client.Response{}, c.err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *clientWithErr) Watch(key string, waitIndex uint64) client.Watcher {
|
func (c *clientWithErr) Watcher(key string, opts client.WatcherOptions) client.Watcher {
|
||||||
return c.w
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *clientWithErr) RWatch(key string, waitIndex uint64) client.Watcher {
|
|
||||||
return c.w
|
return c.w
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -142,7 +142,7 @@ func TestForceNewCluster(t *testing.T) {
|
|||||||
cancel()
|
cancel()
|
||||||
// ensure create has been applied in this machine
|
// ensure create has been applied in this machine
|
||||||
ctx, cancel = context.WithTimeout(context.Background(), requestTimeout)
|
ctx, cancel = context.WithTimeout(context.Background(), requestTimeout)
|
||||||
if _, err := kapi.Watch("/foo", resp.Node.ModifiedIndex).Next(ctx); err != nil {
|
if _, err := kapi.Watcher("/foo", client.WatcherOptions{WaitIndex: resp.Node.ModifiedIndex}).Next(ctx); err != nil {
|
||||||
t.Fatalf("unexpected watch error: %v", err)
|
t.Fatalf("unexpected watch error: %v", err)
|
||||||
}
|
}
|
||||||
cancel()
|
cancel()
|
||||||
@ -163,7 +163,7 @@ func TestForceNewCluster(t *testing.T) {
|
|||||||
kapi = client.NewKeysAPI(cc)
|
kapi = client.NewKeysAPI(cc)
|
||||||
// ensure force restart keep the old data, and new cluster can make progress
|
// ensure force restart keep the old data, and new cluster can make progress
|
||||||
ctx, cancel = context.WithTimeout(context.Background(), requestTimeout)
|
ctx, cancel = context.WithTimeout(context.Background(), requestTimeout)
|
||||||
if _, err := kapi.Watch("/foo", resp.Node.ModifiedIndex).Next(ctx); err != nil {
|
if _, err := kapi.Watcher("/foo", client.WatcherOptions{WaitIndex: resp.Node.ModifiedIndex}).Next(ctx); err != nil {
|
||||||
t.Fatalf("unexpected watch error: %v", err)
|
t.Fatalf("unexpected watch error: %v", err)
|
||||||
}
|
}
|
||||||
cancel()
|
cancel()
|
||||||
@ -189,7 +189,7 @@ func clusterMustProgress(t *testing.T, membs []*member) {
|
|||||||
mcc := mustNewHTTPClient(t, []string{u})
|
mcc := mustNewHTTPClient(t, []string{u})
|
||||||
mkapi := client.NewKeysAPI(mcc)
|
mkapi := client.NewKeysAPI(mcc)
|
||||||
mctx, mcancel := context.WithTimeout(context.Background(), requestTimeout)
|
mctx, mcancel := context.WithTimeout(context.Background(), requestTimeout)
|
||||||
if _, err := mkapi.Watch(key, resp.Node.ModifiedIndex).Next(mctx); err != nil {
|
if _, err := mkapi.Watcher(key, client.WatcherOptions{WaitIndex: resp.Node.ModifiedIndex}).Next(mctx); err != nil {
|
||||||
t.Fatalf("#%d: watch on %s error: %v", i, u, err)
|
t.Fatalf("#%d: watch on %s error: %v", i, u, err)
|
||||||
}
|
}
|
||||||
mcancel()
|
mcancel()
|
||||||
|
@ -95,7 +95,7 @@ func TestSnapshotAndRestartMember(t *testing.T) {
|
|||||||
kapi := client.NewKeysAPI(cc)
|
kapi := client.NewKeysAPI(cc)
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
|
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
|
||||||
key := fmt.Sprintf("foo%d", i)
|
key := fmt.Sprintf("foo%d", i)
|
||||||
resps[i], err = kapi.Create(ctx, "/"+key, "bar", -1)
|
resps[i], err = kapi.Create(ctx, "/"+key, "bar")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("#%d: create on %s error: %v", i, m.URL(), err)
|
t.Fatalf("#%d: create on %s error: %v", i, m.URL(), err)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user