forked from fagongzi/manba
-
Notifications
You must be signed in to change notification settings - Fork 0
/
register_consul.go
116 lines (89 loc) · 2.31 KB
/
register_consul.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package model
import (
"context"
"fmt"
"time"
"github.com/fagongzi/log"
"github.com/hashicorp/consul/api"
"github.com/toolkits/net"
)
// Registry registry self
func (s *consulStore) Registry(proxyInfo *ProxyInfo) error {
timer := time.NewTicker(TICKER)
s.taskRunner.RunCancelableTask(func(ctx context.Context) {
for {
select {
case <-ctx.Done():
timer.Stop()
return
case <-timer.C:
s.doRegistry(proxyInfo)
}
}
})
return nil
}
func (s *consulStore) doRegistry(proxyInfo *ProxyInfo) {
proxyInfo.Conf.Addr = convertIP(proxyInfo.Conf.Addr)
proxyInfo.Conf.MgrAddr = convertIP(proxyInfo.Conf.MgrAddr)
key := fmt.Sprintf("%s/%s", s.proxiesDir, proxyInfo.Conf.Addr)
_, err := s.client.KV().Put(&api.KVPair{
Key: key,
Value: []byte(proxyInfo.Marshal()),
}, nil)
if err != nil {
log.Errorf("store: registry failed, errors:\n%+v",
err)
}
}
// GetProxies return runable proxies
func (s *consulStore) GetProxies() ([]*ProxyInfo, error) {
pairs, _, err := s.client.KV().List(s.proxiesDir, nil)
if nil != err {
return nil, err
}
values := make([]*ProxyInfo, len(pairs))
i := 0
for _, pair := range pairs {
values[i] = UnMarshalProxyInfo(pair.Value)
i++
}
return values, nil
}
// ChangeLogLevel change proxy log level
func (s *consulStore) ChangeLogLevel(addr string, level string) error {
rpcClient, _ := net.RpcClient("tcp", addr, time.Second*5)
req := SetLogReq{
Level: level,
}
rsp := &SetLogRsp{
Code: 0,
}
return rpcClient.Call("Manager.SetLogLevel", req, rsp)
}
// AddAnalysisPoint add a analysis point
func (s *consulStore) AddAnalysisPoint(proxyAddr, serverAddr string, secs int) error {
rpcClient, _ := net.RpcClient("tcp", proxyAddr, time.Second*5)
req := AddAnalysisPointReq{
Addr: serverAddr,
Secs: secs,
}
rsp := &AddAnalysisPointRsp{
Code: 0,
}
return rpcClient.Call("Manager.AddAnalysisPoint", req, rsp)
}
// GetAnalysisPoint return analysis point data
func (s *consulStore) GetAnalysisPoint(proxyAddr, serverAddr string, secs int) (*GetAnalysisPointRsp, error) {
rpcClient, err := net.RpcClient("tcp", proxyAddr, time.Second*5)
if nil != err {
return nil, err
}
req := GetAnalysisPointReq{
Addr: serverAddr,
Secs: secs,
}
rsp := &GetAnalysisPointRsp{}
err = rpcClient.Call("Manager.GetAnalysisPoint", req, rsp)
return rsp, err
}