diff --git a/Pyjo/Util.py b/Pyjo/Util.py index 5971ee49..ffbbfec0 100644 --- a/Pyjo/Util.py +++ b/Pyjo/Util.py @@ -3718,14 +3718,19 @@ def _split(pattern: str, string: str) -> list[str]: return fields -# The name that a mistake of the name of an attribute most likely meant: one that the name starts with or that starts -# with the name, of three letters at least, as "req" for "request", or else the closest one +# The name that a mistake of the name of an attribute most likely meant: one that the name starts with, of three letters +# at least, as "req" for "request" of another framework, which comes before one that starts with the name, as +# "request_timeout", or else the closest one def _suggest(name: str, names: Iterable[str]) -> str | None: # pyright: ignore[reportUnusedFunction] import difflib others = [n for n in names if n != name and not n.startswith('_')] - if prefixes := [n for n in others if len(n) > 2 and (name.startswith(n) or n.startswith(name))]: - return difflib.get_close_matches(name, prefixes, n=1, cutoff=0)[0] + for prefixes in ( + [n for n in others if len(n) > 2 and name.startswith(n)], + [n for n in others if len(n) > 2 and n.startswith(name)], + ): + if prefixes: + return difflib.get_close_matches(name, prefixes, n=1, cutoff=0)[0] return next(iter(difflib.get_close_matches(name, others, n=1)), None) diff --git a/Pyjoyment/Controller.py b/Pyjoyment/Controller.py index 074c1d15..46fdb0ca 100644 --- a/Pyjoyment/Controller.py +++ b/Pyjoyment/Controller.py @@ -953,9 +953,10 @@ def stash(self, *args: Any, **values: Any) -> Any: :meth:`Pyjoyment.Pyjoyment_App.defaults`. Some stash values have a special meaning and are reserved, the full list is currently ``action``, ``app``, ``cb``, ``controller``, ``data``, ``extends``, - ``format``, ``handler``, ``inline``, ``json``, ``layout``, - ``namespace``, ``path``, ``status``, ``template``, ``text``, - ``toml``, ``variant`` and ``yaml``. Note that all stash values with a ``mojo.*`` prefix are + ``format``, ``handler``, ``inactivity_timeout``, ``inline``, + ``json``, ``layout``, ``namespace``, ``path``, ``request_timeout``, + ``status``, ``template``, ``text``, ``toml``, ``variant`` and + ``yaml``. Note that all stash values with a ``mojo.*`` prefix are reserved for internal use. :: # Remove value diff --git a/Pyjoyment/Plugin/DefaultHelpers.py b/Pyjoyment/Plugin/DefaultHelpers.py index 86c4149b..9ae2d42d 100644 --- a/Pyjoyment/Plugin/DefaultHelpers.py +++ b/Pyjoyment/Plugin/DefaultHelpers.py @@ -420,6 +420,21 @@ def catch(err): Render a plain text response and set the response status to ``404``. +request_timeout :: + + c = c.request_timeout(120) + +The most time in seconds from now to the end of the response, as for a +request that waits for a model of language, which the value +``request_timeout`` of the stash of a route sets before its first action. +When it passes, the actions that are coroutine functions are cancelled where +they wait, with the requests of :mod:`Pyjo.UserAgent` that they wait for, and +the request gets an empty ``504`` response, or the connection closes when the +response has started already, as a stream. A later call replaces the time. :: + + # A route whose response has two minutes at most + app.routes.post('/chat', {'request_timeout': 120}).to('chat#reply') + respond_to :: c = c.respond_to(json={'json': {'message': 'Welcome!'}}, html={'template': 'welcome'}, any=lambda c: ...) @@ -661,6 +676,7 @@ def register(self, app: 'Pyjoyment.object', conf: dict[str, Any] | None = None) app.helper('inactivity_timeout', _inactivity_timeout) app.helper('is_fresh', _is_fresh) app.helper('redirect_to', _redirect_to) + app.helper('request_timeout', _request_timeout) app.helper('respond_to', _respond_to) app.helper('url_with', _url_with) app.helper('validation', _validation) @@ -985,6 +1001,52 @@ def _redirect_to(c: 'Controller', *args: Any, **values: Any) -> 'Controller': return c.rendered(None if res.is_redirect else 302) +# Not in Mojolicious: the deadline of a request, which Mojolicious leaves to the inactivity timeout of its connection +def _request_timeout(c: 'Controller', timeout: float) -> 'Controller': + stash = c.stash() + loop = Pyjo.IOLoop.singleton + if (old := stash.pop('pyjo.request_timeout', None)) is not None: + loop.remove(old) + if (tx := c.tx) is None or tx.is_finished: + return c + + # Weakly, as the loop keeps the timer, and the transaction its subscribers + ref = weakref.ref(c) + + def expired(loop: Pyjo.IOLoop.object) -> None: + if (c := ref()) is not None: + _expire(c, timeout) + + id = stash['pyjo.request_timeout'] = loop.timer(timeout, expired) + + def finish(tx: Any) -> None: + loop.remove(id) + + tx.on('finish', finish) + return c + + +def _expire(c: 'Controller', timeout: float) -> None: + stash = c.stash() + stash.pop('pyjo.request_timeout', None) + if (tx := c.tx) is None or tx.is_finished: + return + c.app.log.info(f'Request timeout of {timeout:g} seconds') + + # The actions that wait, which nothing renders the response of anymore + stash['pyjo.cancelled'] = True + for action in stash.get('pyjo.actions', ()): + if (promise := action()) is not None: + promise.cancel() + + # A response that has not started yet, or the end of its connection + if not stash.get('mojo.finished') and not c.res.content.is_dynamic and not tx.is_websocket: + c.res.body = b'' + c.rendered(504) + elif (id := tx.connection) is not None: + Pyjo.IOLoop.remove(id) + + def _respond_to(c: 'Controller', *args: dict[str, Any], **values: Any) -> 'Controller': targets = args[0] if args else values @@ -1181,6 +1243,7 @@ class Helpers: inactivity_timeout = Helper(_inactivity_timeout) is_fresh = Helper(_is_fresh) redirect_to = Helper(_redirect_to) + request_timeout = Helper(_request_timeout) respond_to = Helper(_respond_to) url_with = Helper(_url_with) validation = Helper(_validation) diff --git a/Pyjoyment/Routes/Route.py b/Pyjoyment/Routes/Route.py index a4949cbc..25ff6f27 100644 --- a/Pyjoyment/Routes/Route.py +++ b/Pyjoyment/Routes/Route.py @@ -55,9 +55,10 @@ # Reserved stash values # Not in Mojolicious: toml and yaml, which the renderer renders as json +# Unlike Mojolicious: the timeouts of a route, inactivity_timeout and request_timeout, are reserved too _RESERVED = frozenset( - 'action app cb controller data extends format handler inline json layout namespace path status template text ' - 'toml variant yaml'.split() + 'action app cb controller data extends format handler inactivity_timeout inline json layout namespace path ' + 'request_timeout status template text toml variant yaml'.split() ) diff --git a/Pyjoyment/__init__.py b/Pyjoyment/__init__.py index bd609020..5c88aadd 100644 --- a/Pyjoyment/__init__.py +++ b/Pyjoyment/__init__.py @@ -1045,6 +1045,14 @@ def websocket( def _action(next: Any, c: Pyjoyment.Controller.object, action: Callable[..., Any], last: bool) -> Any: + # Not in Mojolicious: the timeouts of the stash of a route, as of its defaults or of a route of under before it, set + # before its action + stash = c.stash() + if (timeout := stash.get('inactivity_timeout')) is not None: + c.helpers.inactivity_timeout(timeout) + if (timeout := stash.get('request_timeout')) is not None and 'pyjo.request_timeout' not in stash: + c.helpers.request_timeout(timeout) + try: val = action(c) except TypeError as e: @@ -1061,6 +1069,7 @@ def _action(next: Any, c: Pyjoyment.Controller.object, action: Callable[..., Any if inspect.iscoroutine(val): val = Pyjo.Promise.new(val) _cancel_on_close(c, val) + stash.setdefault('pyjo.actions', []).append(weakref.ref(val)) if isinstance(val, Pyjo.Promise.object): val.catch(functools.partial(_reply_exception, c)) return val diff --git a/docs/guides/routing.rst b/docs/guides/routing.rst index 5f2b6ad0..219c41fb 100644 --- a/docs/guides/routing.rst +++ b/docs/guides/routing.rst @@ -425,6 +425,24 @@ For security reasons the dispatcher will always check if the ``controller`` is actually a subclass of :mod:`Pyjoyment.Controller` or :mod:`Pyjoyment` before dispatching to it. +The values ``inactivity_timeout`` and ``request_timeout`` are the timeouts of +a route, which are set before its first action: the inactivity timeout of the +connection, as the helper ``inactivity_timeout`` sets it, and the most time +to the end of the response, as the helper ``request_timeout`` sets it, after +which the actions are cancelled and the request gets a ``504`` response. A +request that waits for a model of language, which can be silent for minutes, +needs both, and the routes of :ref:`under ` give them to all +the routes of a group. + +.. code-block:: python + + # Ten minutes of silence, and two minutes to the end of the response at most + r.post('/chat', {'inactivity_timeout': 600, 'request_timeout': 120}).to('chat#reply') + + # The same for every route of a group + llm = r.under('/llm', {'inactivity_timeout': 600, 'request_timeout': 120}) + llm.post('/complete').to('llm#complete') + Namespaces ~~~~~~~~~~ @@ -647,6 +665,8 @@ expressions. +/baz ...W GET baz ^/baz /yada .... * yada ^/yada +.. _routing-under: + Under ~~~~~ diff --git a/plugins/pyjoyment/skills/async/SKILL.md b/plugins/pyjoyment/skills/async/SKILL.md index 13d6a26e..b41f717c 100644 --- a/plugins/pyjoyment/skills/async/SKILL.md +++ b/plugins/pyjoyment/skills/async/SKILL.md @@ -249,8 +249,12 @@ app.start() does not know them, `Task got bad yield: `: await `Steps(...).run_p(c.ua())`. Inside the steps, a promise or a coroutine of asyncio is a `TypeError`, as nothing runs the event loop for `run`. -- A long wait, as a stream of events, needs a longer inactivity timeout of its - connection: `c.inactivity_timeout(300)`. +- A long wait, as a stream of events or a model of language that is silent + for minutes, needs a longer inactivity timeout of its connection: + `c.inactivity_timeout(300)`, or the value of the stash of its route, + `get('/chat', {'inactivity_timeout': 600}, chat)`, or of a group with + `under`. `request_timeout` there, or `c.request_timeout(120)`, bounds the + whole response: the actions are cancelled, and the request gets `504`. ## Documentation diff --git a/t/Pyjoyment/app.py b/t/Pyjoyment/app.py index f1ac78f2..7779e0b6 100644 --- a/t/Pyjoyment/app.py +++ b/t/Pyjoyment/app.py @@ -199,6 +199,10 @@ def local(**env): ok(t.app.routes.is_reserved('yaml'), 'is reserved') ok(t.app.routes.is_reserved('toml'), 'is reserved') + # Not in Mojolicious: the timeouts of a route + ok(t.app.routes.is_reserved('inactivity_timeout'), 'is reserved') + ok(t.app.routes.is_reserved('request_timeout'), 'is reserved') + with subtest('Reserved stash value (in placeholder)'): throws_ok( lambda: t.app.routes.any('/:controller'), diff --git a/t/Pyjoyment/timeouts_lite_app.py b/t/Pyjoyment/timeouts_lite_app.py new file mode 100644 index 00000000..3269e41c --- /dev/null +++ b/t/Pyjoyment/timeouts_lite_app.py @@ -0,0 +1,135 @@ +import os + + +os.environ['PYJO_MODE'] = 'development' + +# A short inactivity timeout of the server, which a route makes longer +os.environ['PYJO_INACTIVITY_TIMEOUT'] = '0.3' + +import Pyjo.IOLoop +import Pyjo.Promise +import Pyjo.Test.Pyjo + +from Pyjo.Test import * +from Pyjoyment.Lite import app, get, group, under + + +# Not in Mojolicious: the timeouts of a route, the inactivity timeout of its connection and the most time to the end of +# its response, of the values inactivity_timeout and request_timeout of the stash, set before its first action + + +# A reactor whose run_coroutine cannot cancel a coroutine returns None, and leaves an action running after its timeout +async def probe(): + pass + + +cancels = Pyjo.IOLoop.singleton.reactor.run_coroutine(probe(), lambda value: None, lambda e: None) is not None + +app.log.level = 'fatal' + +events = [] + + +def cancelled(expected, name): + return is_deeply_ok(events, expected, name) if cancels else skip('the reactor cannot cancel a coroutine') + + +async def silent(c): + try: + await Pyjo.Promise.timer(float(c.param('wait') or 0.6)) + c.render(text='answered') + finally: + events.append('ended') + + +get('/short', silent) +get('/long', {'inactivity_timeout': 2}, silent) +get('/deadline', {'request_timeout': 0.2}, silent) + + +@get('/helper') +async def helper(c): + c.request_timeout(0.2) + await silent(c) + + +@get('/callback', {'request_timeout': 0.2}) +def callback(c): + Pyjo.IOLoop.timer(1, lambda loop: c.render(text='late')) + + +@get('/stream', {'request_timeout': 0.2, 'inactivity_timeout': 2}) +async def stream(c): + c.write_chunk(b'started') + try: + await Pyjo.Promise.timer(1) + finally: + events.append('stream ended') + + +with group(): + under({'inactivity_timeout': 2, 'request_timeout': 1}) + get('/group', silent) + + +def settle(): + # The events of the server after the end of a response or of its connection + Pyjo.Promise.timer(0.1).wait() + + +t = Pyjo.Test.Pyjo.new() +t.ua.inactivity_timeout = 10 + +with subtest('Inactivity timeout of the server'): + events.clear() + tx = t.ua.get('/short') + settle() + is_ok(tx.error, {'message': 'Premature connection close'}, 'closed by the server') + cancelled(['ended'], 'action cancelled') + +with subtest('Inactivity timeout of a route'): + events.clear() + t.get_ok('/long').status_is(200).content_is('answered') + +with subtest('Request timeout of a route'): + events.clear() + with app.log.capture('info') as logs: + t.get_ok('/deadline?wait=5').status_is(504).content_is('') + settle() + cancelled(['ended'], 'action cancelled') + like_ok(str(logs), r'Request timeout of 0\.2 seconds', 'logged') + + with app.log.capture('info') as logs: + t.get_ok('/deadline?wait=0.05').status_is(200).content_is('answered') + Pyjo.Promise.timer(0.3).wait() + unlike_ok(str(logs), r'Request timeout', 'answered before the time, which does not pass later') + +with subtest('Request timeout of the helper'): + events.clear() + t.get_ok('/helper?wait=5').status_is(504) + settle() + cancelled(['ended'], 'action cancelled') + +with subtest('Request timeout of an action with a callback'): + t.get_ok('/callback').status_is(504).content_is('') + Pyjo.Promise.timer(1).wait() + +with subtest('Request timeout of a response that has started'): + events.clear() + tx = t.ua.get('/stream') + settle() + is_ok(tx.res.code, 200, 'started') + is_ok(tx.res.body, b'started', 'first chunk') + ok(not tx.res.content.is_finished, 'connection closed without the last chunk') + cancelled(['stream ended'], 'action cancelled') + +with subtest('Timeouts of a group'): + events.clear() + t.get_ok('/group').status_is(200).content_is('answered') + t.get_ok('/group?wait=5').status_is(504) + settle() + cancelled(['ended', 'ended'], 'answered, then cancelled') + +t.close() + +done_testing() diff --git a/t/docs/guides_routing.py b/t/docs/guides_routing.py index 6feca2cf..cc705d13 100644 --- a/t/docs/guides_routing.py +++ b/t/docs/guides_routing.py @@ -218,6 +218,23 @@ def bye(self): r.get('/routes').to('Routes#bye', namespace='Pyjoyment') t.get_ok('/routes').status_is(500) +with subtest('Timeouts of routes'): + r = Pyjoyment.Routes.new() + + # Ten minutes of silence, and two minutes to the end of the response at most + r.post('/chat', {'inactivity_timeout': 600, 'request_timeout': 120}).to('chat#reply') + + # The same for every route of a group + llm = r.under('/llm', {'inactivity_timeout': 600, 'request_timeout': 120}) + llm.post('/complete').to('llm#complete') + + timeouts = {'inactivity_timeout': 600, 'request_timeout': 120} + is_deeply_ok(endpoint(r, '/chat', 'POST'), {**timeouts, 'controller': 'chat', 'action': 'reply'}, 'route') + is_deeply_ok(match(r, '/llm/complete', 'POST')[0], timeouts, 'group') + is_deeply_ok( + endpoint(r, '/llm/complete', 'POST'), {**timeouts, 'controller': 'llm', 'action': 'complete'}, 'nested' + ) + with subtest('Namespaces'): with application('special'): with Pyjo.Test.Pyjo.new('MyApp') as t: