diff --git a/backend/reconcile/src/operation_transformation/edited_text.rs b/backend/reconcile/src/operation_transformation/edited_text.rs index 38402d09..7b710bb4 100644 --- a/backend/reconcile/src/operation_transformation/edited_text.rs +++ b/backend/reconcile/src/operation_transformation/edited_text.rs @@ -38,7 +38,8 @@ impl<'a> EditedText<'a, String> { /// the original text, it will result in the updated text. The default /// word tokenizer is used to tokenize the text which splits the text on /// whitespaces. - #[must_use] pub fn from_strings(original: &'a str, updated: &str) -> Self { + #[must_use] + pub fn from_strings(original: &'a str, updated: &str) -> Self { Self::from_strings_with_tokenizer(original, updated, &word_tokenizer) } } @@ -186,7 +187,8 @@ where Self { text, operations } } - #[must_use] pub fn merge(self, other: Self) -> Self { + #[must_use] + pub fn merge(self, other: Self) -> Self { debug_assert_eq!( self.text, other.text, "EditedTexts must be derived from the same text to be mergable" @@ -235,7 +237,8 @@ where /// /// Returns an `SyncLibError::OperationError` if the operations cannot be /// applied to the text. - #[must_use] pub fn apply(&self) -> String { + #[must_use] + pub fn apply(&self) -> String { let mut builder: StringBuilder<'_> = StringBuilder::new(self.text); for OrderedOperation { operation, .. } in &self.operations { diff --git a/backend/reconcile/src/operation_transformation/operation.rs b/backend/reconcile/src/operation_transformation/operation.rs index bac6972f..062b8987 100644 --- a/backend/reconcile/src/operation_transformation/operation.rs +++ b/backend/reconcile/src/operation_transformation/operation.rs @@ -110,7 +110,8 @@ where #[cfg(debug_assertions)] debug_assert!( deleted_text - .as_ref().is_none_or(|text| builder.get_slice(self.range()) == *text), + .as_ref() + .is_none_or(|text| builder.get_slice(self.range()) == *text), "Text to delete does not match the text in the rope" ); diff --git a/backend/sync_server/src/server.rs b/backend/sync_server/src/server.rs index 7ebe789f..34a5a059 100644 --- a/backend/sync_server/src/server.rs +++ b/backend/sync_server/src/server.rs @@ -14,6 +14,7 @@ use axum::{ Extension, Json, }; use log::info; +use tokio::signal; use tower_http::cors::CorsLayer; use crate::app_state::AppState; @@ -91,8 +92,34 @@ pub async fn create_server(app_state: AppState) -> Result<()> { ); axum::serve(listener, app) + .with_graceful_shutdown(shutdown_signal()) + .tcp_nodelay(true) .await .context("Failed to start server") } async fn serve_api(Extension(api): Extension) -> impl IntoResponse { Json(api) } + +async fn shutdown_signal() { + let ctrl_c = async { + signal::ctrl_c() + .await + .expect("failed to install Ctrl+C handler"); + }; + + #[cfg(unix)] + let terminate = async { + signal::unix::signal(signal::unix::SignalKind::terminate()) + .expect("failed to install signal handler") + .recv() + .await; + }; + + #[cfg(not(unix))] + let terminate = std::future::pending::<()>(); + + tokio::select! { + _ = ctrl_c => {}, + _ = terminate => {}, + } +}