-
Notifications
You must be signed in to change notification settings - Fork 291
/
Copy pathredis.patch
126 lines (115 loc) · 4.34 KB
/
redis.patch
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
From f63a7449e5bde06c8da766ec1ea6f4fa9eee9dcd Mon Sep 17 00:00:00 2001
From: Aaron Dewes <aaron.dewes@protonmail.com>
Date: Tue, 2 Jan 2024 13:25:40 +0100
Subject: [PATCH] Support Redis Sentinel
Signed-off-by: Aaron Dewes <aaron.dewes@protonmail.com>
---
configuration/configuration.go | 12 +++++++
registry/handlers/app.go | 62 ++++++++++++++++++++++------------
2 files changed, 53 insertions(+), 21 deletions(-)
diff --git a/configuration/configuration.go b/configuration/configuration.go
index e4d4311d..f25aadf9 100644
--- a/configuration/configuration.go
+++ b/configuration/configuration.go
@@ -271,11 +271,23 @@ type FileChecker struct {
Threshold int `yaml:"threshold,omitempty"`
}
+type RedisSentinel struct {
+ // MasterName specifies the name of the master sentinel.
+ MasterName string `yaml:"masterName,omitempty"`
+
+ // Addresses specifies the addresses of the sentinels.
+ Addresses []string `yaml:"addresses,omitempty"`
+}
+
// Redis configures the redis pool available to the registry webapp.
type Redis struct {
// Addr specifies the the redis instance available to the application.
Addr string `yaml:"addr,omitempty"`
+ // Sentinel specifies the sentinel instance available to the application.
+ // If this is set, the Addr field is ignored.
+ Sentinel RedisSentinel `yaml:"sentinel,omitempty"`
+
// Usernames can be used as a finer-grained permission control since the introduction of the redis 6.0.
Username string `yaml:"username,omitempty"`
diff --git a/registry/handlers/app.go b/registry/handlers/app.go
index 2983176b..11716496 100644
--- a/registry/handlers/app.go
+++ b/registry/handlers/app.go
@@ -487,12 +487,12 @@ func (app *App) configureEvents(configuration *configuration.Configuration) {
}
func (app *App) configureRedis(cfg *configuration.Configuration) {
- if cfg.Redis.Addr == "" {
+ if cfg.Redis.Addr == "" && (len(cfg.Redis.Sentinel.Addresses) == 0 || cfg.Redis.Sentinel.MasterName == "") {
dcontext.GetLogger(app).Infof("redis not configured")
return
}
- app.redis = app.createPool(cfg.Redis)
+ app.redis = app.createRedisClient(cfg.Redis)
// Enable metrics instrumentation.
if err := redisotel.InstrumentMetrics(app.redis); err != nil {
@@ -514,25 +514,45 @@ func (app *App) configureRedis(cfg *configuration.Configuration) {
}))
}
-func (app *App) createPool(cfg configuration.Redis) *redis.Client {
- return redis.NewClient(&redis.Options{
- Addr: cfg.Addr,
- OnConnect: func(ctx context.Context, cn *redis.Conn) error {
- res := cn.Ping(ctx)
- return res.Err()
- },
- Username: cfg.Username,
- Password: cfg.Password,
- DB: cfg.DB,
- MaxRetries: 3,
- DialTimeout: cfg.DialTimeout,
- ReadTimeout: cfg.ReadTimeout,
- WriteTimeout: cfg.WriteTimeout,
- PoolFIFO: false,
- MaxIdleConns: cfg.Pool.MaxIdle,
- PoolSize: cfg.Pool.MaxActive,
- ConnMaxIdleTime: cfg.Pool.IdleTimeout,
- })
+func (app *App) createRedisClient(cfg configuration.Redis) *redis.Client {
+ // This function assumes that cfg.Addresses is not empty, which is checked by the caller.
+ if len(cfg.Sentinel.Addresses) > 0 && cfg.Sentinel.MasterName != "" {
+ return redis.NewFailoverClient(&redis.FailoverOptions{
+ MasterName: cfg.Sentinel.MasterName,
+ SentinelAddrs: cfg.Sentinel.Addresses,
+ OnConnect: nil,
+ Username: cfg.Username,
+ Password: cfg.Password,
+ DB: cfg.DB,
+ MaxRetries: 3,
+ DialTimeout: cfg.DialTimeout,
+ ReadTimeout: cfg.ReadTimeout,
+ WriteTimeout: cfg.WriteTimeout,
+ PoolFIFO: false,
+ MaxIdleConns: cfg.Pool.MaxIdle,
+ PoolSize: cfg.Pool.MaxActive,
+ ConnMaxIdleTime: cfg.Pool.IdleTimeout,
+ })
+ } else {
+ return redis.NewClient(&redis.Options{
+ Addr: cfg.Addr,
+ OnConnect: func(ctx context.Context, cn *redis.Conn) error {
+ res := cn.Ping(ctx)
+ return res.Err()
+ },
+ Username: cfg.Username,
+ Password: cfg.Password,
+ DB: cfg.DB,
+ MaxRetries: 3,
+ DialTimeout: cfg.DialTimeout,
+ ReadTimeout: cfg.ReadTimeout,
+ WriteTimeout: cfg.WriteTimeout,
+ PoolFIFO: false,
+ MaxIdleConns: cfg.Pool.MaxIdle,
+ PoolSize: cfg.Pool.MaxActive,
+ ConnMaxIdleTime: cfg.Pool.IdleTimeout,
+ })
+ }
}
// configureLogHook prepares logging hook parameters.
--
2.39.3 (Apple Git-146)