Skip to content

Commit d9607b4

Browse files
committed
fix formatting
1 parent 934c99f commit d9607b4

5 files changed

Lines changed: 36 additions & 12 deletions

File tree

docs_src/src/pages/documentation/en/api_reference/advanced_routing.mdx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,9 @@ The parameter injection system works in two phases:
185185

186186
<Row>
187187
<Col>
188-
Use `const=True` for responses that never change. These are cached in Rust memory and served without Python execution.
188+
Use `const=True` for responses that never change. These are cached in Rust memory and the handler function is never re-executed after startup.
189+
190+
When no middleware is registered, const routes take a fast path served entirely from the Rust layer without entering Python at all. When middleware is registered (including global before-request and after-request handlers), const routes still serve the cached response but middleware executes normally for every request. This means const routes are always safe to use alongside middleware.
189191
</Col>
190192
<Col sticky>
191193
<CodeGroup title="Const Route Optimization">

docs_src/src/pages/documentation/en/api_reference/architecture_deep_dive.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ Robyn includes a "const routes" optimization for static responses:
118118

119119
<Row>
120120
<Col>
121-
When you mark a route as `const`, Robyn can serve the response directly from the Rust layer without invoking Python at all.
121+
When you mark a route as `const`, Robyn caches the response in Rust memory and never re-executes the Python handler. If no middleware is registered, const routes are served entirely from the Rust layer without entering Python at all. When middleware is present, the cached response is still used but before-request and after-request middleware execute normally.
122122
</Col>
123123
<Col sticky>
124124
<CodeGroup title="Const Routes">
@@ -128,8 +128,8 @@ Robyn includes a "const routes" optimization for static responses:
128128
def health_check():
129129
return {"status": "healthy"}
130130

131-
# Subsequent requests are served directly from Rust
132-
# bypassing Python entirely
131+
# Without middleware: served directly from Rust, bypassing Python entirely
132+
# With middleware: cached response is used, but middleware still runs
133133
```
134134
</CodeGroup>
135135
</Col>

integration_tests/test_middlewares.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,22 @@ def test_global_middleware(session):
2828
def test_response_in_before_middleware(session):
2929
r = get("/sync/middlewares/401", should_check_response=False)
3030
assert r.status_code == 401
31+
32+
33+
@pytest.mark.benchmark
34+
@pytest.mark.parametrize(
35+
"route",
36+
[
37+
"/sync/str/const",
38+
"/async/str/const",
39+
"/sync/dict/const",
40+
"/async/dict/const",
41+
"/sync/response/const",
42+
"/async/response/const",
43+
],
44+
)
45+
def test_global_middleware_applied_to_const_routes(route: str, session):
46+
r = get(route)
47+
assert r.headers.get("global_after") == "global_after_request", (
48+
f"Global after-request middleware was not applied to const route {route}"
49+
)

src/server.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,8 +491,13 @@ async fn index(
491491
Err(_) => return ResponseType::Standard(Response::method_not_allowed(None)),
492492
};
493493

494-
let mut request: Request =
495-
Request::from_actix_request(&req, payload, &global_request_headers).await;
494+
let mut request: Request = match Request::from_actix_request(&req, payload, &global_request_headers).await {
495+
Ok(r) => r,
496+
Err(e) => {
497+
error!("Failed to parse request for `{}`: {}", req.path(), e);
498+
return ResponseType::Standard(Response::internal_server_error(None));
499+
}
500+
};
496501

497502
let route = format!("{}{}", req.method(), request.url.path);
498503

src/types/request.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ impl Request {
124124
req: &HttpRequest,
125125
mut payload: web::Payload,
126126
global_headers: &Headers,
127-
) -> Self {
127+
) -> Result<Self, Error> {
128128
let mut query_params: QueryParams = QueryParams::new();
129129
let mut form_data: HashMap<String, String> = HashMap::new();
130130
let mut files = HashMap::new();
@@ -151,9 +151,7 @@ impl Request {
151151
let multipart = Multipart::new(req.headers(), payload);
152152
let mut body_local: Vec<u8> = Vec::new();
153153

154-
let a = handle_multipart(multipart, &mut files, &mut form_data, &mut body_local).await;
155-
156-
let _ = a;
154+
handle_multipart(multipart, &mut files, &mut form_data, &mut body_local).await?;
157155

158156
body_local
159157
} else {
@@ -180,7 +178,7 @@ impl Request {
180178
);
181179
let ip_addr = req.peer_addr().map(|val| val.ip().to_string());
182180

183-
Self {
181+
Ok(Self {
184182
query_params,
185183
headers,
186184
method: req.method().as_str().to_owned(),
@@ -191,7 +189,7 @@ impl Request {
191189
identity: None,
192190
form_data: Some(form_data),
193191
files: Some(files),
194-
}
192+
})
195193
}
196194
}
197195

0 commit comments

Comments
 (0)