faults

package
v0.10.1 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2025 License: BSD-3-Clause Imports: 12 Imported by: 0

Documentation

Overview

Package faults provides common headers and client-side fault injection functionality.

Index

Constants

View Source
const FaultHeader = "x-bp-fault"

FaultHeader is the sole header used for fault injection configuration. It is formatted as a list of <key=value> pairs delimited by <;>, with support for the following keys:

a = [Required] Server address of outgoing request.

Used to determine whether the current request should have a fault injected.

m = [Optional] Method of outgoing request.

Used to determine whether the current request should have a fault injected.

d = [Optional] Number of milliseconds to delay the outgoing request, if matching.

D = [Optional] Percentage chance to delay outgoing request, if matching.

Only integers within [0-100] allowed.

f = [Optional] Abort current outgoing request and return this response code, if matching.

Only integers within [0-599] allowed.

b = [Optional] Message to return with the aborted request response, if matching.

Only US-ASCII allowed, excluding semicolon <;>, comma <,>, and equal sign <=>.

F = [Optional] Percentage chance to abort outgoing request, if matching.

Only integers within [0-100] allowed.

Example:

x-bp-fault: a=foo.bar;m=MyMethod;f=500;b=Fault injected!;F=50

A request for MyMethod on service foo.bar will fail 50% of the time with a 500 response
containing the body message "Fault injected!".

Variables

This section is empty.

Functions

func WithDefaultAbort

func WithDefaultAbort[T any](fn Abort[T]) func(*Injector[T])

WithDefaultAbort is an option to set the default abort function for the Injector.

Types

type Abort

type Abort[T any] func(code int, message string) (T, error)

Abort is the function type to inject a protocol-specific fault with the given code and message.

type Headers

type Headers interface {
	// LookupValues returns the values of a protocol-specific header with
	// the given key.
	LookupValues(ctx context.Context, key string) ([]string, error)
}

Headers is an interface to be implemented by the caller to allow protocol-specific header lookup. Using an interface here rather than a function type avoids any potential closure requirements of a function.

type InjectParameters added in v0.10.1

type InjectParameters[T any] struct {
	Address     string
	Method      string
	MethodLabel string
	Headers     Headers
	Resume      Resume[T]
	Abort       Abort[T]
}

InjectParameters contains the parameters needed to match and inject a fault into the outgoing request.

type Injector

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

Injector contains the data common across all requests needed to inject faults on outgoing requests.

func NewInjector

func NewInjector[T any](clientName, callerName string, abortCodeMin, abortCodeMax int, option ...func(*Injector[T])) *Injector[T]

NewInjector creates a new Injector with the provided parameters.

func (*Injector[T]) Inject

func (i *Injector[T]) Inject(ctx context.Context, params InjectParameters[T]) (T, error)

Inject injects a fault using the Injector default fault function on the outgoing request if it matches the header configuration.

type Resume

type Resume[T any] func() (T, error)

Resume is the function type to continue processing the protocol-specific request without injecting a fault.

Jump to

Keyboard shortcuts

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