-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
55 lines (50 loc) · 1.1 KB
/
util.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
package main
import (
"crypto/rand"
"fmt"
"sync"
)
func CreateClusters(shards, clusters int) {
shardIds := make([]int, 0, shards)
for i := 0; i < shards; i++ {
shardIds = append(shardIds, i)
}
avgShardsPerCluster := shards / clusters
for i := 0; i < len(shardIds); i += avgShardsPerCluster {
Server.Clients = append(Server.Clients, &Cluster{
ID: len(Server.Clients),
Client: nil,
PingRecv: false,
Block: ClusterBlock{Shards: shardIds[i : i+avgShardsPerCluster], Total: Config.Shards},
State: ClusterWaiting,
pingTicker: nil,
mutex: &sync.Mutex{},
statsChan: make(chan map[string]interface{}),
})
}
}
func RandomID() string {
bytes := make([]byte, 4)
_, err := rand.Read(bytes)
if err != nil {
return ""
}
return fmt.Sprintf("%x", bytes)
}
func NextClusterID() int {
for index, cluster := range Server.Clients {
if cluster.State == ClusterWaiting {
return index
}
}
return -1
}
func GetHealthyClusters() int {
healthy := 0
for _, cluster := range Server.Clients {
if cluster.State == ClusterReady {
healthy++
}
}
return healthy
}