-
Notifications
You must be signed in to change notification settings - Fork 0
/
selector.go
executable file
·54 lines (40 loc) · 896 Bytes
/
selector.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
package main
import (
"errors"
"math/rand"
)
const (
MODE_RANDOM = "random"
MODE_RR = "round-robin"
MODE_LOAD = "load"
)
func selectServer(target string) (serverName string, err error) {
if target == "Master" {
return seerConfig.Master, nil
}
switch seerConfig.SelectionMode {
default:
return selectServerRandom()
break
}
return seerConfig.Master, nil
}
func selectServerRandom() (selectedServer string, err error) {
numServers := len(seerConfig.Servers)
candidate := rand.Intn(numServers)
i := 0
selectedServer = ""
for key, server := range seerConfig.Servers {
if server.Status == SERVER_STATUS_ACTIVE {
selectedServer = key
}
if i == candidate && server.Status == SERVER_STATUS_ACTIVE {
return key, nil
}
i++
}
if selectedServer != "" {
return "", errors.New("Unable to find an available server.")
}
return selectedServer, nil
}