-
Notifications
You must be signed in to change notification settings - Fork 14
/
consulwatch.go
77 lines (64 loc) · 1.7 KB
/
consulwatch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package registry
import (
"context"
"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/api/watch"
"github.com/razorpay/metro/pkg/logger"
)
// ConsulWatcher implements consul watch handler and stores the users handler function
type ConsulWatcher struct {
ctx context.Context
Config *WatchConfig
plan *watch.Plan
client *api.Client
}
// NewConsulWatcher is used to create a new struct of type WatchHandler
func NewConsulWatcher(ctx context.Context, watchConfig *WatchConfig, plan *watch.Plan, client *api.Client) IWatcher {
return &ConsulWatcher{
ctx: ctx,
Config: watchConfig,
plan: plan,
client: client,
}
}
// Handler implements the consul watch handler method and invokes the requester handler
func (cwh *ConsulWatcher) handler(index uint64, result interface{}) {
var (
pair *api.KVPair
pairs api.KVPairs
ok bool
)
results := []Pair{}
if result != nil {
if cwh.plan.Type == "key" {
pair, ok = result.(*api.KVPair)
pairs = append(pairs, pair)
} else if cwh.plan.Type == "keyprefix" {
pairs, ok = result.(api.KVPairs)
}
if !ok {
logger.Ctx(cwh.ctx).Errorw(
"failed to parse consul watch results",
"watchType", cwh.plan.Type,
"data", result)
return
}
for i := range pairs {
results = append(results, Pair{
Key: pairs[i].Key,
Value: pairs[i].Value,
SessionID: pairs[i].Session,
})
}
}
cwh.Config.Handler(cwh.ctx, results)
}
// StartWatch will start the watch
func (cwh *ConsulWatcher) StartWatch() error {
cwh.plan.Handler = cwh.handler
return cwh.plan.RunWithClientAndHclog(cwh.client, nil)
}
// StopWatch will cleanup the active consul watches
func (cwh *ConsulWatcher) StopWatch() {
cwh.plan.Stop()
}