From 6c8f33d95c7ac010d6d6283638bf955aabeef5fd Mon Sep 17 00:00:00 2001 From: Yusuke Kato Date: Tue, 9 Jan 2024 15:04:59 +0900 Subject: [PATCH] disable disconnection during non-IP-direct connection (#2288) Signed-off-by: kpango --- internal/net/grpc/client.go | 17 +++++++++++++++-- internal/net/grpc/pool/pool.go | 6 +++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/internal/net/grpc/client.go b/internal/net/grpc/client.go index 3e0038d6f63..8c0a28897e4 100644 --- a/internal/net/grpc/client.go +++ b/internal/net/grpc/client.go @@ -329,9 +329,22 @@ func (g *gRPCClient) StartConnectionMonitor(ctx context.Context) (<-chan error, } }) cancel() - disconnected := make(map[string]bool, len(disconnectTargets)) + var ( + disconnectFlag bool + isIPv4, isIPv6 bool + host string + port uint16 + disconnected = make(map[string]bool, len(disconnectTargets)) + ) for _, addr := range disconnectTargets { - if !disconnected[addr] { + host, port, _, isIPv4, isIPv6, err = net.Parse(addr) + disconnectFlag = isIPv4 || isIPv6 // Disconnect only if the connection is a direct IP connection; do not delete connections via DNS due to retry. + if err != nil { + log.Warnf("failed to parse addr %s for disconnection checking, will disconnect soon: host: %s, port %d, err: %v", addr, host, port, err) + disconnectFlag = true // Disconnect if the address connected to is not parseable. + } + if disconnectFlag && + !disconnected[addr] { err = g.Disconnect(ctx, addr) if err != nil { if !errors.Is(err, context.Canceled) && diff --git a/internal/net/grpc/pool/pool.go b/internal/net/grpc/pool/pool.go index 0f97bd2f182..d54fcf0e326 100644 --- a/internal/net/grpc/pool/pool.go +++ b/internal/net/grpc/pool/pool.go @@ -359,7 +359,7 @@ func (p *pool) connect(ctx context.Context, ips ...string) (c Conn, err error) { !errors.Is(ierr, context.Canceled) { log.Warnf("An error occurred while dialing pool member connection to %s,\terror: %v", addr, ierr) } else { - log.Debug("Connect loop operation canceled while dialing pool member connection to %s,\terror: %v", addr, ierr) + log.Debugf("Connect loop operation canceled while dialing pool member connection to %s,\terror: %v", addr, ierr) return false } } @@ -421,7 +421,7 @@ func (p *pool) singleTargetConnect(ctx context.Context) (c Conn, err error) { } return true } else { - log.Debug("Connect loop operation canceled while dialing pool member connection to %s,\terror: %v", p.addr, ierr) + log.Debugf("Connect loop operation canceled while dialing pool member connection to %s,\terror: %v", p.addr, ierr) return false } } @@ -449,7 +449,7 @@ func (p *pool) Disconnect() (err error) { log.Debugf("failed to close connection pool addr = %s\terror = %v", pc.addr, ierr) emap[ierr.Error()] = err } else { - log.Debug("Disconnect loop operation canceled while closing pool member connection to %s,\terror: %v", pc.addr, ierr) + log.Debugf("Disconnect loop operation canceled while closing pool member connection to %s,\terror: %v", pc.addr, ierr) return false } }