xtr-http-kernel 6.8.1__tar.gz → 7.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- xtr_http_kernel-6.8.1/README.md → xtr_http_kernel-7.0.0/PKG-INFO +262 -16
- xtr_http_kernel-6.8.1/PKG-INFO → xtr_http_kernel-7.0.0/README.md +234 -46
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/pyproject.toml +24 -27
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/pyproject.toml.orig +25 -29
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/.agents/skills/xtr-http-kernel/SKILL.md +49 -6
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/.agents/skills/xtr-http-kernel/references/configure.md +5 -1
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/.agents/skills/xtr-http-kernel/references/listeners.md +4 -5
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/__init__.py +8 -0
- xtr_http_kernel-7.0.0/src/xtr_http_kernel/_detached.py +54 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/bridge/_headers.py +2 -4
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/bridge/_scope_uri.py +24 -22
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/bridge/http_message_factory.py +2 -3
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/bridge/response.py +6 -4
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/bridge/response_factory.py +1 -2
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/bridge/served_connection.py +14 -3
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/bridge/starlette_http_factory.py +1 -2
- xtr_http_kernel-7.0.0/src/xtr_http_kernel/bundle/__init__.py +20 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/bundle/_messages.py +10 -6
- xtr_http_kernel-7.0.0/src/xtr_http_kernel/bundle/_profiler.py +201 -0
- xtr_http_kernel-7.0.0/src/xtr_http_kernel/bundle/_profiler_logging.py +49 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/bundle/http_kernel_bundle.py +25 -29
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/bundle/http_kernel_config.py +35 -3
- xtr_http_kernel-7.0.0/src/xtr_http_kernel/bundle/profiler_config.py +63 -0
- xtr_http_kernel-7.0.0/src/xtr_http_kernel/bundle/profiler_pass.py +69 -0
- xtr_http_kernel-7.0.0/src/xtr_http_kernel/bundle/profiling_active.py +34 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/bundle/request_lifecycle_middleware_factory.py +29 -4
- xtr_http_kernel-7.0.0/src/xtr_http_kernel/bundle/route_recording_middleware_factory.py +27 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/bundle/route_registry_pass.py +1 -2
- xtr_http_kernel-7.0.0/src/xtr_http_kernel/data_collector/__init__.py +30 -0
- xtr_http_kernel-7.0.0/src/xtr_http_kernel/data_collector/_masking.py +35 -0
- xtr_http_kernel-7.0.0/src/xtr_http_kernel/data_collector/ajax_data_collector.py +33 -0
- xtr_http_kernel-7.0.0/src/xtr_http_kernel/data_collector/clone_var.py +160 -0
- xtr_http_kernel-7.0.0/src/xtr_http_kernel/data_collector/config_data_collector.py +73 -0
- xtr_http_kernel-7.0.0/src/xtr_http_kernel/data_collector/data_collector_interface.py +36 -0
- xtr_http_kernel-7.0.0/src/xtr_http_kernel/data_collector/data_collector_tag.py +26 -0
- xtr_http_kernel-7.0.0/src/xtr_http_kernel/data_collector/data_collector_templates.py +29 -0
- xtr_http_kernel-7.0.0/src/xtr_http_kernel/data_collector/event_data_collector.py +67 -0
- xtr_http_kernel-7.0.0/src/xtr_http_kernel/data_collector/exception_data_collector.py +101 -0
- xtr_http_kernel-7.0.0/src/xtr_http_kernel/data_collector/late_data_collector_interface.py +30 -0
- xtr_http_kernel-7.0.0/src/xtr_http_kernel/data_collector/logger_data_collector.py +91 -0
- xtr_http_kernel-7.0.0/src/xtr_http_kernel/data_collector/memory_data_collector.py +52 -0
- xtr_http_kernel-7.0.0/src/xtr_http_kernel/data_collector/profiled_request.py +37 -0
- xtr_http_kernel-7.0.0/src/xtr_http_kernel/data_collector/request_data_collector.py +97 -0
- xtr_http_kernel-7.0.0/src/xtr_http_kernel/data_collector/request_trace.py +79 -0
- xtr_http_kernel-7.0.0/src/xtr_http_kernel/data_collector/router_data_collector.py +46 -0
- xtr_http_kernel-7.0.0/src/xtr_http_kernel/data_collector/time_data_collector.py +72 -0
- xtr_http_kernel-7.0.0/src/xtr_http_kernel/data_collector/tracing_data_collector_interface.py +24 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/event_listener/__init__.py +2 -6
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/event_listener/error_logging_listener.py +4 -3
- xtr_http_kernel-7.0.0/src/xtr_http_kernel/event_listener/profiler_listener.py +193 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/exception/__init__.py +8 -0
- xtr_http_kernel-7.0.0/src/xtr_http_kernel/exception/invalid_collector_priority_error.py +36 -0
- xtr_http_kernel-7.0.0/src/xtr_http_kernel/exception/invalid_collector_tag_error.py +36 -0
- xtr_http_kernel-7.0.0/src/xtr_http_kernel/exception/invalid_token_error.py +28 -0
- xtr_http_kernel-7.0.0/src/xtr_http_kernel/exception/unknown_collector_error.py +28 -0
- xtr_http_kernel-7.0.0/src/xtr_http_kernel/profiler/__init__.py +25 -0
- xtr_http_kernel-7.0.0/src/xtr_http_kernel/profiler/_state_keys.py +25 -0
- xtr_http_kernel-7.0.0/src/xtr_http_kernel/profiler/file_profiler_storage.py +287 -0
- xtr_http_kernel-7.0.0/src/xtr_http_kernel/profiler/profile.py +149 -0
- xtr_http_kernel-7.0.0/src/xtr_http_kernel/profiler/profile_summary.py +32 -0
- xtr_http_kernel-7.0.0/src/xtr_http_kernel/profiler/profiler.py +234 -0
- xtr_http_kernel-7.0.0/src/xtr_http_kernel/profiler/profiler_storage_interface.py +64 -0
- xtr_http_kernel-7.0.0/src/xtr_http_kernel/profiler/route_recording_middleware.py +72 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/rate_limiter/rate_limited.py +8 -10
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/request_lifecycle_middleware.py +100 -30
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/routing/decorator/as_route_resource.py +2 -4
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/routing/decorator/as_routes.py +2 -2
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/setup.py +25 -2
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/testing.py +1 -1
- xtr_http_kernel-6.8.1/src/xtr_http_kernel/bundle/__init__.py +0 -9
- xtr_http_kernel-6.8.1/src/xtr_http_kernel/event_listener/log_unit_listener.py +0 -39
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/LICENSE +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/.agents/skills/xtr-http-kernel/references/rate-limits.md +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/.agents/skills/xtr-http-kernel/references/routing.md +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/_kernel_middleware.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/_route_contexts.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/_staging.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/_state.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/bridge/__init__.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/bridge/_media_types.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/bridge/_server_params.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/bridge/http_message_factory_interface.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/bridge/starlette_http_factory_interface.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/command/__init__.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/command/debug_router_command.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/command/route_description.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/command/router_command.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/command/router_match_command.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/event/__init__.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/event/exception_event.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/event/finish_request_event.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/event/request_event.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/event/response_event.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/event/terminate_event.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/event_listener/disallow_robots_indexing_listener.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/event_listener/rate_limit_headers_listener.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/event_listener/request_id_listener.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/exception/http_kernel_error.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/exception/invalid_argument_error.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/exception/invalid_cookie_error.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/exception/invalid_middleware_priority_error.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/exception/invalid_rate_limit_error.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/exception/ranged_response_body_error.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/exception/response_already_a_message_error.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/exception/streamed_response_body_error.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/exception/too_many_requests_error.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/exception/unknown_rate_limiter_error.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/exception/unsupported_connection_error.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/kernel_events.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/middleware_stack.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/middleware_tag.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/py.typed +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/rate_limiter/__init__.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/rate_limiter/_applied_rate_limit.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/recipe/__init__.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/recipe/files/config/http_kernel.py.tmpl +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/recipe/manifest.toml +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/routing/__init__.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/routing/decorator/__init__.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/routing/exception/__init__.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/routing/exception/invalid_argument_error.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/routing/exception/route_conflict_error.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/routing/exception/routing_error.py +0 -0
- {xtr_http_kernel-6.8.1 → xtr_http_kernel-7.0.0}/src/xtr_http_kernel/routing/route_registry.py +0 -0
|
@@ -1,10 +1,38 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: xtr-http-kernel
|
|
3
|
+
Version: 7.0.0
|
|
4
|
+
Summary: Requests turned into responses through events: a request lifecycle for FastAPI applications on the xtr kernel.
|
|
5
|
+
Keywords: http,kernel,request,lifecycle,events,middleware,fastapi
|
|
6
|
+
Author: Xterr
|
|
7
|
+
Author-email: Xterr <me@xterr.dev>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
13
|
+
Classifier: Typing :: Typed
|
|
14
|
+
Requires-Dist: fastapi>=0.143.1
|
|
15
|
+
Requires-Dist: starlette>=1.6
|
|
16
|
+
Requires-Dist: xtr-clock>=7.0,<8
|
|
17
|
+
Requires-Dist: xtr-dependency-injection>=7.0,<8
|
|
18
|
+
Requires-Dist: xtr-event-dispatcher>=7.0,<8
|
|
19
|
+
Requires-Dist: xtr-event-dispatcher-contracts>=7.0,<8
|
|
20
|
+
Requires-Dist: xtr-http-message>=7.0,<8
|
|
21
|
+
Requires-Dist: xtr-http-message-contracts>=7.0,<8
|
|
22
|
+
Requires-Dist: xtr-logging-contracts>=7.0,<8
|
|
23
|
+
Requires-Dist: xtr-service-contracts>=7.0,<8
|
|
24
|
+
Requires-Dist: xtr-stopwatch>=7.0,<8
|
|
25
|
+
Requires-Dist: python-multipart>=0.0.21
|
|
26
|
+
Requires-Python: >=3.14.4
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
|
|
1
29
|
<div align="center">
|
|
2
30
|
|
|
3
31
|
# xtr-http-kernel
|
|
4
32
|
|
|
5
33
|
**Requests turned into responses through events: a request lifecycle for FastAPI applications on the xtr kernel.**
|
|
6
34
|
|
|
7
|
-
<img alt="python 3.
|
|
35
|
+
<img alt="python 3.14+" src="https://img.shields.io/badge/python-%E2%89%A5%203.14-3776AB?logo=python&logoColor=white">
|
|
8
36
|
<img alt="typed" src="https://img.shields.io/badge/typed-ty%20%2B%20basedpyright-1f6feb">
|
|
9
37
|
<img alt="license MIT" src="https://img.shields.io/badge/license-MIT-blue">
|
|
10
38
|
|
|
@@ -44,7 +72,7 @@ uv add xtr-console # beside it: the commands that report on the router
|
|
|
44
72
|
uv add xtr-rate-limiter # beside it: rate limits on routes, routers and the app
|
|
45
73
|
```
|
|
46
74
|
|
|
47
|
-
Requires Python 3.
|
|
75
|
+
Requires Python 3.14.4+. The web framework and the container layer come with the package
|
|
48
76
|
itself — this *is* the integration — and so do the
|
|
49
77
|
[HTTP message contract](../xtr-http-message-contracts) and
|
|
50
78
|
[its implementation](../xtr-http-message): the bridge below is made of them. The three
|
|
@@ -93,13 +121,16 @@ uv run --with 'uvicorn>=0.45' uvicorn --reset-contextvars --app-dir . app:app
|
|
|
93
121
|
|
|
94
122
|
`--reset-contextvars` starts each request in a context of its own. A server gives no promise
|
|
95
123
|
about the context a request's task starts with: on a kept-alive connection it can be a copy of
|
|
96
|
-
an earlier request's, taken while that request was being served. What
|
|
97
|
-
request
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
the next request, and the flag is how it is asked to. With it on, a context
|
|
101
|
-
before the server started is not seen by a request; pass such a value through the
|
|
102
|
-
application's state.
|
|
124
|
+
an earlier request's, taken while that request was being served. What the xtr libraries keep
|
|
125
|
+
per request is safe either way: `setup()` serves every connection in a detached context, in
|
|
126
|
+
which every context variable a library registered as per-request state is set back. A context
|
|
127
|
+
variable the application itself sets has no such registration: only the server can keep it
|
|
128
|
+
from reaching the next request, and the flag is how it is asked to. With it on, a context
|
|
129
|
+
variable set before the server started is not seen by a request; pass such a value through the
|
|
130
|
+
application's state. Middleware added after `setup()` runs outside the request's unit of work.
|
|
131
|
+
So does the framework's own handler for an uncaught exception or a 500: it runs after
|
|
132
|
+
`TerminateEvent`, cannot resolve scoped services, and its response gets no `ResponseEvent` — no
|
|
133
|
+
request id header, no session cookie. Answer an error from an `ExceptionEvent` listener instead.
|
|
103
134
|
|
|
104
135
|
```console
|
|
105
136
|
$ curl -i localhost:8000/books/0262510871
|
|
@@ -168,6 +199,15 @@ out as it arrived — turning it into a response is the application's job, not t
|
|
|
168
199
|
`FinishRequestEvent` runs on every path, which is what makes it the place to close what a
|
|
169
200
|
request opened; `TerminateEvent` carries the status that actually left, `500` when an
|
|
170
201
|
unanswered exception left before the response started.
|
|
202
|
+
Every path includes a file a server sends whole (`http.response.pathsend`), a client gone
|
|
203
|
+
mid-stream and a request listener that raised. `ResponseEvent` is dispatched once per
|
|
204
|
+
request: when one of its listeners raises, the answer sent in its place goes out without it.
|
|
205
|
+
|
|
206
|
+
The finish and terminate listeners, and closing what the request opened, run to their end
|
|
207
|
+
even when the request is cancelled — a disconnect, a server's deadline. Each runs in a task of
|
|
208
|
+
its own, in a copy of the request's context (a listener cannot reset a context variable set
|
|
209
|
+
earlier in the request), and is given 30 seconds; one still running then is cancelled with a
|
|
210
|
+
`TimeoutError`. Keep a slow follow-up out of them: hand it to a message bus.
|
|
171
211
|
|
|
172
212
|
Events are keyed by the qualified name of their class, so a listener declared on a typed
|
|
173
213
|
parameter and one registered under the matching `KernelEvents` constant are the same
|
|
@@ -350,6 +390,14 @@ a `finally`: the engine throws a scope's error into the generator, so a commit t
|
|
|
350
390
|
raises there, and — with the logging listeners active — that failure is written to the log
|
|
351
391
|
against the request that caused it, like any other uncaught exception.
|
|
352
392
|
|
|
393
|
+
A WebSocket or a streamed (SSE) response is one request: its unit, and every scoped service it
|
|
394
|
+
built — a database session, the connection that session holds — lives as long as the
|
|
395
|
+
connection. Open a unit per message for work that should not share one.
|
|
396
|
+
|
|
397
|
+
Each request's own state lives in the ASGI scope's `state`, which the server copies for every
|
|
398
|
+
request; uvicorn and hypercorn do. Under another server, confirm it copies it — a shared one
|
|
399
|
+
would share the request id and the security state between requests.
|
|
400
|
+
|
|
353
401
|
Nothing else is reset between requests. A service built for the application's life is shared
|
|
354
402
|
by every request, including the ones in flight at the same time, so the lifecycle clears no
|
|
355
403
|
state when a request begins or ends: a reset would wipe what a concurrent request is halfway
|
|
@@ -364,13 +412,13 @@ The bundle registers these; which ones depend on what is installed and active:
|
|
|
364
412
|
|---|---|---|---|
|
|
365
413
|
| `RequestIdListener` | `RequestEvent`, `ResponseEvent` | always | mints a `uuid4().hex` — or keeps a well-formed incoming id, where `trust_request_id` says to — puts it on the request's state bag, binds it to the log context when logging is around, and echoes it on the response |
|
|
366
414
|
| `DisallowRobotsIndexingListener` | `ResponseEvent` | always | stamps `X-Robots-Tag: noindex` on every response, when the config turns it on |
|
|
367
|
-
| `
|
|
368
|
-
| `ErrorLoggingListener` | `ExceptionEvent` | logging bundle active | writes every uncaught exception to the request channel — `info` for a cancellation, an interrupt or an exit, `error` below a 500 status, `critical` otherwise — leaving the response to whoever answers it |
|
|
415
|
+
| `ErrorLoggingListener` | `ExceptionEvent` | logging bundle active | writes every uncaught exception to the request channel — `info` for a cancellation, an interrupt or an exit, and for a status below 500 (the caller's doing, so a scanner cannot flood the error log), `critical` otherwise — leaving the response to whoever answers it |
|
|
369
416
|
| `RateLimitHeadersListener` | `ResponseEvent` | rate limiter bundle active | writes the `X-RateLimit-*` headers of the [rate limit](#rate-limits) that speaks for the response — the one a route's limit left on the request's state bag — and makes it private |
|
|
370
417
|
|
|
371
|
-
The
|
|
372
|
-
|
|
373
|
-
|
|
418
|
+
The request's [logging unit of work](../xtr-logging#units-of-work) is not a listener: the
|
|
419
|
+
logging bundle's `LogUnitParticipant` opens it with the request's unit of work and closes it
|
|
420
|
+
with that unit, so every record made while handling — the request id included — belongs to the
|
|
421
|
+
request.
|
|
374
422
|
|
|
375
423
|
## Rate limits
|
|
376
424
|
|
|
@@ -448,6 +496,158 @@ async def login(
|
|
|
448
496
|
...
|
|
449
497
|
```
|
|
450
498
|
|
|
499
|
+
## Profiler
|
|
500
|
+
|
|
501
|
+
A profile is what happened during one request, kept for a development tool to show: the
|
|
502
|
+
request and the response, a timeline of where the time went, every log record, every listener,
|
|
503
|
+
the route, the exception. Profiling is **off by default** — a profile holds every request's
|
|
504
|
+
headers, cookies and state — and turned on by `HttpKernelConfig.profiler`, or by the
|
|
505
|
+
[web profiler](../xtr-web-profiler)'s bundle wherever that is listed, which also draws the
|
|
506
|
+
profiles.
|
|
507
|
+
|
|
508
|
+
```python
|
|
509
|
+
from xtr_dependency_injection import configure
|
|
510
|
+
|
|
511
|
+
from xtr_http_kernel.bundle import HttpKernelConfig, ProfilerConfig
|
|
512
|
+
|
|
513
|
+
|
|
514
|
+
@configure
|
|
515
|
+
def http_kernel() -> HttpKernelConfig:
|
|
516
|
+
return HttpKernelConfig(profiler=ProfilerConfig(enabled=True))
|
|
517
|
+
```
|
|
518
|
+
|
|
519
|
+
With it on, every request is profiled as its response starts, and the response names its
|
|
520
|
+
profile in `X-Debug-Token`. What settles later — the whole duration, the records logged while the
|
|
521
|
+
body streamed, the last events — is added once everything has been sent, and the profile is then
|
|
522
|
+
written in a worker thread, so the caller never waits for the disk. A request whose handling raised
|
|
523
|
+
before any response started is profiled as the 500 the server answered with.
|
|
524
|
+
|
|
525
|
+
- **Per request, whatever runs at once.** Each request's data is its own: the collectors return
|
|
526
|
+
what they kept rather than holding it, the timeline is timed per request, and the event trace
|
|
527
|
+
and the log records are kept per unit of work.
|
|
528
|
+
- **The profiler's own pages are not profiled**: `excluded_paths` leaves out `/_wdt` and
|
|
529
|
+
`/_profiler` by default.
|
|
530
|
+
- **On demand.** `collect=False` with `collect_parameter="profile"` profiles only a request
|
|
531
|
+
carrying `?profile=1` (or a state entry of that name); `only_exceptions=True` keeps only the
|
|
532
|
+
failures.
|
|
533
|
+
- **Kept two days**, as gzipped JSON under `<cache_dir>/profiler/` — `storage_path` moves them —
|
|
534
|
+
found through an index read newest first; about one write in ten deletes the expired ones.
|
|
535
|
+
|
|
536
|
+
| `ProfilerConfig` field | Default | What it sets |
|
|
537
|
+
|---|---|---|
|
|
538
|
+
| `enabled` | `False` | whether requests are profiled at all |
|
|
539
|
+
| `collect` | `True` | profile a request unless it asks otherwise |
|
|
540
|
+
| `collect_parameter` | `None` | a query parameter or state key whose boolean value turns profiling on or off for that request |
|
|
541
|
+
| `only_exceptions` | `False` | keep only requests whose handling raised |
|
|
542
|
+
| `excluded_paths` | `("^/_(wdt\|profiler)",)` | regular expressions; a matching path is not profiled |
|
|
543
|
+
| `excluded_status_codes` | `()` | statuses a profile is not kept for |
|
|
544
|
+
| `storage_path` | `None` | where profiles are kept; `None` is `<kernel.cache_dir>/profiler` |
|
|
545
|
+
| `lifetime_seconds` | `172800` | how long a profile is kept |
|
|
546
|
+
|
|
547
|
+
Read a profile back through the `Profiler` service — `await profiler.load_profile(token)`,
|
|
548
|
+
`await profiler.load_profile_from_response(response.headers)`, `await profiler.find(ip, url,
|
|
549
|
+
limit, method, start=, end=, status_code=)` (a `url` starting with `!` excludes) — or straight
|
|
550
|
+
from `FileProfilerStorage(folder)` in a test. `profiler.disable()` leaves the current request
|
|
551
|
+
unprofiled, and `enable()` profiles it, whatever the other requests do.
|
|
552
|
+
|
|
553
|
+
The core collectors, each a panel of the web profiler:
|
|
554
|
+
|
|
555
|
+
| Collector | Keeps |
|
|
556
|
+
|---|---|
|
|
557
|
+
| `request` | method, URI, headers, query, cookies, path parameters, an already-parsed body, state; the response's status and headers; a `curl` command. Credentials are kept as `******` |
|
|
558
|
+
| `time` | the duration, and every event the request's [stopwatch](../xtr-stopwatch) section timed: the section, the application's handling (`controller`), each event listener |
|
|
559
|
+
| `memory` | the process's peak memory |
|
|
560
|
+
| `ajax` | nothing: the browser lists the page's background requests |
|
|
561
|
+
| `exception` | the exception, its chain of causes, and each frame's source and local variables |
|
|
562
|
+
| `logger` | every record of the request — in debug mode, with the logging bundle active. A context entry named like a credential is kept as `******` |
|
|
563
|
+
| `events` | the listeners called and not called, and the events nobody heard — in debug mode, where the dispatcher is traced |
|
|
564
|
+
| `router` | the route that answered, and a redirect's target |
|
|
565
|
+
| `config` | the Python version, the environment, the active bundles, the xtr packages installed |
|
|
566
|
+
|
|
567
|
+
In debug mode with the logging bundle active, the bundle adds a handler named `profiler` to the
|
|
568
|
+
logging configuration — a `DebugHandler` keeping the records of each unit of work that began
|
|
569
|
+
recording — which the `logger` collector reads. The collector begins it as a profiled request
|
|
570
|
+
arrives, so a request that is not profiled, a command or a worker keeps no record.
|
|
571
|
+
|
|
572
|
+
### Writing a collector
|
|
573
|
+
|
|
574
|
+
A collector implements `DataCollectorInterface` — a `name`, and `collect(profiled)` returning
|
|
575
|
+
what it keeps as plain data — and `LateDataCollectorInterface` when more of it settles once the
|
|
576
|
+
response has left. `profiled` is a `ProfiledRequest`: the request as a message, the response's
|
|
577
|
+
status and headers, the request's state bag and the exception, if any. Something happening while
|
|
578
|
+
the request is handled records itself — on a traced service, on the state — and the collector
|
|
579
|
+
reads it; `clone_var(value)` turns any object into a tree the panel shows.
|
|
580
|
+
|
|
581
|
+
```python
|
|
582
|
+
from xtr_http_kernel.data_collector import DATA_COLLECTOR_TAG, ProfiledRequest, clone_var
|
|
583
|
+
|
|
584
|
+
|
|
585
|
+
class CartCollector:
|
|
586
|
+
@property
|
|
587
|
+
def name(self) -> str:
|
|
588
|
+
return "cart"
|
|
589
|
+
|
|
590
|
+
def collect(self, profiled: ProfiledRequest) -> dict[str, object]:
|
|
591
|
+
return {"cart": clone_var(profiled.state.get("cart"))}
|
|
592
|
+
|
|
593
|
+
|
|
594
|
+
# in a bundle's load_extension
|
|
595
|
+
services.set(CartCollector).add_tag(
|
|
596
|
+
DATA_COLLECTOR_TAG, name="cart", template="@shop/collector/cart.html.j2", priority=250
|
|
597
|
+
)
|
|
598
|
+
```
|
|
599
|
+
|
|
600
|
+
The tag names the collector, the template showing its panel, and a `priority` — higher first,
|
|
601
|
+
in the toolbar and in the menu; `request` is `335`, `config` is `-255`. The web profiler's README
|
|
602
|
+
says [how a panel template is written](../xtr-web-profiler#writing-a-panel).
|
|
603
|
+
|
|
604
|
+
#### Recording while the request is handled
|
|
605
|
+
|
|
606
|
+
A library whose work happens during the request — statements run, messages dispatched, calls
|
|
607
|
+
made — records it into a `RequestTrace` and has its collector read the trace back. One process
|
|
608
|
+
serves many requests at once, so the trace keeps each request's entries in that request's own
|
|
609
|
+
context, a worker thread running a synchronous endpoint included; outside a profiled request,
|
|
610
|
+
`record` does nothing, and the instrumentation costs a lookup.
|
|
611
|
+
|
|
612
|
+
The collector implements `TracingDataCollectorInterface` as well: the profiler calls its
|
|
613
|
+
`begin_request()` once it has decided the request is profiled, and that starts the trace afresh.
|
|
614
|
+
|
|
615
|
+
```python
|
|
616
|
+
from xtr_http_kernel.data_collector import ProfiledRequest, RequestTrace
|
|
617
|
+
|
|
618
|
+
|
|
619
|
+
class CartCollector:
|
|
620
|
+
def __init__(self) -> None:
|
|
621
|
+
self._added: RequestTrace[str] = RequestTrace("cart_added")
|
|
622
|
+
|
|
623
|
+
@property
|
|
624
|
+
def name(self) -> str:
|
|
625
|
+
return "cart"
|
|
626
|
+
|
|
627
|
+
def begin_request(self) -> None:
|
|
628
|
+
self._added.begin()
|
|
629
|
+
|
|
630
|
+
def record(self, sku: str) -> None: # called by the cart while the request is handled
|
|
631
|
+
self._added.record(sku)
|
|
632
|
+
|
|
633
|
+
def collect(self, profiled: ProfiledRequest) -> dict[str, object]:
|
|
634
|
+
return {"added": list(self._added.entries())}
|
|
635
|
+
```
|
|
636
|
+
|
|
637
|
+
A bundle registers such a collector only when requests are profiled. `profiling_active(builder)`
|
|
638
|
+
answers it from `load_extension`, whatever order the bundles load in; import it only once the
|
|
639
|
+
http kernel's bundle is active, so the library keeps working without the http kernel installed:
|
|
640
|
+
|
|
641
|
+
```python
|
|
642
|
+
from xtr_dependency_injection import bundle_active
|
|
643
|
+
|
|
644
|
+
if bundle_active(builder, "http_kernel"):
|
|
645
|
+
from xtr_http_kernel.bundle import profiling_active
|
|
646
|
+
|
|
647
|
+
if profiling_active(builder):
|
|
648
|
+
register_profiler(services)
|
|
649
|
+
```
|
|
650
|
+
|
|
451
651
|
## Use in an application
|
|
452
652
|
|
|
453
653
|
Everything adding this package to an application on
|
|
@@ -507,10 +707,45 @@ default:
|
|
|
507
707
|
| `log_channel` | `"request"` | the channel the error listener writes to |
|
|
508
708
|
| `middleware_priority` | `0` | where the lifecycle middleware sits among the contributed factories — highest outermost |
|
|
509
709
|
| `app` | `None` | the `"package.module:app"` string the router commands load the application from |
|
|
710
|
+
| `trusted_hosts` | `()` | the hosts a request may name — `"shop.test"`, `"*.shop.test"`, an address; any other is answered 400. Empty lets every host through |
|
|
711
|
+
| `max_body_size` | `10485760` | the most bytes of a request body read, uploads included (10 MiB); more is answered 413. `None` reads any body |
|
|
712
|
+
| `profiler` | `ProfilerConfig()` | whether and how requests are profiled — off by default; see [Profiler](#profiler) |
|
|
510
713
|
|
|
511
714
|
The constructor refuses values the lifecycle would silently misread: a `request_id_header` that
|
|
512
|
-
is not an HTTP token, an empty `log_channel`,
|
|
513
|
-
attribute around a single `:`,
|
|
715
|
+
is not an HTTP token, an empty `log_channel`, an `app` that is not exactly one module and one
|
|
716
|
+
attribute around a single `:`, or a trusted host that names no host, each raise
|
|
717
|
+
`InvalidArgumentError` — also a `ValueError`.
|
|
718
|
+
|
|
719
|
+
`trusted_hosts` is matched against the `Host` header lowercased, port left out. A request
|
|
720
|
+
naming another host — or a `Host` header that names none — is answered `400 Invalid host
|
|
721
|
+
header` just outside the lifecycle, so none of its events run and no request id is minted for
|
|
722
|
+
it. A health check that addresses the server by IP needs that address listed. Without
|
|
723
|
+
`trusted_hosts`, a `Host` header the request's URI cannot hold (`a b`, `x/y`) reads as absent:
|
|
724
|
+
the URI names the address the server listens on instead — which a firewall matching on the
|
|
725
|
+
host then sees.
|
|
726
|
+
|
|
727
|
+
A failure nothing answered — no `ExceptionEvent` listener set a response — goes on to the
|
|
728
|
+
application's own error handling, outside the lifecycle: that answer gets no `ResponseEvent`,
|
|
729
|
+
so no request id and no session cookie. Answer failures from an `ExceptionEvent` listener to
|
|
730
|
+
have them.
|
|
731
|
+
|
|
732
|
+
`max_body_size` is enforced outermost, around the lifecycle, so it bounds every read — a firewall
|
|
733
|
+
listener reading a login form before routing as well as the route. A body declared over the
|
|
734
|
+
limit by its `Content-Length`, or sent past it, is answered `413 Content Too Large`. That answer
|
|
735
|
+
leaves from outside the lifecycle: no `ExceptionEvent` is dispatched for it, and
|
|
736
|
+
`TerminateEvent.status_code` is the status the application started with, not the 413. A
|
|
737
|
+
failure the server's `send` raises — a socket gone mid-response — is likewise announced to no
|
|
738
|
+
exception listener. FastAPI ignores `Starlette(max_body_size=...)`; to take larger uploads on one
|
|
739
|
+
route, mount a Starlette `Router` or `Route` with its own `max_body_size`, which raises the limit
|
|
740
|
+
for what it serves. A hand-registered `RequestLifecycleMiddleware` gets neither the host check
|
|
741
|
+
nor the body limit: add Starlette's `TrustedHostMiddleware` and `RequestBodyLimitMiddleware`
|
|
742
|
+
yourself.
|
|
743
|
+
|
|
744
|
+
An upload — a `multipart/form-data` body — read through the request message once a route asks
|
|
745
|
+
for it is parsed straight off the wire into spooled files; its raw bytes are not kept, so the
|
|
746
|
+
message's body is empty and an endpoint calling `await request.body()` after that gets the
|
|
747
|
+
framework's "Stream consumed" error. A body the application read itself cannot be read again
|
|
748
|
+
from a finish or terminate listener: read it at `RequestEvent`, or through the message.
|
|
514
749
|
|
|
515
750
|
`trust_request_id` has one prerequisite: a proxy you control in front of the application, which
|
|
516
751
|
sets `request_id_header` itself and strips whatever the caller sent. Reached directly, the
|
|
@@ -738,6 +973,8 @@ itself, as well as from `xtr_http_kernel.exception`.
|
|
|
738
973
|
| Error | Raised when |
|
|
739
974
|
|---|---|
|
|
740
975
|
| `InvalidArgumentError` | an `HttpKernelConfig` field holds a value the lifecycle would misread; also a `ValueError` |
|
|
976
|
+
| `InvalidCollectorPriorityError` | a `http_kernel.data_collector` tag's `priority` is not an integer; also a `TypeError` |
|
|
977
|
+
| `InvalidCollectorTagError` | a `http_kernel.data_collector` tag lacks its `name` or its `template`; also a `ValueError` |
|
|
741
978
|
| `InvalidCookieError` | a request message carries a cookie a `Cookie` header cannot carry unchanged — a name that is not a token, or a value holding `;`, a control character or anything beyond visible ASCII; also a `ValueError` |
|
|
742
979
|
| `InvalidMiddlewarePriorityError` | a `http_kernel.middleware` tag's `priority` is not an integer |
|
|
743
980
|
| `InvalidRateLimitError` | a `RateLimited` takes fewer than one token, limits a router that already has routes, or its key function returns no string; also a `ValueError` |
|
|
@@ -746,6 +983,8 @@ itself, as well as from `xtr_http_kernel.exception`.
|
|
|
746
983
|
| `StreamedResponseBodyError` | a response being built into a message streams its body, which cannot be read without consuming it |
|
|
747
984
|
| `TooManyRequestsError` | a limiter refused the request — a 429 the framework answers, with `Retry-After` |
|
|
748
985
|
| `UnknownRateLimiterError` | a `RateLimited` names a limiter the application did not configure; also a `LookupError` |
|
|
986
|
+
| `InvalidTokenError` | a profile token holds anything but letters, digits, `_` and `-`; also a `ValueError` |
|
|
987
|
+
| `UnknownCollectorError` | a profile is asked for the data of a collector it does not hold; also a `LookupError` |
|
|
749
988
|
| `UnsupportedConnectionError` | something asked for the request as a message while a WebSocket was being served |
|
|
750
989
|
|
|
751
990
|
The routing layer's errors live in `xtr_http_kernel.routing`, under `RoutingError` — itself an
|
|
@@ -771,12 +1010,19 @@ xtr_http_kernel/
|
|
|
771
1010
|
├── middleware_stack.py the ordered chain a bundle contributes to
|
|
772
1011
|
├── middleware_tag.py MIDDLEWARE_TAG, the tag bundles agree on
|
|
773
1012
|
├── rate_limiter/ RateLimited, when xtr-rate-limiter is installed
|
|
1013
|
+
├── profiler/ Profiler, Profile, FileProfilerStorage
|
|
1014
|
+
├── data_collector/ the collector contracts, clone_var and the core collectors
|
|
774
1015
|
├── routing/ as_route_resource, as_routes, the registry setup stages from
|
|
775
1016
|
├── command/ debug:router and router:match
|
|
776
1017
|
├── exception/ HttpKernelError, the root of everything this library raises
|
|
777
1018
|
└── bundle/ HttpKernelBundle and HttpKernelConfig
|
|
778
1019
|
```
|
|
779
1020
|
|
|
1021
|
+
## Upgrading
|
|
1022
|
+
|
|
1023
|
+
What changes for an application moving to 7.0, and how to keep the old behaviour where that is
|
|
1024
|
+
still possible: [Upgrading to 7.0](../../UPGRADING.md#xtr-http-kernel).
|
|
1025
|
+
|
|
780
1026
|
## Development
|
|
781
1027
|
|
|
782
1028
|
Developed in the [python-xtr](https://github.com/xterr/python-xtr) monorepo, under
|