util

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package util holds small dependency-free helpers shared by all packages: the injected Clock, dependency checks, the Slot container and numeric helpers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AlmostEqual

func AlmostEqual(a, b, eps float64) bool

AlmostEqual reports whether a and b differ by at most eps.

func Clamp

func Clamp(v, lo, hi float64) float64

Clamp limits v to [lo, hi].

func Midnight

func Midnight(t time.Time, loc *time.Location) time.Time

Midnight returns the start of the local day of t in loc.

func RequireAll

func RequireAll(component string, deps ...Dep) error

RequireAll returns a *DependencyError listing every dependency that is nil, including typed nil pointers, maps, funcs, channels and interfaces stored in an interface.

func Round

func Round(v float64, decimals int) float64

Round rounds v to the given number of decimals (half away from zero).

func Sleep

func Sleep(ctx context.Context, c Clock, d time.Duration) error

Sleep waits for d on clock c or returns ctx's error when ctx ends first.

Types

type Backoff

type Backoff struct {
	Min, Max time.Duration
	// Grace is the number of consecutive failures tolerated without a delay: a single
	// dropped request should not hold back the next one.
	Grace int
	// contains filtered or unexported fields
}

Backoff tracks exponential reconnect delays (min → max, doubling) on an injected clock. It is owned by one goroutine and not safe for concurrent use.

func (*Backoff) Fail

func (b *Backoff) Fail(now time.Time) time.Duration

Fail records a failure at now and returns the delay until the next attempt (0 while within the grace).

func (*Backoff) Next

func (b *Backoff) Next() time.Time

Next returns the time of the next allowed attempt.

func (*Backoff) Ready

func (b *Backoff) Ready(now time.Time) bool

Ready reports whether an attempt is allowed at now.

func (*Backoff) Reset

func (b *Backoff) Reset()

Reset clears the backoff after a success.

type Clock

type Clock interface {
	Now() time.Time
	NewTimer(d time.Duration) Timer
	NewTicker(d time.Duration) Ticker
}

Clock is the only source of time in the pipeline. Production code gets the real clock, tests a clocktest.Clock that they advance by hand.

func NewRealClock

func NewRealClock() Clock

NewRealClock returns the Clock backed by the system time.

type Dep

type Dep struct {
	Name  string
	Value any
}

Dep names one constructor dependency for RequireAll.

func D

func D(name string, value any) Dep

D is shorthand for a Dep.

type DependencyError

type DependencyError struct {
	Component string
	Missing   []string
}

DependencyError reports constructor dependencies that were nil.

func (*DependencyError) Error

func (e *DependencyError) Error() string

type Slot

type Slot[T any] struct {
	// contains filtered or unexported fields
}

Slot holds the current instance of a value that is swapped at runtime, for example a component rebuilt by the supervisor while readers keep going through the slot.

func (*Slot[T]) ClearIf

func (s *Slot[T]) ClearIf(match func(T) bool)

ClearIf removes the current value only while match reports it as the caller's own, so a late clear by a replaced instance never withdraws its successor.

func (*Slot[T]) Load

func (s *Slot[T]) Load() (T, bool)

Load returns the current value and whether one is set.

func (*Slot[T]) Store

func (s *Slot[T]) Store(v T)

Store publishes v as the current value.

type Ticker

type Ticker interface {
	C() <-chan time.Time
	Stop()
	Reset(d time.Duration)
}

Ticker mirrors time.Ticker behind an interface.

type Timer

type Timer interface {
	C() <-chan time.Time
	Stop() bool
	Reset(d time.Duration) bool
}

Timer mirrors time.Timer behind an interface.

Directories

Path Synopsis
Package clocktest provides a manually driven util.Clock for tests: time only moves when the test calls Advance or Set, so polling, backoff and debounce run without sleeps.
Package clocktest provides a manually driven util.Clock for tests: time only moves when the test calls Advance or Set, so polling, backoff and debounce run without sleeps.

Jump to

Keyboard shortcuts

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