11use std:: { sync:: Arc , time:: Duration } ;
22
3- use anyhow:: { Error , anyhow, bail } ;
3+ use anyhow:: { Error , anyhow} ;
44use axum:: {
55 Router ,
66 body:: Body ,
@@ -51,6 +51,7 @@ pub struct HandlerState {
5151 retry_interval_no_healthy_nodes : Duration ,
5252}
5353
54+ /// Buffers the request body
5455async fn buffer_request (
5556 state : & HandlerState ,
5657 request : Request ,
@@ -76,7 +77,8 @@ async fn buffer_request(
7677 Ok ( ( parts, body) )
7778}
7879
79- async fn buffer_response ( state : & HandlerState , response : Response ) -> Result < Response , Error > {
80+ /// Buffers the response body
81+ async fn buffer_response ( state : & HandlerState , response : Response ) -> Response {
8082 // Buffer the response
8183 let ( parts, body) = response. into_parts ( ) ;
8284 let backend = REQUEST_CONTEXT
@@ -89,24 +91,30 @@ async fn buffer_response(state: &HandlerState, response: Response) -> Result<Res
8991 let body = Limited :: new ( body, state. response_body_size_limit ) ;
9092
9193 let Ok ( body) = timeout ( state. response_body_timeout , body. collect ( ) ) . await else {
92- let err = format ! ( "Timed out reading response body from backend '{backend}'" ) ;
93- info ! ( err) ;
94- bail ! ( err) ;
94+ info ! ( "Timed out reading response body from backend '{backend}'" ) ;
95+ return (
96+ StatusCode :: GATEWAY_TIMEOUT ,
97+ "Timed out reading response body from the backend" ,
98+ )
99+ . into_response ( ) ;
95100 } ;
96101
97102 let body = match body {
98103 Ok ( v) => Body :: from ( v. to_bytes ( ) ) ,
99104 Err ( e) => {
100- let err = format ! (
105+ info ! (
101106 "Unable to read response body from backend '{backend}': {:#}" ,
102107 anyhow!( e)
103108 ) ;
104- info ! ( err) ;
105- bail ! ( err) ;
109+ return (
110+ StatusCode :: BAD_GATEWAY ,
111+ "Error reading response body from the backend" ,
112+ )
113+ . into_response ( ) ;
106114 }
107115 } ;
108116
109- Ok ( Response :: from_parts ( parts, body) )
117+ Response :: from_parts ( parts, body)
110118}
111119
112120pub async fn handler (
@@ -132,27 +140,24 @@ pub async fn handler(
132140 Err ( BackendRouterError :: Inner ( e) ) => {
133141 info ! ( "Unable to execute the request: {:#}" , anyhow!( e) ) ;
134142 (
135- StatusCode :: SERVICE_UNAVAILABLE ,
143+ StatusCode :: BAD_GATEWAY ,
136144 "Unable to execute the request to the backend" ,
137145 )
138146 . into_response ( )
139147 }
140148 Ok ( v) => v,
141149 } ;
142150
143- return match buffer_response ( & state, response) . await {
144- Ok ( v) => v,
145- Err ( _) => (
146- StatusCode :: BAD_GATEWAY ,
147- "Unable to buffer the response from the backend" ,
148- )
149- . into_response ( ) ,
150- } ;
151+ if !state. response_body_buffer {
152+ return response;
153+ }
154+
155+ return buffer_response ( & state, response) . await ;
151156 }
152157
153158 // Buffer the request body
154159 let Ok ( ( parts, body) ) = buffer_request ( & state, request) . await else {
155- return ( StatusCode :: SERVICE_UNAVAILABLE , "Unable to buffer body" ) . into_response ( ) ;
160+ return ( StatusCode :: REQUEST_TIMEOUT , "Unable to buffer body" ) . into_response ( ) ;
156161 } ;
157162
158163 let mut retries = state. retry_attempts ;
@@ -194,14 +199,7 @@ pub async fn handler(
194199 return response;
195200 }
196201
197- match buffer_response ( & state, response) . await {
198- Ok ( v) => v,
199- Err ( _) => (
200- StatusCode :: BAD_GATEWAY ,
201- "Unable to buffer the response from the backend" ,
202- )
203- . into_response ( ) ,
204- }
202+ buffer_response ( & state, response) . await
205203}
206204
207205/// Creates top-level Axum Router
0 commit comments