This repository has been archived by the owner on Jan 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrouter_client.go
151 lines (118 loc) · 3.37 KB
/
router_client.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package gibson
import (
"encoding/json"
"log"
"sync"
"time"
"github.com/apcera/nats"
"github.com/cloudfoundry/yagnats"
"github.com/nu7hatch/gouuid"
)
type RouterClient interface {
Greet() error
Register(port int, uri string) error
RegisterAll(port int, uris []string) error
Unregister(port int, uri string) error
}
type CFRouterClient struct {
Host string
PrivateInstanceId string
messageBus yagnats.NATSConn
registry *Registry
stopPeriodicCallback chan bool
lock sync.RWMutex
}
type RegistryMessage struct {
URIs []string `json:"uris"`
Host string `json:"host"`
Port int `json:"port"`
PrivateInstanceId string `json:"private_instance_id"`
}
type RouterGreetingMessage struct {
MinimumRegisterInterval int `json:"minimumRegisterIntervalInSeconds"`
}
func NewCFRouterClient(host string, messageBus yagnats.NATSConn) *CFRouterClient {
u4, err := uuid.NewV4()
if err != nil {
log.Printf("failed to create UUID as private instance id: %s\n", err)
}
return &CFRouterClient{
Host: host,
PrivateInstanceId: u4.String(),
registry: NewRegistry(),
messageBus: messageBus,
}
}
func (r *CFRouterClient) Greet() error {
_, err := r.messageBus.Subscribe("router.start", r.handleGreeting)
if err != nil {
return err
}
uuid, err := uuid.NewV4()
if err != nil {
return err
}
replyTo := uuid.String()
r.messageBus.Subscribe(replyTo, r.handleGreeting)
return r.messageBus.PublishRequest("router.greet", replyTo, []byte{})
}
func (r *CFRouterClient) Register(port int, uri string) error {
r.registry.Register(port, uri)
return r.sendRegistryMessage("router.register", port, []string{uri})
}
func (r *CFRouterClient) Unregister(port int, uri string) error {
r.registry.Unregister(port, uri)
return r.sendRegistryMessage("router.unregister", port, []string{uri})
}
func (r *CFRouterClient) RegisterAll(port int, uris []string) error {
r.registry.RegisterAll(port, uris)
return r.sendRegistryMessage("router.register", port, uris)
}
func (r *CFRouterClient) handleGreeting(greeting *nats.Msg) {
interval, err := r.intervalFrom(greeting.Data)
if err != nil {
log.Printf("failed to parse router.start: %s\n", err)
return
}
go r.callbackPeriodically(time.Duration(interval) * time.Second)
}
func (r *CFRouterClient) callbackPeriodically(interval time.Duration) {
if r.stopPeriodicCallback != nil {
r.stopPeriodicCallback <- true
}
cancel := make(chan bool)
r.stopPeriodicCallback = cancel
for stop := false; !stop; {
select {
case <-time.After(interval):
r.registerAllRoutes()
case stop = <-cancel:
}
}
}
func (r *CFRouterClient) sendRegistryMessage(subject string, port int, uris []string) error {
msg := &RegistryMessage{
URIs: uris,
Host: r.Host,
Port: port,
PrivateInstanceId: r.PrivateInstanceId,
}
json, err := json.Marshal(msg)
if err != nil {
return err
}
return r.messageBus.Publish(subject, json)
}
func (r *CFRouterClient) intervalFrom(greetingPayload []byte) (int, error) {
var greeting RouterGreetingMessage
err := json.Unmarshal(greetingPayload, &greeting)
if err != nil {
return 0, err
}
return greeting.MinimumRegisterInterval, nil
}
func (r *CFRouterClient) registerAllRoutes() {
r.registry.InParallel(func(port int, uris []string) {
r.sendRegistryMessage("router.register", port, uris)
})
}