1- use futures:: { sink:: SinkExt , StreamExt , TryStreamExt } ;
1+ use futures:: { sink:: SinkExt , StreamExt } ;
22use http_body_util:: BodyExt ;
33use hyper:: body;
44use pyo3:: { prelude:: * , pybacked:: PyBackedStr } ;
@@ -15,47 +15,40 @@ use super::{
1515} ;
1616use crate :: {
1717 conversion:: FutureResultToPy ,
18- runtime:: { empty_future_into_py, future_into_py_futlike, RuntimeRef } ,
18+ runtime:: { empty_future_into_py, err_future_into_py , future_into_py_futlike, RuntimeRef } ,
1919 ws:: { HyperWebsocket , UpgradeData , WSRxStream , WSTxStream } ,
2020} ;
2121
2222pub ( crate ) type WebsocketDetachedTransport = ( i32 , bool , Option < WSTxStream > ) ;
2323
2424#[ pyclass( frozen, module = "granian._granian" ) ]
2525pub ( crate ) struct RSGIHTTPStreamTransport {
26- rt : RuntimeRef ,
27- tx : mpsc:: Sender < Result < body:: Bytes , anyhow:: Error > > ,
26+ tx : mpsc:: UnboundedSender < body:: Bytes > ,
2827}
2928
3029impl RSGIHTTPStreamTransport {
31- pub fn new ( rt : RuntimeRef , transport : mpsc:: Sender < Result < body:: Bytes , anyhow :: Error > > ) -> Self {
32- Self { rt , tx : transport }
30+ pub fn new ( transport : mpsc:: UnboundedSender < body:: Bytes > ) -> Self {
31+ Self { tx : transport }
3332 }
3433}
3534
35+ // NOTE: the interface doesn't need to be async anymore.
36+ // This would be a breaking change though; probably requires a major version bump in RSGI
3637#[ pymethods]
3738impl RSGIHTTPStreamTransport {
3839 fn send_bytes < ' p > ( & self , py : Python < ' p > , data : Cow < [ u8 ] > ) -> PyResult < Bound < ' p , PyAny > > {
39- let transport = self . tx . clone ( ) ;
40- let bdata: Box < [ u8 ] > = data. into ( ) ;
41-
42- future_into_py_futlike ( self . rt . clone ( ) , py, async move {
43- match transport. send ( Ok ( body:: Bytes :: from ( bdata) ) ) . await {
44- Ok ( ( ) ) => FutureResultToPy :: None ,
45- _ => FutureResultToPy :: Err ( error_stream ! ( ) ) ,
46- }
47- } )
40+ let bdata = body:: Bytes :: from ( std:: convert:: Into :: < Box < [ u8 ] > > :: into ( data) ) ;
41+ match self . tx . send ( bdata) {
42+ Ok ( ( ) ) => empty_future_into_py ( py) ,
43+ _ => err_future_into_py ( py, error_stream ! ( ) ) ,
44+ }
4845 }
4946
5047 fn send_str < ' p > ( & self , py : Python < ' p > , data : String ) -> PyResult < Bound < ' p , PyAny > > {
51- let transport = self . tx . clone ( ) ;
52-
53- future_into_py_futlike ( self . rt . clone ( ) , py, async move {
54- match transport. send ( Ok ( body:: Bytes :: from ( data) ) ) . await {
55- Ok ( ( ) ) => FutureResultToPy :: None ,
56- _ => FutureResultToPy :: Err ( error_stream ! ( ) ) ,
57- }
58- } )
48+ match self . tx . send ( body:: Bytes :: from ( data) ) {
49+ Ok ( ( ) ) => empty_future_into_py ( py) ,
50+ _ => err_future_into_py ( py, error_stream ! ( ) ) ,
51+ }
5952 }
6053}
6154
@@ -162,7 +155,11 @@ impl RSGIHTTPProtocol {
162155 #[ pyo3( signature = ( status=200 , headers=vec![ ] , body=vec![ ] . into( ) ) ) ]
163156 fn response_bytes ( & self , status : u16 , headers : Vec < ( PyBackedStr , PyBackedStr ) > , body : Cow < [ u8 ] > ) {
164157 if let Some ( tx) = self . tx . lock ( ) . unwrap ( ) . take ( ) {
165- _ = tx. send ( PyResponse :: Body ( PyResponseBody :: from_bytes ( status, headers, body) ) ) ;
158+ _ = tx. send ( PyResponse :: Body ( PyResponseBody :: from_bytes (
159+ status,
160+ headers,
161+ body. into ( ) ,
162+ ) ) ) ;
166163 }
167164 }
168165
@@ -188,16 +185,18 @@ impl RSGIHTTPProtocol {
188185 headers : Vec < ( PyBackedStr , PyBackedStr ) > ,
189186 ) -> PyResult < Bound < ' p , RSGIHTTPStreamTransport > > {
190187 if let Some ( tx) = self . tx . lock ( ) . unwrap ( ) . take ( ) {
191- let ( body_tx, body_rx) = mpsc:: channel :: < Result < body:: Bytes , anyhow :: Error > > ( 1 ) ;
188+ let ( body_tx, body_rx) = mpsc:: unbounded_channel :: < body:: Bytes > ( ) ;
192189 let body_stream = http_body_util:: StreamBody :: new (
193- tokio_stream:: wrappers:: ReceiverStream :: new ( body_rx) . map_ok ( body:: Frame :: data) ,
190+ tokio_stream:: wrappers:: UnboundedReceiverStream :: new ( body_rx)
191+ . map ( body:: Frame :: data)
192+ . map ( Result :: Ok ) ,
194193 ) ;
195194 _ = tx. send ( PyResponse :: Body ( PyResponseBody :: new (
196195 status,
197196 headers,
198- BodyExt :: boxed ( BodyExt :: map_err ( body_stream, std :: convert :: Into :: into ) ) ,
197+ BodyExt :: boxed ( body_stream) ,
199198 ) ) ) ;
200- let trx = Py :: new ( py, RSGIHTTPStreamTransport :: new ( self . rt . clone ( ) , body_tx) ) ?;
199+ let trx = Py :: new ( py, RSGIHTTPStreamTransport :: new ( body_tx) ) ?;
201200 return Ok ( trx. into_bound ( py) ) ;
202201 }
203202 error_proto ! ( )
0 commit comments