3.3 KiB
3.3 KiB
pub struct Infra
Infra is a background worker that listens for activity and synchronizes relay configuration to a remote zooid instance.
Members:
env: Env- configuration; supplieszooid_api_url,relay_domain, theBLOSSOM_S3_*andLIVEKIT_*settings, and the robot key used to sign requestsquery: Querycommand: Command
pub fn new(query: Query, command: Command, env: &Env) -> Self
- Stores
query,command, and a clone ofenv
pub async fn start(self)
- Subscribes to
command.notify - Runs
reconcile_relay_state("startup")before entering the loop - Loops on
rx.recv(), callinghandle_activityfor eachActivity - On
Lagged, runsreconcile_relay_state("lagged"); onClosed, exits
async fn handle_activity(&self, activity: &Activity)
- Ignores anything that isn't a
relayresource with activity typecreate_relay,update_relay,activate_relay,deactivate_relay, orfail_relay_sync - For
fail_relay_sync, schedules a delayed retry viaschedule_relay_sync_retry - Otherwise resolves the relay (skip if gone) and calls
sync_relay
async fn reconcile_relay_state(&self, source: &str)
- Lists relays still pending sync (
query.list_relays_pending_sync) - For each:
sync_relayimmediately if itssync_erroris empty, otherwiseschedule_relay_sync_retry
async fn schedule_relay_sync_retry(&self, relay_id: &str, source: &str)
- Counts the relay's consecutive trailing
fail_relay_syncactivities to derive the attempt number - Computes an exponential backoff (base 30s, doubling, capped at 15 minutes); gives up after
RELAY_SYNC_RETRY_MAX_ATTEMPTS(6) to avoid infinite retry loops - Spawns a task that sleeps for the delay, then re-reads the relay and
sync_relays it (no-op if the relay is gone)
async fn sync_relay(&self, relay: &Relay)
- Calls
try_sync_relay; on successcommand.complete_relay_sync, on failurecommand.fail_relay_syncwith the error
async fn try_sync_relay(&self, relay: &Relay)
- A relay is "new" only if it has never completed a sync (
synced != 1and nocomplete_relay_syncactivity exists). New relays are created withPOST /relay/:id; existing relays are updated withPATCH /relay/:id. - A freshly generated
secretis included only for creation (POST), so updates don't rotate relay identity and we never store the secret. - The body carries relay configuration:
host(=subdomain.relay_domain),schema,inactive(true when status isinactiveordelinquent),info(name/icon/description/pubkey),policy,groups,management,blossom,livekit,push, and hard-codedroles. - When
blossom_enabled, the blossom section usesadapter: "s3"with theBLOSSOM_S3_*settings ands3.key_prefixset to the relay'sschema; otherwise it sends{ "enabled": false }. - When
livekit_enabled, the livekit section carries theLIVEKIT_*settings; otherwise{ "enabled": false }.
pub async fn list_relay_members(&self, relay_id: &str) -> Result<Vec<String>>
GET /relay/:id/membersfrom zooid; returns themembersarray
async fn request(&self, method, path, body)
- Sends an authenticated request to the zooid API at
path(relative toenv.zooid_api_url), with a 5s timeout - Authenticates each request with a NIP-98 header via
env.make_auth - Returns the response on 2xx; bails with the status and body text otherwise