|
|
|
@@ -11,16 +11,14 @@ import (
|
|
|
|
|
"context"
|
|
|
|
|
"math"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"go.etcd.io/etcd/clientv3"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// WatchKeyWithOption ...
|
|
|
|
|
// WatchKey 监听key的变化,永久监听
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
|
//
|
|
|
|
|
// Date : 2:29 下午 2021/11/26
|
|
|
|
|
func WatchKeyWithOption(ctx context.Context, watchKey string, callbackFunc WatcherHandler, optionList ...clientv3.OpOption) {
|
|
|
|
|
// Date : 2:58 下午 2021/11/23
|
|
|
|
|
func WatchKey(ctx context.Context, watchKey string, callbackFunc WatcherHandler) {
|
|
|
|
|
if nil == callbackFunc {
|
|
|
|
|
// 变化之后,没有任何逻辑处理,视为不需要监听变化
|
|
|
|
|
return
|
|
|
|
@@ -29,7 +27,7 @@ func WatchKeyWithOption(ctx context.Context, watchKey string, callbackFunc Watch
|
|
|
|
|
ctx = context.Background()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rch := Client.Watch(ctx, watchKey, optionList...) // <-chan WatchResponse
|
|
|
|
|
rch := Client.Watch(ctx, watchKey) // <-chan WatchResponse
|
|
|
|
|
for watchResp := range rch {
|
|
|
|
|
for _, ev := range watchResp.Events {
|
|
|
|
|
callbackFunc(ev)
|
|
|
|
@@ -37,24 +35,6 @@ func WatchKeyWithOption(ctx context.Context, watchKey string, callbackFunc Watch
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WatchKey 监听key的变化,永久监听
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
|
//
|
|
|
|
|
// Date : 2:58 下午 2021/11/23
|
|
|
|
|
func WatchKey(ctx context.Context, watchKey string, callbackFunc WatcherHandler) {
|
|
|
|
|
WatchKeyWithOption(ctx, watchKey, callbackFunc)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WatchWithKeyPrefix ...
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
|
//
|
|
|
|
|
// Date : 2:30 下午 2021/11/26
|
|
|
|
|
func WatchWithKeyPrefix(ctx context.Context, watchKey string, callbackFunc WatcherHandler) {
|
|
|
|
|
WatchKeyWithOption(ctx, watchKey, callbackFunc, clientv3.WithPrefix())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WatchKeyWithCancel 可以随时取消的
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
|