Skip to content

Commit 59d0a22

Browse files
committed
refactor: use helper funcs
1 parent f08bcac commit 59d0a22

File tree

1 file changed

+7
-59
lines changed

1 file changed

+7
-59
lines changed

internal/redis/redis.go

Lines changed: 7 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func NewClient(ctx context.Context, config *RedisConfig) (r.Cmdable, error) {
6464
return createRegularClient(ctx, config)
6565
}
6666

67-
func createClusterClient(ctx context.Context, config *RedisConfig) (r.Cmdable, error) {
67+
func createClusterClient(ctx context.Context, config *RedisConfig) (Client, error) {
6868
// Start with single node - cluster client will auto-discover other nodes
6969
options := &r.ClusterOptions{
7070
Addrs: []string{fmt.Sprintf("%s:%d", config.Host, config.Port)},
@@ -103,7 +103,7 @@ func createClusterClient(ctx context.Context, config *RedisConfig) (r.Cmdable, e
103103
return clusterClient, nil
104104
}
105105

106-
func createRegularClient(ctx context.Context, config *RedisConfig) (r.Cmdable, error) {
106+
func createRegularClient(ctx context.Context, config *RedisConfig) (Client, error) {
107107
options := &r.Options{
108108
Addr: fmt.Sprintf("%s:%d", config.Host, config.Port),
109109
Password: config.Password,
@@ -142,70 +142,18 @@ func instrumentOpenTelemetry() error {
142142
}
143143

144144
func initializeClient(ctx context.Context, config *RedisConfig) {
145+
var err error
145146
if config.ClusterEnabled {
146-
// Create proper cluster client for cluster deployments
147-
// Start with single node - cluster client will auto-discover other nodes
148-
options := &r.ClusterOptions{
149-
Addrs: []string{fmt.Sprintf("%s:%d", config.Host, config.Port)},
150-
Password: config.Password,
151-
// Note: Database is ignored in cluster mode
152-
}
153-
154-
// Development only: Override discovered node IPs with the original host
155-
// This is needed for Docker environments where Redis nodes announce internal IPs
156-
if config.DevClusterHostOverride {
157-
originalHost := config.Host
158-
options.NewClient = func(opt *r.Options) *r.Client {
159-
// Extract port from discovered address and combine with original host
160-
if idx := strings.LastIndex(opt.Addr, ":"); idx > 0 {
161-
port := opt.Addr[idx:] // includes the colon
162-
opt.Addr = originalHost + port
163-
}
164-
return r.NewClient(opt)
165-
}
166-
}
167-
168-
if config.TLSEnabled {
169-
options.TLSConfig = &tls.Config{
170-
MinVersion: tls.VersionTLS12,
171-
InsecureSkipVerify: true, // Some managed Redis services use self-signed certificates
172-
}
173-
}
174-
175-
clusterClient := r.NewClusterClient(options)
176-
177-
// Test the cluster client connectivity
178-
if err := clusterClient.Ping(ctx).Err(); err != nil {
147+
client, err = createClusterClient(ctx, config)
148+
if err != nil {
179149
initializationError = fmt.Errorf("redis cluster connection failed: %w", err)
180150
return
181151
}
182-
183-
// Assign to interface
184-
client = clusterClient
185152
} else {
186-
// Create regular client for non-cluster deployments
187-
options := &r.Options{
188-
Addr: fmt.Sprintf("%s:%d", config.Host, config.Port),
189-
Password: config.Password,
190-
DB: config.Database,
191-
}
192-
193-
if config.TLSEnabled {
194-
options.TLSConfig = &tls.Config{
195-
MinVersion: tls.VersionTLS12,
196-
InsecureSkipVerify: true, // Some managed Redis services use self-signed certificates
197-
}
198-
}
199-
200-
regularClient := r.NewClient(options)
201-
202-
// Test the regular client connectivity
203-
if err := regularClient.Ping(ctx).Err(); err != nil {
153+
client, err = createRegularClient(ctx, config)
154+
if err != nil {
204155
initializationError = fmt.Errorf("redis regular client connection failed: %w", err)
205156
return
206157
}
207-
208-
// Assign to interface
209-
client = regularClient
210158
}
211159
}

0 commit comments

Comments
 (0)