Skip to content

Health Check Package

Low-level health check implementations for different protocols and services in GoDoxy.

Overview

Purpose

This package provides health check implementations for various protocols:

  • HTTP/HTTPS - Standard HTTP health checks with fasthttp
  • H2C - HTTP/2 cleartext health checks
  • Docker - Container health status via Docker API
  • FileServer - Directory accessibility checks
  • Stream - Generic network connection checks

Primary Consumers

  • internal/health/monitor/ - Route health monitoring
  • internal/metrics/uptime/ - Uptime poller integration

Non-goals

  • Complex health check logic (response body validation, etc.)
  • Authentication/authorization in health checks
  • Multi-step health checks (login then check)

Stability

Internal package. Public functions are stable but may be extended with new parameters.

Public API

HTTP Health Check (http.go)

go
func HTTP(
    url *url.URL,
    method string,
    path string,
    timeout time.Duration,
) (types.HealthCheckResult, error)

H2C Health Check (http.go)

go
func H2C(
    ctx context.Context,
    url *url.URL,
    method string,
    path string,
    timeout time.Duration,
) (types.HealthCheckResult, error)

Docker Health Check (docker.go)

go
func Docker(
    ctx context.Context,
    containerID string,
) (types.HealthCheckResult, error)

FileServer Health Check (fileserver.go)

go
func FileServer(
    url *url.URL,
) (types.HealthCheckResult, error)

Stream Health Check (stream.go)

go
func Stream(
    url *url.URL,
) (types.HealthCheckResult, error)

Common Types (internal/types/)

go
type HealthCheckResult struct {
    Healthy  bool
    Latency  time.Duration
    Detail   string
}

type HealthStatus int

const (
    StatusHealthy   HealthStatus = 0
    StatusUnhealthy HealthStatus = 1
    StatusError     HealthStatus = 2
)

Architecture

HTTP Health Check Flow

Docker Health Check Flow

H2C Health Check Flow

FileServer Health Check Flow

Stream Health Check Flow

Configuration Surface

No explicit configuration per health check. Parameters are passed directly:

Check TypeParameters
HTTPURL, Method, Path, Timeout
H2CContext, URL, Method, Path, Timeout
DockerContext, ContainerID
FileServerURL (path component used)
StreamURL (scheme, host, port used)

HTTP Headers

All HTTP/H2C checks set:

  • User-Agent: GoDoxy/<version>
  • Accept: text/plain,text/html,*/*;q=0.8
  • Accept-Encoding: identity
  • Cache-Control: no-cache
  • Pragma: no-cache

Dependency and Integration Map

External Dependencies

  • github.com/valyala/fasthttp - High-performance HTTP client
  • golang.org/x/net/http2 - HTTP/2 transport
  • Docker socket (for Docker health check)

Internal Dependencies

  • internal/types/ - Health check result types
  • goutils/version/ - User-Agent version

Observability

Logs

No direct logging in health check implementations. Errors are returned as part of HealthCheckResult.Detail.

Metrics

  • Check latency (returned in result)
  • Success/failure rates (tracked by caller)

Security Considerations

  • TLS certificate verification skipped (InsecureSkipVerify: true)
  • Docker socket access required for Docker health check
  • No authentication in health check requests
  • User-Agent identifies GoDoxy for server-side filtering

Failure Modes and Recovery

HTTP/H2C

Failure ModeResultNotes
Connection timeoutUnhealthyDetail: timeout message
TLS certificate errorHealthyHandled gracefully
5xx responseUnhealthyDetail: status text
4xx responseHealthyClient error considered healthy

Docker

Failure ModeResultNotes
API call failureErrorThrows error to caller
Container not runningUnhealthyState: "Not Started"
Container dead/exitedUnhealthyState logged
No health check configuredErrorRequires health check in image

FileServer

Failure ModeResultNotes
Path not foundUnhealthyDetail: "path not found"
Permission deniedErrorReturned to caller
Other OS errorErrorReturned to caller

Stream

Failure ModeResultNotes
Connection refusedUnhealthyDetail: error message
Network unreachableUnhealthyDetail: error message
DNS resolution failureUnhealthyDetail: error message
Context deadlineUnhealthyDetail: timeout

Usage Examples

HTTP Health Check

go
url, _ := url.Parse("http://localhost:8080/health")
result, err := healthcheck.HTTP(url, "GET", "/health", 10*time.Second)
if err != nil {
    fmt.Printf("Error: %v\n", err)
}
fmt.Printf("Healthy: %v, Latency: %v, Detail: %s\n",
    result.Healthy, result.Latency, result.Detail)

H2C Health Check

go
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

url, _ := url.Parse("h2c://localhost:8080")
result, err := healthcheck.H2C(ctx, url, "GET", "/health", 10*time.Second)

Docker Health Check

go
ctx := context.Background()
result, err := healthcheck.Docker(ctx, "abc123def456")

FileServer Health Check

go
url, _ := url.Parse("file:///var/www/html")
result, err := healthcheck.FileServer(url)

Stream Health Check

go
url, _ := url.Parse("tcp://localhost:5432")
result, err := healthcheck.Stream(url)

Testing Notes

  • Unit tests for each health check type
  • Mock Docker server for Docker health check tests
  • Integration tests require running services
  • Timeout handling tests

Released under the MIT License.