Files
gluetun/internal/healthcheck/handler.go
Quentin McGaw 85890520ab feat(healthcheck): combination of ICMP and TCP+TLS checks (#2923)
- New option: `HEALTH_ICMP_TARGET_IP` defaults to `0.0.0.0` meaning use the VPN server public IP address.
- Options removed: `HEALTH_VPN_INITIAL_DURATION` and `HEALTH_VPN_ADDITIONAL_DURATION` - times and retries are handpicked and hardcoded.
- Less aggressive checks and less false positive detection
2025-10-17 01:45:50 +02:00

47 lines
980 B
Go

package healthcheck
import (
"errors"
"net/http"
"sync"
)
type handler struct {
healthErr error
healthErrMu sync.RWMutex
logger Logger
}
var errHealthcheckNotRunYet = errors.New("healthcheck did not run yet")
func newHandler(logger Logger) *handler {
return &handler{
healthErr: errHealthcheckNotRunYet,
logger: logger,
}
}
func (h *handler) ServeHTTP(responseWriter http.ResponseWriter, request *http.Request) {
if request.Method != http.MethodGet {
http.Error(responseWriter, "method not supported for healthcheck", http.StatusBadRequest)
return
}
if err := h.getErr(); err != nil {
http.Error(responseWriter, err.Error(), http.StatusInternalServerError)
return
}
responseWriter.WriteHeader(http.StatusOK)
}
func (h *handler) setErr(err error) {
h.healthErrMu.Lock()
defer h.healthErrMu.Unlock()
h.healthErr = err
}
func (h *handler) getErr() (err error) {
h.healthErrMu.RLock()
defer h.healthErrMu.RUnlock()
return h.healthErr
}