hc

package module
v0.1.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 18, 2022 License: MIT Imports: 2 Imported by: 6

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.

// 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 downstream 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.

// PgDownstreamService holds logic of interaction 
// with Postgres database.
type PgDownstreamService struct {
    db *pgxpool.Pool
}

func (s *PgDownstreamService) 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 downstream health checks.

checker := hc.NewMultiChecker(pgDownstrean, storageDownstream)

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

Documentation

Overview

Package hc represents logic of making the health check.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HealthChecker

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
}

HealthChecker represents logic of making the health check.

type MultiChecker

type MultiChecker struct {
	// contains filtered or unexported fields
}

MultiChecker takes multiple health checker and performs health checks for each of them concurrently.

func NewMultiChecker

func NewMultiChecker(hcs ...HealthChecker) *MultiChecker

NewMultiChecker takes several health checkers and performs health checks for each of them concurrently.

func (*MultiChecker) Add added in v0.1.3

func (c *MultiChecker) Add(hc HealthChecker)

Add appends health HealthChecker to internal slice of HealthCheckers.

func (*MultiChecker) Health

func (c *MultiChecker) Health(ctx context.Context) error

Health takes the context and performs the health check. Returns nil in case of success or an error in case of a failure.

type NopChecker added in v0.1.4

type NopChecker struct{}

NopChecker represents nop health checker.

func NewNopChecker added in v0.1.5

func NewNopChecker() NopChecker

NewNopChecker returns new instance of NopChecker.

func (NopChecker) Health added in v0.1.4

func (n NopChecker) Health(context.Context) error

type Synchronizer

type Synchronizer struct {
	// contains filtered or unexported fields
}

Synchronizer holds synchronization mechanics.

func (*Synchronizer) Add

func (s *Synchronizer) Add(delta int)

Add wraps the sync.WaitGroup Add method.

func (*Synchronizer) Cancel added in v0.1.2

func (s *Synchronizer) Cancel()

Cancel calls underlying cancel function to cancel context, which passed to all health checks function.

func (*Synchronizer) Do

func (s *Synchronizer) Do(f func())

Do wrap the sync.Once Do method.

func (*Synchronizer) Done

func (s *Synchronizer) Done()

Done wraps the sync.WaitGroup Done method.

func (*Synchronizer) Error

func (s *Synchronizer) Error() string

Error returns a string representation of underlying error. Implements builtin error interface.

func (*Synchronizer) SetError

func (s *Synchronizer) SetError(err error)

SetError sets an error to the Synchronizer structure. Uses sync.Once to set error only once.

func (*Synchronizer) Wait

func (s *Synchronizer) Wait()

Wait wraps the sync.WaitGroup Wait method.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL