A health check service that implements a ping-pong mechanism between services. It can be used to monitor the health of distributed systems by having services ping each other at configurable intervals.
- Configurable ping intervals
- Custom headers for ping requests
- Health check endpoints
- Consecutive failure tracking
- Graceful shutdown on repeated failures
- Colored logging output
- Environment variable and flag-based configuration
go get github.com/SumonRayy/ping-pong-go# Install the CLI tool
go install github.com/SumonRayy/ping-pong-go/cmd/pingpong@latest
# Or clone and build from source
git clone https://github.com/SumonRayy/ping-pong-go.git
cd ping-pong-go
go install ./cmd/pingpongpackage main
import (
"context"
"time"
"github.com/SumonRayy/ping-pong-go/pkg/pingpong"
)
func main() {
config := pingpong.Config{
ServerURL: "http://example.com/health",
OwnURL: "http://localhost:8080/health",
PingInterval: 2 * time.Second,
MaxConsecutiveFails: 3,
MaxRetries: 3,
}
service := pingpong.NewService(config)
ctx := context.Background()
if err := service.Start(ctx); err != nil {
panic(err)
}
// ... your application code ...
service.Stop()
}# Basic usage
pingpong
# With custom configuration
pingpong --server-url="http://example.com/health" \
--ping-interval="5000" \
--own-url="http://localhost:8080/health" \
--max-retries="5" \
--max-consecutive-fails="3"You can configure the service using environment variables:
SERVER_URL: URL of the server to ping (default: "http://localhost:8081/health")OWN_URL: URL of your own health check endpoint (default: "http://localhost:8080/health")PING_INTERVAL: Ping interval in milliseconds (default: 2000)MAX_RETRIES: Maximum number of retries for each ping (default: 3)MAX_CONSECUTIVE_FAILS: Maximum number of consecutive failures before shutdown (default: 3)
--server-url: Server URL to ping--ping-interval: Ping interval in milliseconds--own-url: Own health check URL--max-retries: Maximum number of retries--max-consecutive-fails: Maximum number of consecutive failures before shutdown
.
├── cmd/
│ └── pingpong/ # CLI application
│ └── main.go
├── pkg/
│ └── pingpong/ # Library package
│ ├── pingpong.go # Main package code
│ └── pingpong_test.go
├── go.mod
├── go.sum
└── README.md
The service exposes a health check endpoint at /health. It returns:
200 OKif the service is healthy (last successful ping within 15 minutes)503 Service Unavailableif the service is unhealthy
Run the tests using:
go test ./...Build and run using Docker:
docker build -t ping-pong-go .
docker run -p 8080:8080 ping-pong-goMIT License - see LICENSE file for details
Follow me: