Read-modify-write a JSON object under an ETag precondition. `mutator` receives the current record and mutates it in place. It is called again on each retry with freshly read data, so any guard it enforces (seat limits, duplicate members) is re-checked against the winning version ra
(key: str, mutator: Callable[[dict], Any], retries: int = 5)
| 164 | |
| 165 | |
| 166 | async def update_json(key: str, mutator: Callable[[dict], Any], retries: int = 5) -> dict: |
| 167 | """ |
| 168 | Read-modify-write a JSON object under an ETag precondition. |
| 169 | |
| 170 | `mutator` receives the current record and mutates it in place. It is called |
| 171 | again on each retry with freshly read data, so any guard it enforces (seat |
| 172 | limits, duplicate members) is re-checked against the winning version rather |
| 173 | than the stale one we lost the race to. |
| 174 | |
| 175 | Raises FileNotFoundError if the key does not exist, PreconditionFailed if |
| 176 | every attempt lost the race. |
| 177 | """ |
| 178 | for attempt in range(retries): |
| 179 | record, etag = await get_json(key) |
| 180 | if record is None: |
| 181 | raise FileNotFoundError(key) |
| 182 | |
| 183 | mutator(record) |
| 184 | |
| 185 | try: |
| 186 | await put_json(key, record, if_match=etag) |
| 187 | return record |
| 188 | except PreconditionFailed: |
| 189 | if attempt == retries - 1: |
| 190 | raise |
| 191 | logger.warning(f"Lost write race on {key}, retrying ({attempt + 1}/{retries})") |
| 192 | await asyncio.sleep(0.1 * (attempt + 1)) |
| 193 | |
| 194 | raise PreconditionFailed(key) |
| 195 | |
| 196 | |
| 197 | def org_key(org_id: str) -> str: |
nothing calls this directly
no test coverage detected