generic

package
v2.0.4 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2025 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package generic provides generic retry combinators that work with any monadic type.

This package implements retry logic for monads that don't raise exceptions but signal failure in their type, such as Option, Either, and EitherT. The retry logic is parameterized over the monad operations, making it highly composable and reusable across different effect types.

Key Concepts

The retry combinator takes:

  • A retry policy that determines when and how long to wait between retries
  • An action that produces a monadic value
  • A check function that determines if the result should be retried

Usage with Different Monads

The generic retry function can be used with any monad by providing the appropriate monad operations (Chain, Of, and Delay). This allows the same retry logic to work with IO, IOEither, ReaderIO, and other monadic types.

Example conceptual usage:

// For IOEither[E, A]
result := Retrying(
	IOE.Chain[E, A],
	IOE.Chain[E, R.RetryStatus],
	IOE.Of[E, A],
	IOE.Of[E, R.RetryStatus],
	IOE.Delay[E, R.RetryStatus],
	policy,
	action,
	shouldRetry,
)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Retrying

func Retrying[HKTA, HKTSTATUS, A any](
	monadChain func(func(A) HKTA) func(HKTA) HKTA,
	monadChainStatus func(func(R.RetryStatus) HKTA) func(HKTSTATUS) HKTA,
	monadOf func(A) HKTA,
	monadOfStatus func(R.RetryStatus) HKTSTATUS,
	monadDelay func(time.Duration) func(HKTSTATUS) HKTSTATUS,

	policy R.RetryPolicy,
	action func(R.RetryStatus) HKTA,
	check func(A) bool,
) HKTA

Retrying is a generic retry combinator for actions that don't raise exceptions, but signal failure in their type. This works with monads like Option, Either, and EitherT where the type itself indicates success or failure.

The function repeatedly executes an action until either:

  1. The action succeeds (check returns false)
  2. The retry policy returns None (retry limit reached)
  3. The action fails in a way that shouldn't be retried

Type parameters:

  • HKTA: The higher-kinded type for the action result (e.g., IO[A], Either[E, A])
  • HKTSTATUS: The higher-kinded type for the retry status (e.g., IO[RetryStatus])
  • A: The result type of the action

Monad operations (first 5 parameters):

  • monadChain: Chains operations on HKTA (flatMap/bind for the result type)
  • monadChainStatus: Chains operations from HKTSTATUS to HKTA
  • monadOf: Lifts a value A into HKTA (pure/return for the result type)
  • monadOfStatus: Lifts a RetryStatus into HKTSTATUS
  • monadDelay: Delays execution by a duration within the monad

Retry configuration (last 3 parameters):

  • policy: The retry policy that determines delays and limits
  • action: The action to retry, which receives the current RetryStatus
  • check: A predicate that returns true if the result should be retried

Returns:

  • HKTA: The monadic result after retrying according to the policy

Example conceptual usage with IOEither:

policy := R.Monoid.Concat(
	R.LimitRetries(3),
	R.ExponentialBackoff(100*time.Millisecond),
)

action := func(status R.RetryStatus) IOEither[error, string] {
	return fetchData() // some IO operation that might fail
}

shouldRetry := func(result string) bool {
	return result == "" // retry if empty
}

result := Retrying(
	IOE.Chain[error, string],
	IOE.Chain[error, R.RetryStatus],
	IOE.Of[error, string],
	IOE.Of[error, R.RetryStatus],
	IOE.Delay[error, R.RetryStatus],
	policy,
	action,
	shouldRetry,
)

Types

This section is empty.

Jump to

Keyboard shortcuts

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