relay_server/services/buffer/stack_provider/mod.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
use crate::services::buffer::common::ProjectKeyPair;
use crate::EnvelopeStack;
use hashbrown::HashSet;
use std::future::Future;
pub mod memory;
pub mod sqlite;
/// State of the initialization of the [`StackProvider`].
///
/// This state is necessary for initializing resources whenever a [`StackProvider`] is used.
#[derive(Debug)]
pub struct InitializationState {
pub project_key_pairs: HashSet<ProjectKeyPair>,
}
impl InitializationState {
/// Create a new [`InitializationState`].
pub fn new(project_key_pairs: HashSet<ProjectKeyPair>) -> Self {
Self { project_key_pairs }
}
/// Creates a new empty [`InitializationState`].
pub fn empty() -> Self {
Self {
project_key_pairs: HashSet::new(),
}
}
}
/// The creation type for the [`EnvelopeStack`].
pub enum StackCreationType {
/// An [`EnvelopeStack`] that is created during initialization.
Initialization,
/// An [`EnvelopeStack`] that is created when an envelope is received.
New,
}
/// A provider of [`EnvelopeStack`] instances that is responsible for creating them.
pub trait StackProvider: std::fmt::Debug {
/// The implementation of [`EnvelopeStack`] that this manager creates.
type Stack: EnvelopeStack;
/// Initializes the [`StackProvider`].
fn initialize(&self) -> impl Future<Output = InitializationState>;
/// Creates an [`EnvelopeStack`].
fn create_stack(
&self,
stack_creation_type: StackCreationType,
project_key_pair: ProjectKeyPair,
) -> Self::Stack;
/// Returns `true` if the store used by this [`StackProvider`] has space to add new
/// stacks or items to the stacks.
fn has_store_capacity(&self) -> bool;
/// Returns the total count of the store used by this [`StackProvider`].
fn store_total_count(&self) -> impl Future<Output = u64>;
/// Returns the string representation of the stack type offered by this [`StackProvider`].
fn stack_type<'a>(&self) -> &'a str;
/// Flushes the supplied [`EnvelopeStack`]s and consumes the [`StackProvider`].
fn flush(
&mut self,
envelope_stacks: impl IntoIterator<Item = Self::Stack>,
) -> impl Future<Output = ()>;
}