Skip to content

Commit 94aa2aa

Browse files
committed
Return mailbox GET without long-polling
1 parent af6928f commit 94aa2aa

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

payjoin-mailroom/src/db/files.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ impl DbTrait for FilesDb {
246246
Ok(guard.read(id).await?)
247247
}
248248

249+
// Unused by GET after the non-blocking switch; v2 waitmap removal is a follow-up.
249250
async fn wait_for_v2_payload(
250251
&self,
251252
id: &ShortId,

payjoin-mailroom/src/directory.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,16 @@ impl<D: Db> Service<D> {
288288
async fn get_mailbox(&self, id: &str) -> Result<Response<Body>, HandlerError> {
289289
trace!("get_mailbox");
290290
let id = ShortId::from_str(id)?;
291-
let timeout_response = Response::builder().status(StatusCode::ACCEPTED).body(empty())?;
292-
handle_peek(self.db.wait_for_v2_payload(&id).await, timeout_response)
291+
let empty_response = Response::builder().status(StatusCode::ACCEPTED).body(empty())?;
292+
match self.db.peek_v2_payload(&id).await {
293+
Ok(Some(payload)) => Ok(Response::new(full((*payload).clone()))),
294+
Ok(None) => Ok(empty_response),
295+
Err(DbError::Operational(err)) => {
296+
error!("Storage error: {err}");
297+
Err(HandlerError::InternalServerError(anyhow::Error::msg("Internal server error")))
298+
}
299+
Err(_) => Ok(empty_response),
300+
}
293301
}
294302

295303
/// Screen a V1 PSBT body against the address blocklist.
@@ -878,6 +886,25 @@ mod tests {
878886
}
879887
}
880888

889+
#[tokio::test(start_paused = true)]
890+
async fn get_mailbox_returns_immediately_when_empty() {
891+
let svc = test_service(None).await;
892+
let id = valid_short_id_path();
893+
let start = tokio::time::Instant::now();
894+
let res = svc.get_mailbox(&id).await.expect("get_mailbox");
895+
assert_eq!(res.status(), StatusCode::ACCEPTED);
896+
assert_eq!(start.elapsed(), Duration::ZERO, "GET must not block");
897+
}
898+
899+
#[tokio::test]
900+
async fn get_mailbox_returns_payload_when_present() {
901+
let svc = test_service(None).await;
902+
let id = valid_short_id_path();
903+
svc.post_mailbox(&id, Body::from(b"hi".to_vec())).await.expect("post");
904+
let res = svc.get_mailbox(&id).await.expect("get_mailbox");
905+
assert_eq!(res.status(), StatusCode::OK);
906+
}
907+
881908
#[tokio::test]
882909
async fn post_mailbox_records_short_id_cardinality() {
883910
use opentelemetry_sdk::metrics::{

0 commit comments

Comments
 (0)