|
| 1 | +"""A request's timeout measures the peer, so its clock stops while the request waits on a person. |
| 2 | +
|
| 3 | +The clock travels in a `ContextVar`: the HTTP transports run each outgoing message in its sender's |
| 4 | +context, which is how code far below `send_raw_request` finds the clock of the request it serves. |
| 5 | +
|
| 6 | +Nothing in this module is public API: it may change or be removed without notice. It is likely to |
| 7 | +change with the client dispatcher work in https://github.com/modelcontextprotocol/python-sdk/pull/3517. |
| 8 | +""" |
| 9 | + |
| 10 | +import math |
| 11 | +from collections.abc import Iterator |
| 12 | +from contextlib import contextmanager |
| 13 | +from contextvars import ContextVar |
| 14 | + |
| 15 | +import anyio |
| 16 | + |
| 17 | + |
| 18 | +class RequestClock: |
| 19 | + """One request's timeout, as a budget of seconds that is spent only while the clock runs. |
| 20 | +
|
| 21 | + Not public API: may change or be removed without notice. |
| 22 | + """ |
| 23 | + |
| 24 | + def __init__(self, timeout: float | None, scope: anyio.CancelScope) -> None: |
| 25 | + self._budget = math.inf if timeout is None else timeout |
| 26 | + self._scope = scope |
| 27 | + self._pauses = 1 # the write, which is off the clock too; `start()` ends it |
| 28 | + |
| 29 | + def start(self) -> None: |
| 30 | + self.resume() |
| 31 | + |
| 32 | + def pause(self) -> None: |
| 33 | + if not self._pauses: |
| 34 | + self._budget = self._scope.deadline - anyio.current_time() |
| 35 | + self._scope.deadline = math.inf |
| 36 | + self._pauses += 1 |
| 37 | + |
| 38 | + def resume(self) -> None: |
| 39 | + self._pauses -= 1 |
| 40 | + if not self._pauses: |
| 41 | + self._scope.deadline = anyio.current_time() + self._budget |
| 42 | + |
| 43 | + |
| 44 | +_clock: ContextVar[RequestClock | None] = ContextVar("request_clock", default=None) |
| 45 | + |
| 46 | + |
| 47 | +@contextmanager |
| 48 | +def request_clock(timeout: float | None) -> Iterator[RequestClock]: |
| 49 | + """Put a clock, not yet started, in the context of the request sent in this block. |
| 50 | +
|
| 51 | + Raises `TimeoutError` once the clock has run for `timeout` seconds. |
| 52 | +
|
| 53 | + Not public API: may change or be removed without notice. |
| 54 | + """ |
| 55 | + with anyio.CancelScope() as scope: |
| 56 | + clock = RequestClock(timeout, scope) |
| 57 | + token = _clock.set(clock) |
| 58 | + try: |
| 59 | + yield clock |
| 60 | + finally: |
| 61 | + _clock.reset(token) |
| 62 | + # Not `fail_after`: it re-reads the deadline here, which a pause may have moved since it expired. |
| 63 | + if scope.cancelled_caught: |
| 64 | + raise TimeoutError |
| 65 | + |
| 66 | + |
| 67 | +@contextmanager |
| 68 | +def waiting_on_a_person() -> Iterator[None]: |
| 69 | + """Stop the clock of the request this code is serving while the block is open; a no-op if there is none. |
| 70 | +
|
| 71 | + Not public API: may change or be removed without notice. |
| 72 | + """ |
| 73 | + clock = _clock.get() |
| 74 | + if clock is None: |
| 75 | + yield |
| 76 | + return |
| 77 | + clock.pause() |
| 78 | + try: |
| 79 | + yield |
| 80 | + finally: |
| 81 | + clock.resume() |
0 commit comments