Try reconnect on EPIPE maybe fixes conn problems

master
René Jochum 4 years ago
parent 201a979a48
commit 2641b48c6a

@ -15,7 +15,7 @@ type CpConn struct {
// Destroy will close connection and release connection from connection pool.
func (conn *CpConn) Destroy() error {
if conn.pool == nil {
return errors.New("Connection not belong any connection pool.")
return errors.New("Connection not belong any connection pool")
}
err := conn.pool.Remove(conn.Conn)
if err != nil {
@ -28,7 +28,7 @@ func (conn *CpConn) Destroy() error {
// Close will push connection back to connection pool. It will not close the real connection.
func (conn *CpConn) Close() error {
if conn.pool == nil {
return errors.New("Connection not belong any connection pool.")
return errors.New("Connection not belong any connection pool")
}
return conn.pool.Put(conn.Conn)
}

@ -154,7 +154,7 @@ func (p *GncpPool) Put(conn net.Conn) error {
p.lock.Lock()
p.totalConnNum = p.totalConnNum - 1
p.lock.Unlock()
return errors.New("Cannot put nil to connection pool.")
return errors.New("Cannot put nil to connection pool")
}
select {

@ -9,6 +9,7 @@ import (
"net"
"strconv"
"strings"
"syscall"
"github.com/gin-gonic/gin"
"github.com/webmeisterei/lql-api/internal/gncp"
@ -170,13 +171,38 @@ func (c *Client) RequestRaw(context context.Context, request, outputFormat, auth
if err != nil {
return nil, err
}
defer c.pool.Put(conn)
c.logger.WithField("request", request).Debug("Writing request")
_, err = conn.Write([]byte(request))
if err != nil {
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) {
conn.Close()
conn.(*gncp.CpConn).Destroy()
conn, err = c.pool.GetWithContext(context)
if err != nil {
return nil, err
}
conn.Close()
_, err = conn.Write([]byte(request))
if err != nil && errors.Is(err, syscall.EPIPE) {
return nil, err
}
numTries++
if numTries >= 20 {
// Bailout to much tries
return nil, err
}
}
}
defer conn.Close()
tmpBuff := make([]byte, 1024)
n, err := conn.Read(tmpBuff)

Loading…
Cancel
Save