Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(websocket): add local storage support for snapshots #505

Merged
merged 10 commits into from
Sep 23, 2024

Conversation

kasugamirai
Copy link
Member

@kasugamirai kasugamirai commented Sep 17, 2024

Overview

What I've done

What I haven't done

How I tested

Screenshot

Which point I want you to review particularly

Memo

Summary by CodeRabbit

  • New Features

    • Introduced LocalClient for asynchronous file storage operations, allowing users to upload and download files safely.
    • Added ProjectLocalRepository for managing project data and snapshots locally.
  • Chores

    • Reformatted configuration files for improved readability and consistency.
    • Added new dependencies, tempfile and lru, to enhance file management and caching capabilities.
  • Tests

    • Implemented tests for new local storage functionalities and repository methods to ensure reliability.

@kasugamirai kasugamirai requested a review from a team as a code owner September 17, 2024 16:54
Copy link
Contributor

coderabbitai bot commented Sep 17, 2024

Walkthrough

The changes introduced in this pull request include updates to the Cargo.toml files for improved dependency management, the addition of new dependencies tempfile and lru, and the creation of a LocalClient struct for asynchronous file operations. A new module for local storage management has been established, along with a ProjectLocalRepository struct that implements project data handling in the local filesystem. These modifications enhance the organization and modularity of the codebase.

Changes

File Change Summary
websocket/Cargo.toml Reformatted members field and added tempfile and lru dependencies.
websocket/crates/infra/Cargo.toml Added tempfile and lru crates to the workspace.
websocket/crates/infra/src/persistence/local_storage/local_client.rs Introduced LocalClient struct for async file operations, including upload and download methods, with a caching mechanism.
websocket/crates/infra/src/persistence/local_storage/mod.rs Added new local_client module and made LocalClient publicly accessible.
websocket/crates/infra/src/persistence/mod.rs Added local_storage module declaration.
websocket/crates/infra/src/persistence/project_repository.rs Introduced ProjectLocalRepository struct for local storage management of project data, implementing related traits for fetching and storing project data.

Possibly related PRs

Poem

🐇 In the code where bunnies play,
New helpers hop in bright array.
Files stored safe, no need to fret,
LocalClient's here, a fine new pet!
With every change, we leap and cheer,
For clearer paths, our goals are near! 🥕


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    -- I pushed a fix in commit <commit_id>, please review it.
    -- Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    -- @coderabbitai generate unit testing code for this file.
    -- @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    -- @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    -- @coderabbitai read src/utils.ts and generate unit testing code.
    -- @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    -- @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

netlify bot commented Sep 17, 2024

Deploy Preview for reearth-flow canceled.

Name Link
🔨 Latest commit ac423f1
🔍 Latest deploy log https://app.netlify.com/sites/reearth-flow/deploys/66f1dc589409010008103912

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 6

Outside diff range and nitpick comments (3)
websocket/crates/infra/src/persistence/local_storage/mod.rs (1)

1-2: Consider adding module-level documentation for clarity.

Including Rustdoc comments for the local_storage module and the re-exported LocalClient will enhance readability and help other developers understand the purpose and usage of this component.

websocket/crates/infra/src/persistence/local_storage/local_client.rs (2)

128-169: Improve Test Reliability by Eliminating Fixed Delays

The test_concurrent_access test uses tokio::time::sleep to simulate concurrent access, which can lead to flaky tests due to timing issues. Relying on fixed delays may not guarantee that the upload and download operations overlap as intended.

Use synchronization primitives like tokio::sync::Barrier to coordinate the concurrent tasks more reliably:

use tokio::sync::Barrier;

#[tokio::test]
async fn test_concurrent_access() -> io::Result<()> {
    // ...
    let barrier = Arc::new(Barrier::new(2));

    let upload_task = {
        let client = client.clone();
        let barrier = barrier.clone();
        tokio::spawn(async move {
            barrier.wait().await;
            client
                .upload("concurrent_test.json".to_string(), &test_data)
                .await
        })
    };

    let download_task = {
        let client = client.clone();
        let barrier = barrier.clone();
        tokio::spawn(async move {
            barrier.wait().await;
            client
                .download::<TestData>("concurrent_test.json".to_string())
                .await
        })
    };

    upload_task.await??;
    let downloaded_data = download_task.await??;

    assert_eq!(test_data, downloaded_data);

    Ok(())
}

