Skip to content

Commit 233b2e7

Browse files
committed
refactor: extract dialer creation logic to reduce duplication
Consolidates Redis and Sentinel dialer setup into a reusable createDialer helper function, eliminating ~30 lines of duplicated code. Improves logging by including connection target details (e.g., "sentinel(master,host1,host2)") instead of generic "sentinel" string.
1 parent 90fbcce commit 233b2e7

File tree

1 file changed

+26
-44
lines changed

1 file changed

+26
-44
lines changed

src/redis/driver_impl.go

Lines changed: 26 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,9 @@ func checkError(err error) {
7979
}
8080
}
8181

82-
func NewClientImpl(scope stats.Scope, useTls bool, auth, redisSocketType, redisType, url string, poolSize int,
83-
pipelineWindow time.Duration, pipelineLimit int, tlsConfig *tls.Config, healthCheckActiveConnection bool, srv server.Server,
84-
timeout time.Duration, poolOnEmptyBehavior string, poolOnEmptyWaitDuration time.Duration, sentinelAuth string,
85-
useExplicitPipeline bool,
86-
) Client {
87-
maskedUrl := utils.MaskCredentialsInUrl(url)
88-
logger.Warnf("connecting to redis on %s with pool size %d", maskedUrl, poolSize)
89-
90-
// Create Dialer for connecting to Redis
82+
// createDialer creates a radix.Dialer with timeout, TLS, and auth configuration
83+
// targetName is used for logging to identify the connection target (e.g., URL, "sentinel(url)")
84+
func createDialer(timeout time.Duration, useTls bool, tlsConfig *tls.Config, auth string, targetName string) radix.Dialer {
9185
var netDialer net.Dialer
9286
if timeout > 0 {
9387
netDialer.Timeout = timeout
@@ -104,20 +98,38 @@ func NewClientImpl(scope stats.Scope, useTls bool, auth, redisSocketType, redisT
10498
Config: tlsConfig,
10599
}
106100
dialer.NetDialer = &tlsNetDialer
101+
if targetName != "" {
102+
logger.Warnf("enabling TLS to redis %s", targetName)
103+
}
107104
}
108105

106+
// Setup auth if provided
109107
if auth != "" {
110108
user, pass, found := strings.Cut(auth, ":")
111109
if found {
112-
logger.Warnf("enabling authentication to redis on %s with user %s", maskedUrl, user)
110+
logger.Warnf("enabling authentication to redis %s with user %s", targetName, user)
113111
dialer.AuthUser = user
114112
dialer.AuthPass = pass
115113
} else {
116-
logger.Warnf("enabling authentication to redis on %s without user", maskedUrl)
114+
logger.Warnf("enabling authentication to redis %s without user", targetName)
117115
dialer.AuthPass = auth
118116
}
119117
}
120118

119+
return dialer
120+
}
121+
122+
func NewClientImpl(scope stats.Scope, useTls bool, auth, redisSocketType, redisType, url string, poolSize int,
123+
pipelineWindow time.Duration, pipelineLimit int, tlsConfig *tls.Config, healthCheckActiveConnection bool, srv server.Server,
124+
timeout time.Duration, poolOnEmptyBehavior string, poolOnEmptyWaitDuration time.Duration, sentinelAuth string,
125+
useExplicitPipeline bool,
126+
) Client {
127+
maskedUrl := utils.MaskCredentialsInUrl(url)
128+
logger.Warnf("connecting to redis on %s with pool size %d", maskedUrl, poolSize)
129+
130+
// Create Dialer for connecting to Redis
131+
dialer := createDialer(timeout, useTls, tlsConfig, auth, maskedUrl)
132+
121133
stats := newPoolStats(scope)
122134

123135
// Create PoolConfig
@@ -206,39 +218,9 @@ func NewClientImpl(scope stats.Scope, useTls bool, auth, redisSocketType, redisT
206218
panic(RedisError("Expected master name and a list of urls for the sentinels, in the format: <redis master name>,<sentinel1>,...,<sentineln>"))
207219
}
208220

209-
// Create sentinel dialer
210-
var sentinelNetDialer net.Dialer
211-
if timeout > 0 {
212-
sentinelNetDialer.Timeout = timeout
213-
}
214-
215-
sentinelDialer := radix.Dialer{
216-
NetDialer: &sentinelNetDialer,
217-
}
218-
219-
// Setup TLS for sentinel if needed
220-
if useTls {
221-
logger.Warnf("enabling TLS to redis sentinel")
222-
tlsSentinelDialer := tls.Dialer{
223-
NetDialer: &sentinelNetDialer,
224-
Config: tlsConfig,
225-
}
226-
sentinelDialer.NetDialer = &tlsSentinelDialer
227-
}
228-
229-
// Use sentinelAuth for authenticating to Sentinel nodes, not auth
230-
// auth is used for Redis master/replica authentication
231-
if sentinelAuth != "" {
232-
user, pass, found := strings.Cut(sentinelAuth, ":")
233-
if found {
234-
logger.Warnf("enabling authentication to redis sentinel with user %s", user)
235-
sentinelDialer.AuthUser = user
236-
sentinelDialer.AuthPass = pass
237-
} else {
238-
logger.Warnf("enabling authentication to redis sentinel without user")
239-
sentinelDialer.AuthPass = sentinelAuth
240-
}
241-
}
221+
// Create sentinel dialer (may use different auth from Redis master/replica)
222+
// sentinelAuth is for Sentinel nodes, auth is for Redis master/replica
223+
sentinelDialer := createDialer(timeout, useTls, tlsConfig, sentinelAuth, fmt.Sprintf("sentinel(%s)", maskedUrl))
242224

243225
sentinelConfig := radix.SentinelConfig{
244226
PoolConfig: poolConfig,

0 commit comments

Comments
 (0)