feat(home): add support for disabling cluster discovery in Redis configuration
This commit is contained in:
@@ -2,11 +2,12 @@ package config
|
||||
|
||||
// HomeConfig configures the optional "home" control plane integration over Redis protocol.
|
||||
type HomeConfig struct {
|
||||
Enabled bool `yaml:"enabled" json:"enabled"`
|
||||
Host string `yaml:"host" json:"-"`
|
||||
Port int `yaml:"port" json:"-"`
|
||||
Password string `yaml:"password" json:"-"`
|
||||
TLS HomeTLSConfig `yaml:"tls" json:"-"`
|
||||
Enabled bool `yaml:"enabled" json:"enabled"`
|
||||
Host string `yaml:"host" json:"-"`
|
||||
Port int `yaml:"port" json:"-"`
|
||||
Password string `yaml:"password" json:"-"`
|
||||
DisableClusterDiscovery bool `yaml:"disable-cluster-discovery" json:"-"`
|
||||
TLS HomeTLSConfig `yaml:"tls" json:"-"`
|
||||
}
|
||||
|
||||
// HomeTLSConfig configures client-side TLS for the home Redis connection.
|
||||
|
||||
@@ -9,6 +9,7 @@ home:
|
||||
host: home.example.com
|
||||
port: 444
|
||||
password: secret
|
||||
disable-cluster-discovery: true
|
||||
tls:
|
||||
enable: true
|
||||
server-name: home.example.com
|
||||
@@ -31,6 +32,9 @@ home:
|
||||
if cfg.Home.Password != "secret" {
|
||||
t.Fatalf("Home.Password = %q, want secret", cfg.Home.Password)
|
||||
}
|
||||
if !cfg.Home.DisableClusterDiscovery {
|
||||
t.Fatal("Home.DisableClusterDiscovery = false, want true")
|
||||
}
|
||||
if !cfg.Home.TLS.Enable {
|
||||
t.Fatal("Home.TLS.Enable = false, want true")
|
||||
}
|
||||
|
||||
@@ -265,7 +265,23 @@ func (c *Client) Ping(ctx context.Context) error {
|
||||
return cmd.Ping(ctx).Err()
|
||||
}
|
||||
|
||||
func (c *Client) clusterDiscoveryEnabled() bool {
|
||||
if c == nil {
|
||||
return false
|
||||
}
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
return c.clusterDiscoveryEnabledLocked()
|
||||
}
|
||||
|
||||
func (c *Client) clusterDiscoveryEnabledLocked() bool {
|
||||
return !c.homeCfg.DisableClusterDiscovery
|
||||
}
|
||||
|
||||
func (c *Client) refreshBestClusterNode(ctx context.Context) {
|
||||
if !c.clusterDiscoveryEnabled() {
|
||||
return
|
||||
}
|
||||
switched, errRefresh := c.refreshClusterNodes(ctx)
|
||||
if errRefresh != nil {
|
||||
log.Debugf("home cluster nodes unavailable: %v", errRefresh)
|
||||
@@ -279,6 +295,9 @@ func (c *Client) refreshBestClusterNode(ctx context.Context) {
|
||||
}
|
||||
|
||||
func (c *Client) refreshClusterNodes(ctx context.Context) (bool, error) {
|
||||
if !c.clusterDiscoveryEnabled() {
|
||||
return false, nil
|
||||
}
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
@@ -353,6 +372,10 @@ func (c *Client) failoverAfterReconnectFailure() (bool, string) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
if !c.clusterDiscoveryEnabledLocked() {
|
||||
c.reconnectFailures = 0
|
||||
return false, ""
|
||||
}
|
||||
c.reconnectFailures++
|
||||
if c.reconnectFailures < homeReconnectFailoverThreshold {
|
||||
return false, ""
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package home
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
@@ -115,3 +116,44 @@ func TestRedisOptionsHomeTLSEnabledUsesExplicitServerName(t *testing.T) {
|
||||
t.Fatal("InsecureSkipVerify = false, want true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRefreshClusterNodesDisabledSkipsRedisCommand(t *testing.T) {
|
||||
client := New(config.HomeConfig{
|
||||
Enabled: true,
|
||||
Host: "127.0.0.1",
|
||||
Port: 1,
|
||||
DisableClusterDiscovery: true,
|
||||
})
|
||||
|
||||
switched, err := client.refreshClusterNodes(context.Background())
|
||||
if err != nil {
|
||||
t.Fatalf("refreshClusterNodes() error = %v", err)
|
||||
}
|
||||
if switched {
|
||||
t.Fatal("refreshClusterNodes() switched = true, want false")
|
||||
}
|
||||
if client.cmd != nil || client.sub != nil {
|
||||
t.Fatalf("redis clients were initialized when cluster discovery was disabled")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFailoverAfterReconnectFailureDisabledDoesNotSwitchToClusterNode(t *testing.T) {
|
||||
client := New(config.HomeConfig{
|
||||
Enabled: true,
|
||||
Host: "seed.example.com",
|
||||
Port: 8327,
|
||||
DisableClusterDiscovery: true,
|
||||
})
|
||||
client.mu.Lock()
|
||||
client.clusterNodes = []clusterNode{{IP: "other.example.com", Port: 8327}}
|
||||
client.reconnectFailures = homeReconnectFailoverThreshold - 1
|
||||
client.mu.Unlock()
|
||||
|
||||
switched, addr := client.failoverAfterReconnectFailure()
|
||||
if switched {
|
||||
t.Fatalf("failoverAfterReconnectFailure() switched to %s, want no switch", addr)
|
||||
}
|
||||
if got, _ := client.addr(); got != "seed.example.com:8327" {
|
||||
t.Fatalf("addr() = %q, want seed.example.com:8327", got)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user