Skip to content

Commit da515e3

Browse files
Apply suggestions from code review
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Signed-off-by: Yusuke Kato <[email protected]> Signed-off-by: kpango <[email protected]>
1 parent 288aec0 commit da515e3

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

internal/net/grpc/client.go

+14-8
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ func (g *gRPCClient) StartConnectionMonitor(ctx context.Context) (<-chan error,
249249
return ctx.Err()
250250
case <-prTick.C:
251251
if g.enablePoolRebalance {
252-
err = g.rangeConns(ctx, func(addr string, p pool.Conn) bool {
252+
err = g.rangeConns(ctx, true, func(addr string, p pool.Conn) bool {
253253
// if addr or pool is nil or empty the registration of conns is invalid let's disconnect them
254254
if addr == "" || p == nil {
255255
disconnectTargets = append(disconnectTargets, addr)
@@ -286,7 +286,7 @@ func (g *gRPCClient) StartConnectionMonitor(ctx context.Context) (<-chan error,
286286
})
287287
}
288288
case <-hcTick.C:
289-
err = g.rangeConns(ctx, func(addr string, p pool.Conn) bool {
289+
err = g.rangeConns(ctx, true, func(addr string, p pool.Conn) bool {
290290
// if addr or pool is nil or empty the registration of conns is invalid let's disconnect them
291291
if addr == "" || p == nil {
292292
disconnectTargets = append(disconnectTargets, addr)
@@ -415,7 +415,7 @@ func (g *gRPCClient) Range(
415415
if g.conns.Len() == 0 {
416416
return errors.ErrGRPCClientConnNotFound("*")
417417
}
418-
err = g.rangeConns(ctx, func(addr string, p pool.Conn) bool {
418+
err = g.rangeConns(ctx, false, func(addr string, p pool.Conn) bool {
419419
ssctx, sspan := trace.StartSpan(sctx, apiName+"/Client.Range/"+addr)
420420
defer func() {
421421
if sspan != nil {
@@ -478,7 +478,7 @@ func (g *gRPCClient) RangeConcurrent(
478478
if g.conns.Len() == 0 {
479479
return errors.ErrGRPCClientConnNotFound("*")
480480
}
481-
err = g.rangeConns(ctx, func(addr string, p pool.Conn) bool {
481+
err = g.rangeConns(ctx, false, func(addr string, p pool.Conn) bool {
482482
eg.Go(safety.RecoverFunc(func() (err error) {
483483
ssctx, sspan := trace.StartSpan(egctx, apiName+"/Client.RangeConcurrent/"+addr)
484484
defer func() {
@@ -701,7 +701,7 @@ func (g *gRPCClient) RoundRobin(
701701
}
702702

703703
do := func() (data any, err error) {
704-
cerr := g.rangeConns(ctx, func(addr string, p pool.Conn) bool {
704+
cerr := g.rangeConns(ctx, false, func(addr string, p pool.Conn) bool {
705705
select {
706706
case <-ctx.Done():
707707
err = ctx.Err()
@@ -1087,7 +1087,7 @@ func (g *gRPCClient) Disconnect(ctx context.Context, addr string) error {
10871087

10881088
func (g *gRPCClient) ConnectedAddrs(ctx context.Context) (addrs []string) {
10891089
addrs = make([]string, 0, g.conns.Len())
1090-
err := g.rangeConns(ctx, func(addr string, p pool.Conn) bool {
1090+
err := g.rangeConns(ctx, false, func(addr string, p pool.Conn) bool {
10911091
if p != nil && p.IsHealthy(ctx) {
10921092
addrs = append(addrs, addr)
10931093
}
@@ -1118,14 +1118,20 @@ func (g *gRPCClient) Close(ctx context.Context) (err error) {
11181118
return err
11191119
}
11201120

1121-
func (g *gRPCClient) rangeConns(ctx context.Context, fn func(addr string, p pool.Conn) bool) error {
1121+
func (g *gRPCClient) rangeConns(ctx context.Context, force bool, fn func(addr string, p pool.Conn) bool) error {
11221122
var cnt int
11231123
g.conns.Range(func(addr string, p pool.Conn) bool {
1124+
if force {
1125+
cnt++
1126+
return fn(addr, p)
1127+
}
11241128
if p == nil || !p.IsHealthy(ctx) {
11251129
pc, err := p.Connect(ctx)
11261130
if pc == nil || err != nil || !pc.IsHealthy(ctx) {
11271131
if pc != nil {
1128-
pc.Disconnect(ctx)
1132+
if derr := pc.Disconnect(ctx); derr != nil {
1133+
log.Debugf("Failed to disconnect unhealthy connection for %s: %v", addr, derr)
1134+
}
11291135
}
11301136
log.Debugf("Unhealthy connection detected for %s during gRPC Connection Range over Loop:\t%s", addr, p.String())
11311137
return true

internal/net/grpc/client_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -3107,7 +3107,7 @@ package grpc
31073107
// stopMonitor: test.fields.stopMonitor,
31083108
// }
31093109
//
3110-
// gotAddrs := g.ConnectedAddrs(context.Background)
3110+
gotAddrs := g.ConnectedAddrs(context.Background())
31113111
// if err := checkFunc(test.want, gotAddrs); err != nil {
31123112
// tt.Errorf("error = %v", err)
31133113
// }

0 commit comments

Comments
 (0)