-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
✨ feat: add liveness and readiness checks #2509
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ReneWerner87
merged 22 commits into
gofiber:master
from
luk3skyw4lker:feat/add-probe-checkers
Jan 3, 2024
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
144c631
:sparkles: feat: add liveness and readiness checkers
luk3skyw4lker fed6c5e
:memo: docs: add docs for liveness and readiness
luk3skyw4lker 96d3944
:sparkles: feat: add options method for probe checkers
luk3skyw4lker ec98691
:white_check_mark: tests: add tests for liveness and readiness
luk3skyw4lker 7615bec
:recycle: refactor: change default endpoint values
luk3skyw4lker b00ee64
:recycle: refactor: change default value for liveness endpoint
luk3skyw4lker 87faa6a
:memo: docs: add return status for liveness and readiness probes
luk3skyw4lker 789b882
:recycle: refactor: change probechecker to middleware
luk3skyw4lker ef1e265
:twisted_rightwards_arrows: chore: update branch with master
luk3skyw4lker df23f68
:memo: docs: move docs to middleware session
luk3skyw4lker 4ad217e
:recycle: refactor: apply gofumpt formatting
luk3skyw4lker 2c41068
:recycle: refactor: remove unused parameter
luk3skyw4lker 7fdb8aa
split config and apply a review
efectn 931526a
apply reviews and add testcases
efectn 508bddb
add benchmark
efectn 2041679
cleanup
efectn e987a8e
rename middleware
efectn c33626b
fix linter
efectn 013e362
Update docs and config values
gaby 5c08790
Revert change to IsReady
gaby 92e1cac
Updates based on code review
gaby 87b1822
Update docs to match other middlewares
gaby File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| --- | ||
| id: healthcheck | ||
| title: healthcheck | ||
| --- | ||
|
|
||
| Liveness and readiness probes middleware for [Fiber](https://github.com/gofiber/fiber) that provides two endpoints for checking the liveness and readiness state of HTTP applications. | ||
|
|
||
| ## Overview | ||
|
|
||
| - **Liveness Probe**: Checks if the server is up and running. | ||
| - **Default Endpoint**: `/livez` | ||
| - **Behavior**: By default returns `true` immediately when the server is operational. | ||
|
|
||
| - **Readiness Probe**: Assesses if the application is ready to handle requests. | ||
| - **Default Endpoint**: `/readyz` | ||
| - **Behavior**: By default returns `true` immediately when the server is operational. | ||
|
|
||
| - **HTTP Status Codes**: | ||
| - `200 OK`: Returned when the checker function evaluates to `true`. | ||
| - `503 Service Unavailable`: Returned when the checker function evaluates to `false`. | ||
|
|
||
| ## Signatures | ||
|
|
||
| ```go | ||
| func New(config Config) fiber.Handler | ||
| ``` | ||
|
|
||
| ## Examples | ||
|
|
||
| Import the middleware package that is part of the Fiber web framework | ||
| ```go | ||
| import ( | ||
| "github.com/gofiber/fiber/v2" | ||
| "github.com/gofiber/fiber/v2/middleware/healthcheck" | ||
| ) | ||
| ``` | ||
|
|
||
| After you initiate your Fiber app, you can use the following possibilities: | ||
|
|
||
| ```go | ||
| // Provide a minimal config | ||
| app.Use(healthcheck.New()) | ||
|
|
||
| // Or extend your config for customization | ||
| app.Use(healthcheck.New(healthcheck.Config{ | ||
| LivenessProbe: func(c *fiber.Ctx) bool { | ||
| return true | ||
| }, | ||
| LivenessEndpoint: "/live", | ||
| ReadinessProbe: func(c *fiber.Ctx) bool { | ||
| return serviceA.Ready() && serviceB.Ready() && ... | ||
| }, | ||
| ReadinessEndpoint: "/ready", | ||
| })) | ||
| ``` | ||
|
|
||
| ## Config | ||
|
|
||
| ```go | ||
| 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 | ||
| } | ||
| ``` | ||
|
|
||
| ## Default Config | ||
|
|
||
| The default configuration used by this middleware is defined as follows: | ||
| ```go | ||
| func defaultLivenessProbe(*fiber.Ctx) bool { return true } | ||
|
|
||
| func defaultReadinessProbe(*fiber.Ctx) bool { return true } | ||
|
|
||
| var ConfigDefault = Config{ | ||
| LivenessProbe: defaultLivenessProbe, | ||
| ReadinessProbe: defaultReadinessProbe, | ||
| LivenessEndpoint: "/livez", | ||
| ReadinessEndpoint: "/readyz", | ||
| } | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package healthcheck | ||
|
|
||
| import ( | ||
| "github.com/gofiber/fiber/v2" | ||
| ) | ||
|
|
||
| // 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() | ||
| } | ||
|
|
||
| switch c.Path() { | ||
| case cfg.ReadinessEndpoint: | ||
| return isReadyHandler(c) | ||
| case cfg.LivenessEndpoint: | ||
| return isLiveHandler(c) | ||
| } | ||
|
|
||
| return c.Next() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| package healthcheck | ||
|
|
||
| import ( | ||
| "net/http/httptest" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/gofiber/fiber/v2" | ||
| "github.com/gofiber/fiber/v2/utils" | ||
| "github.com/valyala/fasthttp" | ||
| ) | ||
|
|
||
| func Test_HealthCheck_Default(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| app := fiber.New() | ||
| app.Use(New()) | ||
|
|
||
| req, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/readyz", nil)) | ||
| utils.AssertEqual(t, nil, err) | ||
| utils.AssertEqual(t, fiber.StatusOK, req.StatusCode) | ||
|
|
||
| req, err = app.Test(httptest.NewRequest(fiber.MethodGet, "/livez", nil)) | ||
| utils.AssertEqual(t, nil, err) | ||
| utils.AssertEqual(t, fiber.StatusOK, req.StatusCode) | ||
| } | ||
|
|
||
| func Test_HealthCheck_Custom(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| app := fiber.New() | ||
|
|
||
| c1 := make(chan struct{}, 1) | ||
| go func() { | ||
| time.Sleep(1 * time.Second) | ||
| c1 <- struct{}{} | ||
| }() | ||
|
|
||
| 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 | ||
| req, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/live", nil)) | ||
| utils.AssertEqual(t, nil, err) | ||
| utils.AssertEqual(t, fiber.StatusOK, req.StatusCode) | ||
|
|
||
| // 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 | ||
| req, err = app.Test(httptest.NewRequest(fiber.MethodGet, "/ready", nil)) | ||
| utils.AssertEqual(t, nil, err) | ||
| utils.AssertEqual(t, fiber.StatusServiceUnavailable, req.StatusCode) | ||
|
|
||
| time.Sleep(1 * time.Second) | ||
|
|
||
| // Ready should return 200 with GET request after the channel is closed | ||
| req, err = app.Test(httptest.NewRequest(fiber.MethodGet, "/ready", nil)) | ||
| utils.AssertEqual(t, nil, err) | ||
| utils.AssertEqual(t, fiber.StatusOK, req.StatusCode) | ||
| } | ||
|
|
||
| func Test_HealthCheck_Next(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| app := fiber.New() | ||
|
|
||
| app.Use(New(Config{ | ||
| Next: func(c *fiber.Ctx) bool { | ||
| return true | ||
| }, | ||
| })) | ||
|
|
||
| req, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/livez", nil)) | ||
| utils.AssertEqual(t, nil, err) | ||
| utils.AssertEqual(t, fiber.StatusNotFound, req.StatusCode) | ||
| } | ||
|
|
||
| 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()) | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.