Finaly fix reconnections

master
René Jochum 4 years ago
parent 995c397252
commit 4cab8447a9

@ -14,7 +14,7 @@ ARGS=$2
# Execute
set -ex
CURL="/usr/bin/curl $2 -s -f"
CURL="/usr/bin/curl $2 -q -f"
# GET Hosts
# $CURL -X POST -d '{"method": "GET", "table": "hosts", "columns": ["name", "address"]}' $SERVER/v1/raw

@ -13,5 +13,4 @@ require (
github.com/toorop/gin-logrus v0.0.0-20200831135515-d2ee50d38dae
github.com/wI2L/fizz v0.13.4
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a
gopkg.in/fsnotify.v1 v1.4.7
)

@ -177,27 +177,29 @@ func (c *Client) RequestRaw(context context.Context, request, outputFormat, auth
if err != nil && !errors.Is(err, syscall.EPIPE) {
return nil, err
} else if errors.Is(err, syscall.EPIPE) {
conn.Close()
conn.(*gncp.CpConn).Destroy()
// 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()
conn, err = c.pool.GetWithContext(context)
if err != nil {
return nil, err
}
_, err = conn.Write([]byte(request))
if err != nil && errors.Is(err, syscall.EPIPE) {
if err != nil && !errors.Is(err, syscall.EPIPE) {
return nil, err
}
conn.Close()
c.pool.Remove(conn)
numTries++
if numTries >= c.pool.GetMaxConns()*2 {
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

@ -4,12 +4,10 @@ import (
"errors"
"fmt"
"net/http"
"sync"
auth "github.com/abbot/go-http-auth"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"gopkg.in/fsnotify.v1"
)
var (
@ -53,59 +51,3 @@ func basicAuthMiddleware(htpasswdPath, realm string) gin.HandlerFunc {
c.Set("user", user)
}
}
func basicAuthWithWatcherMiddleware(htpasswdPath, realm string) gin.HandlerFunc {
authenticatorLock := sync.RWMutex{}
htpasswd := auth.HtpasswdFileProvider(htpasswdPath)
authenticator := auth.NewBasicAuthenticator(realm, htpasswd)
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
Logger.WithField("event", event).Debug("event")
if event.Op&fsnotify.Write == fsnotify.Write {
authenticatorLock.Lock()
Logger.WithField("path", event.Name).Debug("Modified file")
htpasswd = auth.HtpasswdFileProvider(htpasswdPath)
authenticator = auth.NewBasicAuthenticator(realm, htpasswd)
authenticatorLock.Unlock()
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
Logger.WithField("error", err).Error()
}
}
}()
err = watcher.Add(htpasswdPath)
if err != nil {
log.Fatal(err)
}
realmHeader := fmt.Sprintf("Basic realm=\"%s\"", realm)
return func(c *gin.Context) {
authenticatorLock.RLock()
user := authenticator.CheckAuth(c.Request)
authenticatorLock.RUnlock()
if user == "" {
c.Header("WWW-Authenticate", realmHeader)
c.AbortWithStatus(http.StatusUnauthorized)
return
}
c.Set("user", user)
}
}

@ -47,7 +47,7 @@ All v1/ endpoints require http basic auth`,
// Setup routes.
v1Group := fizz.Group("/v1", "v1", "LQL API v1")
if htpasswdPath != "" {
v1Group.Use(basicAuthWithWatcherMiddleware(htpasswdPath, "LQL API"))
v1Group.Use(basicAuthMiddleware(htpasswdPath, "LQL API"))
} else {
// Inject empty user if not .htpasswd have been given
v1Group.Use(func(c *gin.Context) {

Loading…
Cancel
Save