utils

package
v0.15.1 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package utils provides utility functions for resource cleanup and error handling. This file contains helpers for ignoring errors from cleanup operations like Close().

This file provides a generic fan-in helper for collecting answers from a fixed number of concurrent workers, bounded by a timeout and a caller-supplied context.

This file provides utilities for computing SHA256 hashes. Hashable is a byte slice that can compute its own hash. Hasher provides a builder pattern for incrementally constructing hashes from various data types.

This file provides retry logic with exponential backoff and context cancellation support. RetryRunner executes functions with configurable retry strategies, supporting both fixed and exponential backoff delays with a maximum delay cap.

Index

Constants

View Source
const DefaultAnswersCollectorTimeout = 30 * time.Second

DefaultAnswersCollectorTimeout is applied by NewAnswersCollector when the caller passes a non-positive timeout, so a collector is never accidentally unbounded.

View Source
const Infinitely = -1

Variables

View Source
var ErrAnswersCollectorCanceled = errors.New("canceled while waiting for answers")

ErrAnswersCollectorCanceled is returned (wrapped) by Collect when the supplied context is done before all expected answers arrive.

View Source
var ErrAnswersCollectorTimeout = errors.New("timed out waiting for answers")

ErrAnswersCollectorTimeout is returned (wrapped) by Collect when the timeout elapses before all expected answers arrive.

View Source
var ErrMaxRetriesExceeded = errors.New("maximum number of retries exceeded")

Functions

func IdentityFunc

func IdentityFunc[T any]() func(T) T

func IgnoreError

func IgnoreError(fn func() error)

IgnoreError runs a function that returns an error and silently ignores the error. It is intended to be used when the error from a cleanup operation (e.g., Close) is non-critical and can be safely discarded.

Example:

defer IgnoreError(file.Close)

func IgnoreErrorWithOneArg

func IgnoreErrorWithOneArg[T any](fn func(t T) error, t T)

func IsNil

func IsNil(value any) bool

IsNil returns true for nil values and interfaces containing typed nil values.

func NewRetryRunner

func NewRetryRunner(logger logging2.Logger, maxTimes int, delay time.Duration, expBackoff bool) *retryRunner

func NewRetryRunnerWithJitter

func NewRetryRunnerWithJitter(logger logging2.Logger, maxTimes int, initialDelay, maxDelay time.Duration, backoffMultiplier, jitterFactor float64) *retryRunner

NewRetryRunnerWithJitter creates a retry runner with configurable jitter and backoff multiplier. jitterFactor should be between 0.0 and 1.0, where 0.0 means no jitter and 1.0 means maximum jitter. backoffMultiplier controls the exponential growth rate (e.g., 2.0 doubles the delay each time).

Types

type Answer added in v0.15.1

type Answer[K comparable, T any] struct {
	Key   K
	Value T
	Err   error
}

Answer is a single worker's outcome, keyed by K (e.g. a party identifier).

type AnswersCollector added in v0.15.1

type AnswersCollector[K comparable, T any] struct {
	// contains filtered or unexported fields
}

AnswersCollector collects a fixed number of Answer values sent concurrently by worker goroutines, enforcing a timeout and honoring context cancellation. It replaces the pattern of a bare `<-channel` receive (which can block forever) or a receive `select`-ed only against ctx.Done() (which never returns if the caller's context has no deadline and a worker goes silent).

The underlying channel is buffered to the collector's capacity, so workers that are still in flight when Collect returns (on timeout or cancellation) can always complete their Send and exit without leaking a goroutine.

func NewAnswersCollector added in v0.15.1

func NewAnswersCollector[K comparable, T any](capacity int, timeout time.Duration) *AnswersCollector[K, T]

NewAnswersCollector returns a collector sized for up to `capacity` concurrent senders. If timeout is <= 0, DefaultAnswersCollectorTimeout is used instead.

func (*AnswersCollector[K, T]) Collect added in v0.15.1

func (c *AnswersCollector[K, T]) Collect(ctx context.Context, count int) ([]Answer[K, T], error)

Collect waits for exactly `count` answers, returning them in arrival order. It returns early with a wrapped ErrAnswersCollectorTimeout or ErrAnswersCollectorCanceled if the timeout elapses or ctx is done first.

func (*AnswersCollector[K, T]) Send added in v0.15.1

func (c *AnswersCollector[K, T]) Send(key K, value T, err error)

Send records a worker's outcome. It never blocks as long as the number of Send calls does not exceed the capacity passed to NewAnswersCollector.

type Hashable

type Hashable []byte

func (Hashable) Raw

func (id Hashable) Raw() []byte

func (Hashable) RawString

func (id Hashable) RawString() string

func (Hashable) String

func (id Hashable) String() string

type Hasher

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

func NewSHA256Hasher

func NewSHA256Hasher() *Hasher

func (*Hasher) AddBool

func (h *Hasher) AddBool(b bool) (int, error)

func (*Hasher) AddBytes

func (h *Hasher) AddBytes(b []byte) error

func (*Hasher) AddFloat64

func (h *Hasher) AddFloat64(f float64) error

func (*Hasher) AddG1s

func (h *Hasher) AddG1s(generators []*math.G1) error

func (*Hasher) AddInt

func (h *Hasher) AddInt(i int) error

func (*Hasher) AddInt32

func (h *Hasher) AddInt32(i int32) error

func (*Hasher) AddString

func (h *Hasher) AddString(s string) error

func (*Hasher) AddUInt64

func (h *Hasher) AddUInt64(i uint64) error

func (*Hasher) Digest

func (h *Hasher) Digest() []byte

func (*Hasher) HexDigest

func (h *Hasher) HexDigest() string

type RetryRunner

type RetryRunner interface {
	Run(func() error) error
	// RunWithContext retries like Run but stops early if ctx is canceled.
	RunWithContext(ctx context.Context, runner func() error) error
	// RunWithErrors retries until runner returns true or maxTimes is exhausted.
	RunWithErrors(runner func() (bool, error)) error
	// RunWithErrorsContext retries like RunWithErrors but stops early if ctx is canceled.
	RunWithErrorsContext(ctx context.Context, runner func() (bool, error)) error
}

RetryRunner receives a function that potentially fails and retries according to the specified strategy

Directories

Path Synopsis
This file implements a no-op cache that always misses and never stores values.
This file implements a no-op cache that always misses and never stores values.
This file implements a generic, thread-safe registry for storing and retrieving drivers by key.
This file implements a generic, thread-safe registry for storing and retrieving drivers by key.
This file provides JSON unmarshaling with strict validation.
This file provides JSON unmarshaling with strict validation.
This file provides utilities for converting between domain objects and protobuf messages.
This file provides utilities for converting between domain objects and protobuf messages.
mock
Code generated by counterfeiter.
Code generated by counterfeiter.
This file provides generic slice utility functions.
This file provides generic slice utility functions.
types
This file provides utilities for running views with timeout support.
This file provides utilities for running views with timeout support.

Jump to

Keyboard shortcuts

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