Skip to content

Commit fd934bf

Browse files
author
Runze Cui
committed
Check CB Open and init connection for resolve hystrix attempt
1 parent f7a6e97 commit fd934bf

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

redis.go

+25-11
Original file line numberDiff line numberDiff line change
@@ -265,25 +265,39 @@ func (c *baseClient) withConn(
265265
return err
266266
}
267267

268-
// Request is allowed occasionally to close the cb and cb is still in open state
269-
// Init the connection to avoid nested hystrix call during connection establishment
268+
err := c.executeWithCircuitBreaker(ctx, fn, limiter)
269+
limiter.ReportResult(err)
270+
return err
271+
}
272+
273+
func (c *baseClient) executeWithCircuitBreaker(
274+
ctx context.Context,
275+
fn func(context.Context, *pool.Conn) error,
276+
limiter Limiter,
277+
) error {
278+
// When circuit is open, try to establish connection first
279+
// This is needed for circuit breaker recovery
270280
if limiter.IsCBOpen() {
271-
cn, err := c.getConn(ctx)
272-
if err != nil {
281+
if err := c.preConnect(ctx); err != nil {
273282
return err
274283
}
275-
276-
defer func() {
277-
c.releaseConn(ctx, cn, err)
278-
}()
279284
}
280285

281-
err := limiter.Execute(func() error {
286+
return limiter.Execute(func() error {
282287
return c._withConn(ctx, fn)
283288
})
289+
}
284290

285-
limiter.ReportResult(err)
286-
return err
291+
func (c *baseClient) preConnect(ctx context.Context) error {
292+
cn, err := c.getConn(ctx)
293+
if err != nil {
294+
return err
295+
}
296+
297+
defer func() {
298+
c.releaseConn(ctx, cn, err)
299+
}()
300+
return nil
287301
}
288302

289303
func (c *baseClient) _withConn(

0 commit comments

Comments
 (0)