/
balancev2.go
72 lines (59 loc) · 1.33 KB
/
balancev2.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
package svrbalance
// balance v1: loop servers person, if some one has location to connect, then will push client into it.
// add stefan 20190704 16:25
/*
{
s1
s2 --------> ctrl svr balance
s3
server person
---------s1 sx
client select ---------s2 sy rand server for client to connect.
---------s3 sz
if s1 person sx has beyond max person limit, then begin loop find s2, which sy has not arive person limit,
server will distribute s2 for client connection. firstly, rand server to connect.
}
*/
import (
"math/rand"
"time"
)
type TSvrBalanceV2 struct {
sb map[string]*TExternal
}
func (this *TSvrBalanceV2) NewBalance() {
}
func (this *TSvrBalanceV2) AddSvr(svr string) {
_, ok := this.sb[svr]
if ok {
return
}
this.sb[svr] = &TExternal{
Persons: 0,
}
}
// some one connect gateway to balance route push one server.
func (this *TSvrBalanceV2) Push(svr string) {
ex, ok := this.sb[svr]
if ok {
return
}
ex.Persons++
}
func (this *TSvrBalanceV2) getsvr() (svrs []string) {
svrs = []string{}
for svr, _ := range this.sb {
svrs = append(svrs, svr)
}
return
}
// get second max server persons
func (this *TSvrBalanceV2) GetSvr() (s string) {
var (
svrs []string = this.getsvr()
svrslen int = len(svrs)
)
randidx := rand.Intn(svrslen)
s = svrs[randidx]
return
}