Skip to content

refactor: use ArcSwap and remove Mutex#45

Open
timtorChen wants to merge 4 commits intomainfrom
use-arcswap-remove-lock
Open

refactor: use ArcSwap and remove Mutex#45
timtorChen wants to merge 4 commits intomainfrom
use-arcswap-remove-lock

Conversation

@timtorChen
Copy link
Owner

@timtorChen timtorChen commented Feb 18, 2026

Related issue: #44

@timtorChen timtorChen changed the title refactor: Add ArcSwap and remove Mutex refactor: use ArcSwap and remove Mutex Feb 18, 2026
@gemini-code-assist
Copy link

Summary of Changes

Hello @timtorChen, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refactors the RusticCollector to enhance its concurrency model by replacing the std::sync::Mutex with the arc-swap crate. This change aims to provide more efficient and lock-free access to the repository and snapshot data, improving performance and reducing the complexity associated with mutex-based synchronization in an asynchronous context.

Highlights

  • Concurrency Refactoring: Replaced std::sync::Mutex with arc-swap for managing shared state within the RusticCollector, improving concurrency and reducing potential deadlocks.
  • Dependency Update: Updated the arc-swap crate to version 1.8.2 and added it as a direct dependency to Cargo.toml.
  • State Management Simplification: Removed the internal State struct, directly managing the repository and snapshots using ArcSwap within the RusticCollector.
  • Asynchronous Data Handling: Adapted data update and repository initialization logic to leverage ArcSwap's atomic reference counting for efficient, lock-free access.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • Cargo.lock
    • Updated arc-swap dependency from 1.7.1 to 1.8.2.
    • Added rustversion as a dependency for arc-swap.
    • Added arc-swap as a direct dependency for rustic-exporter.
  • Cargo.toml
    • Added arc-swap = "1.8.2" as a new dependency.
  • src/collector.rs
    • Imported arc_swap::ArcSwap.
    • Removed the State struct definition.
    • Modified RusticCollector to use ArcSwap<Option<Repository>> for repository and ArcSwap<Vec<SnapshotFile>> for snapshots, replacing the Arc<Mutex<State>>.
    • Updated the new function to return Arc<Self> and initialize ArcSwap instances.
    • Refactored set_repository to store the Repository in ArcSwap.
    • Renamed update_data to update_snpashot and updated its implementation to use ArcSwap for snapshot updates.
    • Adjusted the Collector::encode method to retrieve repository and snapshot data directly from ArcSwap instances.
Activity
  • No specific activity (comments, reviews, progress updates) has been recorded for this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@timtorChen timtorChen added enhancement New feature or request bug Something isn't working labels Feb 18, 2026
@codecov
Copy link

codecov bot commented Feb 18, 2026

Codecov Report

❌ Patch coverage is 0% with 35 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/collector.rs 0.00% 35 Missing ⚠️
Files with missing lines Coverage Δ
src/collector.rs 0.00% <0.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the collector to use ArcSwap instead of a Mutex for managing shared state, which is a great improvement for concurrent access. The implementation is mostly correct, but I've found a critical performance regression in the encode method that should be addressed. I've also pointed out a minor typo. Please see my detailed comments.

@timtorChen
Copy link
Owner Author

/gemini review

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the RusticCollector to use arc-swap instead of a Mutex for managing the shared state of repository and snapshots. This is a great improvement as it allows for lock-free reads, which should improve performance, especially under contention. The implementation is well-done. I've identified a critical issue that will prevent compilation, and a minor typo.

impl RusticCollector {
pub fn new(backup: Backup, interval: u64) -> Self {
let collector = Self {
pub fn new(backup: Backup, interval: u64) -> Arc<Self> {

Choose a reason for hiding this comment

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

critical

Changing the return type of new to Arc<Self> is a good move for sharing the collector. However, this breaks its usage in main.rs where it's registered with prometheus-client. Registry::register_collector expects a Box<dyn Collector>, but Box<Arc<RusticCollector>> cannot be used as such because Arc<RusticCollector> doesn't implement Collector.

To fix this, you can implement Collector for Arc<RusticCollector>. Please add the following to this file:

impl Collector for Arc<RusticCollector> {
    fn encode(&self, encoder: DescriptorEncoder) -> Result<(), std::fmt::Error> {
        self.as_ref().encode(encoder)
    }
}

This will allow Box::new(arc_collector) to be treated as a Box<dyn Collector>.

Copy link
Owner Author

@timtorChen timtorChen Feb 19, 2026

Choose a reason for hiding this comment

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

The type looks wrong, but it's common, and works due to Rust’s deref coercion.
Arc<RusticCollector> derefs to &RusticCollector, so prometheus-client can call encode on it.

doc: https://doc.rust-lang.org/book/ch15-02-deref.html

@timtorChen timtorChen force-pushed the use-arcswap-remove-lock branch 2 times, most recently from 614135e to e38f758 Compare February 18, 2026 19:18
@timtorChen timtorChen marked this pull request as ready for review February 19, 2026 06:59
@timtorChen timtorChen force-pushed the use-arcswap-remove-lock branch from 933cb5b to 44dd761 Compare February 19, 2026 07:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments