Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions oxyroute/streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@


class _StreamDone:
__slots__ = ()
__slots__ = ("status",)
__oxyroute_stream_done__ = True

def __init__(self, status: int = 200) -> None:
self.status = status

def stream_done() -> Any:

def stream_done(status: int = 200) -> Any:
"""Return a marker value telling OxyRoute the response was already sent."""
return _StreamDone()
return _StreamDone(status)


async def stream_bytes(
Expand Down Expand Up @@ -44,7 +47,7 @@ async def stream_bytes(
else:
for chunk in iterable: # type: ignore[not-an-iterable]
await stream.send_bytes(chunk)
return stream_done()
return stream_done(status)

# Fallback for test/ASGI transports
chunks: list[bytes] = []
Expand All @@ -55,7 +58,7 @@ async def stream_bytes(
for chunk in iterable: # type: ignore[not-an-iterable]
chunks.append(chunk)
protocol.response_bytes(status, base_headers, b"".join(chunks))
return stream_done()
return stream_done(status)


async def stream_text(
Expand Down Expand Up @@ -85,7 +88,7 @@ async def stream_text(
else:
for chunk in iterable: # type: ignore[not-an-iterable]
await stream.send_str(chunk)
return stream_done()
return stream_done(status)

chunks: list[str] = []
if hasattr(iterable, "__aiter__"):
Expand All @@ -95,7 +98,7 @@ async def stream_text(
for chunk in iterable: # type: ignore[not-an-iterable]
chunks.append(chunk)
protocol.response_str(status, base_headers, "".join(chunks))
return stream_done()
return stream_done(status)


async def stream_jsonl(
Expand Down
14 changes: 7 additions & 7 deletions src/state.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::{HashMap, HashSet};
use std::collections::HashSet;
use std::sync::Arc;

use matchit::Router;
Expand Down Expand Up @@ -200,11 +200,11 @@ pub struct HotSnapshot {
pub fn match_ws_route_compiled(
compiled: &CompiledRouters,
path: &str,
) -> Option<(usize, HashMap<String, String>)> {
) -> Option<(usize, Vec<(String, String)>)> {
compiled.websocket.at(path).ok().map(|m| {
let mut pmap = HashMap::new();
let mut pmap = Vec::new();
for (k, v) in m.params.iter() {
pmap.insert(k.to_string(), v.to_string());
pmap.push((k.to_string(), v.to_string()));
}
(*m.value, pmap)
})
Expand All @@ -217,12 +217,12 @@ pub fn match_route_compiled(
compiled: &CompiledRouters,
method: &str,
path: &str,
) -> Option<Option<(usize, HashMap<String, String>)>> {
) -> Option<Option<(usize, Vec<(String, String)>)>> {
let g = router_for_compiled(compiled, method)?;
Some(g.at(path).ok().map(|m| {
let mut pmap = HashMap::new();
let mut pmap = Vec::new();
for (k, v) in m.params.iter() {
pmap.insert(k.to_string(), v.to_string());
pmap.push((k.to_string(), v.to_string()));
}
(*m.value, pmap)
}))
Expand Down
9 changes: 2 additions & 7 deletions src/websocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//! it would have awaited natively — no extra Tokio future bridge, no scheduling cost.
//!
//! [1]: https://github.com/emmett-framework/granian — see `granian/rsgi.py`.
use std::collections::HashMap;
use std::sync::Arc;

use parking_lot::Mutex;
Expand All @@ -30,16 +29,12 @@ pub struct WebSocket {
protocol: Py<PyAny>,
scope: Py<PyAny>,
transport: Arc<Mutex<Option<Py<PyAny>>>>,
path_params: HashMap<String, String>,
path_params: Vec<(String, String)>,
closed: Arc<Mutex<bool>>,
}

impl WebSocket {
pub fn new(
protocol: Py<PyAny>,
scope: Py<PyAny>,
path_params: HashMap<String, String>,
) -> Self {
pub fn new(protocol: Py<PyAny>, scope: Py<PyAny>, path_params: Vec<(String, String)>) -> Self {
Self {
protocol,
scope,
Expand Down
Loading