-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodes.go
62 lines (56 loc) · 1.78 KB
/
nodes.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
package logic
import (
"context"
"time"
"github.com/Bilibili/discovery/naming"
pb "github.com/youminxue/euler/api/logic/grpc"
"github.com/youminxue/euler/internal/logic/model"
log "github.com/golang/glog"
)
// NodesInstances get servers info.
func (l *Logic) NodesInstances(c context.Context) (res []*naming.Instance) {
return l.nodes
}
// NodesWeighted get node list.
func (l *Logic) NodesWeighted(c context.Context, platform, clientIP string) *pb.NodesReply {
reply := &pb.NodesReply{
Domain: l.c.Node.DefaultDomain,
TcpPort: int32(l.c.Node.TCPPort),
WsPort: int32(l.c.Node.WSPort),
WssPort: int32(l.c.Node.WSSPort),
Heartbeat: int32(time.Duration(l.c.Node.Heartbeat) / time.Second),
HeartbeatMax: int32(l.c.Node.HeartbeatMax),
Backoff: &pb.Backoff{
MaxDelay: l.c.Backoff.MaxDelay,
BaseDelay: l.c.Backoff.BaseDelay,
Factor: l.c.Backoff.Factor,
Jitter: l.c.Backoff.Jitter,
},
}
domains, addrs := l.nodeAddrs(c, clientIP)
if platform == model.PlatformWeb {
reply.Nodes = domains
} else {
reply.Nodes = addrs
}
if len(reply.Nodes) == 0 {
reply.Nodes = []string{l.c.Node.DefaultDomain}
}
return reply
}
func (l *Logic) nodeAddrs(c context.Context, clientIP string) (domains, addrs []string) {
var (
region string
)
province, err := l.location(c, clientIP)
if err == nil {
region = l.regions[province]
}
log.Infof("nodeAddrs clientIP:%s region:%s province:%s domains:%v addrs:%v", clientIP, region, province, domains, addrs)
return l.loadBalancer.NodeAddrs(region, l.c.Node.HostDomain, l.c.Node.RegionWeight)
}
// location find a geolocation of an IP address including province, region and country.
func (l *Logic) location(c context.Context, clientIP string) (province string, err error) {
// province: config mapping
return
}