Skip to content

SumonRayy/ping-pong-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Ping-Pong-Go

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.

Features

  • 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

Installation

As a Library

go get github.com/SumonRayy/ping-pong-go

As a CLI Tool

# 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/pingpong

Usage

As a Library

package 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()
}

As a CLI Tool

# 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"

Environment Variables

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)

Command-line Flags

  • --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

Project Structure

.
├── cmd/
│   └── pingpong/          # CLI application
│       └── main.go
├── pkg/
│   └── pingpong/          # Library package
│       ├── pingpong.go    # Main package code
│       └── pingpong_test.go
├── go.mod
├── go.sum
└── README.md

Health Check

The service exposes a health check endpoint at /health. It returns:

  • 200 OK if the service is healthy (last successful ping within 15 minutes)
  • 503 Service Unavailable if the service is unhealthy

Testing

Run the tests using:

go test ./...

Docker Support

Build and run using Docker:

docker build -t ping-pong-go .
docker run -p 8080:8080 ping-pong-go

License

MIT License - see LICENSE file for details

Author

SumonRayy

🙏 Thanks for checking out the project!

⭐ Give it a star if you like it!

Follow me:

GitHub Buy Me A Coffee Website

About

A robust health check service that pings a specified server and maintains its own health status.

Topics

Resources

License

Stars

Watchers

Forks

Contributors