-
Notifications
You must be signed in to change notification settings - Fork 5
/
pubsub.go
214 lines (181 loc) · 5.07 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
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
package pubsub
import (
"context"
"encoding/json"
"fmt"
"log"
"path/filepath"
"sync"
"github.com/go-redis/redis/v8"
"github.com/golang/glog"
"github.com/livefir/fir/internal/dom"
)
// code modeled after https://github.com/purposeinplay/go-commons/blob/v0.6.2/pubsub/inmem/pubsub.go
// Subscription is a subscription to a channel.
type Subscription interface {
// C returns a receive-only go channel of patches published
C() <-chan []dom.Patch
// Close closes the subscription.
Close()
}
// Adapter is an interface for a pubsub adapter. It allows to publish and subscribe []dom.Patch to views.
type Adapter interface {
// Publish publishes a patchset to a channel.
Publish(ctx context.Context, channel string, patchset ...dom.Patch) error
// Subscribe subscribes to a channel.
Subscribe(ctx context.Context, channel string) (Subscription, error)
// HasSubscribers returns true if there are subscribers to the given pattern.
HasSubscribers(ctx context.Context, pattern string) bool
}
// NewInmem creates a new in-memory pubsub adapter.s
func NewInmem() Adapter {
return &pubsubInmem{
channelsSubscriptions: make(map[string]map[*subscriptionInmem]struct{}),
}
}
type subscriptionInmem struct {
channel string
ch chan []dom.Patch
once sync.Once
pubsub *pubsubInmem
}
// C returns a receive-only go channel of patches published
// on the channel this subscription is subscribed to.
func (s *subscriptionInmem) C() <-chan []dom.Patch {
return s.ch
}
func (s *subscriptionInmem) Close() {
s.pubsub.Lock()
defer s.pubsub.Unlock()
s.pubsub.removeSubscription(s)
}
type pubsubInmem struct {
channelsSubscriptions map[string]map[*subscriptionInmem]struct{}
sync.RWMutex
}
func (p *pubsubInmem) removeSubscription(subscription *subscriptionInmem) {
subscription.once.Do(func() {
close(subscription.ch)
})
subscriptions, ok := p.channelsSubscriptions[subscription.channel]
if !ok {
return
}
delete(subscriptions, subscription)
log.Printf("removed subscribtion for channel %s, count: %d", subscription.channel, len(subscriptions))
if len(subscriptions) == 0 {
delete(p.channelsSubscriptions, subscription.channel)
}
}
func (p *pubsubInmem) Publish(ctx context.Context, channel string, patchset ...dom.Patch) error {
p.Lock()
defer p.Unlock()
if channel == "" {
return fmt.Errorf("channel is empty")
}
subscriptions, ok := p.channelsSubscriptions[channel]
if !ok {
return fmt.Errorf("channel %s has no subscribers", channel)
}
if len(subscriptions) == 0 {
delete(p.channelsSubscriptions, channel)
return nil
}
for subscription := range subscriptions {
go func(sub *subscriptionInmem) { sub.ch <- patchset }(subscription)
}
return nil
}
func (p *pubsubInmem) Subscribe(ctx context.Context, channel string) (Subscription, error) {
p.Lock()
defer p.Unlock()
if channel == "" {
return nil, fmt.Errorf("channel is empty")
}
sub := &subscriptionInmem{
channel: channel,
ch: make(chan []dom.Patch),
pubsub: p,
}
subs, ok := p.channelsSubscriptions[channel]
if !ok {
subs = make(map[*subscriptionInmem]struct{})
p.channelsSubscriptions[channel] = subs
}
subs[sub] = struct{}{}
log.Printf("new subscribtion for channel %s, count: %d", channel, len(subs))
return sub, nil
}
func (p *pubsubInmem) HasSubscribers(ctx context.Context, pattern string) bool {
p.Lock()
defer p.Unlock()
count := 0
for channel := range p.channelsSubscriptions {
matched, err := filepath.Match(pattern, channel)
if err != nil {
continue
}
if matched {
count++
}
}
return count > 0
}
// NewRedis creates a new redis pubsub adapter.
func NewRedis(client *redis.Client) Adapter {
return &pubsubRedis{client: client}
}
type subscriptionRedis struct {
channel string
ch chan []dom.Patch
once sync.Once
pubsub *redis.PubSub
}
func (s *subscriptionRedis) C() <-chan []dom.Patch {
go func() {
for msg := range s.pubsub.Channel() {
var patchset []dom.Patch
err := json.Unmarshal([]byte(msg.Payload), &patchset)
if err != nil {
glog.Errorf("failed to unmarshal patches payload: %v", err)
continue
}
s.ch <- patchset
}
}()
return s.ch
}
func (s *subscriptionRedis) Close() {
s.pubsub.Close()
s.once.Do(func() {
close(s.ch)
})
}
type pubsubRedis struct {
client *redis.Client
}
func (p *pubsubRedis) Publish(ctx context.Context, channel string, patchset ...dom.Patch) error {
patchesBytes, err := json.Marshal(patchset)
if err != nil {
return err
}
return p.client.Publish(ctx, channel, patchesBytes).Err()
}
func (p *pubsubRedis) Subscribe(ctx context.Context, channel string) (Subscription, error) {
if channel == "" {
return nil, fmt.Errorf("channel is empty")
}
pubsub := p.client.Subscribe(ctx, channel)
return &subscriptionRedis{pubsub: pubsub, channel: channel, ch: make(chan []dom.Patch)}, nil
}
func (p *pubsubRedis) HasSubscribers(ctx context.Context, pattern string) bool {
channels, err := p.client.PubSubChannels(ctx, pattern).Result()
if err != nil {
glog.Errorf("error getting channels for pattern: %v : err, %v", pattern, err)
return false
}
if len(channels) == 0 {
return false
}
return true
}