Skip to main content

Overview

The healthcheck middleware provides liveness and readiness probe endpoints for container orchestration platforms like Kubernetes. It supports custom health checks for databases, caches, and other dependencies. Use it when you need:
  • Kubernetes liveness/readiness probes
  • Load balancer health checks
  • Dependency health monitoring

Installation

Quick Start

Configuration

Options

Check

Examples

Basic Endpoints

With Database Check

Multiple Dependencies

Custom Paths

Standalone Handlers

Using DBCheck Helper

API Reference

Functions

Response Format

Liveness Response

Readiness Response (Success)

Readiness Response (Failure)

HTTP Status Codes

Kubernetes Configuration

Technical Details

Implementation Architecture

The healthcheck middleware is designed with the following technical characteristics:
  • Concurrent Check Execution: All health checks run in parallel using goroutines with a sync.WaitGroup to coordinate execution
  • Thread-Safe Results: A sync.Mutex protects the results map during concurrent check execution
  • Context-Based Timeouts: Each check runs with its own context.WithTimeout to prevent hanging checks
  • Default Timeout: If no timeout is specified, checks default to 5 seconds
  • Status Code Mapping: Returns HTTP 200 for healthy, HTTP 503 for unhealthy states

Core Components

  1. Check Struct: Defines a health check with name, check function, and timeout
  2. Status Struct: JSON response structure with overall status and individual check results
  3. Options Struct: Configuration for endpoint paths and checks list
  4. Liveness Handler: Simple text response (β€œok”) with no dependency checks
  5. Readiness Handler: Executes all checks concurrently and aggregates results

Helper Functions

  • DBCheck: Creates a database health check with a 5-second default timeout
  • HTTPCheck: Creates an HTTP endpoint health check with a 10-second default timeout
  • New: Creates a combined handler that routes to liveness or readiness based on path
  • Register: Convenience function to register both endpoints on a router

Best Practices

  • Keep liveness checks simple (no external dependencies)
  • Include all critical dependencies in readiness checks
  • Set appropriate timeouts for each check
  • Use readiness to control traffic during startup/shutdown

Testing

Test Cases