feat: add storage backend

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
2024-11-09 17:19:19 +01:00
parent ecc6d785e7
commit 4d37578c59
8 changed files with 162 additions and 5 deletions

View File

@@ -4,6 +4,8 @@ version = "0.1.0"
edition = "2021"
[dependencies]
nodata-storage.workspace = true
anyhow.workspace = true
tokio.workspace = true
tracing.workspace = true
@@ -12,6 +14,7 @@ clap.workspace = true
dotenv.workspace = true
axum.workspace = true
drift.workspace = true
uuid.workspace = true
serde = { version = "1.0.197", features = ["derive"] }
sqlx = { version = "0.7.3", features = [
@@ -21,7 +24,6 @@ sqlx = { version = "0.7.3", features = [
"uuid",
"time",
] }
uuid = { version = "1.7.0", features = ["v4"] }
tower-http = { version = "0.5.2", features = ["cors", "trace"] }
tokio-util = "0.7.11"
tonic.workspace = true

View File

@@ -288,7 +288,7 @@ mod test {
let topic = "some-topic".to_string();
let offset = 9usize;
let staging = Staging::default();
let staging = Staging::new();
// Publish 10 messages
for _ in 0..10 {
let offset = staging

View File

@@ -1,4 +1,4 @@
use std::{collections::BTreeMap, sync::Arc};
use std::{collections::BTreeMap, env::temp_dir, sync::Arc};
use tokio::sync::RwLock;
@@ -13,14 +13,23 @@ pub struct StagingEvent {
pub value: Vec<u8>,
}
#[derive(Clone, Default)]
#[derive(Clone)]
pub struct Staging {
// Temporary until we've got an actual file disk store
#[allow(clippy::complexity)]
store: Arc<RwLock<BTreeMap<Topic, Vec<StagingEvent>>>>,
storage: nodata_storage::Storage,
}
impl Staging {
pub fn new() -> Self {
Self {
store: Arc::default(),
storage: nodata_storage::Storage::new(nodata_storage::backend::StorageBackend::temp()),
}
}
pub async fn publish(
&self,
staging_event: impl Into<StagingEvent>,
@@ -29,6 +38,10 @@ impl Staging {
let mut store = self.store.write().await;
tracing::trace!(topic = staging_event.topic, "moving event to staging");
self.storage
.push_message(&staging_event.topic, &staging_event.value)
.await?;
let offset = match store.get_mut(&staging_event.topic) {
Some(part) => {
part.push(staging_event);

View File

@@ -43,7 +43,7 @@ impl State {
let _ = sqlx::query("SELECT 1;").fetch_one(&db).await?;
let staging = Staging::default();
let staging = Staging::new();
let handler = Handler::new(staging.clone());
Ok(Self {