From 0cb63b008d13d3411542baebdf39b768fe84a57e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Jochum?= Date: Wed, 30 Sep 2020 15:17:44 +0200 Subject: [PATCH] Properly reconnect in lql.Client --- internal/gncp/pool.go | 5 +++++ lql/client.go | 7 +++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/internal/gncp/pool.go b/internal/gncp/pool.go index 68fa575..c611158 100644 --- a/internal/gncp/pool.go +++ b/internal/gncp/pool.go @@ -70,6 +70,11 @@ func (p *GncpPool) init() error { return nil } +// GetMaxConns returns the maximum number of connections configured +func (p *GncpPool) GetMaxConns() int { + return p.maxConnNum +} + // Get get connection from connection pool. If connection poll is empty and alreay created connection number less than Max number of connection // it will create new one. Otherwise it wil wait someone put connection back. func (p *GncpPool) Get() (net.Conn, error) { diff --git a/lql/client.go b/lql/client.go index 42bc764..858294a 100644 --- a/lql/client.go +++ b/lql/client.go @@ -174,13 +174,15 @@ func (c *Client) RequestRaw(context context.Context, request, outputFormat, auth c.logger.WithField("request", request).Debug("Writing request") _, err = conn.Write([]byte(request)) - if err != nil && errors.Is(err, syscall.EPIPE) { + if err != nil && !errors.Is(err, syscall.EPIPE) { return nil, err } else if errors.Is(err, syscall.EPIPE) { // Destroy -> Create Connections until we don't get EPIPE. numTries := 0 for errors.Is(err, syscall.EPIPE) { + c.logger.WithField("error", err).Debug("Trying to reconnect") + conn.Close() conn.(*gncp.CpConn).Destroy() @@ -196,7 +198,8 @@ func (c *Client) RequestRaw(context context.Context, request, outputFormat, auth } numTries++ - if numTries >= 20 { + if numTries >= c.pool.GetMaxConns()*2 { + c.logger.WithField("error", err).Error("To much retries can't reconnect") // Bailout to much tries return nil, err }