feature/etcd #5

Merged
zhangdeman merged 5 commits from feature/etcd into master 2021-11-24 00:11:59 +08:00
Showing only changes of commit 7bef73334c - Show all commits

View File

@ -13,7 +13,7 @@ import (
"time" "time"
) )
// WatchKey 监听key的变化 // WatchKey 监听key的变化,永久监听
// //
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
@ -26,6 +26,7 @@ func WatchKey(ctx context.Context, watchKey string, callbackFunc WatcherHandler)
if nil == ctx { if nil == ctx {
ctx = context.Background() ctx = context.Background()
} }
rch := Client.Watch(ctx, watchKey) // <-chan WatchResponse rch := Client.Watch(ctx, watchKey) // <-chan WatchResponse
for watchResp := range rch { for watchResp := range rch {
for _, ev := range watchResp.Events { for _, ev := range watchResp.Events {
@ -47,6 +48,11 @@ func WatchKeyWithCancel(ctx context.Context, watchKey string, callbackFunc Watch
if nil == ctx { if nil == ctx {
ctx = context.Background() ctx = context.Background()
} }
var (
cancelFunc context.CancelFunc
)
ctx, cancelFunc = context.WithCancel(ctx)
defer cancelFunc()
rch := Client.Watch(context.Background(), watchKey) // <-chan WatchResponse rch := Client.Watch(context.Background(), watchKey) // <-chan WatchResponse
@ -92,6 +98,11 @@ func WatchKeyOnce(ctx context.Context, watchKey string, callbackFunc WatcherHand
if nil == ctx { if nil == ctx {
ctx = context.Background() ctx = context.Background()
} }
var (
cancelFunc context.CancelFunc
)
ctx, cancelFunc = context.WithCancel(ctx)
defer cancelFunc()
rch := Client.Watch(ctx, watchKey) // <-chan WatchResponse rch := Client.Watch(ctx, watchKey) // <-chan WatchResponse
select { select {
case <-time.After(timeout): case <-time.After(timeout):