-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.go
46 lines (39 loc) · 1003 Bytes
/
config.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
package main
import (
"context"
"os"
"strings"
masterpb "github.com/ibalajiarun/go-consensus/cmd/master/masterpb"
"github.com/ibalajiarun/go-consensus/peer/peerpb"
"google.golang.org/grpc"
)
func getConfigFromMaster(maddr string) (*masterpb.ClientResponse, []int, error) {
conn, err := grpc.Dial(maddr, grpc.WithInsecure())
if err != nil {
return nil, nil, err
}
cli := masterpb.NewServiceDiscoveryClient(conn)
hostname := os.Getenv("HOSTNAME")
region := hostname
pieces := strings.Split(hostname, "-")
if len(pieces) == 4 {
region = pieces[1]
}
nid := &peerpb.BasicPeerInfo{
PodName: os.Getenv("PODNAME"),
HostMachine: hostname,
PodIP: os.Getenv("PODIP"),
Region: region,
}
result, err := cli.GetServiceInfo(context.Background(), nid)
if err != nil {
return nil, nil, err
}
var localIDs []int
for _, s := range result.Config.Nodes {
if s.Region == region {
localIDs = append(localIDs, int(s.PeerID))
}
}
return result, localIDs, nil
}