-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinternal.go
More file actions
91 lines (73 loc) · 2.29 KB
/
internal.go
File metadata and controls
91 lines (73 loc) · 2.29 KB
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
package eureka
import (
"fmt"
"github.com/phpdragon/go-eureka-client/core"
"strings"
"unsafe"
)
func (client *Client) getActiveServiceIpPortByAppId(appId string) (map[int]map[int]string, error) {
id := strings.ToUpper(appId)
cache := client.activeServiceIpPortMap[id]
if nil != cache {
return client.activeServiceIpPortMap[id], nil
}
err := client.doRefreshByAppId(appId)
if nil != err {
return nil, err
}
return client.activeServiceIpPortMap[id], nil
}
func (client *Client) getActiveInstancesByAppId(appId string) (map[int]*core.Instance, error) {
id := strings.ToUpper(appId)
cache := client.activeInstanceMap[id]
if nil != cache {
return client.activeInstanceMap[id], nil
}
err := client.doRefreshByAppId(appId)
if nil != err {
return nil, err
}
return client.activeInstanceMap[id], nil
}
func (client *Client) doRefreshByAppId(appId string) error {
application, errr := client.apiClient.QueryAllInstanceByAppId(appId)
if errr != nil {
return errr
}
instances, urls := getActiveInstancesAndIpPorts(client.config.ClientConfig.FilterOnlyUpInstances, application.Instances)
client.mutex.Lock()
defer client.mutex.Unlock()
client.registryAppMap[appId] = application
client.activeInstanceMap[appId] = instances
client.activeServiceIpPortMap[appId] = urls
return nil
}
// 获取有效的实例和链接
func getActiveInstancesAndIpPorts(filterOnlyUpInstances bool, instances []core.Instance) (map[int]*core.Instance, map[int]map[int]string) {
instancesX := make(map[int]*core.Instance)
//
urls := make(map[int]map[int]string)
httpUrls := make(map[int]string)
httpsUrls := make(map[int]string)
instanceTotal := len(instances)
for i := 0; i < instanceTotal; i++ {
instance := instances[i]
if filterOnlyUpInstances && core.STATUS_UP != instance.Status {
continue
}
instancesX[i] = &instance
if "true" == instance.Port.Enabled {
httpUrls[i] = fmt.Sprintf("%s:%d", instance.IpAddr, instance.Port.Port)
}
if "false" == instance.SecurePort.Enabled {
httpsUrls[i] = fmt.Sprintf("%s:%d", instance.IpAddr, instance.SecurePort.Port)
}
}
urls[httpKey] = httpUrls
urls[httpsKey] = httpsUrls
return instancesX, urls
}
func (client *Client) getRandIndex(total int) int {
var index64 = client.autoIncr.Inc() % int64(total)
return *(*int)(unsafe.Pointer(&index64))
}