forked from fagongzi/manba
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster.go
183 lines (142 loc) · 3.35 KB
/
cluster.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package model
import (
"container/list"
"encoding/json"
"io"
"sync"
"github.com/fagongzi/gateway/pkg/lb"
"github.com/fagongzi/gateway/pkg/util"
"github.com/fagongzi/log"
"github.com/valyala/fasthttp"
)
// Cluster cluster
type Cluster struct {
Name string `json:"name,omitempty"`
LbName string `json:"lbName,omitempty"`
// External external, e.g. create from external service discovery
External bool `json:"external,omitempty"`
BindServers []string `json:"bindServers,omitempty"`
svrs *list.List
rwLock *sync.RWMutex
lb lb.LoadBalance
}
// UnMarshalCluster unmarshal
func UnMarshalCluster(data []byte) *Cluster {
v := &Cluster{}
err := json.Unmarshal(data, v)
if err != nil {
return v
}
if len(data) > 0 && v.LbName != "" {
v.init()
}
return v
}
// UnMarshalClusterFromReader unmarshal from reader
func UnMarshalClusterFromReader(r io.Reader) (*Cluster, error) {
v := &Cluster{}
decoder := json.NewDecoder(r)
err := decoder.Decode(v)
if nil != err {
return nil, err
}
return v, v.init()
}
// NewCluster create a cluster
func NewCluster(name string, lbName string) (*Cluster, error) {
c := &Cluster{
Name: name,
LbName: lbName,
}
return c, c.init()
}
// AddBind add bind
func (c *Cluster) AddBind(bind *Bind) {
index := c.indexOf(bind.ServerAddr)
if index == -1 {
c.BindServers = append(c.BindServers, bind.ServerAddr)
}
}
// HasBind add bind
func (c *Cluster) HasBind() bool {
return len(c.BindServers) > 0
}
// RemoveBind remove bind
func (c *Cluster) RemoveBind(serverAddr string) {
index := c.indexOf(serverAddr)
if index >= 0 {
c.BindServers = append(c.BindServers[:index], c.BindServers[index+1:]...)
}
}
func (c *Cluster) indexOf(serverAddr string) int {
for index, s := range c.BindServers {
if s == serverAddr {
return index
}
}
return -1
}
func (c *Cluster) init() error {
c.svrs = list.New()
c.lb = lb.NewLoadBalance(c.LbName)
c.rwLock = &sync.RWMutex{}
return nil
}
func (c *Cluster) updateFrom(cluster *Cluster) {
if c.rwLock != nil {
c.rwLock.Lock()
defer c.rwLock.Unlock()
}
c.LbName = cluster.LbName
c.lb = lb.NewLoadBalance(c.LbName)
log.Infof("meta: cluster <%s> updated", c.Name)
}
func (c *Cluster) doInEveryBindServers(callback func(string)) {
c.rwLock.Lock()
defer c.rwLock.Unlock()
for iter := c.svrs.Back(); iter != nil; iter = iter.Prev() {
addr, _ := iter.Value.(string)
callback(addr)
}
}
func (c *Cluster) unbind(svr *Server) {
c.rwLock.Lock()
defer c.rwLock.Unlock()
c.doUnBind(svr)
}
func (c *Cluster) doUnBind(svr *Server) {
util.Remove(c.svrs, svr.Addr)
log.Infof("meta: unBind <%s,%s> succ.", svr.Addr, c.Name)
}
func (c *Cluster) bind(svr *Server) {
c.rwLock.Lock()
defer c.rwLock.Unlock()
if util.IndexOf(c.svrs, svr.Addr) >= 0 {
log.Infof("meta: bind <%s,%s> already created.", svr.Addr, c.Name)
return
}
c.svrs.PushBack(svr.Addr)
log.Infof("meta: bind <%s,%s> created.", svr.Addr, c.Name)
}
// Select return a server using spec loadbalance
func (c *Cluster) Select(req *fasthttp.Request) string {
c.rwLock.RLock()
index := c.lb.Select(req, c.svrs)
if 0 > index {
c.rwLock.RUnlock()
return ""
}
e := util.Get(c.svrs, index)
if nil == e {
c.rwLock.RUnlock()
return ""
}
s, _ := e.Value.(string)
c.rwLock.RUnlock()
return s
}
// Marshal marshal
func (c *Cluster) Marshal() []byte {
v, _ := json.Marshal(c)
return v
}