Skip to content

Hide the statistics repositories' read lock and metric collection field #2536

Closed

Description

@josecelano

Subissue of EPIC #1669 (Overhaul: Packages). This is the follow-up recorded in the Out of Scope section of #2517.

Goal

Remove the lock type and the MetricCollection field from the public read API of the five statistics repositories. Callers read metrics through a closure that the repository runs while it holds its read lock, and through a read-only metric_collection() accessor on Metrics. The repositories can then change their locking or storage without a breaking release.

Problem

#2517 made each repository's stats: Arc<RwLock<Metrics>> field private, but the read accessors still return the lock's read guard:

Package Read accessor Return type
udp-core get_stats() RwLockReadGuard<'_, Metrics>
udp-server get_stats() RwLockReadGuard<'_, Metrics>
http-core get_stats() RwLockReadGuard<'_, Metrics>
tracker-core get_metrics() RwLockReadGuard<'_, Metrics>
swarm-coordination-registry get_metrics() RwLockReadGuard<'_, Metrics>
  • tokio::sync::RwLockReadGuard is part of each crate's public API, so replacing the lock would be a breaking change.
  • A caller can hold the guard across an .await, blocking the statistics event listeners that write metrics.
  • Each Metrics type has a pub metric_collection: MetricCollection field, which makes its layout public API too.

None of the five crates is published yet, so this can change without a semver-major bump if it lands before their first publish.

Proposed design

  • Replace get_stats() / get_metrics() with pub async fn read_metrics<R>(&self, read: impl FnOnce(&Metrics) -> R) -> R in all five repositories. It needs no lock type in its signature, gives consistent multi-value reads from one lock acquisition, clones nothing, and the closure cannot await.
  • Make Metrics.metric_collection pub(crate) and add pub fn metric_collection(&self) -> &MetricCollection.

Scope

  • The five repositories and their Metrics types.
  • Callers:
    • rest-api-runtime-adapter, the only production caller outside the crates;
    • the debug logging in the udp-core, udp-server, and http-core event handlers, which must stay lazy so that disabled debug logging takes no lock;
    • the tracker-core repository's own counter reads;
    • tests in other crates.
  • Doctests that pin the private field and show the new reads, and an update to the Make the statistics repositories' metrics lock private #2517 doctests that call the removed accessors.

Out of scope: the write methods, the lock type itself, the Metrics value methods, unifying the repositories, and the stats field name.

Specification

The full specification, with acceptance criteria and the verification plan, is added to docs/issues/open/ by its spec-only PR.

Activity

  1. added
    Code Cleanup / Refactoring
    - Developer -
    on Oct 14, 2026
  2. added 10 commits that reference this issue on Oct 14, 2026
  3. 41 remaining items

  4. added 4 commits that reference this issue on Oct 16, 2026
  5. added a commit that references this issue on Oct 18, 2026
  6. added 10 commits that reference this issue on Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

- Developer -Code Cleanup / Refactoringpackagestask

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions