From d0983696c0ef2d62114e96337ca63b8cf073a0d6 Mon Sep 17 00:00:00 2001 From: forehalo Date: Thu, 2 Jan 2025 02:47:15 +0000 Subject: [PATCH] fix(native): round trip timestamp to millis (#9468) --- packages/frontend/native/nbstore/src/doc.rs | 39 +++++++++++++++++++-- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/packages/frontend/native/nbstore/src/doc.rs b/packages/frontend/native/nbstore/src/doc.rs index 28088da6f1339..00706f1de1e8a 100644 --- a/packages/frontend/native/nbstore/src/doc.rs +++ b/packages/frontend/native/nbstore/src/doc.rs @@ -1,6 +1,6 @@ use std::ops::Deref; -use chrono::NaiveDateTime; +use chrono::{DateTime, NaiveDateTime}; use sqlx::{QueryBuilder, Row}; use super::storage::{Result, SqliteDocStorage}; @@ -66,7 +66,40 @@ impl SqliteDocStorage { doc_id: String, update: Update, ) -> Result { - let timestamp = chrono::Utc::now().naive_utc(); + let mut timestamp = DateTime::from_timestamp_millis(chrono::Utc::now().timestamp_millis()) + .unwrap() + .naive_utc(); + + let mut tried = 0; + + // Keep trying with incremented timestamps until success + loop { + match self + .try_insert_update_with_timestamp(&doc_id, update.as_ref(), timestamp) + .await + { + Ok(()) => break, + Err(e) => { + if tried > 10 { + return Err(e); + } + + // Increment timestamp by 1ms and retry + timestamp = timestamp + chrono::Duration::milliseconds(1); + tried += 1; + } + } + } + + Ok(timestamp) + } + + async fn try_insert_update_with_timestamp( + &self, + doc_id: &str, + update: &[u8], + timestamp: NaiveDateTime, + ) -> sqlx::Result<()> { let mut tx = self.pool.begin().await?; sqlx::query(r#"INSERT INTO updates (doc_id, data, created_at) VALUES ($1, $2, $3);"#) @@ -89,7 +122,7 @@ impl SqliteDocStorage { tx.commit().await?; - Ok(timestamp) + Ok(()) } pub async fn get_doc_snapshot(&self, doc_id: String) -> Result> {