@@ -15,6 +15,8 @@ use tokio::net::TcpListener;
1515use tokio:: sync:: Mutex ;
1616use tracing:: { debug, error, info, trace} ;
1717
18+ use crate :: db:: { DbPool , Error } ;
19+
1820pub const DEFAULT_DIR_PORT : u16 = 8080 ;
1921pub const DEFAULT_DB_HOST : & str = "localhost:6379" ;
2022pub const DEFAULT_TIMEOUT_SECS : u64 = 30 ;
@@ -34,7 +36,6 @@ const V1_UNAVAILABLE_RES_JSON: &str = r#"{{"errorCode": "unavailable", "message"
3436const ID_LENGTH : usize = 13 ;
3537
3638mod db;
37- use crate :: db:: DbPool ;
3839
3940#[ cfg( feature = "_danger-local-https" ) ]
4041type BoxError = Box < dyn std:: error:: Error + Send + Sync > ;
@@ -312,6 +313,22 @@ impl From<hyper::http::Error> for HandlerError {
312313 fn from ( e : hyper:: http:: Error ) -> Self { HandlerError :: InternalServerError ( e. into ( ) ) }
313314}
314315
316+ fn handle_peek (
317+ result : Result < Vec < u8 > , Error > ,
318+ timeout_response : Response < BoxBody < Bytes , hyper:: Error > > ,
319+ ) -> Result < Response < BoxBody < Bytes , hyper:: Error > > , HandlerError > {
320+ match result {
321+ Ok ( buffered_req) => Ok ( Response :: new ( full ( buffered_req) ) ) ,
322+ Err ( e) => match e {
323+ Error :: Redis ( re) => {
324+ error ! ( "Redis error: {}" , re) ;
325+ Err ( HandlerError :: InternalServerError ( anyhow:: Error :: msg ( "Internal server error" ) ) )
326+ } ,
327+ Error :: Timeout ( _) => Ok ( timeout_response) ,
328+ } ,
329+ }
330+ }
331+
315332async fn post_fallback_v1 (
316333 id : & str ,
317334 query : String ,
@@ -340,13 +357,7 @@ async fn post_fallback_v1(
340357 pool. push_default ( id, v2_compat_body. into ( ) )
341358 . await
342359 . map_err ( |e| HandlerError :: BadRequest ( e. into ( ) ) ) ?;
343- match pool. peek_v1 ( id) . await {
344- Some ( result) => match result {
345- Ok ( buffered_req) => Ok ( Response :: new ( full ( buffered_req) ) ) ,
346- Err ( e) => Err ( HandlerError :: BadRequest ( e. into ( ) ) ) ,
347- } ,
348- None => Ok ( none_response) ,
349- }
360+ handle_peek ( pool. peek_v1 ( id) . await , none_response)
350361}
351362
352363async fn put_payjoin_v1 (
@@ -408,13 +419,8 @@ async fn get_subdir(
408419) -> Result < Response < BoxBody < Bytes , hyper:: Error > > , HandlerError > {
409420 trace ! ( "get_subdir" ) ;
410421 let id = check_id_length ( id) ?;
411- match pool. peek_default ( id) . await {
412- Some ( result) => match result {
413- Ok ( buffered_req) => Ok ( Response :: new ( full ( buffered_req) ) ) ,
414- Err ( e) => Err ( HandlerError :: BadRequest ( e. into ( ) ) ) ,
415- } ,
416- None => Ok ( Response :: builder ( ) . status ( StatusCode :: ACCEPTED ) . body ( empty ( ) ) ?) ,
417- }
422+ let timeout_response = Response :: builder ( ) . status ( StatusCode :: ACCEPTED ) . body ( empty ( ) ) ?;
423+ handle_peek ( pool. peek_default ( id) . await , timeout_response)
418424}
419425
420426fn not_found ( ) -> Response < BoxBody < Bytes , hyper:: Error > > {
0 commit comments