core
Token Bucket Rate Limiter Implementation
AsyncTokenBucket
Asynchronous Leaky Bucket Rate Limiter
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bucket_config
|
BucketConfig | None
|
Configuration for the token bucket with the max capacity and time period in seconds |
None
|
max_concurrent
|
int | None
|
Maximum number of concurrent requests allowed to acquire capacity |
None
|
Note
This implementation is synchronous and supports bursts up to the capacity within the specified time period
Source code in limitor/token_bucket/core.py
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | |
__aenter__()
async
__aexit__(exc_type, exc_val, exc_tb)
async
Exit the context manager, releasing any resources if necessary
acquire(amount=1, timeout=None)
async
Acquire capacity from the token bucket, waiting asynchronously until allowed.
Supports timeouts and cancellations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
amount
|
float
|
The amount of capacity to acquire, defaults to 1 |
1
|
timeout
|
float | None
|
Optional timeout in seconds for the acquire operation |
None
|
Raises:
| Type | Description |
|---|---|
TimeoutError
|
If the acquire operation times out after the specified timeout period |
Source code in limitor/token_bucket/core.py
capacity_info(amount=1)
Get the current capacity information of the token bucket
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
amount
|
float
|
The amount of capacity to check for, defaults to 1 |
1
|
Returns:
| Type | Description |
|---|---|
Capacity
|
A named tuple indicating if the bucket has enough capacity and how much more is needed |
Source code in limitor/token_bucket/core.py
SyncTokenBucket
Token Bucket Rate Limiter
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bucket_config
|
BucketConfig | None
|
Configuration for the token bucket with the max capacity and time period in seconds |
None
|
Note
This implementation is synchronous and supports bursts up to the capacity within the specified time period
Source code in limitor/token_bucket/core.py
__enter__()
__exit__(exc_type, exc_val, exc_tb)
Exit the context manager, releasing any resources if necessary
acquire(amount=1)
Acquire capacity from the token bucket, blocking until enough capacity is available.
This method will block and sleep until the requested amount can be acquired without exceeding the bucket's capacity, simulating rate limiting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
amount
|
float
|
The amount of capacity to acquire, defaults to 1 |
1
|
Notes
The while loop is just to make sure nothing funny happens while waiting
Source code in limitor/token_bucket/core.py
capacity_info(amount=1)
Get the current capacity information of the token bucket
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
amount
|
float
|
The amount of capacity to check for, defaults to 1 |
1
|
Returns:
| Type | Description |
|---|---|
Capacity
|
A named tuple indicating if the bucket has enough capacity and how much more is needed |