Newer
Older
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
package redislib
import (
"context"
"sync"
"github.com/redis/go-redis/v9"
"github.com/turt2live/matrix-media-repo/common/rcontext"
)
var subscribeMutex = new(sync.Mutex)
var subscribeChans = make(map[string][]chan string)
type PubSubValue struct {
Err error
Str string
}
func Publish(ctx rcontext.RequestContext, channel string, payload string) error {
makeConnection()
if ring == nil {
return nil
}
if ring.PoolStats().TotalConns == 0 {
ctx.Log.Warn("Not broadcasting upload to Redis - no connections available")
return nil
}
r := ring.Publish(ctx.Context, channel, payload)
if r.Err() != nil {
if r.Err() == redis.Nil {
ctx.Log.Warn("Not broadcasting upload to Redis - no connections available")
return nil
}
return r.Err()
}
return nil
}
func Subscribe(channel string) <-chan string {
makeConnection()
if ring == nil {
return nil
}
ch := make(chan string)
subscribeMutex.Lock()
if _, ok := subscribeChans[channel]; !ok {
subscribeChans[channel] = make([]chan string, 0)
}
subscribeChans[channel] = append(subscribeChans[channel], ch)
subscribeMutex.Unlock()
doSubscribe(channel, ch)
return ch
}
func doSubscribe(channel string, ch chan<- string) {
sub := ring.Subscribe(context.Background(), channel)
go func(ch chan<- string) {
recvCh := sub.Channel()
for {
val := <-recvCh
if val != nil {
ch <- val.Payload
} else {
break
}
}
}(ch)
}
func resubscribeAll() {
subscribeMutex.Lock()
defer subscribeMutex.Unlock()
for channel, chs := range subscribeChans {
for _, ch := range chs {
if ring == nil {
close(ch)
} else {
doSubscribe(channel, ch)
}
}
}
if ring == nil {
subscribeChans = make(map[string][]chan string)
}
}