Skip to content
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

✨ feat: add liveness and readiness checks #2509

Merged
merged 22 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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 Jun 17, 2023
fed6c5e
:memo: docs: add docs for liveness and readiness
luk3skyw4lker Jun 17, 2023
96d3944
:sparkles: feat: add options method for probe checkers
luk3skyw4lker Jun 17, 2023
ec98691
:white_check_mark: tests: add tests for liveness and readiness
luk3skyw4lker Jun 17, 2023
7615bec
:recycle: refactor: change default endpoint values
luk3skyw4lker Jun 17, 2023
b00ee64
:recycle: refactor: change default value for liveness endpoint
luk3skyw4lker Jun 17, 2023
87faa6a
:memo: docs: add return status for liveness and readiness probes
luk3skyw4lker Jun 17, 2023
789b882
:recycle: refactor: change probechecker to middleware
luk3skyw4lker Jun 27, 2023
ef1e265
:twisted_rightwards_arrows: chore: update branch with master
luk3skyw4lker Jun 27, 2023
df23f68
:memo: docs: move docs to middleware session
luk3skyw4lker Jun 30, 2023
4ad217e
:recycle: refactor: apply gofumpt formatting
luk3skyw4lker Aug 15, 2023
2c41068
:recycle: refactor: remove unused parameter
luk3skyw4lker Aug 15, 2023
7fdb8aa
split config and apply a review
efectn Dec 23, 2023
931526a
apply reviews and add testcases
efectn Dec 23, 2023
508bddb
add benchmark
efectn Dec 23, 2023
2041679
cleanup
efectn Dec 23, 2023
e987a8e
rename middleware
efectn Dec 24, 2023
c33626b
fix linter
efectn Dec 24, 2023
013e362
Update docs and config values
gaby Jan 3, 2024
5c08790
Revert change to IsReady
gaby Jan 3, 2024
92e1cac
Updates based on code review
gaby Jan 3, 2024
87b1822
Update docs to match other middlewares
gaby Jan 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ const Version = "2.46.0"
// Handler defines a function to serve HTTP requests.
type Handler = func(*Ctx) error

// Probe Checker defines a function to check liveness or readiness of the application
luk3skyw4lker marked this conversation as resolved.
Show resolved Hide resolved
type ProbeChecker = func(*Ctx) bool

// Map is a shortcut for map[string]interface{}, useful for JSON returns
type Map map[string]interface{}

Expand Down Expand Up @@ -390,6 +393,26 @@ type Config struct {
//
// Optional. Default: DefaultMethods
RequestMethods []string

// Config for liveness probe of the container engine being used
//
// Optional. Default: func(c *Ctx) bool { return true }
IsLive ProbeChecker

// Config for liveness probe of the container engine being used
luk3skyw4lker marked this conversation as resolved.
Show resolved Hide resolved
//
// Optional. Default: /liveness
luk3skyw4lker marked this conversation as resolved.
Show resolved Hide resolved
IsLiveEndpoint string

// Config for readiness probe of the container engine being used
//
// Optional. Default: func(c *Ctx) bool { return true }
IsReady ProbeChecker

// Config for readiness probe of the container engine being used
luk3skyw4lker marked this conversation as resolved.
Show resolved Hide resolved
//
// Optional. Default: /readiness
luk3skyw4lker marked this conversation as resolved.
Show resolved Hide resolved
IsReadyEndpoint string
}

// Static defines configuration options when defining static assets.
Expand Down Expand Up @@ -453,8 +476,22 @@ const (
DefaultReadBufferSize = 4096
DefaultWriteBufferSize = 4096
DefaultCompressedFileSuffix = ".fiber.gz"
DefaultLivenessEndpoint = "/healthz"
DefaultReadinessEndpoint = "/readyz"
)

var DefaultHealthFunction = func(c *Ctx) bool { return true }
luk3skyw4lker marked this conversation as resolved.
Show resolved Hide resolved

var ProbeCheckerHandler = func(checker ProbeChecker) Handler {
luk3skyw4lker marked this conversation as resolved.
Show resolved Hide resolved
return func(c *Ctx) error {
if checker(c) {
return c.SendStatus(StatusOK)
}

return c.SendStatus(StatusServiceUnavailable)
}
}

// HTTP methods enabled by default
var DefaultMethods = []string{
MethodGet,
Expand Down Expand Up @@ -564,6 +601,15 @@ func New(config ...Config) *App {
if len(app.config.RequestMethods) == 0 {
app.config.RequestMethods = DefaultMethods
}
if app.config.IsLiveEndpoint == "" {
app.config.IsLiveEndpoint = DefaultLivenessEndpoint
}
if app.config.IsReadyEndpoint == "" {
app.config.IsReadyEndpoint = DefaultReadinessEndpoint
}
if app.config.IsLive == nil {
app.config.IsLive = DefaultHealthFunction
}

app.config.trustedProxiesMap = make(map[string]struct{}, len(app.config.TrustedProxies))
for _, ipAddress := range app.config.TrustedProxies {
Expand All @@ -580,6 +626,14 @@ func New(config ...Config) *App {
// Init app
app.init()

if app.config.IsReady != nil {
app.Get(app.config.IsReadyEndpoint, ProbeCheckerHandler(app.config.IsReady))
app.Options(app.config.IsReadyEndpoint, ProbeCheckerHandler(app.config.IsReady))
}

app.Get(app.config.IsLiveEndpoint, ProbeCheckerHandler(app.config.IsLive))
app.Options(app.config.IsLiveEndpoint, ProbeCheckerHandler(app.config.IsLive))

// Return app
return app
}
Expand Down
44 changes: 44 additions & 0 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1862,3 +1862,47 @@ func Test_Middleware_Route_Naming_With_Use(t *testing.T) {
}
}
}

func Test_Default_Liveness_Probe(t *testing.T) {
app := New()

resp, err := app.Test(httptest.NewRequest(MethodGet, "/healthz", nil))

utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, 200, resp.StatusCode)
}

func Test_Custom_Liveness_Probe(t *testing.T) {
app := New(Config{
IsLive: func(c *Ctx) bool { return true },
IsLiveEndpoint: "/live",
})

resp, err := app.Test(httptest.NewRequest(MethodGet, "/live", nil))

utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, 200, resp.StatusCode)
}

func Test_Default_Readiness_Probe(t *testing.T) {
app := New(Config{
IsReady: func(c *Ctx) bool { return true },
})

resp, err := app.Test(httptest.NewRequest(MethodGet, "/readyz", nil))

utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, 200, resp.StatusCode)
}

func Test_Custom_Readiness_Probe(t *testing.T) {
app := New(Config{
IsReady: func(c *Ctx) bool { return true },
IsReadyEndpoint: "/ready",
})

resp, err := app.Test(httptest.NewRequest(MethodGet, "/ready", nil))

utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, 200, resp.StatusCode)
}
23 changes: 23 additions & 0 deletions docs/api/app.md
Original file line number Diff line number Diff line change
Expand Up @@ -655,3 +655,26 @@ Hooks is a method to return [hooks](../guide/hooks.md) property.
```go title="Signature"
func (app *App) Hooks() *Hooks
```

## Liveness And Readiness Checks

Fiber comes with an out of the box way of implementing your liveness and readiness checking, when creating a new app, you can pass your own handlers for liveness and readiness checks:

```go title="Examples"
// Create route with GET method for test:
app := fiber.New(fiber.Config{
IsLive: func (c *fiber.Ctx) bool {
return true
},
IsLiveEndpoint: "/liveness",
luk3skyw4lker marked this conversation as resolved.
Show resolved Hide resolved
IsReady: func (c *fiber.Ctx) bool {
return serviceA.Ready() && serviceB.Ready() && ...
}
IsReadyEndpoint: "/readiness",
luk3skyw4lker marked this conversation as resolved.
Show resolved Hide resolved
})

// Listen on port :8080
app.Listen(":8080")
```

The endpoint values default to `/healthz` for liveness and `/readyz` for readiness. Both functions are optional, the liveness endpoint will return `true` right when the server is up and running but the readiness endpoint will not answer any requests if an `IsReady` function isn't provided.