chore: change to byte slice

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
Kasper Juul Hermansen 2023-08-27 20:16:27 +02:00
parent d3beab5006
commit 541b9b22d2
Signed by: kjuulh
GPG Key ID: 9AA7BC13CE474394
10 changed files with 829 additions and 746 deletions

View File

@ -10,3 +10,8 @@ struct LogEvent {
struct Agent {
name @0 :Text;
}
struct Lease {
id @0 :Text;
lease @1 :Text;
}

View File

@ -3,7 +3,7 @@ use capnp::message::{ReaderOptions, TypedReader};
use capnp::serialize::{self, SliceSegments};
use capnp::traits::Owned;
use churn_domain::{Agent, LogEvent};
use churn_domain::{Agent, Lease, LogEvent};
mod models_capnp;
@ -11,19 +11,18 @@ pub trait CapnpPackExt {
type Return;
fn serialize_capnp(&self) -> Vec<u8>;
fn deserialize_capnp(content: &Vec<u8>) -> anyhow::Result<Self::Return>;
fn deserialize_capnp(content: &[u8]) -> anyhow::Result<Self::Return>;
fn capnp_to_string(builder: &Builder<HeapAllocator>) -> Vec<u8> {
serialize::write_message_to_words(builder)
}
fn string_to_capnp<S>(content: &Vec<u8>) -> TypedReader<SliceSegments, S>
fn string_to_capnp<S>(mut content: &[u8]) -> TypedReader<SliceSegments, S>
where
S: Owned,
{
let log_event =
serialize::read_message_from_flat_slice(&mut content.as_slice(), ReaderOptions::new())
.unwrap();
serialize::read_message_from_flat_slice(&mut content, ReaderOptions::new()).unwrap();
log_event.into_typed::<S>()
}
@ -43,7 +42,7 @@ impl CapnpPackExt for LogEvent {
Self::capnp_to_string(&builder)
}
fn deserialize_capnp(content: &Vec<u8>) -> anyhow::Result<Self> {
fn deserialize_capnp(content: &[u8]) -> anyhow::Result<Self> {
let log_event = Self::string_to_capnp::<models_capnp::log_event::Owned>(content);
let log_event = log_event.get()?;
@ -71,7 +70,7 @@ impl CapnpPackExt for Agent {
Self::capnp_to_string(&builder)
}
fn deserialize_capnp(content: &Vec<u8>) -> anyhow::Result<Self::Return> {
fn deserialize_capnp(content: &[u8]) -> anyhow::Result<Self::Return> {
let item = Self::string_to_capnp::<models_capnp::agent::Owned>(content);
let item = item.get()?;
@ -80,3 +79,27 @@ impl CapnpPackExt for Agent {
})
}
}
impl CapnpPackExt for Lease {
type Return = Self;
fn serialize_capnp(&self) -> Vec<u8> {
let mut builder = Builder::new_default();
let mut item = builder.init_root::<models_capnp::lease::Builder>();
item.set_id(&self.id.to_string());
item.set_lease(&self.lease.to_string());
Self::capnp_to_string(&builder)
}
fn deserialize_capnp(content: &[u8]) -> anyhow::Result<Self::Return> {
let item = Self::string_to_capnp::<models_capnp::lease::Owned>(content);
let item = item.get()?;
Ok(Self {
id: uuid::Uuid::parse_str(item.get_id()?)?,
lease: uuid::Uuid::parse_str(item.get_lease()?)?,
})
}
}

File diff suppressed because it is too large Load Diff

View File

@ -47,3 +47,9 @@ impl LogEvent {
pub struct Agent {
pub name: String,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Lease {
pub id: uuid::Uuid,
pub lease: uuid::Uuid,
}

View File

@ -24,13 +24,6 @@ impl std::ops::Deref for AgentService {
}
}
impl Default for AgentService {
fn default() -> Self {
Self(Arc::new(DefaultAgentService::default()))
}
}
#[derive(Default)]
struct DefaultAgentService {
agents: Db,
}

View File

@ -1,6 +1,6 @@
use core::slice::SlicePattern;
use std::path::{Path, PathBuf};
use std::path::{Path};
use std::sync::Arc;
use async_trait::async_trait;
@ -22,22 +22,10 @@ impl std::ops::Deref for Db {
}
}
impl Default for Db {
fn default() -> Self {
Self(Arc::new(DefaultDb::default()))
}
}
struct DefaultDb {
db: sled::Db,
}
impl Default for DefaultDb {
fn default() -> Self {
Self::new(&PathBuf::from("churn-server.sled"))
}
}
impl DefaultDb {
pub fn new(path: &Path) -> Self {
Self {

View File

@ -26,13 +26,6 @@ impl std::ops::Deref for EventService {
}
}
impl Default for EventService {
fn default() -> Self {
Self(Arc::new(DefaultEventService::default()))
}
}
#[derive(Default)]
struct DefaultEventService {
db: Db,
}
@ -84,6 +77,7 @@ impl EventServiceTrait for DefaultEventService {
let events = events
.iter()
.map(|x| x.as_slice())
.flat_map(LogEvent::deserialize_capnp)
.sorted_by_key(|i| i.timestamp)
.collect();

View File

@ -2,11 +2,21 @@ use std::sync::Arc;
use axum::async_trait;
use tokio::sync::Mutex;
use churn_capnp::CapnpPackExt;
use churn_domain::Lease;
use crate::db::Db;
#[derive(Clone)]
pub struct LeaseService(Arc<dyn LeaseServiceTrait + Send + Sync + 'static>);
impl LeaseService {
pub fn new(db: Db) -> Self {
Self(Arc::new(DefaultLeaseService::new(db)))
}
}
impl std::ops::Deref for LeaseService {
type Target = Arc<dyn LeaseServiceTrait + Send + Sync + 'static>;
@ -15,15 +25,14 @@ impl std::ops::Deref for LeaseService {
}
}
impl Default for LeaseService {
fn default() -> Self {
Self(Arc::new(DefaultLeaseService::default()))
}
struct DefaultLeaseService {
db: Db,
}
#[derive(Default)]
struct DefaultLeaseService {
leases: Arc<Mutex<Vec<String>>>,
impl DefaultLeaseService {
pub fn new(db: Db) -> Self {
Self { db }
}
}
#[async_trait]
@ -34,12 +43,17 @@ pub trait LeaseServiceTrait {
#[async_trait]
impl LeaseServiceTrait for DefaultLeaseService {
async fn create_lease(&self) -> anyhow::Result<String> {
let mut leases = self.leases.lock().await;
let lease = uuid::Uuid::new_v4();
let id = uuid::Uuid::new_v4();
let lease = uuid::Uuid::new_v4().to_string();
self.db
.insert(
"lease",
&lease.to_string(),
&Lease { id, lease }.serialize_capnp(),
)
.await?;
leases.push(lease.clone());
Ok(lease)
Ok(lease.to_string())
}
}

View File

@ -70,37 +70,34 @@ async fn main() -> anyhow::Result<()> {
let cli = Command::parse();
match cli.command {
Some(Commands::Serve { host }) => {
tracing::info!("Starting churn server");
let db = match cli.global.database {
DatabaseType::Sled => Db::new_sled(&cli.global.sled_path),
};
if let Some(Commands::Serve { host }) = cli.command {
tracing::info!("Starting churn server");
let db = match cli.global.database {
DatabaseType::Sled => Db::new_sled(&cli.global.sled_path),
};
let app = Router::new()
.route("/ping", get(ping))
.route("/logs", get(logs))
.nest(
"/agent",
Router::new()
.route("/enroll", post(enroll))
.route("/ping", post(agent_ping))
.route("/events", post(get_tasks))
.route("/lease", post(agent_lease)),
)
.with_state(AppState {
agent: AgentService::new(db.clone()),
leases: LeaseService::default(),
events: EventService::new(db.clone()),
});
let app = Router::new()
.route("/ping", get(ping))
.route("/logs", get(logs))
.nest(
"/agent",
Router::new()
.route("/enroll", post(enroll))
.route("/ping", post(agent_ping))
.route("/events", post(get_tasks))
.route("/lease", post(agent_lease)),
)
.with_state(AppState {
agent: AgentService::new(db.clone()),
leases: LeaseService::new(db.clone()),
events: EventService::new(db.clone()),
});
tracing::info!("churn server listening on {}", host);
axum::Server::bind(&host)
.serve(app.into_make_service())
.await
.unwrap();
}
None => {}
tracing::info!("churn server listening on {}", host);
axum::Server::bind(&host)
.serve(app.into_make_service())
.await
.unwrap();
}
Ok(())
@ -231,10 +228,10 @@ async fn logs(
.await
.map_err(AppError::Internal)?;
return Ok(Json(ServerMonitorResp {
Ok(Json(ServerMonitorResp {
cursor: Some(cursor),
logs: Vec::new(),
}));
}))
}
}
}

View File

@ -1,13 +1,13 @@
use std::{path::PathBuf, sync::Arc};
use dagger_rust::build::{RustVersion, SlimImage};
use dagger_sdk::{Config, Query};
use dagger_sdk::Query;
use tokio::io::{AsyncBufReadExt, AsyncWriteExt};
#[tokio::main]
async fn main() -> eyre::Result<()> {
let mut config = Config::default();
config.logger = None;
// let mut config = Config::default();
// config.logger = None;
println!("Building churning...");