-
Notifications
You must be signed in to change notification settings - Fork 1
/
syncpub.go
57 lines (47 loc) · 1.35 KB
/
syncpub.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
//
// Synchronized publisher.
//
// This diverts from the C example by introducing time delays.
// Without these delays, the subscribers won't catch the END message.
//
package main
import (
zmq "github.com/pebbe/zmq3"
"fmt"
"time"
)
const (
// We wait for 10 subscribers
SUBSCRIBERS_EXPECTED = 10
)
func main() {
// Socket to talk to clients
publisher, _ := zmq.NewSocket(zmq.PUB)
defer publisher.Close()
publisher.Bind("tcp://*:5561")
// Socket to receive signals
syncservice, _ := zmq.NewSocket(zmq.REP)
defer syncservice.Close()
syncservice.Bind("tcp://*:5562")
// Get synchronization from subscribers
fmt.Println("Waiting for subscribers")
for subscribers := 0; subscribers < SUBSCRIBERS_EXPECTED; subscribers++ {
// - wait for synchronization request
syncservice.Recv(0)
// - send synchronization reply
syncservice.Send("", 0)
}
// Now broadcast exactly 1M updates followed by END
fmt.Println("Broadcasting messages")
for update_nbr := 0; update_nbr < 1000000; update_nbr++ {
publisher.Send("Rhubarb", 0)
// subscribers don't get all messages if publisher is too fast
// a one microsecond pause may still be too short
time.Sleep(time.Microsecond)
}
// a longer pause ensures subscribers are ready to receive this
time.Sleep(time.Second)
publisher.Send("END", 0)
// what's another second?
time.Sleep(time.Second)
}