API Reference¶
Auto-generated from source using mkdocstrings.
deadline_budget¶
Request deadline budget tracking for distributed orchestrations.
BudgetContext
¶
Convenient wrapper around DeadlineBudget with per-call timeout caps.
Encapsulates deadline budget and per-call timeout configuration for cleaner orchestration code.
Example
Define caps for each specific call in the operation¶
call_caps = { "identity_create_user": 3.0, "verification_verify_code": 2.0, "credential_set_password": 3.0, } ctx = BudgetContext.create(total_seconds=10.0, call_caps=call_caps)
Get timeout for specific call (with configured cap)¶
timeout = ctx.timeout_for_call("identity_create_user") await identity_client.create_user(timeout=timeout)
Get timeout for unconfigured call (uses remaining budget)¶
timeout = ctx.timeout_for_call("identity_get_user") await identity_client.get_user(timeout=timeout)
Source code in deadline_budget/context.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | |
budget
property
¶
Access to underlying budget for advanced use cases.
call_caps
property
¶
Access to configured call caps.
__init__(budget, call_caps)
¶
Initialize context with a budget and per-call caps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
budget
|
DeadlineBudget
|
The underlying DeadlineBudget instance. |
required |
call_caps
|
dict[str, float]
|
Mapping of call names to their timeout caps in seconds. If a call is not in this dict, it will use remaining budget without cap. |
required |
Source code in deadline_budget/context.py
check_expired()
¶
Raise DeadlineExceededError if budget is exhausted.
Raises:
| Type | Description |
|---|---|
DeadlineExceededError
|
If the deadline has passed. |
create(total_seconds, call_caps=None, *, min_timeout=0.1, safety_margin=0.0)
classmethod
¶
Create a new budget context with the given parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
total_seconds
|
float
|
Total budget for this request in seconds. |
required |
call_caps
|
dict[str, float] | None
|
Mapping of call names to their timeout caps (optional). |
None
|
min_timeout
|
float
|
Minimum timeout to return (default 0.1s). |
0.1
|
safety_margin
|
float
|
Safety margin reserved at the end (default 0.0s). |
0.0
|
Returns:
| Type | Description |
|---|---|
BudgetContext
|
A new BudgetContext instance. |
Source code in deadline_budget/context.py
elapsed()
¶
expired()
¶
remaining()
¶
Return remaining time budget in seconds.
Returns negative value if deadline already exceeded.
timeout_for_call(call_name, reserve_for_next=0.0)
¶
Compute timeout for the next downstream call.
If call_name has a configured cap, applies it. Otherwise uses remaining budget.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
call_name
|
str
|
Name of the call (e.g., "identity_create_user", "verification_verify_code"). |
required |
reserve_for_next
|
float
|
Reserve this many seconds for subsequent steps. |
0.0
|
Returns:
| Type | Description |
|---|---|
float
|
Computed timeout in seconds, bounded by [min_timeout, call_cap] if cap exists, |
float
|
or [min_timeout, remaining] if no cap configured. |
Raises:
| Type | Description |
|---|---|
DeadlineExceededError
|
If remaining budget is already exhausted. |
Source code in deadline_budget/context.py
DeadlineBudget
¶
Tracks request deadline budget using monotonic time.
Example
budget = DeadlineBudget(total_seconds=9.5) timeout1 = budget.timeout_for(cap=5.0) await some_call(timeout=timeout1) timeout2 = budget.timeout_for(cap=5.0) await another_call(timeout=timeout2) if budget.expired(): raise DeadlineExceededError(...)
Source code in deadline_budget/budget.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | |
total_seconds
property
¶
Return total budget in seconds.
__init__(total_seconds, *, min_timeout=0.1, safety_margin=0.0)
¶
Initialize deadline budget.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
total_seconds
|
float
|
Total budget for this request in seconds. |
required |
min_timeout
|
float
|
Minimum timeout to return from timeout_for (default 0.1s). |
0.1
|
safety_margin
|
float
|
Safety margin reserved at the end (subtracted from total). |
0.0
|
Source code in deadline_budget/budget.py
check_expired()
¶
Raise DeadlineExceededError if budget is exhausted.
elapsed()
¶
expired()
¶
remaining()
¶
Return remaining time budget in seconds.
Returns negative value if deadline already exceeded.
timeout_for(cap=None, min_timeout=None, reserve_for_next=0.0)
¶
Compute timeout for the next downstream call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cap
|
float | None
|
Maximum allowed timeout for this call (service-level cap). |
None
|
min_timeout
|
float | None
|
Minimum timeout override (default: use budget min_timeout). |
None
|
reserve_for_next
|
float
|
Reserve this many seconds for subsequent steps. |
0.0
|
Returns:
| Type | Description |
|---|---|
float
|
Computed timeout in seconds, bounded by [min_timeout, cap]. |
Raises:
| Type | Description |
|---|---|
DeadlineExceededError
|
If remaining budget is already exhausted. |
Source code in deadline_budget/budget.py
DeadlineExceededError
¶
Bases: Exception
Raised when request deadline budget is exhausted.