11package checker
22
33import (
4+ "cmp"
45 "context"
6+ "fmt"
57 "net/url"
68 "time"
79
@@ -11,83 +13,68 @@ import (
1113
1214// ConsulLeaderChecker is used to check state of the leader key in Consul
1315type ConsulLeaderChecker struct {
14- key string
15- nodename string
16- apiClient * api.Client
16+ * vipconfig.Config
17+ * api.Client
1718}
1819
19- // naming this cConf to avoid conflict with conf in etcd_leader_checker.go
20- var cConf * vipconfig.Config
21-
2220// NewConsulLeaderChecker returns a new instance
23- func NewConsulLeaderChecker (con * vipconfig.Config ) (* ConsulLeaderChecker , error ) {
24- cConf = con
25- lc := & ConsulLeaderChecker {
26- key : cConf .TriggerKey ,
27- nodename : cConf .TriggerValue ,
28- }
21+ func NewConsulLeaderChecker (con * vipconfig.Config ) (lc * ConsulLeaderChecker , err error ) {
22+ lc = & ConsulLeaderChecker {Config : con }
2923
30- url , err := url .Parse (cConf .Endpoints [0 ])
24+ url , err := url .Parse (con .Endpoints [0 ])
3125 if err != nil {
3226 return nil , err
3327 }
34- address := url .Hostname () + ":" + url .Port ()
3528
3629 config := & api.Config {
37- Address : address ,
30+ Address : fmt . Sprintf ( "%s:%s" , url . Hostname (), url . Port ()) ,
3831 Scheme : url .Scheme ,
3932 WaitTime : time .Second ,
33+ Token : cmp .Or (con .ConsulToken , "" ),
4034 }
4135
42- if cConf .ConsulToken != "" {
43- config .Token = cConf .ConsulToken
44- }
45-
46- apiClient , err := api .NewClient (config )
47- if err != nil {
36+ if lc .Client , err = api .NewClient (config ); err != nil {
4837 return nil , err
4938 }
5039
51- lc .apiClient = apiClient
52-
5340 return lc , nil
5441}
5542
5643// GetChangeNotificationStream checks the status in the loop
5744func (c * ConsulLeaderChecker ) GetChangeNotificationStream (ctx context.Context , out chan <- bool ) error {
58- kv := c .apiClient .KV ()
45+ kv := c .Client .KV ()
5946
6047 queryOptions := & api.QueryOptions {
6148 RequireConsistent : true ,
6249 }
6350
6451checkLoop:
6552 for {
66- resp , _ , err := kv .Get (c .key , queryOptions )
53+ resp , _ , err := kv .Get (c .TriggerKey , queryOptions )
6754 if err != nil {
6855 if ctx .Err () != nil {
6956 break checkLoop
7057 }
71- cConf .Logger .Sugar ().Error ("consul error: " , err )
58+ c .Logger .Sugar ().Error ("consul error: " , err )
7259 out <- false
73- time .Sleep (time .Duration (cConf .Interval ) * time .Millisecond )
60+ time .Sleep (time .Duration (c .Interval ) * time .Millisecond )
7461 continue
7562 }
7663 if resp == nil {
77- cConf .Logger .Sugar ().Errorf ("Cannot get variable for key %s. Will try again in a second." , c .key )
64+ c .Logger .Sugar ().Errorf ("Cannot get variable for key %s. Will try again in a second." , c .TriggerKey )
7865 out <- false
79- time .Sleep (time .Duration (cConf .Interval ) * time .Millisecond )
66+ time .Sleep (time .Duration (c .Interval ) * time .Millisecond )
8067 continue
8168 }
8269
83- state := string (resp .Value ) == c .nodename
70+ state := string (resp .Value ) == c .TriggerValue
8471 queryOptions .WaitIndex = resp .ModifyIndex
8572
8673 select {
8774 case <- ctx .Done ():
8875 break checkLoop
8976 case out <- state :
90- time .Sleep (time .Duration (cConf .Interval ) * time .Millisecond )
77+ time .Sleep (time .Duration (c .Interval ) * time .Millisecond )
9178 continue
9279 }
9380 }
0 commit comments