forked from wangaoone/redeo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pubsub.go
131 lines (110 loc) · 2.6 KB
/
pubsub.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
package redeo
import (
"sync"
"sync/atomic"
//"github.com/bsm/redeo/resp"
"github.com/wangaoone/redeo/resp"
)
// PubSubBroker can be used to emulate redis'
// native pub/sub functionality
type PubSubBroker struct {
channels map[string]*pubSubChannel
mu sync.RWMutex
}
// NewPubSubBroker inits a new pub-sub broker
func NewPubSubBroker() *PubSubBroker {
return &PubSubBroker{
channels: make(map[string]*pubSubChannel),
}
}
// Subscribe returns a subscribe handler
func (b *PubSubBroker) Subscribe() Handler {
return HandlerFunc(func(w resp.ResponseWriter, c *resp.Command) {
if c.ArgN() != 1 {
w.AppendError(WrongNumberOfArgs(c.Name))
return
}
b.subscribe(c.Arg(0).String(), w)
})
}
// Publish acts as a publish handler
func (b *PubSubBroker) Publish() Handler {
return HandlerFunc(func(w resp.ResponseWriter, c *resp.Command) {
if c.ArgN() != 2 {
w.AppendError(WrongNumberOfArgs(c.Name))
return
}
n := b.PublishMessage(c.Arg(0).String(), c.Arg(1).String())
w.AppendInt(n)
})
}
// PublishMessage allows to publish a message to the broker
// outside the command-cycle. Returns the number of subscribers
func (b *PubSubBroker) PublishMessage(name, msg string) int64 {
b.mu.RLock()
ch, ok := b.channels[name]
b.mu.RUnlock()
if ok {
return ch.Publish(name, msg)
}
return 0
}
func (b *PubSubBroker) subscribe(name string, w resp.ResponseWriter) {
b.mu.RLock()
ch, ok := b.channels[name]
b.mu.RUnlock()
if !ok {
b.mu.Lock()
if ch, ok = b.channels[name]; !ok {
ch = &pubSubChannel{
subscribers: make(map[int64]resp.ResponseWriter),
}
b.channels[name] = ch
}
b.mu.Unlock()
}
ch.Subscribe(w)
w.AppendArrayLen(3)
w.AppendBulkString("subscribe")
w.AppendBulkString(name)
w.AppendInt(1)
}
// --------------------------------------------------------------------
type pubSubChannel struct {
subscribers map[int64]resp.ResponseWriter
mu sync.RWMutex
nextID int64
}
func (c *pubSubChannel) Subscribe(w resp.ResponseWriter) {
sid := atomic.AddInt64(&c.nextID, 1)
c.mu.Lock()
c.subscribers[sid] = w
c.mu.Unlock()
}
func (c *pubSubChannel) Publish(name, msg string) (n int64) {
var failed []int64
c.mu.RLock()
for sid, w := range c.subscribers {
w.AppendArrayLen(3)
w.AppendBulkString("message")
w.AppendBulkString(name)
w.AppendBulkString(msg)
if err := w.Flush(); err != nil {
failed = append(failed, sid)
} else {
n++
}
}
c.mu.RUnlock()
if len(failed) != 0 {
c.evict(failed)
}
return
}
func (c *pubSubChannel) evict(failed []int64) {
c.mu.Lock()
for _, sid := range failed {
delete(c.subscribers, sid)
}
c.mu.Unlock()
}