Skip to content

Commit

Permalink
Enumerate errors for peek_with_timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
shinghim committed Jan 3, 2025
1 parent d940ed2 commit c385f96
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
9 changes: 5 additions & 4 deletions payjoin-directory/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::time::Duration;

use futures::StreamExt;
use redis::{AsyncCommands, Client, ErrorKind, RedisError, RedisResult};
use tokio::time::error::Elapsed;
use tracing::debug;

const DEFAULT_COLUMN: &str = "";
Expand All @@ -23,15 +24,15 @@ impl DbPool {
self.push(subdirectory_id, DEFAULT_COLUMN, data).await
}

pub async fn peek_default(&self, subdirectory_id: &str) -> Option<RedisResult<Vec<u8>>> {
pub async fn peek_default(&self, subdirectory_id: &str) -> Result<RedisResult<Vec<u8>>, Elapsed> {
self.peek_with_timeout(subdirectory_id, DEFAULT_COLUMN).await
}

pub async fn push_v1(&self, subdirectory_id: &str, data: Vec<u8>) -> RedisResult<()> {
self.push(subdirectory_id, PJ_V1_COLUMN, data).await
}

pub async fn peek_v1(&self, subdirectory_id: &str) -> Option<RedisResult<Vec<u8>>> {
pub async fn peek_v1(&self, subdirectory_id: &str) -> Result<RedisResult<Vec<u8>>, Elapsed> {
self.peek_with_timeout(subdirectory_id, PJ_V1_COLUMN).await
}

Expand All @@ -52,8 +53,8 @@ impl DbPool {
&self,
subdirectory_id: &str,
channel_type: &str,
) -> Option<RedisResult<Vec<u8>>> {
tokio::time::timeout(self.timeout, self.peek(subdirectory_id, channel_type)).await.ok()
) -> Result<RedisResult<Vec<u8>>, Elapsed> {
tokio::time::timeout(self.timeout, self.peek(subdirectory_id, channel_type)).await
}

async fn peek(&self, subdirectory_id: &str, channel_type: &str) -> RedisResult<Vec<u8>> {
Expand Down
8 changes: 4 additions & 4 deletions payjoin-directory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,11 +341,11 @@ async fn post_fallback_v1(
.await
.map_err(|e| HandlerError::BadRequest(e.into()))?;
match pool.peek_v1(id).await {
Some(result) => match result {
Ok(result) => match result {
Ok(buffered_req) => Ok(Response::new(full(buffered_req))),
Err(e) => Err(HandlerError::BadRequest(e.into())),
},
None => Ok(none_response),
_ => Ok(none_response),
}
}

Expand Down Expand Up @@ -409,11 +409,11 @@ async fn get_subdir(
trace!("get_subdir");
let id = check_id_length(id)?;
match pool.peek_default(id).await {
Some(result) => match result {
Ok(result) => match result {
Ok(buffered_req) => Ok(Response::new(full(buffered_req))),
Err(e) => Err(HandlerError::BadRequest(e.into())),
},
None => Ok(Response::builder().status(StatusCode::ACCEPTED).body(empty())?),
_ => Ok(Response::builder().status(StatusCode::ACCEPTED).body(empty())?),
}
}

Expand Down

0 comments on commit c385f96

Please sign in to comment.