-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbroadcasts.go
90 lines (77 loc) · 1.65 KB
/
broadcasts.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
package GoPlus
import (
"fmt"
"time"
)
type Broker struct {
stopCh chan struct{}
publishCh chan interface{}
subCh chan chan interface{}
unsubCh chan chan interface{}
}
func NewBroadcast() *Broker {
return &Broker{
stopCh: make(chan struct{}),
publishCh: make(chan interface{}, 10),
subCh: make(chan chan interface{}, 1),
unsubCh: make(chan chan interface{}, 1),
}
}
func (broker *Broker) Start() {
subs := map[chan interface{}]struct{}{}
for {
select {
case <-broker.stopCh:
return
case msgCh := <-broker.subCh:
subs[msgCh] = struct{}{}
case msgCh := <-broker.unsubCh:
delete(subs, msgCh)
case msg := <-broker.publishCh:
for msgCh := range subs {
// msgCh is buffered, use non-blocking send to protect the broker:
select {
case msgCh <- msg:
default:
}
}
}
}
}
func (broker *Broker) Stop() {
close(broker.stopCh)
}
func (broker *Broker) Subscribe() chan interface{} {
msgCh := make(chan interface{}, 10)
broker.subCh <- msgCh
return msgCh
}
func (broker *Broker) Unsubscribe(msgCh chan interface{}) {
broker.unsubCh <- msgCh
}
func (broker *Broker) Publish(msg interface{}) {
broker.publishCh <- msg
}
func example() {
// Create and start a broker:
b := NewBroadcast()
go b.Start()
// Create and subscribe 3 clients:
clientFunc := func(id int) {
msgCh := b.Subscribe()
for {
fmt.Printf("Client %d got message: %v\n", id, <-msgCh)
}
}
for i := 0; i < 3; i++ {
go clientFunc(i)
}
// Start publishing messages:
go func() {
for msgId := 0; ; msgId++ {
b.Publish(fmt.Sprintf("msg#%d", msgId))
time.Sleep(300 * time.Millisecond)
}
}()
time.Sleep(time.Second)
}