forked from fagongzi/manba
-
Notifications
You must be signed in to change notification settings - Fork 0
/
etcd_register.go
125 lines (94 loc) · 2.43 KB
/
etcd_register.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
117
118
119
120
121
122
123
124
125
package model
import (
"fmt"
"strings"
"time"
"github.com/CodisLabs/codis/pkg/utils/log"
"github.com/toolkits/net"
)
var (
// TICKER ticket
TICKER = time.Second * 3
// TTL timeout
TTL = uint64(5)
)
// Registry registry self
func (e EtcdStore) Registry(proxyInfo *ProxyInfo) error {
timer := time.NewTicker(TICKER)
go func() {
for {
<-timer.C
log.Debug("Registry start")
e.doRegistry(proxyInfo)
}
}()
return nil
}
func (e EtcdStore) doRegistry(proxyInfo *ProxyInfo) {
proxyInfo.Conf.Addr = convertIP(proxyInfo.Conf.Addr)
proxyInfo.Conf.MgrAddr = convertIP(proxyInfo.Conf.MgrAddr)
key := fmt.Sprintf("%s/%s", e.proxiesDir, proxyInfo.Conf.Addr)
_, err := e.cli.Set(key, proxyInfo.Marshal(), TTL)
if err != nil {
log.ErrorError(err, "Registry fail.")
}
}
// GetProxies return runable proxies
func (e EtcdStore) GetProxies() ([]*ProxyInfo, error) {
rsp, err := e.cli.Get(e.proxiesDir, true, false)
if nil != err {
return nil, err
}
l := rsp.Node.Nodes.Len()
proxies := make([]*ProxyInfo, l)
for i := 0; i < l; i++ {
proxies[i] = UnMarshalProxyInfo([]byte(rsp.Node.Nodes[i].Value))
}
return proxies, nil
}
// ChangeLogLevel change proxy log level
func (e EtcdStore) 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 (e EtcdStore) 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 (e EtcdStore) 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
}
func convertIP(addr string) string {
if strings.HasPrefix(addr, ":") {
ips, err := net.IntranetIP()
if err == nil {
addr = strings.Replace(addr, ":", fmt.Sprintf("%s:", ips[0]), 1)
}
}
return addr
}