[relay] Wake-up: a push endpoint per mailbox, and a constant wake to it when a blob arrives
ci / check (pull_request) Has been cancelled
ci / check (pull_request) Has been cancelled
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
//! address, and nothing here writes a request anywhere.
|
||||
|
||||
use crate::store::{Refused, Store};
|
||||
use crate::wake::Waker;
|
||||
use axum::body::Bytes;
|
||||
use axum::extract::{DefaultBodyLimit, Path, State};
|
||||
use axum::http::{header, HeaderMap, StatusCode};
|
||||
@@ -11,8 +12,8 @@ use axum::{Json, Router};
|
||||
use serde::de::DeserializeOwned;
|
||||
use std::sync::Arc;
|
||||
use tabellarius_protocol::{
|
||||
CreateInvite, CreateMailbox, FetchedBlobs, InviteId, MailboxId, RotatePostToken, Sha256Hash,
|
||||
Token, VersionInfo, MAX_BLOB_BYTES, VERSION,
|
||||
CreateInvite, CreateMailbox, FetchedBlobs, InviteId, MailboxId, RotatePostToken, SetPush,
|
||||
Sha256Hash, Token, VersionInfo, MAX_BLOB_BYTES, VERSION,
|
||||
};
|
||||
|
||||
/// Unix seconds. The server passes the system clock; tests pass their own.
|
||||
@@ -22,6 +23,23 @@ pub type Clock = Arc<dyn Fn() -> i64 + Send + Sync>;
|
||||
pub struct AppState {
|
||||
pub store: Arc<Store>,
|
||||
pub clock: Clock,
|
||||
/// Wakes a mailbox's owner when a blob arrives (`PROTOCOL.md` §6).
|
||||
pub waker: Arc<Waker>,
|
||||
}
|
||||
|
||||
impl AppState {
|
||||
/// A relay that wakes only global `https://` endpoints: what a relay facing the internet runs.
|
||||
pub fn new(store: Arc<Store>, clock: Clock) -> Self {
|
||||
Self::with_waker(store, clock, Waker::new(false))
|
||||
}
|
||||
|
||||
pub fn with_waker(store: Arc<Store>, clock: Clock, waker: Waker) -> Self {
|
||||
Self {
|
||||
store,
|
||||
clock,
|
||||
waker: Arc::new(waker),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// JSON bodies are small: the largest is an invite with a 16 KiB KeyPackage in base64url.
|
||||
@@ -35,6 +53,7 @@ pub fn router(state: AppState) -> Router {
|
||||
.route("/v0/mailboxes", post(create_mailbox))
|
||||
.route("/v0/mailboxes/{id}/blobs/{hash}", delete(acknowledge))
|
||||
.route("/v0/mailboxes/{id}/post-token", put(rotate_post_token))
|
||||
.route("/v0/mailboxes/{id}/push", put(set_push).delete(clear_push))
|
||||
.route("/v0/invites", post(create_invite))
|
||||
.route("/v0/invites/{id}", get(claim_invite))
|
||||
.route("/version", get(version))
|
||||
@@ -98,7 +117,12 @@ async fn post_blob(
|
||||
) -> Result<StatusCode, Status> {
|
||||
let id = mailbox(&id)?;
|
||||
let token = bearer(&headers)?;
|
||||
s.store.post(&id, &token, &body, (s.clock)())?;
|
||||
let now = (s.clock)();
|
||||
s.store.post(&id, &token, &body, now)?;
|
||||
// Only after the blob is stored under a valid post token; the 201 does not wait for the wake.
|
||||
if let Ok(Some(endpoint)) = s.store.push_endpoint(&id) {
|
||||
s.waker.wake(Arc::clone(&s.store), id, endpoint, now);
|
||||
}
|
||||
Ok(StatusCode::CREATED)
|
||||
}
|
||||
|
||||
@@ -139,6 +163,33 @@ async fn rotate_post_token(
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
||||
async fn set_push(
|
||||
State(s): State<AppState>,
|
||||
Path(id): Path<String>,
|
||||
headers: HeaderMap,
|
||||
body: Bytes,
|
||||
) -> Result<StatusCode, Status> {
|
||||
let id = mailbox(&id)?;
|
||||
let token = bearer(&headers)?;
|
||||
let req: SetPush = json(&body)?;
|
||||
if !s.waker.accepts(&req.endpoint) {
|
||||
return Err(Status(StatusCode::BAD_REQUEST));
|
||||
}
|
||||
s.store.set_push(&id, &token, Some(&req.endpoint))?;
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
||||
async fn clear_push(
|
||||
State(s): State<AppState>,
|
||||
Path(id): Path<String>,
|
||||
headers: HeaderMap,
|
||||
) -> Result<StatusCode, Status> {
|
||||
let id = mailbox(&id)?;
|
||||
let token = bearer(&headers)?;
|
||||
s.store.set_push(&id, &token, None)?;
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
||||
async fn create_invite(State(s): State<AppState>, body: Bytes) -> Result<StatusCode, Status> {
|
||||
let req: CreateInvite = json(&body)?;
|
||||
s.store.create_invite(&req, (s.clock)())?;
|
||||
|
||||
Reference in New Issue
Block a user