-
Notifications
You must be signed in to change notification settings - Fork 0
/
broker.go
385 lines (325 loc) · 10.4 KB
/
broker.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package pigato
import (
"fmt"
zmq "github.com/pebbe/zmq4"
pgtlib "github.com/prdn/pigato-go/lib"
"log"
"runtime"
"time"
)
const (
// We'd normally pull these from config data
HEARTBEAT_LIVENESS = 3 // 3-5 is reasonable
HEARTBEAT_INTERVAL = 2500 * time.Millisecond // msecs
HEARTBEAT_EXPIRY = HEARTBEAT_INTERVAL * HEARTBEAT_LIVENESS
)
// The broker class defines a single broker instance:
type Broker struct {
socket *zmq.Socket // Socket for clients & workers
verbose bool // Print activity to stdout
endpoint string // Broker binds to this endpoint
services map[string]*Service // Hash of known services
workers map[string]*Worker // Hash of known workers
waiting []*Worker // List of waiting workers
heartbeat_at time.Time // When to send HEARTBEAT
}
// The service class defines a single service instance:
type Service struct {
broker *Broker // Broker instance
name string // Service name
requests [][]string // List of client requests
waiting []*Worker // List of waiting workers
}
// The worker class defines a single worker, idle or active:
type Worker struct {
broker *Broker // Broker instance
id_string string // Identity of worker as string
identity string // Identity frame for routing
service *Service // Owning service, if known
expiry time.Time // Expires at unless heartbeat
}
// Here are the constructor and destructor for the broker:
func NewBroker(verbose bool) (broker *Broker, err error) {
// Initialize broker state
broker = &Broker{
verbose: verbose,
services: make(map[string]*Service),
workers: make(map[string]*Worker),
waiting: make([]*Worker, 0),
heartbeat_at: time.Now().Add(HEARTBEAT_INTERVAL),
}
broker.socket, err = zmq.NewSocket(zmq.ROUTER)
// broker.socket.SetRcvhwm(500000) // or example mdclient2 won't work
runtime.SetFinalizer(broker, (*Broker).Close)
return
}
func (broker *Broker) Close() (err error) {
if broker.socket != nil {
err = broker.socket.Close()
broker.socket = nil
}
return
}
// The bind method binds the broker instance to an endpoint. We can call
// this multiple times. Note that MDP uses a single socket for both clients
// and workers:
func (broker *Broker) Bind(endpoint string) (err error) {
err = broker.socket.Bind(endpoint)
if err != nil {
log.Println("E: MDP broker/0.2.0 failed to bind at", endpoint)
return
}
log.Println("I: MDP broker/0.2.0 is active at", endpoint)
return
}
// The WorkerMsg method processes one READY, REPLY, HEARTBEAT or
// DISCONNECT message sent to the broker by a worker:
func (broker *Broker) WorkerMsg(sender string, msg []string) {
// At least, command
if len(msg) == 0 {
panic("len(msg) == 0")
}
command, msg := pgtlib.PopStr(msg)
id_string := fmt.Sprintf("%q", sender)
_, worker_ready := broker.workers[id_string]
worker := broker.WorkerRequire(sender)
switch command {
case pgtlib.W_READY:
if worker_ready { // Not first command in session
worker.Delete(true)
} else if len(sender) >= 4 /* Reserved service name */ && sender[:4] == "mmi." {
worker.Delete(true)
} else {
// Attach worker to service and mark as idle
worker.service = broker.ServiceRequire(msg[0])
worker.Waiting()
}
case pgtlib.W_REPLY:
if worker_ready {
// Remove & save client return envelope and insert the
// protocol header and service name, then rewrap envelope.
broker.socket.SendMessage(msg[0], pgtlib.C_CLIENT, command, "", msg[2], msg[3], msg[4])
worker.Waiting()
} else {
worker.Delete(true)
}
case pgtlib.W_HEARTBEAT:
if worker_ready {
worker.expiry = time.Now().Add(HEARTBEAT_EXPIRY)
} else {
worker.Delete(true)
}
case pgtlib.W_DISCONNECT:
worker.Delete(false)
default:
log.Printf("E: invalid input message %q\n", msg)
}
}
// Process a request coming from a client. We implement MMI requests
// directly here (at present, we implement only the mmi.service request):
func (broker *Broker) ClientMsg(sender string, msg []string) {
// Service name + body
if len(msg) < 2 {
// heartbeat
return
}
_, msg = pgtlib.PopStr(msg)
service_frame, msg := pgtlib.PopStr(msg)
service := broker.ServiceRequire(service_frame)
// Set reply return identity to client sender
m := []string{sender, ""}
msg = append(m, msg...)
// If we got a MMI service request, process that internally
if len(service_frame) >= 4 && service_frame[:4] == "mmi." {
var return_code string
if service_frame == "mmi.service" {
name := msg[len(msg)-1]
service, ok := broker.services[name]
if ok && len(service.waiting) > 0 {
return_code = "200"
} else {
return_code = "404"
}
} else {
return_code = "501"
}
msg[len(msg)-1] = return_code
// Remove & save client return envelope and insert the
// protocol header and service name, then rewrap envelope.
broker.socket.SendMessage("", "", pgtlib.C_CLIENT, service_frame, msg)
} else {
// Else dispatch the message to the requested service
service.Dispatch(msg)
}
}
// The purge method deletes any idle workers that haven't pinged us in a
// while. We hold workers from oldest to most recent, so we can stop
// scanning whenever we find a live worker. This means we'll mainly stop
// at the first worker, which is essential when we have large numbers of
// workers (since we call this method in our critical path):
func (broker *Broker) Purge() {
now := time.Now()
for len(broker.waiting) > 0 {
if broker.waiting[0].expiry.After(now) {
break // Worker is alive, we're done here
}
if broker.verbose {
log.Println("I: deleting expired worker:", broker.waiting[0].id_string)
}
broker.waiting[0].Delete(false)
}
}
// Here is the implementation of the methods that work on a service:
// Lazy constructor that locates a service by name, or creates a new
// service if there is no service already with that name.
func (broker *Broker) ServiceRequire(service_frame string) (service *Service) {
name := service_frame
service, ok := broker.services[name]
if !ok {
service = &Service{
broker: broker,
name: name,
requests: make([][]string, 0),
waiting: make([]*Worker, 0),
}
broker.services[name] = service
if broker.verbose {
log.Println("I: added service:", name)
}
}
return
}
// The dispatch method sends requests to waiting workers:
func (service *Service) Dispatch(msg []string) {
if len(msg) > 0 {
// Queue message if any
service.requests = append(service.requests, msg)
}
service.broker.Purge()
for len(service.waiting) > 0 && len(service.requests) > 0 {
var worker *Worker
worker, service.waiting = popWorker(service.waiting)
service.broker.waiting = delWorker(service.broker.waiting, worker)
msg, service.requests = pgtlib.PopMsg(service.requests)
wm := make([]string, 6)
wm[0] = msg[0]
wm[1] = service.name
wm[2] = ""
wm[3] = msg[2]
wm[4] = msg[3]
wm[5] = msg[4]
worker.Send(pgtlib.W_REQUEST, wm)
}
}
// Here is the implementation of the methods that work on a worker:
// Lazy constructor that locates a worker by identity, or creates a new
// worker if there is no worker already with that identity.
func (broker *Broker) WorkerRequire(identity string) (worker *Worker) {
// broker.workers is keyed off worker identity
id_string := fmt.Sprintf("%q", identity)
worker, ok := broker.workers[id_string]
if !ok {
worker = &Worker{
broker: broker,
id_string: id_string,
identity: identity,
}
broker.workers[id_string] = worker
if broker.verbose {
log.Printf("I: registering new worker: %s\n", id_string)
}
}
return
}
// The delete method deletes the current worker.
func (worker *Worker) Delete(disconnect bool) {
if disconnect {
worker.Send(pgtlib.W_DISCONNECT, []string{})
}
if worker.service != nil {
worker.service.waiting = delWorker(worker.service.waiting, worker)
}
worker.broker.waiting = delWorker(worker.broker.waiting, worker)
delete(worker.broker.workers, worker.id_string)
}
// The send method formats and sends a command to a worker. The caller may
// also provide a command option, and a message payload:
func (worker *Worker) Send(command string, msg []string) (err error) {
n := 3
m := make([]string, n, n+len(msg))
m = append(m, msg...)
// Stack protocol envelope to start of message
m[2] = command
m[1] = pgtlib.W_WORKER
// Stack routing envelope to start of message
m[0] = worker.identity
if worker.broker.verbose {
log.Printf("I: sending %d to worker %q\n", command, m)
}
_, err = worker.broker.socket.SendMessage(m)
return
}
// This worker is now waiting for work
func (worker *Worker) Waiting() {
// Queue to broker and service waiting lists
worker.broker.waiting = append(worker.broker.waiting, worker)
worker.service.waiting = append(worker.service.waiting, worker)
worker.expiry = time.Now().Add(HEARTBEAT_EXPIRY)
worker.service.Dispatch([]string{})
}
func popWorker(workers []*Worker) (worker *Worker, workers2 []*Worker) {
worker = workers[0]
workers2 = workers[1:]
return
}
func delWorker(workers []*Worker, worker *Worker) []*Worker {
for i := 0; i < len(workers); i++ {
if workers[i] == worker {
workers = append(workers[:i], workers[i+1:]...)
i--
}
}
return workers
}
func BrokerStart(verbose bool) {
broker, _ := NewBroker(verbose)
broker.Bind("tcp://*:55555")
poller := zmq.NewPoller()
poller.Add(broker.socket, zmq.POLLIN)
// Get and process messages forever or until interrupted
for {
polled, err := poller.Poll(HEARTBEAT_INTERVAL)
if err != nil {
break // Interrupted
}
// Process next input message, if any
if len(polled) > 0 {
msg, err := broker.socket.RecvMessage(0)
if err != nil {
break // Interrupted
}
if broker.verbose {
log.Printf("I: received message: %q\n", msg)
}
sender, msg := pgtlib.PopStr(msg)
header, msg := pgtlib.PopStr(msg)
switch header {
case pgtlib.C_CLIENT:
broker.ClientMsg(sender, msg)
case pgtlib.W_WORKER:
broker.WorkerMsg(sender, msg)
default:
log.Printf("E: invalid message (sender=%s, header=%s): %q\n", sender, header, msg)
}
}
// Disconnect and delete any expired workers
// Send heartbeats to idle workers if needed
if time.Now().After(broker.heartbeat_at) {
broker.Purge()
for _, worker := range broker.waiting {
worker.Send(pgtlib.W_HEARTBEAT, []string{})
}
broker.heartbeat_at = time.Now().Add(HEARTBEAT_INTERVAL)
}
}
}