forked from pebbe/zmq3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lvcache.go
69 lines (61 loc) · 1.52 KB
/
lvcache.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
//
// Last value cache
// Uses XPUB subscription messages to re-send data
//
package main
import (
zmq "github.com/pebbe/zmq3"
"fmt"
"time"
)
func main() {
frontend, _ := zmq.NewSocket(zmq.SUB)
frontend.Bind("tcp://*:5557")
backend, _ := zmq.NewSocket(zmq.XPUB)
backend.Bind("tcp://*:5558")
// Subscribe to every single topic from publisher
frontend.SetSubscribe("")
// Store last instance of each topic in a cache
cache := make(map[string]string)
// We route topic updates from frontend to backend, and
// we handle subscriptions by sending whatever we cached,
// if anything:
poller := zmq.NewPoller()
poller.Add(frontend, zmq.POLLIN)
poller.Add(backend, zmq.POLLIN)
LOOP:
for {
polled, err := poller.Poll(1000 * time.Millisecond)
if err != nil {
break // Interrupted
}
for _, item := range polled {
switch socket := item.Socket; socket {
case frontend:
// Any new topic data we cache and then forward
msg, err := frontend.RecvMessage(0)
if err != nil {
break LOOP
}
cache[msg[0]] = msg[1]
backend.SendMessage(msg)
case backend:
// When we get a new subscription we pull data from the cache:
msg, err := backend.RecvMessage(0)
if err != nil {
break LOOP
}
frame := msg[0]
// Event is one byte 0=unsub or 1=sub, followed by topic
if frame[0] == 1 {
topic := frame[1:]
fmt.Println("Sending cached topic", topic)
previous, ok := cache[topic]
if ok {
backend.SendMessage(topic, previous)
}
}
}
}
}
}