-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdns_service_discovery.go
71 lines (63 loc) · 1.7 KB
/
dns_service_discovery.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
63
64
65
66
67
68
69
70
71
package leaderless_key_value_store
import (
"context"
"fmt"
"log"
"net"
"time"
"github.com/46bit/leaderless-key-value-store/api"
"google.golang.org/grpc"
)
// FIXME: Add TTL-style system to remove expired records
func PerformDnsServiceDiscovery(ctx context.Context, config *DnsServiceDiscoveryConfig, clusterDesc *ClusterDescription) {
ticker := time.NewTicker(config.UpdateInterval)
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
}
ips, err := net.LookupIP(config.StorageNodeDomain)
if err != nil {
// FIXME: Improve errors
log.Println(err)
}
for _, ip := range ips {
address := fmt.Sprintf("%s:%d", ip.String(), config.StorageNodePort)
go func() {
healthCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
health, err := getHealthFromNode(healthCtx, address)
if err != nil {
log.Println(err)
}
clusterDesc.Lock()
defer clusterDesc.Unlock()
storageNode, ok := clusterDesc.StorageNodes[health.NodeId]
if !ok {
log.Println(fmt.Errorf("got DNS for unknown Storage Node with ID: '%s'", health.NodeId))
return
}
storageNode.Address = &address
err = clusterDesc.ConnManager.Add(ctx, address, true)
if err != nil {
fmt.Println(err)
}
}()
}
}
}
func getHealthFromNode(ctx context.Context, address string) (*api.HealthResponse, error) {
conn, err := grpc.DialContext(
ctx,
address,
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(64<<20), grpc.MaxCallSendMsgSize(64<<20)),
grpc.WithInsecure(),
grpc.WithBlock(),
)
if err != nil {
return nil, fmt.Errorf("did not connect: %w", err)
}
defer conn.Close()
return api.NewNodeClient(conn).Health(ctx, &api.HealthRequest{})
}