From 7bef73334ce833236570ef71134ff6ee1572cf68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 24 Nov 2021 00:11:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dwatcher=E5=8F=96=E6=B6=88?= =?UTF-8?q?=E5=90=8E=EF=BC=8C=E7=9B=91=E5=90=AC=E5=8D=8F=E7=A8=8B=E4=BB=8D?= =?UTF-8?q?=E5=9C=A8=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- middleware/etcd/watch.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/middleware/etcd/watch.go b/middleware/etcd/watch.go index 4e43dc7..9340e75 100644 --- a/middleware/etcd/watch.go +++ b/middleware/etcd/watch.go @@ -13,7 +13,7 @@ import ( "time" ) -// WatchKey 监听key的变化 +// WatchKey 监听key的变化,永久监听 // // Author : go_developer@163.com<白茶清欢> // @@ -26,6 +26,7 @@ func WatchKey(ctx context.Context, watchKey string, callbackFunc WatcherHandler) if nil == ctx { ctx = context.Background() } + rch := Client.Watch(ctx, watchKey) // <-chan WatchResponse for watchResp := range rch { for _, ev := range watchResp.Events { @@ -47,6 +48,11 @@ func WatchKeyWithCancel(ctx context.Context, watchKey string, callbackFunc Watch if nil == ctx { ctx = context.Background() } + var ( + cancelFunc context.CancelFunc + ) + ctx, cancelFunc = context.WithCancel(ctx) + defer cancelFunc() rch := Client.Watch(context.Background(), watchKey) // <-chan WatchResponse @@ -92,6 +98,11 @@ func WatchKeyOnce(ctx context.Context, watchKey string, callbackFunc WatcherHand if nil == ctx { ctx = context.Background() } + var ( + cancelFunc context.CancelFunc + ) + ctx, cancelFunc = context.WithCancel(ctx) + defer cancelFunc() rch := Client.Watch(ctx, watchKey) // <-chan WatchResponse select { case <-time.After(timeout):