Hone logo
Hone
Problems

Go Liveness Probe Implementation

Kubernetes and other container orchestration platforms rely on liveness probes to determine if a container is still running and healthy. A liveness probe periodically checks the application's status and signals whether it needs to be restarted. This challenge asks you to implement a simple liveness probe in Go that simulates a health check endpoint.

Problem Description

You need to create a Go program that exposes a simple HTTP endpoint (/health) which acts as a liveness probe. This endpoint should return a 200 OK status code if the application is considered "alive" and a 500 Internal Server Error status code if it's considered "dead." The "alive" or "dead" state should be controlled by a global variable isAlive which is initialized to true. The program should listen on port 8080. The program should also include a mechanism to simulate a failure – after 10 seconds of running, set isAlive to false.

Key Requirements:

  • Implement an HTTP server.
  • Expose a /health endpoint.
  • Return 200 OK if isAlive is true.
  • Return 500 Internal Server Error if isAlive is false.
  • Simulate a failure by setting isAlive to false after 10 seconds.
  • Listen on port 8080.

Expected Behavior:

  • Initially, the /health endpoint should return 200 OK.
  • After 10 seconds, the /health endpoint should start returning 500 Internal Server Error.
  • The server should continue running indefinitely (until manually stopped).

Edge Cases to Consider:

  • Handle potential errors during server startup (e.g., port already in use). While error handling doesn't need to be extensive, it should prevent the program from crashing.
  • Ensure the isAlive variable is correctly updated after the 10-second delay.

Examples

Example 1:

Input:  (Program running, sending a GET request to /health before 10 seconds)
Output: HTTP 200 OK
Explanation: The program is running and isAlive is true.

Example 2:

Input: (Program running, sending a GET request to /health after 10 seconds)
Output: HTTP 500 Internal Server Error
Explanation: The program has been running for 10 seconds, isAlive is false.

Example 3: (Edge Case)

Input: (Attempting to start the server on a port already in use)
Output: Error message indicating the port is already in use (printed to console). The program should not crash.
Explanation: The server fails to start due to a port conflict, but the program handles the error gracefully.

Constraints

  • The program must be written in Go.
  • The program must listen on port 8080.
  • The isAlive variable must be a global variable.
  • The failure simulation (setting isAlive to false) must occur exactly 10 seconds after the program starts.
  • The program should be reasonably efficient; avoid unnecessary resource consumption.

Notes

  • Consider using the net/http package for creating the HTTP server.
  • You can use the time.Sleep function to introduce the 10-second delay.
  • Focus on the core functionality of the liveness probe. Advanced features like logging or configuration are not required.
  • Think about how to handle potential errors when starting the server. A simple error message printed to the console is sufficient.
Loading editor...
go