Retry Module API Reference¶
This section documents the internals of the retry
module in Bijux CLI.
bijux_cli.infra.retry ¶
Provides concrete asynchronous retry policy implementations.
This module defines classes that implement the RetryPolicyProtocol
to handle transient errors in asynchronous operations. It offers two main strategies:
* `TimeoutRetryPolicy`: A simple policy that applies a single timeout to an
operation.
* `ExponentialBackoffRetryPolicy`: A more advanced policy that retries an
operation multiple times with an exponentially increasing delay and
random jitter between attempts.
These components are designed to be used by services to build resilience against temporary failures, such as network issues.
ExponentialBackoffRetryPolicy ¶
ExponentialBackoffRetryPolicy(telemetry: TelemetryProtocol)
Bases: RetryPolicyProtocol
A retry policy with exponential backoff, jitter, and per-attempt timeouts.
Attributes:
-
_telemetry
(TelemetryProtocol
) –The service for emitting telemetry events.
Initializes the ExponentialBackoffRetryPolicy
.
Parameters:
-
telemetry
(TelemetryProtocol
) –The service for emitting events.
Source code in src/bijux_cli/infra/retry.py
reset ¶
run async
¶
run(
supplier: Callable[[], Awaitable[T]],
seconds: float = 1.0,
retries: int = 3,
delay: float = 1.0,
backoff: float = 2.0,
jitter: float = 0.3,
retry_on: tuple[type[BaseException], ...] = (
Exception,
),
) -> T
Executes a supplier with a timeout and exponential-backoff retries.
Parameters:
-
supplier
(Callable[[], Awaitable[T]]
) –The async operation to run.
-
seconds
(float
, default:1.0
) –The timeout for each attempt in seconds. Must be > 0.
-
retries
(int
, default:3
) –The maximum number of retry attempts.
-
delay
(float
, default:1.0
) –The initial delay in seconds before the first retry.
-
backoff
(float
, default:2.0
) –The multiplier for the delay after each failure.
-
jitter
(float
, default:0.3
) –The random fractional jitter to apply to each delay.
-
retry_on
(tuple[type[BaseException], ...]
, default:(Exception,)
) –A tuple of exception types that will trigger a retry.
Returns:
-
T
(T
) –The result of the
supplier
if one of the attempts succeeds.
Raises:
-
ValueError
–If
seconds
is less than or equal to 0. -
BaseException
–The last exception raised by
supplier
if all attempts fail.
Source code in src/bijux_cli/infra/retry.py
TimeoutRetryPolicy ¶
TimeoutRetryPolicy(telemetry: TelemetryProtocol)
Bases: RetryPolicyProtocol
A retry policy that applies a single, one-time timeout to an operation.
Attributes:
-
_telemetry
(TelemetryProtocol
) –The service for emitting telemetry events.
Initializes the TimeoutRetryPolicy
.
Parameters:
-
telemetry
(TelemetryProtocol
) –The service for emitting events.
Source code in src/bijux_cli/infra/retry.py
reset ¶
run async
¶
Executes an awaitable supplier
with a single timeout.
This method uses the modern asyncio.timeout
context manager if available, otherwise it falls back to asyncio.wait_for
.
Parameters:
-
supplier
(Callable[[], Awaitable[T]]
) –The async operation to run.
-
seconds
(float
, default:1.0
) –The timeout duration in seconds. Must be positive.
Returns:
-
T
(T
) –The result of the
supplier
if it completes in time.
Raises:
-
ValueError
–If
seconds
is less than or equal to 0. -
BijuxError
–If the operation times out.