clientx

package
v0.12.1 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package clientx provides shared client-side reliability helpers.

Index

Constants

This section is empty.

Variables

View Source
var ErrCircuitOpen = errors.New("circuit breaker open")
View Source
var ErrNilOperation = errors.New("clientx: operation is nil")

Functions

func CodeOf

func CodeOf(err error) int

CodeOf returns a normalized code from err when possible.

func Do

func Do(ctx context.Context, cfg RetryConfig, fn func(context.Context) error) error

Do executes fn with retry. MaxAttempts includes the first attempt.

func IsRetryable

func IsRetryable(err error) bool

IsRetryable reports whether err is generally safe to retry.

func MapGRPCError

func MapGRPCError(err error) error

MapGRPCError maps gRPC errors to standard response codes.

func NewError

func NewError(code int, message string, err error) error

NewError creates a normalized client error.

Types

type Breaker

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

Breaker is a small client-side circuit breaker.

func NewBreaker

func NewBreaker(cfg BreakerConfig) *Breaker

NewBreaker creates a circuit breaker.

func (*Breaker) Do

func (b *Breaker) Do(ctx context.Context, fn func(context.Context) error) error

Do executes fn when the circuit allows it.

func (*Breaker) State

func (b *Breaker) State() BreakerState

State returns the current breaker state.

type BreakerConfig

type BreakerConfig struct {
	FailureThreshold int
	Cooldown         time.Duration
	Now              func() time.Time
	ShouldCount      func(error) bool
}

BreakerConfig configures a circuit breaker.

type BreakerState

type BreakerState int
const (
	BreakerClosed BreakerState = iota
	BreakerOpen
	BreakerHalfOpen
)

type ClientError

type ClientError struct {
	Code    int
	Message string
	Err     error
}

ClientError is a normalized outbound service call error.

func (*ClientError) Error

func (e *ClientError) Error() string

func (*ClientError) Unwrap

func (e *ClientError) Unwrap() error

type RetryConfig

type RetryConfig struct {
	MaxAttempts    int
	InitialBackoff time.Duration
	MaxBackoff     time.Duration
	Multiplier     float64
	Retryable      func(error) bool
	Sleep          func(context.Context, time.Duration) error
	JitterFraction float64
	DisableJitter  bool
}

RetryConfig configures retry behavior for outbound service calls.

Jump to

Keyboard shortcuts

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