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
package redislib
import (
"sync"
"time"
"github.com/go-redsync/redsync/v4"
rsredis "github.com/go-redsync/redsync/v4/redis"
"github.com/go-redsync/redsync/v4/redis/goredis/v9"
"github.com/redis/go-redis/v9"
"github.com/turt2live/matrix-media-repo/common/config"
)
var connectionLock = &sync.Once{}
var ring *redis.Ring
var rs *redsync.Redsync
var pools = make([]rsredis.Pool, 0)
var clients = make([]*redis.Client, 0)
func makeConnection() {
if ring != nil {
return
}
connectionLock.Do(func() {
conf := config.Get().Redis
if !conf.Enabled {
return
}
addresses := make(map[string]string)
for _, c := range conf.Shards {
addresses[c.Name] = c.Address
client := redis.NewClient(&redis.Options{
DialTimeout: 10 * time.Second,
DB: conf.DbNum,
Addr: c.Address,
})
clients = append(clients, client)
pools = append(pools, goredis.NewPool(client))
}
ring = redis.NewRing(&redis.RingOptions{
Addrs: addresses,
DialTimeout: 10 * time.Second,
DB: conf.DbNum,
})
rs = redsync.New(pools...)
})
}
func Reconnect() {
Stop()
makeConnection()
}
func Stop() {
if ring != nil {
_ = ring.Close()
}
for _, c := range clients {
_ = c.Close()
}
ring = nil
rs = nil
pools = make([]rsredis.Pool, 0)
clients = make([]*redis.Client, 0)
connectionLock = &sync.Once{}
}