chadtmiller.com/group-host
atmosphere group host
0
0
A relay pages through the host's repos as atproto has it, and a refusal's line says why
crates/group-host/src/api.rs+9−2
| @@ -183,8 +183,15 @@ pub async fn get_latest_commit(State(host): State<Arc<Host>>, Query(p): Query<Pa | ||
Ok(Json(json!({"cid": head.cid.to_string(), "rev": head.rev}))) | ||
} | ||
| ||
/// The page size of the sync methods that list a host's repos or a repo's | ||
/// blobs, as atproto's lexicons have it: 1 to 1000, 500 unless asked; a | ||
/// relay crawling the host asks for 1000. | ||
fn sync_limit(params: &Params) -> Result<u32, XrpcError> { | ||
page(params, 500, 1000) | ||
} | ||
| ||
pub async fn list_repos(State(host): State<Arc<Host>>, Query(p): Query<Params>) -> Answer { | ||
let limit = limit(&p)?; | ||
let limit = sync_limit(&p)?; | ||
let repos = host | ||
.store | ||
.accounts(p.get("cursor").map(String::as_str), limit) | ||
| @@ -277,7 +284,7 @@ pub async fn get_blob( | ||
| ||
pub async fn list_blobs(State(host): State<Arc<Host>>, Query(p): Query<Params>) -> Answer { | ||
let did = did_of(&host, param(&p, "did")?)?; | ||
let limit = limit(&p)?; | ||
let limit = sync_limit(&p)?; | ||
let (since, cursor) = ( | ||
p.get("since").map(String::as_str), | ||
p.get("cursor").map(String::as_str), | ||
crates/group-host/src/observe.rs+25−3
| @@ -1,6 +1,6 @@ | ||
//! What the host logs and counts of each request: a line, `request`, | ||
//! with its method, route, path (never its query), status and duration, | ||
//! in a trace of its own or the one its `traceparent` names, which every | ||
//! and for an XRPC failure, its `error` and `error_message`, in a trace of its own or the one its `traceparent` names, which every | ||
//! line logged while answering it carries and the answer gives back as | ||
//! `x-trace-id`; and a count in [`crate::stats`]. A trace is the | ||
//! caller's to name, so one may name another's and have its lines read | ||
| @@ -80,12 +80,34 @@ pub async fn observe(State(host): State<Arc<Host>>, req: Request, next: Next) -> | ||
let id = HeaderValue::try_from(trace.as_str()).expect("hex is a header's value"); | ||
res.headers_mut().insert("x-trace-id", id); | ||
let duration_ms = u64::try_from(took.as_millis()).unwrap_or(u64::MAX); | ||
// Why it was refused or failed, as its answer said. | ||
let failure = res.extensions().get::<crate::xrpc::XrpcError>(); | ||
let error = failure.map(|f| f.error); | ||
let error_message = failure.map(|f| f.message.as_str()); | ||
span.in_scope(|| { | ||
if route == HEALTH { | ||
tracing::debug!(method, route, path, status, duration_ms, "request"); | ||
tracing::debug!( | ||
method, | ||
route, | ||
path, | ||
status, | ||
duration_ms, | ||
error, | ||
error_message, | ||
"request" | ||
); | ||
return; | ||
} | ||
tracing::info!(method, route, path, status, duration_ms, "request"); | ||
tracing::info!( | ||
method, | ||
route, | ||
path, | ||
status, | ||
duration_ms, | ||
error, | ||
error_message, | ||
"request" | ||
); | ||
let streams = STREAMS.contains(&route.as_str()); | ||
host.stats.served(&route, &method, status, took, streams); | ||
}); | ||
crates/group-host/src/xrpc.rs+5−1
| @@ -45,9 +45,13 @@ impl XrpcError { | ||
} | ||
| ||
impl IntoResponse for XrpcError { | ||
/// The failure as JSON, and kept with the answer, for the line logged | ||
/// of the request to say why. | ||
fn into_response(self) -> Response { | ||
let body = json!({ "error": self.error, "message": self.message }); | ||
(self.status, Json(body)).into_response() | ||
let mut res = (self.status, Json(body)).into_response(); | ||
res.extensions_mut().insert(self); | ||
res | ||
} | ||
} | ||
| ||
crates/group-host/tests/blobs.rs+15−0
| @@ -145,6 +145,21 @@ async fn lists_blobs_a_page_at_a_time_and_since_a_rev() { | ||
) | ||
.await; | ||
assert_eq!(page["cids"], json!([both[1]])); | ||
// A page is up to 1000, as atproto's lexicon has it. | ||
let (_, page) = h | ||
.get( | ||
"com.atproto.sync.listBlobs", | ||
&format!("did={did}&limit=1000"), | ||
) | ||
.await; | ||
assert_eq!(page["cids"].as_array().unwrap().len(), 2); | ||
let (_, refused) = h | ||
.get( | ||
"com.atproto.sync.listBlobs", | ||
&format!("did={did}&limit=1001"), | ||
) | ||
.await; | ||
assert_eq!(refused["message"], "limit is 1 to 1000"); | ||
let (_, since) = h | ||
.get( | ||
"com.atproto.sync.listBlobs", | ||
crates/group-host/tests/repos.rs+5−2
| @@ -362,8 +362,11 @@ async fn answers_what_is_not_here() { | ||
} | ||
let (_, body) = h.get("com.atproto.repo.getRecord", "").await; | ||
assert_eq!(body["message"], "repo is required"); | ||
let (_, body) = h.get("com.atproto.sync.listRepos", "limit=0").await; | ||
assert_eq!(body["message"], "limit is 1 to 100"); | ||
// Its pages are atproto's: up to 1000, as a relay asks. | ||
let (_, body) = h.get("com.atproto.sync.listRepos", "limit=1001").await; | ||
assert_eq!(body["message"], "limit is 1 to 1000"); | ||
let (status, _) = h.get("com.atproto.sync.listRepos", "limit=1000").await; | ||
assert_eq!(status, 200); | ||
let res = h | ||
.http | ||
.get(format!("{}/.well-known/atproto-did", h.url)) | ||
crates/group-host/tests/serve.rs+4−0
| @@ -106,6 +106,10 @@ async fn keeps_the_lines_it_logs_and_counts_its_requests() { | ||
assert_eq!(fields["route"], "/xrpc/{nsid}"); | ||
assert_eq!(fields["path"], "/xrpc/fyi.opensocial.noSuchQuery"); | ||
assert_eq!(fields["status"], 501); | ||
// A failure says why, as its answer did. | ||
assert_eq!(fields["error"], "MethodNotImplemented"); | ||
let said = "fyi.opensocial.noSuchQuery is not a method this host answers"; | ||
assert_eq!(fields["error_message"], said); | ||
// The health checks are logged at debug, below what it was told. | ||
assert!(read(Some(&health), None).is_empty()); | ||
for said in ["serving", "stopped"] { | ||
docs/reference/api.md+5−2
| @@ -214,7 +214,10 @@ by service auth or the console's cookie; anyone else is refused | ||
| `GET fyi.opensocial.getLogs` | the lines the host logged, oldest first, each as it wrote it: one request's by its `trace`, or the last of a `message`, a `level` and above, or those that took `minMs` or longer; 100 unless `limit` asks for up to 500, or for a trace, 500. The read is itself logged, `logs read`, with who asked and for what | | ||
| `GET fyi.opensocial.getStats` | how the host was used over the last `days` (7 unless asked, 400 at most), day by day in UTC, and what it holds: requests by route and by the class of their status, slow ones, MCP tools called by name, and groups and spaces made. Nothing in it names who asked | | ||
| ||
Every request is answered with `x-trace-id`, the trace each line logged | ||
Every request is logged as a `request` line, with its method, route, | ||
path (never its query), status and `duration_ms`, and, when it is | ||
refused or fails, its `error` and `error_message` as it answered them. | ||
It is answered with `x-trace-id`, the trace each line logged | ||
while answering it carries: one of its own, or the one a W3C | ||
`traceparent` header names. The caller names that, so a trace read may | ||
hold the lines of another request that named it too. A method other | ||
| @@ -266,7 +269,7 @@ repos as any PDS does: | ||
| --- | --- | | ||
| `GET com.atproto.server.describeServer` | the host as a PDS: its DID, the domain it gives its groups names under, `BLOB_UPLOAD_LIMIT`, and that no one signs up | | ||
| `GET com.atproto.repo.describeRepo`, `GET com.atproto.repo.getRecord`, `GET com.atproto.repo.listRecords` | a group's public repo | | ||
| `GET com.atproto.sync.getRepo`, `GET com.atproto.sync.getLatestCommit`, `GET com.atproto.sync.listRepos`, `GET com.atproto.sync.getBlob`, `GET com.atproto.sync.listBlobs` | syncing them; a blob only a space holds is not served or listed | | ||
| `GET com.atproto.sync.getRepo`, `GET com.atproto.sync.getLatestCommit`, `GET com.atproto.sync.listRepos`, `GET com.atproto.sync.getBlob`, `GET com.atproto.sync.listBlobs` | syncing them; a blob only a space holds is not served or listed. `listRepos` and `listBlobs` answer pages of up to 1000, 500 unless asked, as atproto's lexicons have them | | ||
| `GET com.atproto.sync.subscribeRepos` | the firehose, over a WebSocket | | ||
| `GET com.atproto.identity.resolveHandle` | a handle here, to its DID; for a caller signed in, any handle, through `HANDLE_RESOLVERS` | | ||
| `GET /.well-known/did.json` | the host's own DID document, when `SERVICE_DID` is a `did:web`: its `#atproto_pds` and `#opensocial` services at `PUBLIC_URL` | | ||