forked from name5566/leaf
-
Notifications
You must be signed in to change notification settings - Fork 7
/
cluster.go
391 lines (322 loc) · 8.08 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package cluster
import (
"math"
"time"
"reflect"
"net"
"fmt"
"github.com/name5566/leaf/log"
"github.com/name5566/leaf/conf"
"github.com/name5566/leaf/network"
"github.com/name5566/leaf/chanrpc"
lgob "github.com/name5566/leaf/network/gob"
"sync"
"sync/atomic"
)
const (
NeedWaitRequestTimes = 5
)
var (
closing bool
closeSig = make(chan bool, 1)
wg sync.WaitGroup
server *network.TCPServer
clientsMutex sync.Mutex
clients = map[string]*network.TCPClient{}
agentsMutex sync.RWMutex
agents = map[string]*Agent{}
AgentChanRPC *chanrpc.Server
)
func Init() {
if conf.ListenAddr != "" {
server = new(network.TCPServer)
server.Addr = conf.ListenAddr
server.MaxConnNum = int(math.MaxInt32)
server.PendingWriteNum = conf.PendingWriteNum
server.LenMsgLen = 4
server.MaxMsgLen = math.MaxUint32
server.NewAgent = newAgent
server.Start()
}
for serverName, addr := range conf.ConnAddrs {
AddClient(serverName, addr)
}
if conf.HeartBeatInterval <= 0 {
conf.HeartBeatInterval = 5
log.Release("invalid HeartBeatInterval, reset to %v", conf.HeartBeatInterval)
}
wg.Add(1)
go run()
}
func run() {
defer wg.Done()
msg := &S2S_HeartBeat{}
timer := time.NewTicker(time.Duration(conf.HeartBeatInterval) * time.Second)
for {
select {
case <-closeSig:
return
case <-timer.C:
agentsMutex.RLock()
for _, agent := range agents {
if atomic.AddInt32(&agent.heartBeatWaitTimes, 1) >= 2 {
agent.conn.Destroy()
} else {
agent.WriteMsg(msg)
}
}
agentsMutex.RUnlock()
}
}
}
func AddClient(serverName, addr string) {
clientsMutex.Lock()
defer clientsMutex.Unlock()
_removeClient(serverName)
client := new(network.TCPClient)
client.Addr = addr
client.ConnNum = 1
client.ConnectInterval = 3 * time.Second
client.PendingWriteNum = conf.PendingWriteNum
client.LenMsgLen = 4
client.MaxMsgLen = math.MaxUint32
client.NewAgent = newAgent
client.AutoReconnect = true
client.Start()
clients[serverName] = client
}
func _removeClient(serverName string) {
client, ok := clients[serverName]
if ok {
client.Close()
delete(clients, serverName)
}
}
func RemoveClient(serverName string) {
clientsMutex.Lock()
defer clientsMutex.Unlock()
_removeClient(serverName)
}
func addAgent(serverName string, agent *Agent) {
agentsMutex.Lock()
defer agentsMutex.Unlock()
_removeAgent(serverName)
agent.ServerName = serverName
agents[agent.ServerName] = agent
log.Release("%v server is online", serverName)
if AgentChanRPC != nil {
AgentChanRPC.Go("NewServerAgent", serverName, agent)
}
}
func _removeAgent(serverName string) {
agent, ok := agents[serverName]
if ok {
delete(agents, serverName)
agent.Destroy()
log.Release("%v server is offline", serverName)
if AgentChanRPC != nil {
AgentChanRPC.Go("CloseServerAgent", serverName, agent)
}
}
}
func removeAgent(serverName string) {
agentsMutex.Lock()
defer agentsMutex.Unlock()
_removeAgent(serverName)
}
func Destroy() {
closing = true
waitRequestTimes := 0
for {
time.Sleep(time.Second)
requestCount := GetRequestCount()
if requestCount == 0 {
waitRequestTimes += 1
if waitRequestTimes >= NeedWaitRequestTimes {
break
} else {
log.Release("wait request count down %v", NeedWaitRequestTimes-waitRequestTimes)
}
} else {
waitRequestTimes = 0
log.Release("has %v request", requestCount)
}
}
closeSig <- true
wg.Wait()
if server != nil {
server.Close()
}
clientsMutex.Lock()
for _, client := range clients {
client.Close()
}
clientsMutex.Unlock()
}
type Agent struct {
ServerName string
conn *network.TCPConn
userData interface{}
heartBeatWaitTimes int32
encMutex sync.Mutex
encoder *lgob.Encoder
decoder *lgob.Decoder
sync.Mutex
requestID uint32
requestMap map[uint32]*RequestInfo
}
func newAgent(conn *network.TCPConn) network.Agent {
a := new(Agent)
a.conn = conn
a.requestMap = make(map[uint32]*RequestInfo)
a.encoder = lgob.NewEncoder()
a.decoder = lgob.NewDecoder()
msg := &S2S_NotifyServerName{ServerName: conf.ServerName}
a.WriteMsg(msg)
return a
}
func (a *Agent) GetRequestCount() int {
a.Lock()
defer a.Unlock()
return len(a.requestMap)
}
func (a *Agent) registerRequest(request *RequestInfo) uint32 {
a.Lock()
defer a.Unlock()
reqID := a.requestID
a.requestMap[reqID] = request
a.requestID += 1
return reqID
}
func (a *Agent) popRequest(requestID uint32) *RequestInfo {
a.Lock()
defer a.Unlock()
request, ok := a.requestMap[requestID]
if ok {
delete(a.requestMap, requestID)
return request
} else {
return nil
}
}
func (a *Agent) clearRequest(err error) {
a.Lock()
defer a.Unlock()
for _, request := range a.requestMap {
ret := &chanrpc.RetInfo{Err: err, Cb: request.cb}
request.chanRet <- ret
}
a.requestMap = make(map[uint32]*RequestInfo)
}
func (a *Agent) Run() {
for {
data, err := a.conn.ReadMsg()
if err != nil {
log.Debug("read message: %v", err)
break
}
if Processor != nil {
msg, err := Processor.Unmarshal(a.decoder, data)
if err != nil {
log.Debug("unmarshal message error: %v", err)
break
}
err = Processor.Route(msg, a)
if err != nil {
log.Debug("route message error: %v", err)
break
}
}
}
}
func (a *Agent) OnClose() {
removeAgent(a.ServerName)
a.clearRequest(fmt.Errorf("%v server is offline", a.ServerName))
}
func (a *Agent) WriteMsg(msg interface{}) {
if Processor != nil {
a.encMutex.Lock()
defer a.encMutex.Unlock()
data, err := Processor.Marshal(a.encoder, msg)
if err != nil {
log.Error("marshal message %v error: %v", reflect.TypeOf(msg), err)
return
}
err = a.conn.WriteMsg(data...)
if err != nil {
log.Error("write message %v error: %v", reflect.TypeOf(msg), err)
}
}
}
func (a *Agent) LocalAddr() net.Addr {
return a.conn.LocalAddr()
}
func (a *Agent) RemoteAddr() net.Addr {
return a.conn.RemoteAddr()
}
func (a *Agent) Close() {
a.conn.Close()
}
func (a *Agent) Destroy() {
a.conn.Destroy()
}
func (a *Agent) UserData() interface{} {
return a.userData
}
func (a *Agent) SetUserData(data interface{}) {
a.userData = data
}
func (a *Agent) Go(id interface{}, args ...interface{}) {
msg := &S2S_RequestMsg{MsgID: id, CallType: callNotForResult, Args: args}
a.WriteMsg(msg)
}
func (a *Agent) Call0(id interface{}, args ...interface{}) error {
chanSyncRet := make(chan *chanrpc.RetInfo, 1)
request := &RequestInfo{chanRet: chanSyncRet}
requestID := a.registerRequest(request)
msg := &S2S_RequestMsg{RequestID: requestID, MsgID: id, CallType: callForResult, Args: args}
a.WriteMsg(msg)
ri := <-chanSyncRet
return ri.Err
}
func (a *Agent) Call1(id interface{}, args ...interface{}) (interface{}, error) {
chanSyncRet := make(chan *chanrpc.RetInfo, 1)
request := &RequestInfo{chanRet: chanSyncRet}
requestID := a.registerRequest(request)
msg := &S2S_RequestMsg{RequestID: requestID, MsgID: id, CallType: callForResult, Args: args}
a.WriteMsg(msg)
ri := <-chanSyncRet
return ri.Ret, ri.Err
}
func (a *Agent) CallN(id interface{}, args ...interface{}) ([]interface{}, error) {
chanSyncRet := make(chan *chanrpc.RetInfo, 1)
request := &RequestInfo{chanRet: chanSyncRet}
requestID := a.registerRequest(request)
msg := &S2S_RequestMsg{RequestID: requestID, MsgID: id, CallType: callForResult, Args: args}
a.WriteMsg(msg)
ri := <-chanSyncRet
return chanrpc.Assert(ri.Ret), ri.Err
}
func (a *Agent) AsynCall(chanAsynRet chan *chanrpc.RetInfo, id interface{}, args ...interface{}) {
if len(args) < 1 {
panic(fmt.Sprintf("%v asyn call of callback function not found", id))
}
lastIndex := len(args) - 1
cb := args[lastIndex]
args = args[:lastIndex]
var callType uint8
switch cb.(type) {
case func(error):
callType = callForResult
case func(interface{}, error):
callType = callForResult
case func([]interface{}, error):
callType = callForResult
default:
panic(fmt.Sprintf("%v asyn call definition of callback function is invalid", id))
}
request := &RequestInfo{cb: cb, chanRet: chanAsynRet}
requestID := a.registerRequest(request)
msg := &S2S_RequestMsg{RequestID: requestID, MsgID: id, CallType: callType, Args: args}
a.WriteMsg(msg)
}