use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex, Weak};
use std::time::Duration;
use snodra_messaging::store::AppendListener;
use sqlx::postgres::{PgConnectOptions, PgListener, PgPool, PgPoolOptions};
use tokio_util::sync::CancellationToken;
pub(crate) const CHANNEL: &str = "snodra_outbox";
const HEALTH_CHECK: Duration = Duration::from_secs(30);
const NETWORK_TIMEOUT: Duration = Duration::from_secs(10);
const MIN_BACKOFF: Duration = Duration::from_millis(200);
const MAX_BACKOFF: Duration = Duration::from_secs(30);
type Wake = Arc<dyn Fn() + Send + Sync>;
pub(crate) struct Notifications {
options: Arc<PgConnectOptions>,
state: Mutex<State>,
listens: AtomicU64,
}
#[derive(Default)]
struct State {
listeners: HashMap<u64, Wake>,
next: u64,
running: Option<CancellationToken>,
}
impl Notifications {
pub(crate) fn new(pool: &PgPool) -> Arc<Notifications> {
Arc::new(Notifications {
options: pool.connect_options(),
state: Mutex::new(State::default()),
listens: AtomicU64::new(0),
})
}
pub(crate) fn listens(&self) -> u64 {
self.listens.load(Ordering::SeqCst)
}
fn lock(&self) -> std::sync::MutexGuard<'_, State> {
self.state.lock().unwrap_or_else(|p| p.into_inner())
}
pub(crate) fn register(self: &Arc<Self>, wake: Wake) -> AppendListener {
let Ok(runtime) = tokio::runtime::Handle::try_current() else {
return AppendListener::none();
};
let id = {
let mut state = self.lock();
let id = state.next;
state.next += 1;
state.listeners.insert(id, wake);
if state.running.is_none() {
let stop = CancellationToken::new();
state.running = Some(stop.clone());
runtime.spawn(listen(Arc::downgrade(self), stop));
}
id
};
let me = Arc::downgrade(self);
AppendListener::new(move || {
if let Some(me) = me.upgrade() {
let mut state = me.lock();
if state.listeners.remove(&id).is_some()
&& state.listeners.is_empty()
&& let Some(stop) = state.running.take()
{
stop.cancel();
}
}
})
}
fn wake_all(&self) {
let listeners: Vec<Wake> = self.lock().listeners.values().cloned().collect();
for wake in listeners {
wake();
}
}
async fn connect(&self) -> Result<(PgPool, PgListener), sqlx::Error> {
let pool = PgPoolOptions::new()
.max_connections(1)
.min_connections(0)
.idle_timeout(None)
.max_lifetime(None)
.acquire_timeout(NETWORK_TIMEOUT)
.connect_lazy_with((*self.options).clone());
let mut listener = PgListener::connect_with(&pool).await?;
listener.listen(CHANNEL).await?;
Ok((pool, listener))
}
}
async fn listen(me: Weak<Notifications>, stop: CancellationToken) {
let mut backoff = MIN_BACKOFF;
let mut failing = false;
loop {
let Some(n) = me.upgrade() else { return };
let connected = tokio::select! {
_ = stop.cancelled() => return,
c = n.connect() => c,
};
let failure = match connected {
Ok((pool, mut listener)) => {
if failing {
tracing::info!("listening for outbox appends again");
}
failing = false;
backoff = MIN_BACKOFF;
n.wake_all(); n.listens.fetch_add(1, Ordering::SeqCst);
drop(n);
let failure = receive(&me, &mut listener, &stop).await;
drop(listener);
pool.close().await;
match failure {
None => return,
Some(e) => e,
}
}
Err(e) => e,
};
if stop.is_cancelled() {
return;
}
if !failing {
tracing::warn!(
"cannot listen for outbox appends (the relay polls meanwhile); retrying: {failure}"
);
}
failing = true;
tokio::select! {
_ = stop.cancelled() => return,
_ = tokio::time::sleep(backoff) => {}
}
backoff = (backoff * 2).min(MAX_BACKOFF);
}
}
async fn receive(
me: &Weak<Notifications>,
listener: &mut PgListener,
stop: &CancellationToken,
) -> Option<sqlx::Error> {
loop {
let received = tokio::select! {
_ = stop.cancelled() => return None,
r = tokio::time::timeout(HEALTH_CHECK, listener.try_recv()) => r,
};
let n = me.upgrade()?;
match received {
Ok(Ok(_)) => n.wake_all(),
Ok(Err(e)) => return Some(e),
Err(_) => {
match tokio::time::timeout(
NETWORK_TIMEOUT,
sqlx::query("SELECT 1").execute(&mut *listener),
)
.await
{
Ok(Ok(_)) => {}
Ok(Err(e)) => return Some(e),
Err(_) => {
return Some(sqlx::Error::Io(std::io::Error::new(
std::io::ErrorKind::TimedOut,
"the listening connection did not answer its health check",
)));
}
}
}
}
}
}