Something went wrong. Try again.
contributor fork of @ptr.pet hydrant forked from ptr.pet/hydrant
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162use jacquard_api::com_atproto::sync::request_crawl::{ RequestCrawlError, RequestCrawlRequest, RequestCrawlResponse,};use url::Url;
use super::*;
pub async fn handle( State(hydrant): State<Hydrant>, ExtractXrpc(req): ExtractXrpc<RequestCrawlRequest>,) -> XrpcResult<StatusCode, RequestCrawlError<'static>> { let nsid = RequestCrawlResponse::NSID;
let url_str = format!("wss://{}/", req.hostname); let url = Url::parse(&url_str).map_err(|e| bad_request(nsid, e))?;
if hydrant.pds.is_banned(&req.hostname) { return Err(XrpcErrorResponse { status: StatusCode::BAD_REQUEST, error: jacquard_common::xrpc::XrpcError::Xrpc(RequestCrawlError::HostBanned(Some( "host is banned".into(), ))), }); }
// if this host is already running, everything is already ok if hydrant.firehose.is_source_running(&url) { return Ok(StatusCode::OK); }
// enforce daily new pds limit on unknown hosts if !hydrant.firehose.is_source_known(&url) { let (allowed, to_persist) = hydrant.state.pds_daily_limit.try_increment(); if !allowed { return Err(rate_limited( nsid, "POST", "daily limit for new PDS sources reached", )); }
// persist the new count before returning so a crash cannot reset the counter // and allow the budget to be replayed. if let Some((day, count)) = to_persist { hydrant .state .db .run(move |db| crate::db::save_pds_daily_adds(db, day, count)) .await .map_err(|e| internal_error(nsid, e))?; } }
hydrant .firehose .add_source(url, true) .await .map_err(|e| internal_error(nsid, e))?;
Ok(StatusCode::OK)}