use std::future::Future;
use std::pin::Pin;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
pub trait SecondaryStorage: Send + Sync + 'static {
fn get(&self, key: &str) -> impl Future<Output = Result<Option<String>, StorageError>> + Send;
fn set(
&self,
key: &str,
value: &str,
ttl: Option<Duration>,
) -> impl Future<Output = Result<(), StorageError>> + Send;
fn delete(&self, key: &str) -> impl Future<Output = Result<(), StorageError>> + Send;
fn increment(
&self,
key: &str,
ttl: Duration,
) -> impl Future<Output = Result<u64, StorageError>> + Send;
}
#[derive(Debug, thiserror::Error)]
#[error("secondary storage: {0}")]
pub struct StorageError(#[source] Box<dyn std::error::Error + Send + Sync>);
impl StorageError {
pub fn new(err: impl std::error::Error + Send + Sync + 'static) -> Self {
Self(Box::new(err))
}
}
type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
pub(crate) trait DynStorage: Send + Sync {
fn increment<'a>(
&'a self,
key: &'a str,
ttl: Duration,
) -> BoxFuture<'a, Result<u64, StorageError>>;
}
impl<S: SecondaryStorage> DynStorage for S {
fn increment<'a>(
&'a self,
key: &'a str,
ttl: Duration,
) -> BoxFuture<'a, Result<u64, StorageError>> {
Box::pin(SecondaryStorage::increment(self, key, ttl))
}
}
pub const DEFAULT_STORAGE_TIMEOUT: Duration = Duration::from_millis(100);
const BREAKER_THRESHOLD: u32 = 5;
const BREAKER_COOL_DOWN: Duration = Duration::from_secs(5);
pub(crate) struct GuardedStorage {
inner: Arc<dyn DynStorage>,
timeout: Duration,
threshold: u32,
cool_down: Duration,
breaker: Mutex<Breaker>,
}
#[derive(Default)]
struct Breaker {
failures: u32,
open_until: Option<Instant>,
}
impl GuardedStorage {
pub(crate) fn new(inner: Arc<dyn DynStorage>, timeout: Duration) -> Self {
Self {
inner,
timeout,
threshold: BREAKER_THRESHOLD,
cool_down: BREAKER_COOL_DOWN,
breaker: Mutex::default(),
}
}
#[cfg(test)]
pub(crate) fn with_breaker(mut self, threshold: u32, cool_down: Duration) -> Self {
self.threshold = threshold;
self.cool_down = cool_down;
self
}
pub(crate) async fn increment(&self, key: &str, ttl: Duration) -> Result<u64, StorageError> {
if !self.admit() {
return Err(StorageError::new(std::io::Error::other(format!(
"skipped after {} failures in a row; probing again every {:?}",
self.threshold, self.cool_down
))));
}
let result = tokio::time::timeout(self.timeout, self.inner.increment(key, ttl))
.await
.unwrap_or_else(|_| {
Err(StorageError::new(std::io::Error::new(
std::io::ErrorKind::TimedOut,
format!("no answer within {:?}", self.timeout),
)))
});
self.record(result.is_ok());
result
}
fn admit(&self) -> bool {
let mut breaker = self.breaker.lock().unwrap();
let now = Instant::now();
match breaker.open_until {
Some(until) if now < until => false,
Some(_) => {
breaker.open_until = Some(now + self.cool_down);
true
}
None => true,
}
}
fn record(&self, ok: bool) {
let mut breaker = self.breaker.lock().unwrap();
if ok {
*breaker = Breaker::default();
return;
}
breaker.failures = breaker.failures.saturating_add(1);
if breaker.failures >= self.threshold {
breaker.open_until = Some(Instant::now() + self.cool_down);
}
}
}
#[cfg(test)]
pub(crate) mod fake {
use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex, MutexGuard};
use std::time::{Duration, Instant};
use super::{SecondaryStorage, StorageError};
type Entries = HashMap<String, (String, Option<Instant>)>;
#[derive(Clone, Default)]
pub(crate) struct FakeStorage {
entries: Arc<Mutex<Entries>>,
down: Arc<AtomicBool>,
hanging: Arc<AtomicBool>,
increments: Arc<AtomicUsize>,
}
impl FakeStorage {
pub(crate) fn set_down(&self, down: bool) {
self.down.store(down, Ordering::SeqCst);
}
pub(crate) fn set_hanging(&self, hanging: bool) {
self.hanging.store(hanging, Ordering::SeqCst);
}
pub(crate) fn increments(&self) -> usize {
self.increments.load(Ordering::SeqCst)
}
pub(crate) fn keys(&self) -> Vec<String> {
self.entries.lock().unwrap().keys().cloned().collect()
}
fn live(&self) -> Result<MutexGuard<'_, Entries>, StorageError> {
if self.down.load(Ordering::SeqCst) {
return Err(StorageError::new(std::io::Error::other(
"connection refused",
)));
}
let mut entries = self.entries.lock().unwrap();
let now = Instant::now();
entries.retain(|_, (_, expires)| expires.is_none_or(|at| at > now));
Ok(entries)
}
}
fn expiry(ttl: Duration) -> Instant {
Instant::now() + ttl.max(Duration::from_millis(1))
}
impl SecondaryStorage for FakeStorage {
async fn get(&self, key: &str) -> Result<Option<String>, StorageError> {
Ok(self.live()?.get(key).map(|(value, _)| value.clone()))
}
async fn set(
&self,
key: &str,
value: &str,
ttl: Option<Duration>,
) -> Result<(), StorageError> {
self.live()?
.insert(key.to_owned(), (value.to_owned(), ttl.map(expiry)));
Ok(())
}
async fn delete(&self, key: &str) -> Result<(), StorageError> {
self.live()?.remove(key);
Ok(())
}
async fn increment(&self, key: &str, ttl: Duration) -> Result<u64, StorageError> {
self.increments.fetch_add(1, Ordering::SeqCst);
if self.hanging.load(Ordering::SeqCst) {
std::future::pending::<()>().await;
}
let mut entries = self.live()?;
let (value, _) = entries
.entry(key.to_owned())
.or_insert_with(|| ("0".into(), Some(expiry(ttl))));
let next = value
.parse::<u64>()
.map_err(|_| StorageError::new(std::io::Error::other("not a counter")))?
+ 1;
*value = next.to_string();
Ok(next)
}
}
#[tokio::test]
async fn fake_storage_passes_the_conformance_suite() {
crate::testing::run_storage_conformance(FakeStorage::default()).await;
}
}