Skip to content

Commit c005cf0

Browse files
committed
Add DB/password config for backends and ignore SELECT/AUTH commands
- Add PrimaryDB, PrimaryPassword, SecondaryDB, SecondaryPassword to ProxyConfig - Add DB and Password fields to BackendOptions, passed to go-redis client - Add CLI flags: -primary-db, -primary-password, -secondary-db, -secondary-password - Silently accept SELECT and AUTH commands (return OK without forwarding) since DB/auth are configured at the connection-pool level
1 parent 534a39b commit c005cf0

5 files changed

Lines changed: 46 additions & 14 deletions

File tree

cmd/redis-proxy/main.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ func run() error {
3232

3333
flag.StringVar(&cfg.ListenAddr, "listen", cfg.ListenAddr, "Proxy listen address")
3434
flag.StringVar(&cfg.PrimaryAddr, "primary", cfg.PrimaryAddr, "Primary (Redis) address")
35+
flag.IntVar(&cfg.PrimaryDB, "primary-db", cfg.PrimaryDB, "Primary Redis DB number")
36+
flag.StringVar(&cfg.PrimaryPassword, "primary-password", cfg.PrimaryPassword, "Primary Redis password")
3537
flag.StringVar(&cfg.SecondaryAddr, "secondary", cfg.SecondaryAddr, "Secondary (ElasticKV) address")
38+
flag.IntVar(&cfg.SecondaryDB, "secondary-db", cfg.SecondaryDB, "Secondary Redis DB number")
39+
flag.StringVar(&cfg.SecondaryPassword, "secondary-password", cfg.SecondaryPassword, "Secondary Redis password")
3640
flag.StringVar(&modeStr, "mode", "dual-write", "Proxy mode: redis-only, dual-write, dual-write-shadow, elastickv-primary, elastickv-only")
3741
flag.DurationVar(&cfg.SecondaryTimeout, "secondary-timeout", cfg.SecondaryTimeout, "Secondary write timeout")
3842
flag.DurationVar(&cfg.ShadowTimeout, "shadow-timeout", cfg.ShadowTimeout, "Shadow read timeout")
@@ -59,14 +63,21 @@ func run() error {
5963
metrics := proxy.NewProxyMetrics(reg)
6064

6165
// Backends
66+
primaryOpts := proxy.DefaultBackendOptions()
67+
primaryOpts.DB = cfg.PrimaryDB
68+
primaryOpts.Password = cfg.PrimaryPassword
69+
secondaryOpts := proxy.DefaultBackendOptions()
70+
secondaryOpts.DB = cfg.SecondaryDB
71+
secondaryOpts.Password = cfg.SecondaryPassword
72+
6273
var primary, secondary proxy.Backend
6374
switch cfg.Mode {
6475
case proxy.ModeElasticKVPrimary, proxy.ModeElasticKVOnly:
65-
primary = proxy.NewRedisBackend(cfg.SecondaryAddr, "elastickv")
66-
secondary = proxy.NewRedisBackend(cfg.PrimaryAddr, "redis")
76+
primary = proxy.NewRedisBackendWithOptions(cfg.SecondaryAddr, "elastickv", secondaryOpts)
77+
secondary = proxy.NewRedisBackendWithOptions(cfg.PrimaryAddr, "redis", primaryOpts)
6778
case proxy.ModeRedisOnly, proxy.ModeDualWrite, proxy.ModeDualWriteShadow:
68-
primary = proxy.NewRedisBackend(cfg.PrimaryAddr, "redis")
69-
secondary = proxy.NewRedisBackend(cfg.SecondaryAddr, "elastickv")
79+
primary = proxy.NewRedisBackendWithOptions(cfg.PrimaryAddr, "redis", primaryOpts)
80+
secondary = proxy.NewRedisBackendWithOptions(cfg.SecondaryAddr, "elastickv", secondaryOpts)
7081
}
7182
defer primary.Close()
7283
defer secondary.Close()

proxy/backend.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ type Backend interface {
2929

3030
// BackendOptions configures the underlying go-redis connection pool.
3131
type BackendOptions struct {
32+
DB int
33+
Password string
3234
PoolSize int
3335
DialTimeout time.Duration
3436
ReadTimeout time.Duration
@@ -67,6 +69,8 @@ func NewRedisBackendWithOptions(addr string, name string, opts BackendOptions) *
6769
return &RedisBackend{
6870
client: redis.NewClient(&redis.Options{
6971
Addr: addr,
72+
DB: opts.DB,
73+
Password: opts.Password,
7074
PoolSize: opts.PoolSize,
7175
DialTimeout: opts.DialTimeout,
7276
ReadTimeout: opts.ReadTimeout,

proxy/config.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,20 @@ func (m ProxyMode) String() string {
4949

5050
// ProxyConfig holds all configuration for the dual-write proxy.
5151
type ProxyConfig struct {
52-
ListenAddr string
53-
PrimaryAddr string
54-
SecondaryAddr string
55-
Mode ProxyMode
56-
SecondaryTimeout time.Duration
57-
ShadowTimeout time.Duration
58-
SentryDSN string
59-
SentryEnv string
60-
SentrySampleRate float64
61-
MetricsAddr string
52+
ListenAddr string
53+
PrimaryAddr string
54+
PrimaryDB int
55+
PrimaryPassword string
56+
SecondaryAddr string
57+
SecondaryDB int
58+
SecondaryPassword string
59+
Mode ProxyMode
60+
SecondaryTimeout time.Duration
61+
ShadowTimeout time.Duration
62+
SentryDSN string
63+
SentryEnv string
64+
SentrySampleRate float64
65+
MetricsAddr string
6266
}
6367

6468
// DefaultConfig returns a ProxyConfig with sensible defaults.

proxy/proxy.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,13 @@ func (p *ProxyServer) handleAdmin(conn redcon.Conn, args [][]byte) {
283283
return
284284
}
285285

286+
// SELECT and AUTH are handled at the connection-pool level via config.
287+
// Silently accept them so clients don't break.
288+
if name == "SELECT" || name == "AUTH" {
289+
conn.WriteString("OK")
290+
return
291+
}
292+
286293
resp, err := p.dual.Admin(context.Background(), args)
287294
writeResponse(conn, resp, err)
288295
}

proxy/pubsub.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,12 @@ func (s *pubsubSession) handleTxnInSession(name string, args [][]byte) bool {
263263

264264
// dispatchRegularCommand sends a non-transaction, non-special command to the backend.
265265
func (s *pubsubSession) dispatchRegularCommand(name string, args [][]byte) {
266+
// SELECT and AUTH are handled at the connection-pool level; accept silently.
267+
if name == "SELECT" || name == "AUTH" {
268+
s.writeString("OK")
269+
return
270+
}
271+
266272
cat := ClassifyCommand(name, args[1:])
267273
ctx := context.Background()
268274

0 commit comments

Comments
 (0)