This approach ensures both tasks start concurrently without relying on arbitrary delays.


85-89: Simplify Test Code by Deriving Clone for TestData

In the tests, TestData instances are manually recreated instead of cloned, leading to code duplication.

Derive the Clone trait for TestData to enable easy cloning:

 #[derive(Serialize, Deserialize, PartialEq, Debug)]
+#[derive(Clone)]
 struct TestData {
     field1: String,
     field2: i32,
 }

Then, you can clone the existing instance:

let test_data_clone = test_data.clone();

This reduces code repetition and makes the test code cleaner.

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between b46945b and 083ae37.

Files ignored due to path filters (1)
  • websocket/Cargo.lock is excluded by !**/*.lock, !**/*.lock
Files selected for processing (7)
  • websocket/Cargo.toml (2 hunks)
  • websocket/crates/domain/src/project.rs (1 hunks)
  • websocket/crates/infra/Cargo.toml (1 hunks)
  • websocket/crates/infra/src/persistence/local_storage/local_client.rs (1 hunks)
  • websocket/crates/infra/src/persistence/local_storage/mod.rs (1 hunks)
  • websocket/crates/infra/src/persistence/mod.rs (1 hunks)
  • websocket/crates/infra/src/persistence/project_repository.rs (2 hunks)
Files skipped from review due to trivial changes (4)
  • websocket/Cargo.toml
  • websocket/crates/domain/src/project.rs
  • websocket/crates/infra/Cargo.toml
  • websocket/crates/infra/src/persistence/mod.rs
Additional comments not posted (1)
websocket/crates/infra/src/persistence/project_repository.rs (1)

92-93: Question on Necessity of Arc around LocalClient

In ProjectLocalRepository, the client field is wrapped in an Arc:

client: Arc<LocalClient>,

If ProjectLocalRepository is not being shared across threads or does not require multiple ownership, the Arc may be unnecessary. Removing it can simplify the code and reduce overhead.

Please verify if Arc is needed in this context. If not, consider refactoring to use LocalClient directly.

