use std::collections::HashMap;
use std::path::Path;
use std::str::FromStr;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use deadpool_postgres::{Manager, ManagerConfig, Pool, RecyclingMethod, Runtime};
use interloper::Address;
use jiff::Timestamp;
use rusqlite::{Connection, OptionalExtension, params};
#[derive(Debug)]
pub struct StoreUnavailable(pub String);
impl std::fmt::Display for StoreUnavailable {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0)
}
}
pub(crate) fn unavailable(e: impl std::error::Error) -> StoreUnavailable {
let mut msg = e.to_string();
let mut src = e.source();
while let Some(s) = src {
msg = format!("{msg}: {s}");
src = s.source();
}
StoreUnavailable(msg)
}
const ACQUIRE_TIMEOUT: Duration = Duration::from_secs(5);
const PG_SCHEMA: &str = "
create table if not exists addresses (
value text primary key,
body text not null,
expires_at bigint
);
create index if not exists addresses_expiry on addresses (expires_at);
create table if not exists instance_health (
instance_id text primary key,
snapshot jsonb not null,
updated_at timestamptz not null default now()
);
create table if not exists provider_quarantine (
provider text primary key,
reason text not null,
instance_id text not null,
at timestamptz not null default now()
);
";
pub enum Store {
Memory(Mutex<HashMap<String, Address>>),
Sqlite(Mutex<Connection>),
Postgres(Pool),
}
fn tls_connector() -> tokio_postgres_rustls::MakeRustlsConnect {
let roots = rustls::RootCertStore {
roots: webpki_roots::TLS_SERVER_ROOTS.to_vec(),
};
let config = rustls::ClientConfig::builder_with_provider(Arc::new(
rustls::crypto::ring::default_provider(),
))
.with_safe_default_protocol_versions()
.expect("ring supports the default protocol versions")
.with_root_certificates(roots)
.with_no_client_auth();
tokio_postgres_rustls::MakeRustlsConnect::new(config)
}
impl Store {
pub fn memory() -> Self {
Store::Memory(Mutex::new(HashMap::new()))
}
pub fn sqlite(path: &Path) -> Result<Self, String> {
if let Some(dir) = path.parent() {
std::fs::create_dir_all(dir).map_err(|e| e.to_string())?;
}
let conn = Connection::open(path).map_err(|e| e.to_string())?;
conn.execute_batch(
"pragma journal_mode = wal;
create table if not exists addresses (
value text primary key,
body text not null,
expires_at integer
);
create index if not exists addresses_expiry on addresses(expires_at);",
)
.map_err(|e| e.to_string())?;
Ok(Store::Sqlite(Mutex::new(conn)))
}
pub async fn postgres(url: &str) -> Result<Self, String> {
let mut cfg = tokio_postgres::Config::from_str(url).map_err(|e| e.to_string())?;
if cfg.get_connect_timeout().is_none() {
cfg.connect_timeout(Duration::from_secs(5));
}
let mgr = Manager::from_config(
cfg,
tls_connector(),
ManagerConfig {
recycling_method: RecyclingMethod::Fast,
},
);
let pool = Pool::builder(mgr)
.max_size(16)
.runtime(Runtime::Tokio1)
.wait_timeout(Some(ACQUIRE_TIMEOUT))
.create_timeout(Some(ACQUIRE_TIMEOUT))
.build()
.map_err(|e| e.to_string())?;
let client = pool.get().await.map_err(|e| {
let mut msg = e.to_string();
let mut src = std::error::Error::source(&e);
while let Some(c) = src {
msg.push_str(": ");
msg.push_str(&c.to_string());
src = c.source();
}
if msg.contains("TLS") || msg.contains("certificate") {
msg.push_str(
" (the server's certificate must chain to a public CA; on a private \
network use ?sslmode=disable)",
);
}
msg
})?;
client
.execute("select pg_advisory_lock(7280431)", &[])
.await
.map_err(|e| unavailable(e).0)?;
let migrated = client
.batch_execute(PG_SCHEMA)
.await
.map_err(|e| unavailable(e).0);
let _ = client
.execute("select pg_advisory_unlock(7280431)", &[])
.await;
migrated?;
drop(client);
Ok(Store::Postgres(pool))
}
pub fn pg_pool(&self) -> Option<&Pool> {
match self {
Store::Postgres(p) => Some(p),
_ => None,
}
}
pub async fn put(&self, a: &Address) -> Result<(), StoreUnavailable> {
let key = a.value.to_lowercase();
let body = serde_json::to_string(a).expect("address serializes");
let expires = a.expires_at.map(|t| t.as_second());
match self {
Store::Memory(m) => {
m.lock().unwrap().insert(key, a.clone());
Ok(())
}
Store::Sqlite(c) => c
.lock()
.unwrap()
.execute(
"insert or replace into addresses (value, body, expires_at) values (?1, ?2, ?3)",
params![key, body, expires],
)
.map(|_| ())
.map_err(unavailable),
Store::Postgres(p) => p
.get()
.await
.map_err(unavailable)?
.execute(
"insert into addresses (value, body, expires_at) values ($1, $2, $3)
on conflict (value) do update
set body = excluded.body, expires_at = excluded.expires_at",
&[&key, &body, &expires],
)
.await
.map(|_| ())
.map_err(unavailable),
}
}
pub async fn get(&self, value: &str) -> Result<Option<Address>, StoreUnavailable> {
let key = value.to_lowercase();
let now = Timestamp::now();
let body: Option<String> = match self {
Store::Memory(m) => {
let found = m.lock().unwrap().get(&key).cloned();
return match found {
Some(a) if a.is_expired(now) => {
self.remove(&key).await?;
Ok(None)
}
other => Ok(other),
};
}
Store::Sqlite(c) => c
.lock()
.unwrap()
.query_row(
"select body from addresses where value = ?1",
params![key],
|r| r.get(0),
)
.optional()
.map_err(unavailable)?,
Store::Postgres(p) => p
.get()
.await
.map_err(unavailable)?
.query_opt("select body from addresses where value = $1", &[&key])
.await
.map_err(unavailable)?
.map(|r| r.get(0)),
};
match body.and_then(|b| serde_json::from_str::<Address>(&b).ok()) {
Some(a) if a.is_expired(now) => {
self.remove(&key).await?;
Ok(None)
}
other => Ok(other),
}
}
async fn remove(&self, key: &str) -> Result<(), StoreUnavailable> {
match self {
Store::Memory(m) => {
m.lock().unwrap().remove(key);
Ok(())
}
Store::Sqlite(c) => c
.lock()
.unwrap()
.execute("delete from addresses where value = ?1", params![key])
.map(|_| ())
.map_err(unavailable),
Store::Postgres(p) => p
.get()
.await
.map_err(unavailable)?
.execute("delete from addresses where value = $1", &[&key])
.await
.map(|_| ())
.map_err(unavailable),
}
}
pub async fn sweep(&self) -> Result<usize, StoreUnavailable> {
let now = Timestamp::now();
match self {
Store::Memory(m) => {
let mut m = m.lock().unwrap();
let before = m.len();
m.retain(|_, a| !a.is_expired(now));
Ok(before - m.len())
}
Store::Sqlite(c) => c
.lock()
.unwrap()
.execute(
"delete from addresses where expires_at is not null and expires_at <= ?1",
params![now.as_second()],
)
.map_err(unavailable),
Store::Postgres(p) => p
.get()
.await
.map_err(unavailable)?
.execute(
"delete from addresses where expires_at is not null and expires_at <= $1",
&[&now.as_second()],
)
.await
.map(|n| n as usize)
.map_err(unavailable),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn past(value: &str) -> Address {
let mut a = Address::new(value, "p");
a.expires_at = Some(Timestamp::from_second(1).unwrap());
a
}
async fn check(store: Store, tag: &str) {
let me = format!("Me-{tag}@X.test");
let old = format!("old-{tag}@x.test");
let a = Address::new(&me, "p").with_state("secret");
store.put(&a).await.unwrap();
assert_eq!(store.get(&me.to_lowercase()).await.unwrap(), Some(a));
let b = Address::new(&me, "p").with_state("refreshed");
store.put(&b).await.unwrap();
assert_eq!(store.get(&me).await.unwrap(), Some(b));
store.put(&past(&old)).await.unwrap();
assert_eq!(store.get(&old).await.unwrap(), None);
store.put(&past(&old)).await.unwrap();
assert!(store.sweep().await.unwrap() >= 1);
assert_eq!(store.get(&old).await.unwrap(), None);
assert_eq!(
store.get(&format!("nobody-{tag}@x.test")).await.unwrap(),
None
);
}
#[tokio::test]
async fn memory_store() {
check(Store::memory(), "m").await;
}
#[tokio::test]
async fn sqlite_store_survives_reopen() {
let dir = std::env::temp_dir().join(format!("ilp-store-{}", std::process::id()));
let path = dir.join("a.db");
check(Store::sqlite(&path).unwrap(), "s").await;
let a = Address::new("keep@x.test", "p").with_state("s");
Store::sqlite(&path).unwrap().put(&a).await.unwrap();
assert_eq!(
Store::sqlite(&path)
.unwrap()
.get("keep@x.test")
.await
.unwrap(),
Some(a)
);
std::fs::remove_dir_all(dir).ok();
}
#[tokio::test]
async fn postgres_store() {
let Some(url) = crate::fleet::test_database_url() else {
eprintln!("skipped: INTERLOPER_TEST_DATABASE_URL is unset");
return;
};
let tag = format!("{:x}", rand::random::<u64>());
check(Store::postgres(&url).await.unwrap(), &tag).await;
let a = Address::new(format!("shared-{tag}@x.test"), "p").with_state("s");
Store::postgres(&url).await.unwrap().put(&a).await.unwrap();
assert_eq!(
Store::postgres(&url)
.await
.unwrap()
.get(&a.value)
.await
.unwrap(),
Some(a)
);
}
#[tokio::test]
async fn unreachable_postgres_fails_fast() {
let r = Store::postgres("postgres://u:p@127.0.0.1:1/db").await;
assert!(r.is_err());
}
}