Something went wrong. Try again.
contributor fork of @ptr.pet hydrant forked from ptr.pet/hydrant
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076use crate::pds_meta::{TierPolicy, TierRule};use miette::Result;use serde::{Deserialize, Serialize};use smol_str::{SmolStr, ToSmolStr};use std::collections::HashMap;use std::fmt;use std::path::PathBuf;use std::str::FromStr;use std::time::Duration;use url::Url;
/// rate limit parameters for a named tier of PDS connections.////// the per-second limit is `max(per_second_base, accounts * per_second_account_mul)`,/// giving a floor at `per_second_base` that scales up with the PDS's active account count.#[derive(Debug, Clone, Copy)]pub struct RateTier { /// floor for the per-second limit, regardless of account count. pub per_second_base: u64, /// per-second events allowed per active account on this PDS. pub per_second_account_mul: f64, /// per-hour limit. pub per_hour: u64, /// per-day limit. pub per_day: u64, /// maximum active account limit for this host before dropping tracking of new accounts pub account_limit: Option<u64>,}
impl RateTier { /// built-in "trusted" tier: high limits for well-behaved PDS operators. pub fn trusted() -> Self { Self { per_second_base: 5000, per_second_account_mul: 10.0, per_hour: 5000 * 3600, per_day: 5000 * 86400, account_limit: Some(10_000_000), } }
/// built-in "default" tier: conservative limits for unknown PDS operators. pub fn default_tier() -> Self { Self { per_second_base: 50, per_second_account_mul: 0.5, per_hour: 1000 * 3600, per_day: 1000 * 86400, account_limit: Some(100), } }
/// parse `base/mul/hourly/daily[/account_limit]` format used by `HYDRANT_RATE_TIERS`. fn parse(s: &str) -> Option<Self> { let parts: Vec<&str> = s.split('/').collect(); if parts.len() < 4 || parts.len() > 5 { return None; } Some(Self { per_second_base: parts[0].parse().ok()?, per_second_account_mul: parts[1].parse().ok()?, per_hour: parts[2].parse().ok()?, per_day: parts[3].parse().ok()?, account_limit: parts.get(4).and_then(|p| p.parse().ok()), }) }}
/// this is for internal use only, please don't use this macro.#[doc(hidden)]#[macro_export]macro_rules! __cfg { (@val $key:expr) => { std::env::var(concat!("HYDRANT_", $key)) }; ($key:expr, $default:expr, sec) => { cfg!(@val $key) .ok() .and_then(|s| humantime::parse_duration(&s).ok()) .unwrap_or($default) }; ($key:expr, $default:expr) => { cfg!(@val $key) .ok() .and_then(|s| s.parse().ok()) .unwrap_or($default.to_owned()) .into() };}use crate::__cfg as cfg;
/// loads `.env` from the current directory, setting any variables not already in the environment.fn load_dotenv() { let Ok(contents) = std::fs::read_to_string(".env") else { return; }; for line in contents.lines() { let line = line.trim(); if line.is_empty() || line.starts_with('#') { continue; } let Some((key, val)) = line.split_once('=') else { continue; }; let key = key.trim(); let val = val.trim(); let val = val .strip_prefix('"') .and_then(|v| v.strip_suffix('"')) .or_else(|| val.strip_prefix('\'').and_then(|v| v.strip_suffix('\''))) .unwrap_or(val); if std::env::var(key).is_err() { // SAFETY: single-threaded at startup; no other threads are reading env yet. unsafe { std::env::set_var(key, val) }; } }}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]pub enum CrawlerMode { /// enumerate via `com.atproto.sync.listRepos`, check signals with `describeRepo`. ListRepos, /// enumerate via `com.atproto.sync.listReposByCollection` for each configured signal. /// note: if no signals are specified, this won't crawl for any repos. ByCollection,}
impl CrawlerMode { fn default_for(full_network: bool) -> Self { full_network .then_some(Self::ListRepos) .unwrap_or(Self::ByCollection) }}
impl Serialize for CrawlerMode { fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: serde::Serializer, { serializer.serialize_str(&self.to_smolstr()) }}
impl<'de> Deserialize<'de> for CrawlerMode { fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: serde::Deserializer<'de>, { let s = String::deserialize(deserializer)?; FromStr::from_str(&s).map_err(serde::de::Error::custom) }}
impl FromStr for CrawlerMode { type Err = miette::Error; fn from_str(s: &str) -> Result<Self> { match s { "list_repos" | "list-repos" => Ok(Self::ListRepos), "by_collection" | "by-collection" => Ok(Self::ByCollection), _ => Err(miette::miette!( "invalid crawler mode: expected 'list_repos' or 'by_collection'" )), } }}
impl fmt::Display for CrawlerMode { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Self::ListRepos => write!(f, "list_repos"), Self::ByCollection => write!(f, "by_collection"), } }}
/// a single crawler source: a URL and the mode used to enumerate it.#[derive(Debug, Clone)]pub struct CrawlerSource { pub url: Url, pub mode: CrawlerMode,}
/// a single firehose source: a URL and whether it is a direct PDS connection.////// set via `HYDRANT_RELAY_HOSTS` as a comma-separated list of `[pds::]url` entries./// e.g. `wss://bsky.network,pds::wss://pds.example.com`./// a bare URL (no `pds::` prefix) is treated as an aggregating relay (`is_pds = false`).#[derive(Debug, Clone)]pub struct FirehoseSource { pub url: Url, /// true when this is a direct PDS connection; enables host authority enforcement. pub is_pds: bool,}
impl FirehoseSource { /// parse `[pds::]url`. the `pds::` prefix marks the source as a direct PDS connection. pub fn parse(s: &str) -> Option<Self> { if let Some(url_str) = s.strip_prefix("pds::") { let url = Url::parse(url_str).ok()?; Some(Self { url, is_pds: true }) } else { let url = Url::parse(s).ok()?; Some(Self { url, is_pds: false }) } }}
impl CrawlerSource { /// parse `[mode::]url`. mode prefix is optional, falls back to `default_mode`. fn parse(s: &str, default_mode: CrawlerMode) -> Option<Self> { if let Some((prefix, rest)) = s.split_once("::") { let mode = prefix.parse().ok()?; let url = Url::parse(rest).ok()?; Some(Self { url, mode }) } else { let url = Url::parse(s).ok()?; Some(Self { url, mode: default_mode, }) } }}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]pub enum Compression { Lz4, Zstd, None,}
impl FromStr for Compression { type Err = miette::Error; fn from_str(s: &str) -> Result<Self> { match s { "lz4" => Ok(Self::Lz4), "zstd" => Ok(Self::Zstd), "none" => Ok(Self::None), _ => Err(miette::miette!("invalid compression type")), } }}
impl fmt::Display for Compression { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Self::Lz4 => write!(f, "lz4"), Self::Zstd => write!(f, "zstd"), Self::None => write!(f, "none"), } }}
#[derive(Debug, Clone, Copy)]pub enum SignatureVerification { /// verify all commits, from the firehose and when backfilling a repo from a PDS. Full, /// only verify commits when backfilling a repo from a PDS. BackfillOnly, /// don't verify anything. None,}
impl FromStr for SignatureVerification { type Err = miette::Error; fn from_str(s: &str) -> Result<Self> { match s { "full" => Ok(Self::Full), "backfill-only" => Ok(Self::BackfillOnly), "none" => Ok(Self::None), _ => Err(miette::miette!("invalid signature verification level")), } }}
impl fmt::Display for SignatureVerification { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Self::Full => write!(f, "full"), Self::BackfillOnly => write!(f, "backfill-only"), Self::None => write!(f, "none"), } }}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]pub enum BackfillStrategy { /// always fetch full repo cars with `com.atproto.sync.getRepo`. Full, /// use sparse collection backfill when possible, falling back to full repo cars. SparseFilter, /// choose sparse collection backfill for filtered repos unless the seed proof suggests /// the repo is small enough for full `getRepo` to be cheaper. Auto,}
impl FromStr for BackfillStrategy { type Err = miette::Error;
fn from_str(s: &str) -> Result<Self> { match s { "full" => Ok(Self::Full), "sparse-filter" => Ok(Self::SparseFilter), "auto" => Ok(Self::Auto), _ => Err(miette::miette!("invalid backfill strategy")), } }}
impl fmt::Display for BackfillStrategy { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Self::Full => write!(f, "full"), Self::SparseFilter => write!(f, "sparse-filter"), Self::Auto => write!(f, "auto"), } }}
#[derive(Debug, Clone)]pub struct Config { /// path to the database folder. set via `HYDRANT_DATABASE_PATH`. pub database_path: PathBuf, /// if `true`, discovers and indexes all repositories in the network. /// set via `HYDRANT_FULL_NETWORK`. pub full_network: bool, /// if `true`, no records are stored; events are deleted after `ephemeral_ttl`. /// set via `HYDRANT_EPHEMERAL`. pub ephemeral: bool, /// how long events are retained in ephemeral mode before deletion. /// set via `HYDRANT_EPHEMERAL_TTL` (humantime duration, e.g. `60min`). pub ephemeral_ttl: Duration,
/// firehose sources for ingestion. set via `HYDRANT_RELAY_HOST` (single) /// or `HYDRANT_RELAY_HOSTS` (comma-separated; takes precedence). /// prefix a URL with `pds::` to mark it as a direct PDS connection. pub relays: Vec<FirehoseSource>, /// base URL(s) of the PLC directory (comma-separated for multiple). /// defaults to `https://plc.wtf`, or `https://plc.directory` in full-network mode. /// set via `HYDRANT_PLC_URL`. pub plc_urls: Vec<Url>, /// whether to ingest events from relay firehose subscriptions. /// set via `HYDRANT_ENABLE_FIREHOSE`. pub enable_firehose: bool, /// number of concurrent workers processing firehose events. /// set via `HYDRANT_FIREHOSE_WORKERS`. pub firehose_workers: usize, /// number of consecutive firehose connection failures before a PDS is marked offline. /// set via `HYDRANT_FIREHOSE_MAX_FAILURES`. pub firehose_max_failures: usize, /// how often the firehose cursor is persisted to disk. /// set via `HYDRANT_CURSOR_SAVE_INTERVAL` (humantime duration, e.g. `3sec`). pub cursor_save_interval: Duration, /// timeout for fetching a full repository CAR during backfill. /// set via `HYDRANT_REPO_FETCH_TIMEOUT` (humantime duration, e.g. `5min`). pub repo_fetch_timeout: Duration, /// maximum number of concurrent backfill tasks. /// set via `HYDRANT_BACKFILL_CONCURRENCY_LIMIT`. pub backfill_concurrency_limit: usize, /// backfill strategy. `full` preserves existing full-repo backfill behavior. /// `sparse-filter` attempts authenticated sparse collection backfill first and falls back /// to full repo backfill. `auto` probes filtered repos and falls back to full repo backfill /// for tiny MST roots. /// set via `HYDRANT_BACKFILL_STRATEGY`. pub backfill_strategy: BackfillStrategy,
/// whether to run the network crawler. `None` defers to the default for the current mode. /// set via `HYDRANT_ENABLE_CRAWLER`. pub enable_crawler: Option<bool>, /// maximum number of repos allowed in the backfill pending queue before the crawler pauses. /// set via `HYDRANT_CRAWLER_MAX_PENDING_REPOS`. pub crawler_max_pending_repos: usize, /// pending queue size at which the crawler resumes after being paused. /// set via `HYDRANT_CRAWLER_RESUME_PENDING_REPOS`. pub crawler_resume_pending_repos: usize, /// crawler sources: each entry pairs a URL with a discovery mode. /// /// set via `HYDRANT_CRAWLER_URLS` as a comma-separated list of `[mode::]url` entries, /// e.g. `relay::wss://bsky.network,by_collection::https://lightrail.microcosm.blue`. /// a bare URL without a `mode::` prefix uses the default mode (`relay` for full-network, /// `by_collection` otherwise). defaults to the relay hosts with the default mode. /// set to an empty string to disable crawling entirely. pub crawler_sources: Vec<CrawlerSource>,
/// signature verification level for incoming commits. /// set via `HYDRANT_VERIFY_SIGNATURES` (`full`, `backfill-only`, or `none`). pub verify_signatures: SignatureVerification, /// number of resolved identities to keep in the in-memory LRU cache. /// set via `HYDRANT_IDENTITY_CACHE_SIZE`. pub identity_cache_size: u64, /// enable MST inversion validation on incoming commits (expensive). /// set via `HYDRANT_VERIFY_MST`. pub verify_mst: bool, /// clock drift window for future-rev rejection, in seconds. /// commits with a rev timestamp more than this many seconds in the future are rejected. /// set via `HYDRANT_REV_CLOCK_SKEW`. default: 300 (5 minutes). pub rev_clock_skew_secs: i64,
/// NSID patterns that trigger auto-discovery in filter mode (e.g. `app.bsky.feed.post`). /// set via `HYDRANT_FILTER_SIGNALS` as a comma-separated list. pub filter_signals: Option<Vec<String>>, /// NSID patterns used to filter which record collections are stored. /// if `None`, all collections are stored. set via `HYDRANT_FILTER_COLLECTIONS`. pub filter_collections: Option<Vec<String>>, /// DIDs that are always skipped, regardless of mode. /// set via `HYDRANT_FILTER_EXCLUDES` as a comma-separated list. pub filter_excludes: Option<Vec<String>>,
/// enable backlinks indexing (only meaningful in non-ephemeral mode). /// set via `HYDRANT_ENABLE_BACKLINKS=true`. pub enable_backlinks: bool,
/// if `true`, record blocks are not stored; only the index (records, counts, events) is kept. /// `getRecord`, `listRecords`, and `getRepo` will return errors when this is enabled. /// event stream still functions but create/update events will not include record values. /// only valid in indexer mode (not relay). /// set via `HYDRANT_ONLY_INDEX_LINKS=true`. pub only_index_links: bool,
/// maximum number of new PDS sources that may be added (via seeding or API) in a single /// UTC calendar day. `None` means unlimited. /// set via `HYDRANT_NEW_HOST_LIMIT`. pub new_host_limit: Option<u64>,
/// how often offline firehose sources are automatically retried. /// set via `HYDRANT_OFFLINE_HOST_RETRY_INTERVAL` (humantime duration, e.g. `30min`). /// set to `none` to disable automatic retries. pub offline_host_retry_interval: Option<Duration>,
/// base URL(s) of relay or aggregator services to seed firehose PDS sources from at startup. /// /// hydrant calls `com.atproto.sync.listHosts` on each URL and adds the returned PDSes /// as firehose sources (with `is_pds = true`). account counts from the response are /// applied to newly-seen hosts to initialise rate-limiting immediately. /// /// set via `HYDRANT_SEED_HOSTS` as a comma-separated list of base URLs. pub seed_hosts: Vec<Url>, /// named rate tier definitions for PDS rate limiting. /// /// built-in tiers ("default" and "trusted") are always present and may be overridden. /// set via `HYDRANT_RATE_TIERS` as a comma-separated list of `name:base/mul/hourly/daily` entries, /// e.g. `trusted:5000/10.0/18000000/432000000,custom:100/1.0/7200000/172800000`. /// /// built from `HYDRANT_TIER_RULES` and `HYDRANT_RATE_TIERS` at startup. pub tier_policy: TierPolicy,
/// glob rules mapping host patterns to named rate tiers. /// /// set via `HYDRANT_TIER_RULES` as a comma-separated list of `pattern:tiername` entries, /// e.g. `*.bsky.network:trusted,pds.example.com:custom`. rules are evaluated in order; /// api-assigned per-host overrides always take priority over these rules. pub tier_rules: Vec<(String, String)>,
/// db internals, tune only if you know what you're doing. /// /// size of the fjall block cache in MB. set via `HYDRANT_CACHE_SIZE`. pub cache_size: u64, /// db internals, tune only if you know what you're doing. /// /// compression algorithm for data keyspaces (blocks, records, repos, events). /// set via `HYDRANT_DATA_COMPRESSION` (`lz4`, `zstd`, or `none`). pub data_compression: Compression, /// db internals, tune only if you know what you're doing. /// /// compression algorithm for the fjall journal. /// set via `HYDRANT_JOURNAL_COMPRESSION` (`lz4`, `zstd`, or `none`). pub journal_compression: Compression, /// db internals, tune only if you know what you're doing. /// /// number of background threads used by the fjall storage engine. /// set via `HYDRANT_DB_WORKER_THREADS`. pub db_worker_threads: usize, /// db internals, tune only if you know what you're doing. /// /// maximum total size of the fjall journal in MB before a flush is forced. /// set via `HYDRANT_DB_MAX_JOURNALING_SIZE_MB`. pub db_max_journaling_size_mb: u64, /// db internals, tune only if you know what you're doing. /// /// in-memory write buffer (memtable) size for the blocks keyspace in MB. /// set via `HYDRANT_DB_BLOCKS_MEMTABLE_SIZE_MB`. pub db_blocks_memtable_size_mb: u64, /// db internals, tune only if you know what you're doing. /// /// in-memory write buffer (memtable) size for the repos keyspace in MB. /// set via `HYDRANT_DB_REPOS_MEMTABLE_SIZE_MB`. pub db_repos_memtable_size_mb: u64, /// db internals, tune only if you know what you're doing. /// /// in-memory write buffer (memtable) size for the events keyspace in MB. /// set via `HYDRANT_DB_EVENTS_MEMTABLE_SIZE_MB`. pub db_events_memtable_size_mb: u64, /// db internals, tune only if you know what you're doing. /// /// in-memory write buffer (memtable) size for the records keyspace in MB. /// set via `HYDRANT_DB_RECORDS_MEMTABLE_SIZE_MB`. pub db_records_memtable_size_mb: u64,
/// replay batch size. /// /// `0` (the default) means auto: use about half the subscriber channel capacity /// (`STREAM_CHANNEL_CAPACITY / 2`), capped to the channel's current available /// capacity. this prevents DB reads when the output buffer is already saturated. /// /// set to a non-zero value to fix the batch size manually. /// /// set via `HYDRANT_STREAM_REPLAY_CHUNK_SIZE`. pub stream_replay_chunk_size: usize, /// optional pause between replay batches. /// /// normally zero. slow consumers are handled by bounded-channel backpressure /// and `HYDRANT_STREAM_SEND_TIMEOUT`. only set this as an emergency knob if /// you need to artificially throttle replay throughput. /// /// set via `HYDRANT_STREAM_REPLAY_CHUNK_PAUSE` (humantime duration, e.g. `2ms`). pub stream_replay_chunk_pause: Duration, /// maximum number of live in-memory stream events buffered per subscriber while it catches up. /// set via `HYDRANT_STREAM_PENDING_EVENT_LIMIT`. pub stream_pending_event_limit: usize, /// maximum time a subscriber may block stream delivery before being disconnected. /// set via `HYDRANT_STREAM_SEND_TIMEOUT` (humantime duration, e.g. `30sec`). pub stream_send_timeout: Duration,}
impl Default for Config { fn default() -> Self { const BASE_MEMTABLE_MB: u64 = 32; Self { database_path: PathBuf::from("./hydrant.db"), #[cfg(feature = "indexer")] ephemeral: false, #[cfg(feature = "relay")] ephemeral: true, #[cfg(feature = "indexer")] ephemeral_ttl: Duration::from_secs(3600), // 1 hour #[cfg(feature = "relay")] ephemeral_ttl: Duration::from_secs(3600 * 24 * 3), // 3 days #[cfg(not(feature = "relay"))] full_network: false, #[cfg(feature = "relay")] full_network: true, #[cfg(not(feature = "relay"))] relays: vec![FirehoseSource { url: Url::parse("wss://relay.fire.hose.cam/").unwrap(), is_pds: false, }], #[cfg(feature = "relay")] relays: vec![], #[cfg(not(feature = "relay"))] seed_hosts: vec![], #[cfg(feature = "relay")] seed_hosts: vec![Url::parse("https://bsky.network").unwrap()], plc_urls: vec![Url::parse("https://plc.wtf").unwrap()], enable_firehose: true, firehose_workers: 8, firehose_max_failures: 15, cursor_save_interval: Duration::from_secs(3), repo_fetch_timeout: Duration::from_secs(300), backfill_concurrency_limit: 16, backfill_strategy: BackfillStrategy::Auto, enable_crawler: None, crawler_max_pending_repos: 2000, crawler_resume_pending_repos: 1000, crawler_sources: vec![CrawlerSource { url: Url::parse("https://lightrail.microcosm.blue").unwrap(), mode: CrawlerMode::ByCollection, }], verify_signatures: SignatureVerification::Full, identity_cache_size: 1_000_000, verify_mst: false, rev_clock_skew_secs: 300, filter_signals: None, filter_collections: None, filter_excludes: None, enable_backlinks: false, only_index_links: false, new_host_limit: Some(50), offline_host_retry_interval: Some(Duration::from_secs(30 * 60)), tier_rules: vec![], tier_policy: { let mut tiers = HashMap::new(); tiers.insert(SmolStr::new("default"), RateTier::default_tier()); tiers.insert(SmolStr::new("trusted"), RateTier::trusted()); TierPolicy { tiers, rules: vec![], } }, cache_size: 256, data_compression: Compression::Zstd, journal_compression: Compression::Lz4, db_worker_threads: 4, db_max_journaling_size_mb: 400, db_blocks_memtable_size_mb: BASE_MEMTABLE_MB, db_repos_memtable_size_mb: BASE_MEMTABLE_MB / 2, db_events_memtable_size_mb: BASE_MEMTABLE_MB, db_records_memtable_size_mb: BASE_MEMTABLE_MB / 3 * 2, stream_replay_chunk_size: 0, stream_replay_chunk_pause: Duration::ZERO, stream_pending_event_limit: 4096, stream_send_timeout: Duration::from_secs(30), } }}
impl Config { /// returns the default config for full network usage. pub fn full_network() -> Self { const BASE_MEMTABLE_MB: u64 = 192; Self { full_network: true, plc_urls: vec![Url::parse("https://plc.directory").unwrap()], firehose_workers: 24, backfill_concurrency_limit: 64, crawler_sources: vec![CrawlerSource { url: Url::parse("wss://relay.fire.hose.cam/").unwrap(), mode: CrawlerMode::ListRepos, }], db_worker_threads: 8, db_max_journaling_size_mb: 1024, db_blocks_memtable_size_mb: BASE_MEMTABLE_MB, db_repos_memtable_size_mb: BASE_MEMTABLE_MB / 4, db_events_memtable_size_mb: BASE_MEMTABLE_MB, db_records_memtable_size_mb: BASE_MEMTABLE_MB / 3 * 2, ..Self::default() } }
/// reads and builds the config from environment variables, loading `.env` first if present. pub fn from_env() -> Result<Self> { load_dotenv();
// full_network is read first since it determines which defaults to use. // relay mode defaults to true so that the network is indexed by default. #[cfg(feature = "relay")] let default_full_network = true; #[cfg(not(feature = "relay"))] let default_full_network = false; let full_network: bool = cfg!("FULL_NETWORK", default_full_network); let defaults = full_network .then(Self::full_network) .unwrap_or_else(Self::default);
let relay_hosts = match std::env::var("HYDRANT_RELAY_HOSTS") { Ok(hosts) if !hosts.trim().is_empty() => hosts .split(',') .filter_map(|s| { let s = s.trim(); (!s.is_empty()) .then(|| { FirehoseSource::parse(s).or_else(|| { tracing::warn!("invalid relay host URL: {s}"); None }) }) .flatten() }) .collect(), // HYDRANT_RELAY_HOSTS explicitly set to "" Ok(_) => vec![], // not set at all, fall back to RELAY_HOST (bare URL, no pds:: prefix support here) Err(_) => match std::env::var("HYDRANT_RELAY_HOST") { Ok(s) if !s.trim().is_empty() => { FirehoseSource::parse(s.trim()).into_iter().collect() } _ => defaults.relays.clone(), }, };
let plc_urls: Vec<Url> = std::env::var("HYDRANT_PLC_URL") .ok() .map(|s| { s.split(',') .map(|s| Url::parse(s.trim())) .collect::<Result<Vec<_>, _>>() .map_err(|e| miette::miette!("invalid PLC URL: {e}")) }) .unwrap_or_else(|| Ok(defaults.plc_urls.clone()))?;
let cursor_save_interval = cfg!("CURSOR_SAVE_INTERVAL", defaults.cursor_save_interval, sec); let repo_fetch_timeout = cfg!("REPO_FETCH_TIMEOUT", defaults.repo_fetch_timeout, sec);
let ephemeral: bool = cfg!("EPHEMERAL", defaults.ephemeral); let ephemeral_ttl = cfg!("EPHEMERAL_TTL", defaults.ephemeral_ttl, sec); let database_path = cfg!("DATABASE_PATH", defaults.database_path); let cache_size = cfg!("CACHE_SIZE", defaults.cache_size); let data_compression = cfg!("DATA_COMPRESSION", defaults.data_compression); let journal_compression = cfg!("JOURNAL_COMPRESSION", defaults.journal_compression);
let verify_signatures = cfg!("VERIFY_SIGNATURES", defaults.verify_signatures); let identity_cache_size = cfg!("IDENTITY_CACHE_SIZE", defaults.identity_cache_size); let verify_mst: bool = cfg!("VERIFY_MST", defaults.verify_mst); let rev_clock_skew_secs: i64 = cfg!("REV_CLOCK_SKEW", defaults.rev_clock_skew_secs); let enable_firehose = cfg!("ENABLE_FIREHOSE", defaults.enable_firehose); let enable_crawler = std::env::var("HYDRANT_ENABLE_CRAWLER") .ok() .and_then(|s| s.parse().ok());
let backfill_concurrency_limit = cfg!( "BACKFILL_CONCURRENCY_LIMIT", defaults.backfill_concurrency_limit ); let backfill_strategy = cfg!("BACKFILL_STRATEGY", defaults.backfill_strategy); let firehose_workers = cfg!("FIREHOSE_WORKERS", defaults.firehose_workers); let firehose_max_failures = cfg!("FIREHOSE_MAX_FAILURES", defaults.firehose_max_failures);
let db_worker_threads = cfg!("DB_WORKER_THREADS", defaults.db_worker_threads); let db_max_journaling_size_mb = cfg!( "DB_MAX_JOURNALING_SIZE_MB", defaults.db_max_journaling_size_mb ); let db_blocks_memtable_size_mb = cfg!( "DB_BLOCKS_MEMTABLE_SIZE_MB", defaults.db_blocks_memtable_size_mb ); let db_events_memtable_size_mb = cfg!( "DB_EVENTS_MEMTABLE_SIZE_MB", defaults.db_events_memtable_size_mb ); let db_records_memtable_size_mb = cfg!( "DB_RECORDS_MEMTABLE_SIZE_MB", defaults.db_records_memtable_size_mb ); let db_repos_memtable_size_mb = cfg!( "DB_REPOS_MEMTABLE_SIZE_MB", defaults.db_repos_memtable_size_mb ); let stream_replay_chunk_size = cfg!( "STREAM_REPLAY_CHUNK_SIZE", defaults.stream_replay_chunk_size ); let stream_replay_chunk_pause = cfg!( "STREAM_REPLAY_CHUNK_PAUSE", defaults.stream_replay_chunk_pause, sec ); let stream_pending_event_limit = cfg!( "STREAM_PENDING_EVENT_LIMIT", defaults.stream_pending_event_limit ); let stream_send_timeout = cfg!("STREAM_SEND_TIMEOUT", defaults.stream_send_timeout, sec);
let crawler_max_pending_repos = cfg!( "CRAWLER_MAX_PENDING_REPOS", defaults.crawler_max_pending_repos ); let crawler_resume_pending_repos = cfg!( "CRAWLER_RESUME_PENDING_REPOS", defaults.crawler_resume_pending_repos );
let filter_signals = std::env::var("HYDRANT_FILTER_SIGNALS").ok().map(|s| { s.split(',') .map(|s| s.trim().to_string()) .filter(|s| !s.is_empty()) .collect() });
let filter_collections = std::env::var("HYDRANT_FILTER_COLLECTIONS").ok().map(|s| { s.split(',') .map(|s| s.trim().to_string()) .filter(|s| !s.is_empty()) .collect() });
let filter_excludes = std::env::var("HYDRANT_FILTER_EXCLUDES").ok().map(|s| { s.split(',') .map(|s| s.trim().to_string()) .filter(|s| !s.is_empty()) .collect() });
let enable_backlinks: bool = cfg!("ENABLE_BACKLINKS", defaults.enable_backlinks); let only_index_links: bool = cfg!("ONLY_INDEX_LINKS", defaults.only_index_links); let max_pds_added_per_day: Option<u64> = std::env::var("HYDRANT_NEW_HOST_LIMIT") .ok() .and_then(|s| s.parse().ok());
let offline_retry_interval: Option<Duration> = match std::env::var("HYDRANT_OFFLINE_HOST_RETRY_INTERVAL") .ok() .as_deref() { None => defaults.offline_host_retry_interval, Some("none") => None, Some(s) => humantime::parse_duration(s) .ok() .or(defaults.offline_host_retry_interval), };
// start with built-in tier definitions, then layer in any env-defined overrides. // format: HYDRANT_RATE_TIERS=name:base/mul/hourly/daily,... let mut tiers = defaults.tier_policy.tiers.clone(); if let Ok(s) = std::env::var("HYDRANT_RATE_TIERS") { for entry in s.split(',') { let entry = entry.trim(); if let Some((name, spec)) = entry.split_once(':') { match RateTier::parse(spec) { Some(tier) => { tiers.insert(SmolStr::new(name.trim()), tier); } None => tracing::warn!( "ignoring invalid rate tier '{name}': expected base/mul/hourly/daily format" ), } } } }
let seed_hosts: Vec<Url> = std::env::var("HYDRANT_SEED_HOSTS") .ok() .map(|s| { s.split(',') .filter_map(|u| { let u = u.trim(); if u.is_empty() { return None; } Url::parse(u).ok().or_else(|| { tracing::warn!("invalid seed host URL: {u}"); None }) }) .collect() }) .unwrap_or_else(|| defaults.seed_hosts.clone());
// build ordered glob rules from HYDRANT_TIER_RULES let mut rules: Vec<TierRule> = vec![]; let mut tier_rules: Vec<(String, String)> = vec![]; if let Ok(s) = std::env::var("HYDRANT_TIER_RULES") { for entry in s.split(',') { let entry = entry.trim(); if entry.is_empty() { continue; } if let Some((pattern_str, tier_name)) = entry.split_once(':') { let pattern_str = pattern_str.trim(); let tier_name = tier_name.trim(); match glob::Pattern::new(pattern_str) { Ok(pattern) => { rules.push(TierRule { pattern, tier_name: SmolStr::new(tier_name), }); tier_rules.push((pattern_str.to_string(), tier_name.to_string())); } Err(e) => tracing::warn!( "ignoring invalid tier rule pattern '{pattern_str}': {e}" ), } } } }
let tier_policy = TierPolicy { tiers, rules };
let default_mode = CrawlerMode::default_for(full_network); let crawler_sources = match std::env::var("HYDRANT_CRAWLER_URLS") { Ok(s) => s .split(',') .map(|s| s.trim()) .filter(|s| !s.is_empty()) .filter_map(|s| CrawlerSource::parse(s, default_mode)) .collect(), Err(_) => match default_mode { CrawlerMode::ListRepos => relay_hosts .iter() .map(|source| CrawlerSource { url: source.url.clone(), mode: CrawlerMode::ListRepos, }) .collect(), CrawlerMode::ByCollection => defaults.crawler_sources.clone(), }, };
Ok(Self { database_path, full_network, ephemeral, seed_hosts, ephemeral_ttl, relays: relay_hosts, plc_urls, enable_firehose, firehose_workers, firehose_max_failures, cursor_save_interval, repo_fetch_timeout, backfill_concurrency_limit, backfill_strategy, enable_crawler, crawler_max_pending_repos, crawler_resume_pending_repos, crawler_sources, verify_signatures, identity_cache_size, verify_mst, rev_clock_skew_secs, filter_signals, filter_collections, filter_excludes, enable_backlinks, only_index_links, new_host_limit: max_pds_added_per_day, offline_host_retry_interval: offline_retry_interval, tier_policy, tier_rules, cache_size, data_compression, journal_compression, db_worker_threads, db_max_journaling_size_mb, db_blocks_memtable_size_mb, db_repos_memtable_size_mb, db_events_memtable_size_mb, db_records_memtable_size_mb, stream_replay_chunk_size, stream_replay_chunk_pause, stream_pending_event_limit, stream_send_timeout, }) }}
macro_rules! config_line { ($f:expr, $label:expr, $value:expr) => { writeln!($f, " {:<width$}{}", $label, $value, width = LABEL_WIDTH) };}
impl fmt::Display for Config { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { const LABEL_WIDTH: usize = 27;
writeln!(f, "hydrant configuration:")?; config_line!( f, "relay hosts", format_args!( "{:?}", self.relays .iter() .map(|s| if s.is_pds { format!("pds::{}", s.url) } else { s.url.to_string() }) .collect::<Vec<_>>() ) )?; config_line!(f, "plc urls", format_args!("{:?}", self.plc_urls))?; config_line!(f, "full network indexing", self.full_network)?; config_line!(f, "verify signatures", self.verify_signatures)?; config_line!(f, "backfill concurrency", self.backfill_concurrency_limit)?; config_line!(f, "backfill strategy", self.backfill_strategy)?; config_line!(f, "identity cache size", self.identity_cache_size)?; config_line!( f, "cursor save interval", format_args!("{}sec", self.cursor_save_interval.as_secs()) )?; config_line!( f, "repo fetch timeout", format_args!("{}sec", self.repo_fetch_timeout.as_secs()) )?; config_line!(f, "ephemeral", self.ephemeral)?; config_line!(f, "database path", self.database_path.to_string_lossy())?; config_line!(f, "cache size", format_args!("{} mb", self.cache_size))?; config_line!(f, "data compression", self.data_compression)?; config_line!(f, "journal compression", self.journal_compression)?; config_line!(f, "firehose workers", self.firehose_workers)?; config_line!(f, "firehose max failures", self.firehose_max_failures)?; config_line!(f, "db worker threads", self.db_worker_threads)?; config_line!( f, "db journal size", format_args!("{} mb", self.db_max_journaling_size_mb) )?; config_line!( f, "db blocks memtable", format_args!("{} mb", self.db_blocks_memtable_size_mb) )?; config_line!( f, "db repos memtable", format_args!("{} mb", self.db_repos_memtable_size_mb) )?; config_line!( f, "db events memtable", format_args!("{} mb", self.db_events_memtable_size_mb) )?; config_line!( f, "db records memtable", format_args!("{} mb", self.db_records_memtable_size_mb) )?; let replay_chunk = if self.stream_replay_chunk_size == 0 { "auto".to_owned() } else { self.stream_replay_chunk_size.to_string() }; config_line!(f, "stream replay chunk", replay_chunk)?; config_line!( f, "stream replay pause", format_args!("{}ms", self.stream_replay_chunk_pause.as_millis()) )?; config_line!(f, "stream pending limit", self.stream_pending_event_limit)?; config_line!( f, "stream send timeout", format_args!("{}sec", self.stream_send_timeout.as_secs()) )?; config_line!(f, "crawler max pending", self.crawler_max_pending_repos)?; config_line!( f, "crawler resume pending", self.crawler_resume_pending_repos )?; if !self.crawler_sources.is_empty() { let sources: Vec<_> = self .crawler_sources .iter() .map(|s| format!("{}::{}", s.mode, s.url)) .collect(); config_line!(f, "crawler sources", sources.join(", "))?; } if let Some(signals) = &self.filter_signals { config_line!(f, "filter signals", format_args!("{:?}", signals))?; } if let Some(collections) = &self.filter_collections { config_line!(f, "filter collections", format_args!("{:?}", collections))?; } if let Some(excludes) = &self.filter_excludes { config_line!(f, "filter excludes", format_args!("{:?}", excludes))?; } if self.enable_backlinks { config_line!(f, "backlinks", "enabled")?; } if self.only_index_links { config_line!(f, "only index links", "true")?; } if !self.seed_hosts.is_empty() { config_line!( f, "seed hosts", format_args!( "{:?}", self.seed_hosts .iter() .map(|u| u.as_str()) .collect::<Vec<_>>() ) )?; } if let Some(limit) = self.new_host_limit { config_line!(f, "max pds/day", limit)?; } match self.offline_host_retry_interval { Some(d) => config_line!( f, "offline retry interval", format_args!("{}sec", d.as_secs()) )?, None => config_line!(f, "offline retry interval", "disabled")?, } Ok(()) }}