probes

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2021 License: MIT Imports: 2 Imported by: 0

README

Probes

codecov Test Action Status

Probes is a simple package to implement readiness and liveness endpoints.

⚙️ Installation

go get -u czechia.dev/probes

👀 Example

package main

import (
	"time"

	"czechia.dev/probes"
	"github.com/gofiber/fiber/v2"
)

func NewFiberRoute(p *probes.Probe) func(ctx *fiber.Ctx) error {
	return func(ctx *fiber.Ctx) error {
		switch p.IsDown() {
		case false:
			return ctx.SendStatus(fiber.StatusOK)
		case true:
			return ctx.SendStatus(fiber.StatusServiceUnavailable)
		}
	}
}

func main() {
	go probes.RunProbe(probes.Liveness)
	go probes.RunProbe(probes.Readiness)

	app := fiber.New()
	app.Get("/liveness", NewFiberRoute(probes.Liveness))
	app.Get("/readiness", NewFiberRoute(probes.Readiness))
	go app.Listen(":8080")

	for ; true; <-time.NewTicker(3 * time.Second).C {
		probes.LivenessProbe(probes.Liveness)
		probes.ReadinessProbe(probes.Readiness, func() error { return nil })
	}
}

Documentation

Index

Constants

View Source
const (
	Up   status = "UP"
	Down status = "DOWN"
)

Variables

This section is empty.

Functions

func LivenessProbe

func LivenessProbe(liveness *Probe, readiness ...*Probe) error

LivenessProbe sets the given Liveness probe to DOWN if any given Readiness probe is DOWN for more than 5 minutes.

This function typically runs in its own goroutine. The return parameter may be used for tests.

func ReadinessProbe

func ReadinessProbe(p *Probe, tests ...func() error) error

ReadinessProbe runs any test functions passed to it. If any of the tests fail the given probe is set to DOWN.

This function typically runs in its own goroutine. The return parameter may be used for tests.

func RunProbe

func RunProbe(p *Probe) error

RunProbe waits for status messages on the probe channel.

The probe status is updated if the recieved status differs from the current status. The probe timestamp is also updated when the status changes to DOWN.

This function typically runs in its own goroutine. The return parameter may be used for tests.

Types

type Probe

type Probe struct {
	// contains filtered or unexported fields
}
var (
	Liveness  *Probe
	Readiness *Probe
)

func NewProbe

func NewProbe(n string, s status) *Probe

NewProbe initializes a new status probe.

func (*Probe) Chan

func (p *Probe) Chan() chan<- status

Chan exposes the write end of the probe channel.

func (*Probe) Downtime

func (p *Probe) Downtime() time.Duration

Downtime is the duration since the probe went DOWN.

func (*Probe) IsDown

func (p *Probe) IsDown() bool

IsDown tests if the probe status is DOWN.

func (*Probe) IsUp added in v1.0.1

func (p *Probe) IsUp() bool

IsUp tests if the probe status is UP.

Jump to

Keyboard shortcuts

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