internal: add internal healthchecking to prevent websocket errors
Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
		@ -1,10 +1,13 @@
 | 
			
		||||
package gounicorn
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"os"
 | 
			
		||||
	"os/exec"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	log "github.com/sirupsen/logrus"
 | 
			
		||||
	"goauthentik.io/internal/outpost/ak"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type GoUnicorn struct {
 | 
			
		||||
@ -12,6 +15,7 @@ type GoUnicorn struct {
 | 
			
		||||
	p       *exec.Cmd
 | 
			
		||||
	started bool
 | 
			
		||||
	killed  bool
 | 
			
		||||
	alive   bool
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewGoUnicorn() *GoUnicorn {
 | 
			
		||||
@ -20,6 +24,7 @@ func NewGoUnicorn() *GoUnicorn {
 | 
			
		||||
		log:     logger,
 | 
			
		||||
		started: false,
 | 
			
		||||
		killed:  false,
 | 
			
		||||
		alive:   false,
 | 
			
		||||
	}
 | 
			
		||||
	g.initCmd()
 | 
			
		||||
	return g
 | 
			
		||||
@ -35,6 +40,10 @@ func (g *GoUnicorn) initCmd() {
 | 
			
		||||
	g.p.Stderr = os.Stderr
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (g *GoUnicorn) IsRunning() bool {
 | 
			
		||||
	return g.alive
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (g *GoUnicorn) Start() error {
 | 
			
		||||
	if g.killed {
 | 
			
		||||
		g.log.Debug("Not restarting gunicorn since we're killed")
 | 
			
		||||
@ -44,9 +53,38 @@ func (g *GoUnicorn) Start() error {
 | 
			
		||||
		g.initCmd()
 | 
			
		||||
	}
 | 
			
		||||
	g.started = true
 | 
			
		||||
	go g.healthcheck()
 | 
			
		||||
	return g.p.Run()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (g *GoUnicorn) healthcheck() {
 | 
			
		||||
	g.log.Debug("starting healthcheck")
 | 
			
		||||
	h := &http.Client{
 | 
			
		||||
		Transport: ak.NewUserAgentTransport("goauthentik.io go proxy healthcheck", http.DefaultTransport),
 | 
			
		||||
	}
 | 
			
		||||
	check := func() bool {
 | 
			
		||||
		res, err := h.Get("http://localhost:8000/-/health/live/")
 | 
			
		||||
		if err == nil && res.StatusCode == 204 {
 | 
			
		||||
			g.alive = true
 | 
			
		||||
			return true
 | 
			
		||||
		}
 | 
			
		||||
		return false
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Default healthcheck is every 1 second on startup
 | 
			
		||||
	// once we've been healthy once, increase to 30 seconds
 | 
			
		||||
	for range time.Tick(time.Second) {
 | 
			
		||||
		if check() {
 | 
			
		||||
			g.log.Info("backend is alive, backing off with healthchecks")
 | 
			
		||||
			break
 | 
			
		||||
		}
 | 
			
		||||
		g.log.Debug("backend not alive yet")
 | 
			
		||||
	}
 | 
			
		||||
	for range time.Tick(30 * time.Second) {
 | 
			
		||||
		check()
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (g *GoUnicorn) Kill() {
 | 
			
		||||
	g.killed = true
 | 
			
		||||
	err := g.p.Process.Kill()
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user