https://github.com/heartwilltell/hc
👩⚕️👨⚕️ Mission-critical concurrent health checks synchronization
https://github.com/heartwilltell/hc
go golang health-check health-checks healthcheck healthchecks
Last synced: 6 months ago
JSON representation
👩⚕️👨⚕️ Mission-critical concurrent health checks synchronization
- Host: GitHub
- URL: https://github.com/heartwilltell/hc
- Owner: heartwilltell
- License: mit
- Created: 2020-05-26T13:13:02.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2025-05-02T10:36:33.000Z (about 1 year ago)
- Last Synced: 2025-05-02T11:42:12.609Z (about 1 year ago)
- Topics: go, golang, health-check, health-checks, healthcheck, healthchecks
- Language: Go
- Homepage: https://pkg.go.dev/github.com/heartwilltell/hc?tab=doc
- Size: 38.1 KB
- Stars: 31
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 👩⚕️👨⚕️ hc
`hc` is a tiny library for synchronization of mission critical concurrent health checks
The `HealthChecker` interface is a heart of this small library.
```go
// HealthChecker represents logic of making the health check.
type HealthChecker interface {
// Health takes the context and performs the health check.
// Returns nil in case of success or an error in case
// of a failure.
Health(ctx context.Context) error
}
```
## Usage
Let's say that we have a web application with some upstream services (database, remote storage etc.),
Work of these services is critical for our application. So we need to check if they are reachable and healthy,
to provide the overall service health check information to orchestrator or load balancer.
With `hc` it is simple. You just need to implement the `HealthChecker` interface for you're downstream.
```go
// PgUpstreamService holds logic of interaction
// with Postgres database.
type PgUpstreamService struct {
db *pgxpool.Pool
}
func (s *PgUpstreamService) Health(ctx context.Context) error {
conn, err := s.db.Acquire(ctx)
if err != nil {
return fmt.Errorf("unable to aquire connection from pool: %w", err)
}
defer conn.Release()
q := `SELECT count(*) FROM information_schema.tables WHERE table_type='public';`
var count int
if err := conn.QueryRow(ctx, q).Scan(&count); err != nil {
return fmt.Errorf("query failed: %w", err)
}
return nil
}
```
Now in your http server health check endpoint you just need to gather information about all upstream health checks.
```go
checker := hc.NewMultiChecker(pgUpstream, storageUpstream)
mux := http.NewServeMux()
mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
if err := checker.Health(r.Context()); err != nil {
w.WriteHeader(http.StatusServiceUnavailable)
return
}
w.WriteHeader(http.StatusOK)
})
```