1
0
Fork 0

Adding upstream version 2.52.6.

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-05-17 06:50:16 +02:00
parent a960158181
commit 6d002e9543
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
441 changed files with 95392 additions and 0 deletions

View file

@ -0,0 +1,84 @@
package healthcheck
import (
"github.com/gofiber/fiber/v2"
)
// Config defines the configuration options for the healthcheck middleware.
type Config struct {
// Next defines a function to skip this middleware when returned true.
//
// Optional. Default: nil
Next func(c *fiber.Ctx) bool
// Function used for checking the liveness of the application. Returns true if the application
// is running and false if it is not. The liveness probe is typically used to indicate if
// the application is in a state where it can handle requests (e.g., the server is up and running).
//
// Optional. Default: func(c *fiber.Ctx) bool { return true }
LivenessProbe HealthChecker
// HTTP endpoint at which the liveness probe will be available.
//
// Optional. Default: "/livez"
LivenessEndpoint string
// Function used for checking the readiness of the application. Returns true if the application
// is ready to process requests and false otherwise. The readiness probe typically checks if all necessary
// services, databases, and other dependencies are available for the application to function correctly.
//
// Optional. Default: func(c *fiber.Ctx) bool { return true }
ReadinessProbe HealthChecker
// HTTP endpoint at which the readiness probe will be available.
// Optional. Default: "/readyz"
ReadinessEndpoint string
}
const (
DefaultLivenessEndpoint = "/livez"
DefaultReadinessEndpoint = "/readyz"
)
func defaultLivenessProbe(*fiber.Ctx) bool { return true }
func defaultReadinessProbe(*fiber.Ctx) bool { return true }
// ConfigDefault is the default config
var ConfigDefault = Config{
LivenessProbe: defaultLivenessProbe,
ReadinessProbe: defaultReadinessProbe,
LivenessEndpoint: DefaultLivenessEndpoint,
ReadinessEndpoint: DefaultReadinessEndpoint,
}
// defaultConfig returns a default config for the healthcheck middleware.
func defaultConfig(config ...Config) Config {
if len(config) < 1 {
return ConfigDefault
}
cfg := config[0]
if cfg.Next == nil {
cfg.Next = ConfigDefault.Next
}
if cfg.LivenessProbe == nil {
cfg.LivenessProbe = defaultLivenessProbe
}
if cfg.ReadinessProbe == nil {
cfg.ReadinessProbe = defaultReadinessProbe
}
if cfg.LivenessEndpoint == "" {
cfg.LivenessEndpoint = DefaultLivenessEndpoint
}
if cfg.ReadinessEndpoint == "" {
cfg.ReadinessEndpoint = DefaultReadinessEndpoint
}
return cfg
}

View file

@ -0,0 +1,61 @@
package healthcheck
import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/utils"
)
// HealthChecker defines a function to check liveness or readiness of the application
type HealthChecker func(*fiber.Ctx) bool
// ProbeCheckerHandler defines a function that returns a ProbeChecker
type HealthCheckerHandler func(HealthChecker) fiber.Handler
func healthCheckerHandler(checker HealthChecker) fiber.Handler {
return func(c *fiber.Ctx) error {
if checker == nil {
return c.Next()
}
if checker(c) {
return c.SendStatus(fiber.StatusOK)
}
return c.SendStatus(fiber.StatusServiceUnavailable)
}
}
func New(config ...Config) fiber.Handler {
cfg := defaultConfig(config...)
isLiveHandler := healthCheckerHandler(cfg.LivenessProbe)
isReadyHandler := healthCheckerHandler(cfg.ReadinessProbe)
return func(c *fiber.Ctx) error {
// Don't execute middleware if Next returns true
if cfg.Next != nil && cfg.Next(c) {
return c.Next()
}
if c.Method() != fiber.MethodGet {
return c.Next()
}
prefixCount := len(utils.TrimRight(c.Route().Path, '/'))
if len(c.Path()) >= prefixCount {
checkPath := c.Path()[prefixCount:]
checkPathTrimmed := checkPath
if !c.App().Config().StrictRouting {
checkPathTrimmed = utils.TrimRight(checkPath, '/')
}
switch {
case checkPath == cfg.ReadinessEndpoint || checkPathTrimmed == cfg.ReadinessEndpoint:
return isReadyHandler(c)
case checkPath == cfg.LivenessEndpoint || checkPathTrimmed == cfg.LivenessEndpoint:
return isLiveHandler(c)
}
}
return c.Next()
}
}

View file

@ -0,0 +1,237 @@
package healthcheck
import (
"fmt"
"net/http/httptest"
"testing"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/utils"
"github.com/valyala/fasthttp"
)
func shouldGiveStatus(t *testing.T, app *fiber.App, path string, expectedStatus int) {
t.Helper()
req, err := app.Test(httptest.NewRequest(fiber.MethodGet, path, nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, expectedStatus, req.StatusCode, "path: "+path+" should match "+fmt.Sprint(expectedStatus))
}
func shouldGiveOK(t *testing.T, app *fiber.App, path string) {
t.Helper()
shouldGiveStatus(t, app, path, fiber.StatusOK)
}
func shouldGiveNotFound(t *testing.T, app *fiber.App, path string) {
t.Helper()
shouldGiveStatus(t, app, path, fiber.StatusNotFound)
}
func Test_HealthCheck_Strict_Routing_Default(t *testing.T) {
t.Parallel()
app := fiber.New(fiber.Config{
StrictRouting: true,
})
app.Use(New())
shouldGiveOK(t, app, "/readyz")
shouldGiveOK(t, app, "/livez")
shouldGiveNotFound(t, app, "/readyz/")
shouldGiveNotFound(t, app, "/livez/")
shouldGiveNotFound(t, app, "/notDefined/readyz")
shouldGiveNotFound(t, app, "/notDefined/livez")
}
func Test_HealthCheck_Group_Default(t *testing.T) {
t.Parallel()
app := fiber.New()
app.Group("/v1", New())
v2Group := app.Group("/v2/")
customer := v2Group.Group("/customer/")
customer.Use(New())
v3Group := app.Group("/v3/")
v3Group.Group("/todos/", New(Config{ReadinessEndpoint: "/readyz/", LivenessEndpoint: "/livez/"}))
shouldGiveOK(t, app, "/v1/readyz")
shouldGiveOK(t, app, "/v1/livez")
shouldGiveOK(t, app, "/v1/readyz/")
shouldGiveOK(t, app, "/v1/livez/")
shouldGiveOK(t, app, "/v2/customer/readyz")
shouldGiveOK(t, app, "/v2/customer/livez")
shouldGiveOK(t, app, "/v2/customer/readyz/")
shouldGiveOK(t, app, "/v2/customer/livez/")
shouldGiveNotFound(t, app, "/v3/todos/readyz")
shouldGiveNotFound(t, app, "/v3/todos/livez")
shouldGiveOK(t, app, "/v3/todos/readyz/")
shouldGiveOK(t, app, "/v3/todos/livez/")
shouldGiveNotFound(t, app, "/notDefined/readyz")
shouldGiveNotFound(t, app, "/notDefined/livez")
shouldGiveNotFound(t, app, "/notDefined/readyz/")
shouldGiveNotFound(t, app, "/notDefined/livez/")
// strict routing
app = fiber.New(fiber.Config{
StrictRouting: true,
})
app.Group("/v1", New())
v2Group = app.Group("/v2/")
customer = v2Group.Group("/customer/")
customer.Use(New())
v3Group = app.Group("/v3/")
v3Group.Group("/todos/", New(Config{ReadinessEndpoint: "/readyz/", LivenessEndpoint: "/livez/"}))
shouldGiveOK(t, app, "/v1/readyz")
shouldGiveOK(t, app, "/v1/livez")
shouldGiveNotFound(t, app, "/v1/readyz/")
shouldGiveNotFound(t, app, "/v1/livez/")
shouldGiveOK(t, app, "/v2/customer/readyz")
shouldGiveOK(t, app, "/v2/customer/livez")
shouldGiveNotFound(t, app, "/v2/customer/readyz/")
shouldGiveNotFound(t, app, "/v2/customer/livez/")
shouldGiveNotFound(t, app, "/v3/todos/readyz")
shouldGiveNotFound(t, app, "/v3/todos/livez")
shouldGiveOK(t, app, "/v3/todos/readyz/")
shouldGiveOK(t, app, "/v3/todos/livez/")
shouldGiveNotFound(t, app, "/notDefined/readyz")
shouldGiveNotFound(t, app, "/notDefined/livez")
shouldGiveNotFound(t, app, "/notDefined/readyz/")
shouldGiveNotFound(t, app, "/notDefined/livez/")
}
func Test_HealthCheck_Default(t *testing.T) {
t.Parallel()
app := fiber.New()
app.Use(New())
shouldGiveOK(t, app, "/readyz")
shouldGiveOK(t, app, "/livez")
shouldGiveOK(t, app, "/readyz/")
shouldGiveOK(t, app, "/livez/")
shouldGiveNotFound(t, app, "/notDefined/readyz")
shouldGiveNotFound(t, app, "/notDefined/livez")
}
func Test_HealthCheck_Custom(t *testing.T) {
t.Parallel()
app := fiber.New()
c1 := make(chan struct{}, 1)
app.Use(New(Config{
LivenessProbe: func(c *fiber.Ctx) bool {
return true
},
LivenessEndpoint: "/live",
ReadinessProbe: func(c *fiber.Ctx) bool {
select {
case <-c1:
return true
default:
return false
}
},
ReadinessEndpoint: "/ready",
}))
// Live should return 200 with GET request
shouldGiveOK(t, app, "/live")
// Live should return 404 with POST request
req, err := app.Test(httptest.NewRequest(fiber.MethodPost, "/live", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, fiber.StatusNotFound, req.StatusCode)
// Ready should return 404 with POST request
req, err = app.Test(httptest.NewRequest(fiber.MethodPost, "/ready", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, fiber.StatusNotFound, req.StatusCode)
// Ready should return 503 with GET request before the channel is closed
shouldGiveStatus(t, app, "/ready", fiber.StatusServiceUnavailable)
// Ready should return 200 with GET request after the channel is closed
c1 <- struct{}{}
shouldGiveOK(t, app, "/ready")
}
func Test_HealthCheck_Custom_Nested(t *testing.T) {
t.Parallel()
app := fiber.New()
c1 := make(chan struct{}, 1)
app.Use(New(Config{
LivenessProbe: func(c *fiber.Ctx) bool {
return true
},
LivenessEndpoint: "/probe/live",
ReadinessProbe: func(c *fiber.Ctx) bool {
select {
case <-c1:
return true
default:
return false
}
},
ReadinessEndpoint: "/probe/ready",
}))
shouldGiveOK(t, app, "/probe/live")
shouldGiveStatus(t, app, "/probe/ready", fiber.StatusServiceUnavailable)
shouldGiveOK(t, app, "/probe/live/")
shouldGiveStatus(t, app, "/probe/ready/", fiber.StatusServiceUnavailable)
shouldGiveNotFound(t, app, "/probe/livez")
shouldGiveNotFound(t, app, "/probe/readyz")
shouldGiveNotFound(t, app, "/probe/livez/")
shouldGiveNotFound(t, app, "/probe/readyz/")
shouldGiveNotFound(t, app, "/livez")
shouldGiveNotFound(t, app, "/readyz")
shouldGiveNotFound(t, app, "/readyz/")
shouldGiveNotFound(t, app, "/livez/")
c1 <- struct{}{}
shouldGiveOK(t, app, "/probe/ready")
c1 <- struct{}{}
shouldGiveOK(t, app, "/probe/ready/")
}
func Test_HealthCheck_Next(t *testing.T) {
t.Parallel()
app := fiber.New()
app.Use(New(Config{
Next: func(c *fiber.Ctx) bool {
return true
},
}))
shouldGiveNotFound(t, app, "/readyz")
shouldGiveNotFound(t, app, "/livez")
}
func Benchmark_HealthCheck(b *testing.B) {
app := fiber.New()
app.Use(New())
h := app.Handler()
fctx := &fasthttp.RequestCtx{}
fctx.Request.Header.SetMethod(fiber.MethodGet)
fctx.Request.SetRequestURI("/livez")
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
h(fctx)
}
utils.AssertEqual(b, fiber.StatusOK, fctx.Response.Header.StatusCode())
}