Hone logo
Hone
Problems

Robust Health Endpoint Implementation in Go

Building reliable and scalable applications often requires implementing health checks. These endpoints allow monitoring systems and load balancers to determine if an application instance is healthy and ready to serve traffic. This challenge asks you to implement a simple, yet robust, health check endpoint in Go, incorporating configurable checks and graceful degradation.

Problem Description

You are tasked with creating a Go program that exposes a health check endpoint. This endpoint should return a 200 OK status code if the application is considered healthy. Healthiness is determined by a configurable number of dependencies. The application is considered healthy only if all dependencies are healthy. If any dependency is unhealthy, the endpoint should return a 503 Service Unavailable status code.

The dependencies are represented as a slice of functions. Each function, when called, returns a boolean indicating whether the dependency is healthy. The health check endpoint should call each dependency function and aggregate the results.

Key Requirements:

  • Configurable Dependencies: The program should accept a slice of functions as input, representing the dependencies to check.
  • All Dependencies Must Be Healthy: The endpoint should only return 200 OK if all dependency functions return true.
  • 503 Service Unavailable: If any dependency function returns false, the endpoint should return a 503 Service Unavailable status code.
  • HTTP Endpoint: The health check should be exposed via an HTTP endpoint at /health.
  • Graceful Degradation: The program should handle potential errors within the dependency functions gracefully (e.g., panics, network errors). If a dependency function panics, treat it as unhealthy.
  • Clear Error Reporting (Optional): While not strictly required, providing some indication of which dependency failed would be beneficial for debugging.

Expected Behavior:

  • When the application starts, it should listen for HTTP requests on /health.
  • Upon receiving a request to /health, it should execute all dependency functions.
  • If all dependencies are healthy, it should return a 200 OK response.
  • If any dependency is unhealthy (returns false or panics), it should return a 503 Service Unavailable response.

Examples

Example 1:

Input: dependencies = []func()bool{func()bool { return true }, func()bool { return true }}
Output: 200 OK
Explanation: Both dependencies return true, so the endpoint returns 200 OK.

Example 2:

Input: dependencies = []func()bool{func()bool { return true }, func()bool { return false }}
Output: 503 Service Unavailable
Explanation: One dependency returns false, so the endpoint returns 503 Service Unavailable.

Example 3: (Edge Case - Dependency Panics)

Input: dependencies = []func()bool{func()bool { return true }, func()bool { panic("Dependency failed") }}
Output: 503 Service Unavailable
Explanation: The second dependency panics. The health check treats panics as unhealthy and returns 503.

Constraints

  • The program must be written in Go.
  • The HTTP server should listen on port 8080 (configurable via environment variable PORT, defaulting to 8080).
  • The dependency functions should be provided as a slice of func()bool at program startup.
  • The program should handle panics within dependency functions without crashing.
  • The response body for 503 should be a simple text message: "Service Unavailable".

Notes

  • Consider using the net/http package for creating the HTTP server and handling requests.
  • Error handling is crucial. Ensure that panics within dependency functions are caught and handled gracefully.
  • Think about how to structure your code to make it easy to add or remove dependencies in the future.
  • You can use a simple testing framework (like the built-in testing package) to verify the functionality of your health check endpoint. Focus on testing both healthy and unhealthy scenarios.
  • While detailed error reporting is optional, consider how you might extend the solution to provide more information about which dependencies are failing.
Loading editor...
go