From 4d464c4c5085f1f17d8b2a43845891893376f9db Mon Sep 17 00:00:00 2001 From: Ellie Huxtable Date: Fri, 10 Jun 2022 09:17:41 +0100 Subject: [PATCH 1/2] Add configurable history length This allows servers to decide the max length of each history item they want to store! Some users might have much larger history lines than others. This setting can be set to 0 to allow for unlimited history length. This is not recommended for a public server install, but for a private one it can work nicely. --- .../20220610074049_history-length.sql | 2 ++ atuin-server/src/database.rs | 19 +++++++++++++++---- atuin-server/src/lib.rs | 2 +- atuin-server/src/settings.rs | 2 ++ 4 files changed, 20 insertions(+), 5 deletions(-) create mode 100644 atuin-server/migrations/20220610074049_history-length.sql diff --git a/atuin-server/migrations/20220610074049_history-length.sql b/atuin-server/migrations/20220610074049_history-length.sql new file mode 100644 index 00000000000..b1c23016733 --- /dev/null +++ b/atuin-server/migrations/20220610074049_history-length.sql @@ -0,0 +1,2 @@ +-- Add migration script here +alter table history alter column data type text; diff --git a/atuin-server/src/database.rs b/atuin-server/src/database.rs index d2246918f86..41bb7b232eb 100644 --- a/atuin-server/src/database.rs +++ b/atuin-server/src/database.rs @@ -4,13 +4,14 @@ use async_trait::async_trait; use chrono::{Datelike, TimeZone}; use chronoutil::RelativeDuration; use sqlx::{postgres::PgPoolOptions, Result}; -use tracing::{debug, instrument}; +use tracing::{debug, warn, instrument}; use super::{ calendar::{TimePeriod, TimePeriodInfo}, models::{History, NewHistory, NewSession, NewUser, Session, User}, }; use crate::settings::HISTORY_PAGE_SIZE; +use crate::settings::Settings; use atuin_common::utils::get_days_from_month; @@ -61,18 +62,19 @@ pub trait Database { #[derive(Clone)] pub struct Postgres { pool: sqlx::Pool, + settings: Settings, } impl Postgres { - pub async fn new(uri: &str) -> Result { + pub async fn new(settings: Settings) -> Result { let pool = PgPoolOptions::new() .max_connections(100) - .connect(uri) + .connect(settings.db_uri.as_str()) .await?; sqlx::migrate!("./migrations").run(&pool).await?; - Ok(Self { pool }) + Ok(Self { pool, settings }) } } @@ -252,6 +254,15 @@ impl Database for Postgres { let hostname: &str = &i.hostname; let data: &str = &i.data; + if data.len() > self.settings.max_history_length && self.settings.max_history_length != 0 { + // Don't return an error here. We want to insert as much of the + // history list as we can, so log the error and continue going. + + warn!("history too long, got length {}, max {}", data.len(), self.settings.max_history_length); + + continue; + } + sqlx::query( "insert into history (client_id, user_id, hostname, timestamp, data) diff --git a/atuin-server/src/lib.rs b/atuin-server/src/lib.rs index 7be54e58f7c..571c09bb0b4 100644 --- a/atuin-server/src/lib.rs +++ b/atuin-server/src/lib.rs @@ -19,7 +19,7 @@ pub mod settings; pub async fn launch(settings: Settings, host: String, port: u16) -> Result<()> { let host = host.parse::()?; - let postgres = Postgres::new(settings.db_uri.as_str()) + let postgres = Postgres::new(settings.clone()) .await .wrap_err_with(|| format!("failed to connect to db: {}", settings.db_uri))?; diff --git a/atuin-server/src/settings.rs b/atuin-server/src/settings.rs index e67451735f3..b0e07b718af 100644 --- a/atuin-server/src/settings.rs +++ b/atuin-server/src/settings.rs @@ -13,6 +13,7 @@ pub struct Settings { pub port: u16, pub db_uri: String, pub open_registration: bool, + pub max_history_length: usize, } impl Settings { @@ -33,6 +34,7 @@ impl Settings { .set_default("host", "127.0.0.1")? .set_default("port", 8888)? .set_default("open_registration", false)? + .set_default("max_history_length", 8192)? .add_source( Environment::with_prefix("atuin") .prefix_separator("_") From d3cc0affa0a51660e96ba3fc03deb7628373ebc1 Mon Sep 17 00:00:00 2001 From: Ellie Huxtable Date: Fri, 10 Jun 2022 09:59:52 +0100 Subject: [PATCH 2/2] Format lol --- atuin-server/src/database.rs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/atuin-server/src/database.rs b/atuin-server/src/database.rs index 41bb7b232eb..ef6c6d85951 100644 --- a/atuin-server/src/database.rs +++ b/atuin-server/src/database.rs @@ -4,14 +4,14 @@ use async_trait::async_trait; use chrono::{Datelike, TimeZone}; use chronoutil::RelativeDuration; use sqlx::{postgres::PgPoolOptions, Result}; -use tracing::{debug, warn, instrument}; +use tracing::{debug, instrument, warn}; use super::{ calendar::{TimePeriod, TimePeriodInfo}, models::{History, NewHistory, NewSession, NewUser, Session, User}, }; -use crate::settings::HISTORY_PAGE_SIZE; use crate::settings::Settings; +use crate::settings::HISTORY_PAGE_SIZE; use atuin_common::utils::get_days_from_month; @@ -254,11 +254,17 @@ impl Database for Postgres { let hostname: &str = &i.hostname; let data: &str = &i.data; - if data.len() > self.settings.max_history_length && self.settings.max_history_length != 0 { + if data.len() > self.settings.max_history_length + && self.settings.max_history_length != 0 + { // Don't return an error here. We want to insert as much of the // history list as we can, so log the error and continue going. - - warn!("history too long, got length {}, max {}", data.len(), self.settings.max_history_length); + + warn!( + "history too long, got length {}, max {}", + data.len(), + self.settings.max_history_length + ); continue; }