Comment on lines 23 to 26
fn get_full_path(&self, path: &str) -> PathBuf {
self.base_path.join(path)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential Security Risk: Path Traversal Vulnerability

The get_full_path method directly joins user-provided paths without sanitization. This could allow for path traversal attacks, where an attacker provides input like "../../sensitive_file" to access or modify files outside the intended directory.

To mitigate this risk, sanitize the input path to prevent directory traversal:

fn get_full_path(&self, path: &str) -> PathBuf {
    let sanitized_path = Path::new(path)
        .components()
        .filter(|c| matches!(c, std::path::Component::Normal(_)))
        .collect::<PathBuf>();
    self.base_path.join(sanitized_path)
}

This code filters out any . or .. components, ensuring that the path remains within the base_path directory.

Comment on lines +27 to +36
async fn lock_file(&self, path: &Path) {
let mut locks = self.file_locks.lock().await;
locks.entry(path.to_path_buf()).or_insert(());
}

async fn unlock_file(&self, path: &PathBuf) {
let mut locks = self.file_locks.lock().await;
locks.remove(path);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Improper File Locking Mechanism Leading to Race Conditions

The current implementation of lock_file and unlock_file does not provide actual synchronization for file access. Merely inserting and removing entries in a HashMap does not prevent concurrent access to the same file, potentially causing data races and inconsistent data states.

To properly synchronize file access, consider using per-file mutexes:

 pub struct LocalClient {
     base_path: PathBuf,
-    file_locks: Mutex<HashMap<PathBuf, ()>>,
+    file_locks: Mutex<HashMap<PathBuf, Arc<Mutex<()>>>>,
 }

 impl LocalClient {
     // ...

-    async fn lock_file(&self, path: &Path) {
+    async fn lock_file(&self, path: &Path) -> Arc<Mutex<()>> {
         let mut locks = self.file_locks.lock().await;
-        locks.entry(path.to_path_buf()).or_insert(());
+        let lock = locks
+            .entry(path.to_path_buf())
+            .or_insert_with(|| Arc::new(Mutex::new(())))
+            .clone();
+        drop(locks); // Release the lock on the HashMap before acquiring the file lock
+        lock.lock().await;
+        lock
     }

-    async fn unlock_file(&self, path: &PathBuf) {
-        let mut locks = self.file_locks.lock().await;
-        locks.remove(path);
-    }

     // ...
 }

 // Update the `upload` method:
 pub async fn upload<T: Serialize + Send + Sync>(
     &self,
     path: String,
     data: &T,
 ) -> io::Result<()> {
     let full_path = self.get_full_path(&path);
-    self.lock_file(&full_path).await;
+    let file_lock = self.lock_file(&full_path).await;

     let result = async {
         // Existing code...
     }
     .await;

     // The file lock will be released when `file_lock` goes out of scope
-    self.unlock_file(&full_path).await;
     result
 }

 // Similarly update the `download` method

This ensures that each file access is protected by its own mutex, preventing simultaneous reads or writes to the same file.

Committable suggestion was skipped due to low confidence.

Comment on lines +164 to +165
use tokio::test;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incorrect Test Annotations for Asynchronous Tests

The test functions are asynchronous but are annotated with #[test]. To run async tests with Tokio, you should use #[tokio::test] instead. Additionally, the import use tokio::test; is unnecessary.

Apply this diff to fix the test annotations and remove the unnecessary import:

-    use tokio::test;
+    // The above import is unnecessary and can be removed.

     #[test]
-    async fn test_get_project_non_existent() -> Result<(), Box<dyn std::error::Error>> {
+    #[tokio::test]
+    async fn test_get_project_non_existent() -> Result<(), Box<dyn std::error::Error>> {

     #[test]
-    async fn test_get_project_existing() -> Result<(), Box<dyn std::error::Error>> {
+    #[tokio::test]
+    async fn test_get_project_existing() -> Result<(), Box<dyn std::error::Error>> {

     #[test]
-    async fn test_create_and_get_snapshot() -> Result<(), Box<dyn std::error::Error>> {
+    #[tokio::test]
+    async fn test_create_and_get_snapshot() -> Result<(), Box<dyn std::error::Error>> {

     #[test]
-    async fn test_get_latest_snapshot_state() -> Result<(), Box<dyn std::error::Error>> {
+    #[tokio::test]
+    async fn test_get_latest_snapshot_state() -> Result<(), Box<dyn std::error::Error>> {

Ensure that all async test functions are annotated with #[tokio::test] to properly utilize the Tokio runtime.

Also applies to: 172-172, 180-180, 199-199, 216-216

Comment on lines +103 to +116
#[async_trait]
impl ProjectRepository for ProjectLocalRepository {
async fn get_project(
&self,
project_id: &str,
) -> Result<Option<Project>, Box<dyn std::error::Error>> {
let path = format!("projects/{}", project_id);
match self.client.download::<Project>(path).await {
Ok(project) => Ok(Some(project)),
Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(None),
Err(e) => Err(Box::new(e)),
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use More Specific Error Types Instead of Box<dyn std::error::Error>

Currently, the methods return Result<..., Box<dyn std::error::Error>>, which can make error handling less precise. Using a concrete error type or a custom error enum can improve error clarity and make handling errors more manageable.

Consider defining a custom error type for your repository operations. This can encapsulate different kinds of errors (e.g., I/O errors, serialization errors) and provide more context.

Example:

#[derive(Debug)]
pub enum RepositoryError {
    IoError(io::Error),
    SerializationError(serde_json::Error),
    // Add other variants as needed
}

impl std::fmt::Display for RepositoryError {
    // Implement display formatting
}

impl Error for RepositoryError {
    // Implement source error retrieval if necessary
}

Then, adjust your method signatures:

- ) -> Result<Option<Project>, Box<dyn std::error::Error>> {
+ ) -> Result<Option<Project>, RepositoryError> {

And handle errors accordingly within your methods.

Comment on lines +90 to +101

pub struct ProjectLocalRepository {
client: Arc<LocalClient>,
}

impl ProjectLocalRepository {
pub async fn new(base_path: PathBuf) -> io::Result<Self> {
Ok(Self {
client: Arc::new(LocalClient::new(base_path).await?),
})
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider Implementing Clone for ProjectLocalRepository

If instances of ProjectLocalRepository need to be cloned, especially when shared across different parts of your application, consider deriving the Clone trait. This is particularly useful if LocalClient implements Clone.

Apply this diff to derive Clone:

+#[derive(Clone)]
 pub struct ProjectLocalRepository {
     client: Arc<LocalClient>,
 }

This change allows for easier duplication of the repository instance when needed.

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
pub struct ProjectLocalRepository {
client: Arc<LocalClient>,
}
impl ProjectLocalRepository {
pub async fn new(base_path: PathBuf) -> io::Result<Self> {
Ok(Self {
client: Arc::new(LocalClient::new(base_path).await?),
})
}
}
#[derive(Clone)]
pub struct ProjectLocalRepository {
client: Arc<LocalClient>,
}
impl ProjectLocalRepository {
pub async fn new(base_path: PathBuf) -> io::Result<Self> {
Ok(Self {
client: Arc::new(LocalClient::new(base_path).await?),
})
}
}

Comment on lines +118 to +156
#[async_trait]
impl ProjectSnapshotRepository for ProjectLocalRepository {
async fn create_snapshot(
&self,
snapshot: ProjectSnapshot,
) -> Result<(), Box<dyn std::error::Error>> {
let path = format!("snapshots/{}", snapshot.metadata.id);
self.client.upload(path, &snapshot).await?;

// Update latest snapshot
let latest_path = format!("latest_snapshots/{}", snapshot.metadata.project_id);
self.client.upload(latest_path, &snapshot).await?;

Ok(())
}

async fn get_latest_snapshot(
&self,
project_id: &str,
) -> Result<Option<ProjectSnapshot>, Box<dyn std::error::Error>> {
let path = format!("latest_snapshots/{}", project_id);
match self.client.download::<ProjectSnapshot>(path).await {
Ok(snapshot) => Ok(Some(snapshot)),
Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(None),
Err(e) => Err(Box::new(e)),
}
}

async fn get_latest_snapshot_state(
&self,
project_id: &str,
) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
let path = format!("latest_snapshots/{}", project_id);
match self.client.download::<ProjectSnapshot>(path).await {
Ok(snapshot) => Ok(serde_json::to_vec(&snapshot)?),
Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(vec![]),
Err(e) => Err(Box::new(e)),
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent Handling of Non-existent Snapshots

In the get_latest_snapshot_state method, when a snapshot is not found, you're returning an empty Vec<u8> (Ok(vec![])), whereas in get_latest_snapshot, you return Ok(None) for the same scenario. This inconsistency can lead to confusion and bugs, as callers might interpret an empty vector differently than a None value.

Consider modifying get_latest_snapshot_state to return Result<Option<Vec<u8>>, Box<dyn std::error::Error>> and returning Ok(None) when the snapshot is not found. This aligns the behavior with get_latest_snapshot and provides a clear indication of the absence of data.

Apply this diff to adjust the method signature and return value:

-    async fn get_latest_snapshot_state(
+    async fn get_latest_snapshot_state(
         &self,
         project_id: &str,
-    ) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
+    ) -> Result<Option<Vec<u8>>, Box<dyn std::error::Error>> {
         let path = format!("latest_snapshots/{}", project_id);
         match self.client.download::<ProjectSnapshot>(path).await {
-            Ok(snapshot) => Ok(serde_json::to_vec(&snapshot)?),
-            Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(vec![]),
+            Ok(snapshot) => Ok(Some(serde_json::to_vec(&snapshot)?)),
+            Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(None),
             Err(e) => Err(Box::new(e)),
         }
     }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
#[async_trait]
impl ProjectSnapshotRepository for ProjectLocalRepository {
async fn create_snapshot(
&self,
snapshot: ProjectSnapshot,
) -> Result<(), Box<dyn std::error::Error>> {
let path = format!("snapshots/{}", snapshot.metadata.id);
self.client.upload(path, &snapshot).await?;
// Update latest snapshot
let latest_path = format!("latest_snapshots/{}", snapshot.metadata.project_id);
self.client.upload(latest_path, &snapshot).await?;
Ok(())
}
async fn get_latest_snapshot(
&self,
project_id: &str,
) -> Result<Option<ProjectSnapshot>, Box<dyn std::error::Error>> {
let path = format!("latest_snapshots/{}", project_id);
match self.client.download::<ProjectSnapshot>(path).await {
Ok(snapshot) => Ok(Some(snapshot)),
Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(None),
Err(e) => Err(Box::new(e)),
}
}
async fn get_latest_snapshot_state(
&self,
project_id: &str,
) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
let path = format!("latest_snapshots/{}", project_id);
match self.client.download::<ProjectSnapshot>(path).await {
Ok(snapshot) => Ok(serde_json::to_vec(&snapshot)?),
Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(vec![]),
Err(e) => Err(Box::new(e)),
}
}
#[async_trait]
impl ProjectSnapshotRepository for ProjectLocalRepository {
async fn create_snapshot(
&self,
snapshot: ProjectSnapshot,
) -> Result<(), Box<dyn std::error::Error>> {
let path = format!("snapshots/{}", snapshot.metadata.id);
self.client.upload(path, &snapshot).await?;
// Update latest snapshot
let latest_path = format!("latest_snapshots/{}", snapshot.metadata.project_id);
self.client.upload(latest_path, &snapshot).await?;
Ok(())
}
async fn get_latest_snapshot(
&self,
project_id: &str,
) -> Result<Option<ProjectSnapshot>, Box<dyn std::error::Error>> {
let path = format!("latest_snapshots/{}", project_id);
match self.client.download::<ProjectSnapshot>(path).await {
Ok(snapshot) => Ok(Some(snapshot)),
Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(None),
Err(e) => Err(Box::new(e)),
}
}
async fn get_latest_snapshot_state(
&self,
project_id: &str,
) -> Result<Option<Vec<u8>>, Box<dyn std::error::Error>> {
let path = format!("latest_snapshots/{}", project_id);
match self.client.download::<ProjectSnapshot>(path).await {
Ok(snapshot) => Ok(Some(serde_json::to_vec(&snapshot)?)),
Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(None),
Err(e) => Err(Box::new(e)),
}
}

@kasugamirai kasugamirai force-pushed the feat/local-storage branch 2 times, most recently from 409536a to 3033b4c Compare September 17, 2024 19:21
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

Outside diff range and nitpick comments (2)
websocket/crates/infra/src/persistence/project_repository.rs (2)

109-114: Add logging for error cases

In methods like get_project and get_latest_snapshot, when an error occurs (other than NotFound), the error is returned but not logged. Adding logging statements in the error handling branches can aid in debugging and provide valuable information during runtime.

Also applies to: 138-143


229-251: Enhance test utility by externalizing create_test_snapshot function

The create_test_snapshot function is currently defined within the test module. If this function is or will be used across multiple test files, consider moving it to a common test utilities module to promote code reuse and improve maintainability.

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 9c78d81 and 3033b4c.

Files ignored due to path filters (1)
  • websocket/Cargo.lock is excluded by !**/*.lock, !**/*.lock
Files selected for processing (6)
  • websocket/Cargo.toml (2 hunks)
  • websocket/crates/infra/Cargo.toml (1 hunks)
  • websocket/crates/infra/src/persistence/local_storage/local_client.rs (1 hunks)
  • websocket/crates/infra/src/persistence/local_storage/mod.rs (1 hunks)
  • websocket/crates/infra/src/persistence/mod.rs (1 hunks)
  • websocket/crates/infra/src/persistence/project_repository.rs (2 hunks)
Files skipped from review due to trivial changes (4)
  • websocket/Cargo.toml
  • websocket/crates/infra/Cargo.toml
  • websocket/crates/infra/src/persistence/local_storage/local_client.rs
  • websocket/crates/infra/src/persistence/mod.rs
Files skipped from review as they are similar to previous changes (1)
  • websocket/crates/infra/src/persistence/local_storage/mod.rs

Comment on lines +96 to +101
pub async fn new(base_path: PathBuf) -> io::Result<Self> {
Ok(Self {
client: Arc::new(LocalClient::new(base_path).await?),
})
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider making ProjectLocalRepository::new synchronous

The new method is currently asynchronous due to the call to LocalClient::new(base_path).await?. If possible, consider making new synchronous to align with Rust conventions, as constructors are typically synchronous. This can simplify the API and usage of ProjectLocalRepository.

Comment on lines +124 to +130
let path = format!("snapshots/{}", snapshot.metadata.id);
self.client.upload(path, &snapshot).await?;

// Update latest snapshot
let latest_path = format!("latest_snapshots/{}", snapshot.metadata.project_id);
self.client.upload(latest_path, &snapshot).await?;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure atomicity when updating snapshots

In the create_snapshot method, if the first upload succeeds but the second one fails, the system might end up in an inconsistent state where the snapshot is stored but the latest snapshot reference isn't updated. Consider implementing a transactional mechanism or error handling strategy to ensure both operations succeed together or handle failures appropriately.

@kasugamirai kasugamirai requested a review from pyshx September 23, 2024 21:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants