health

package
v0.1.21 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 29, 2026 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package health provides a composable health-check mechanism.

Each infrastructure component (postgres, redis, …) supplies a Checker that reports its own name and reachability. Check runs all checkers and returns their combined results, suitable for a /health HTTP endpoint.

Basic usage:

router.GET("/health", func(c *gin.Context) {
    c.JSON(http.StatusOK, health.Check(
        c.Request.Context(),
        postgres.HealthChecker(),
    ))
})

Index

Examples

Constants

View Source
const StatusHealthy = 1

StatusHealthy is the status value returned when a component is reachable.

View Source
const StatusUnhealthy = 0

StatusUnhealthy is the status value returned when a component is unreachable.

Variables

This section is empty.

Functions

This section is empty.

Types

type Checker

type Checker func(ctx context.Context) Result

Checker is a function that returns the health status of one component. The Checker owns both its name and status; callers compose multiple checkers via Check without knowledge of individual component names.

type Result

type Result struct {
	Name   string `json:"name"`
	Status int    `json:"status"`
}

Result holds the health status of a single named component.

func Check

func Check(ctx context.Context, checkers ...Checker) []Result

Check runs each checker in registration order and returns their combined results. An empty checkers list returns an empty (non-nil) slice.

Example

ExampleCheck shows how to compose multiple health checkers for a /health endpoint. Each checker reports its own component name and status independently.

package main

import (
	"context"
	"fmt"

	"github.com/phcp-tech/common-library-golang/health"
)

func main() {
	mockDB := func(ctx context.Context) health.Result {
		return health.Result{Name: "postgres", Status: health.StatusHealthy}
	}
	mockRedis := func(ctx context.Context) health.Result {
		return health.Result{Name: "redis", Status: health.StatusHealthy}
	}

	results := health.Check(context.Background(), mockDB, mockRedis)
	for _, r := range results {
		fmt.Printf("%s: %d\n", r.Name, r.Status)
	}
}
Output:
postgres: 1
redis: 1

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL