forked from dahernan/gopherdiscovery
/
server.go
234 lines (192 loc) · 4.59 KB
/
server.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package gopherdiscovery
import (
"log"
"strings"
"time"
"golang.org/x/net/context"
"github.com/gdamore/mangos"
"github.com/gdamore/mangos/protocol/pub"
"github.com/gdamore/mangos/protocol/surveyor"
"github.com/gdamore/mangos/transport/ipc"
"github.com/gdamore/mangos/transport/tcp"
)
type Options struct {
// SurveyTime is used to indicate the deadline for survey
// responses
SurveyTime time.Duration
// RecvDeadline is the time until the next recived of the SURVEY times out.
RecvDeadline time.Duration
// PollTime is minimal time between SURVEYS (The time between SURVEYS could be greater than this time
// if the SURVEY process takes longer than that time)
PollTime time.Duration
}
type DiscoveryServer struct {
// url for the survey heartbeat
// for example tcp://127.0.0.1:40007
urlServer string
// url for the Pub/Sub
// in this url you are going to get the changes on the set of nodes
// for example tcp://127.0.0.1:50007
urlPubSub string
// Time options
opt Options
// Set of the services that has been discovered
services *Services
ctx context.Context
cancel context.CancelFunc
sock mangos.Socket
}
type Services struct {
// set of nodes discovered
nodes StringSet
// publisher, we are going to publish the changes of the set here
publisher *Publisher
}
type Publisher struct {
// url for pub/sub
url string
ctx context.Context
sock mangos.Socket
publishCh chan []string
}
func Server(urlServer string, urlPubSub string, opt Options) (*DiscoveryServer, error) {
var sock mangos.Socket
var err error
var publisher *Publisher
ctx, cancel := context.WithCancel(context.Background())
sock, err = surveyor.NewSocket()
if err != nil {
return nil, err
}
sock.AddTransport(ipc.NewTransport())
sock.AddTransport(tcp.NewTransport())
err = sock.Listen(urlServer)
if err != nil {
return nil, err
}
err = sock.SetOption(mangos.OptionSurveyTime, opt.SurveyTime)
if err != nil {
return nil, err
}
err = sock.SetOption(mangos.OptionRecvDeadline, opt.RecvDeadline)
if err != nil {
return nil, err
}
pubCtx, pubCancel := context.WithCancel(ctx)
publisher, err = NewPublisher(pubCtx, urlPubSub)
if err != nil {
pubCancel()
return nil, err
}
services := NewServices(publisher)
server := &DiscoveryServer{
services: services,
urlServer: urlServer,
urlPubSub: urlPubSub,
opt: opt,
ctx: ctx,
cancel: cancel,
sock: sock,
}
go server.run()
return server, nil
}
// Shutdown the server
func (d *DiscoveryServer) Cancel() {
d.cancel()
}
// Waits until the server finish running
func (d *DiscoveryServer) Wait() {
<-d.ctx.Done()
}
func (d *DiscoveryServer) run() {
for {
select {
case <-time.After(d.opt.PollTime):
d.poll()
case <-d.ctx.Done():
return
}
}
}
func (d *DiscoveryServer) poll() {
var err error
var msg []byte
var responses StringSet
err = d.sock.Send([]byte(""))
if err != nil {
log.Println("DiscoveryServer: Error sending the SURVEY", err.Error())
return
}
responses = NewStringSet()
for {
msg, err = d.sock.Recv()
if err != nil {
if err == mangos.ErrRecvTimeout {
// Timeout means I can add the current responses to the SET
d.services.Add(responses)
return
}
log.Println("DiscoveryServer: Error reading SURVEY responses", err.Error())
} else {
responses.Add(string(msg))
}
}
}
func NewPublisher(ctx context.Context, url string) (*Publisher, error) {
var sock mangos.Socket
var err error
sock, err = pub.NewSocket()
if err != nil {
return nil, err
}
sock.AddTransport(ipc.NewTransport())
sock.AddTransport(tcp.NewTransport())
err = sock.Listen(url)
if err != nil {
return nil, err
}
publiser := &Publisher{
ctx: ctx,
url: url,
sock: sock,
publishCh: make(chan []string),
}
go publiser.run()
return publiser, nil
}
func (p *Publisher) Publish(msg []string) {
p.publishCh <- msg
}
func (p *Publisher) run() {
for {
select {
case <-p.ctx.Done():
close(p.publishCh)
return
case msg := <-p.publishCh:
err := p.sock.Send([]byte(strings.Join(msg, "|")))
if err != nil {
log.Println("DiscoveryServer: Error PUBLISHING changes to the socket", err.Error())
}
}
}
}
func NewServices(publisher *Publisher) *Services {
s := &Services{
nodes: NewStringSet(),
publisher: publisher,
}
return s
}
func (s *Services) Add(responses StringSet) {
removed := s.nodes.Difference(responses)
added := responses.Difference(s.nodes)
// Do not publish anything if there is no changes
if removed.Cardinality() == 0 && added.Cardinality() == 0 {
return
}
s.nodes = responses
// publish the changes
s.publisher.Publish(s.nodes.ToSlice())
}