cluster.go 1.3 KB
Newer Older
E
eoLinker API Management 已提交
1 2
package cluster

黄孟柱 已提交
3
import "github.com/eolinker/goku-api-gateway/server/entity"
E
eoLinker API Management 已提交
4 5 6

var (
	byName       map[string]*entity.ClusterInfo
Y
Your Name 已提交
7
	byID         map[int]*entity.ClusterInfo
E
eoLinker API Management 已提交
8 9 10 11
	clusterInfos []*entity.ClusterInfo
	nodes        []*entity.Cluster
)

Y
Your Name 已提交
12
//Init 初始化集群
E
eoLinker API Management 已提交
13 14 15 16 17 18 19
func Init(cs []*entity.ClusterInfo) {
	nt := make(map[string]*entity.ClusterInfo)
	it := make(map[int]*entity.ClusterInfo)
	ns := make([]*entity.Cluster, 0, len(cs))
	cis := make([]*entity.ClusterInfo, 0, len(cs))
	for _, c := range cs {
		nt[c.Name] = c
Y
Your Name 已提交
20
		it[c.ID] = c
E
eoLinker API Management 已提交
21 22 23 24 25 26
		ns = append(ns, c.Cluster())
		cis = append(cis, c)
	}

	byName = nt

Y
Your Name 已提交
27
	byID = it
E
eoLinker API Management 已提交
28 29 30 31 32

	clusterInfos = cis
	nodes = ns

}
Y
Your Name 已提交
33 34

//Get 根据集群名获取集群信息
E
eoLinker API Management 已提交
35 36 37 38 39 40
func Get(name string) (*entity.ClusterInfo, bool) {

	v, has := byName[name]
	return v, has
}

Y
Your Name 已提交
41
//GetAll 获取所有集群信息
E
eoLinker API Management 已提交
42 43 44 45
func GetAll() []*entity.ClusterInfo {
	return clusterInfos
}

Y
Your Name 已提交
46
//GetList 获取集群列表
E
eoLinker API Management 已提交
47 48 49 50
func GetList() []*entity.Cluster {
	return nodes
}

Y
Your Name 已提交
51 52
//GetID 通过集群名获取集群ID
func GetID(name string) (id int, has bool) {
E
eoLinker API Management 已提交
53 54 55 56
	v, h := byName[name]
	if !h {
		return 0, false
	}
Y
Your Name 已提交
57
	return v.ID, true
E
eoLinker API Management 已提交
58 59
}

Y
Your Name 已提交
60
//GetClusterCount 获取集群数量
E
eoLinker API Management 已提交
61 62 63 64
func GetClusterCount() int {
	return len(nodes)
}

Y
Your Name 已提交
65 66 67
//GetByID 通过ID获取集群信息
func GetByID(id int) (*entity.ClusterInfo, bool) {
	v, h := byID[id]
E
eoLinker API Management 已提交
68 69
	return v, h
}