Skip to content

Commit 5099c25

Browse files
lorenzleutgebFintanH
authored andcommitted
node/debug: Use derived serializers
The construction of the debug object is unwieldy, and error prone (for example, renamed struct members have to be manually renamed in the serialization code, see "refs" vs. "refsAt"). Use derived serializers where possible to make this easier to maintain.
1 parent a1fa380 commit 5099c25

File tree

3 files changed

+12
-34
lines changed

3 files changed

+12
-34
lines changed

crates/radicle-node/src/runtime/handle.rs

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -350,35 +350,10 @@ impl radicle::node::Handle for Handle {
350350
fn debug(&self) -> Result<serde_json::Value, Self::Error> {
351351
let (sender, receiver) = chan::bounded(1);
352352
let query: Arc<QueryState> = Arc::new(move |state| {
353-
let fetcher_state = state.fetching();
354353
let debug = serde_json::json!({
355354
"outboxSize": state.outbox().len(),
356-
"fetching": fetcher_state.active_fetches()
357-
.iter()
358-
.map(|(rid, active)| {
359-
json!({
360-
"rid": rid,
361-
"from": active.from(),
362-
"refsAt": active.refs(),
363-
})
364-
}).collect::<Vec<_>>(),
365-
"queue": fetcher_state.queued_fetches().iter().map(|(node, queue)| {
366-
json!({
367-
"nid": node,
368-
"queue": queue.iter().map(|fetch| {
369-
json!({
370-
"rid": fetch.rid,
371-
"refsAt": fetch.refs,
372-
})
373-
}).collect::<Vec<_>>()
374-
})
375-
}).collect::<Vec<_>>(),
376-
"rateLimiter": state.limiter().buckets.iter().map(|(host, bucket)| {
377-
json!({
378-
"host": host.to_string(),
379-
"bucket": bucket
380-
})
381-
}).collect::<Vec<_>>(),
355+
"fetching": state.fetching(),
356+
"rateLimiter": state.limiter(),
382357
"events": json!({
383358
"subscribers": state.emitter().subscriptions(),
384359
"pending": state.emitter().pending(),

crates/radicle-protocol/src/fetcher/state.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub mod event;
1010

1111
pub use command::Command;
1212
pub use event::Event;
13+
use serde::Serialize;
1314

1415
use std::collections::{BTreeMap, VecDeque};
1516
use std::num::NonZeroUsize;
@@ -42,7 +43,7 @@ pub const MAX_CONCURRENCY: NonZeroUsize = NonZeroUsize::MIN;
4243
/// of fetches can happen with it concurrently. This does not guarantee that the
4344
/// node will actually allow this node to fetch from it – since it will maintain
4445
/// its own capacity for connections and load.
45-
#[derive(Clone, Debug, PartialEq, Eq)]
46+
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
4647
pub struct FetcherState {
4748
/// The active fetches that are occurring, ensuring only one fetch per repository.
4849
active: BTreeMap<RepoId, ActiveFetch>,
@@ -235,7 +236,7 @@ impl FetcherState {
235236
}
236237

237238
/// Configuration for the [`FetcherState`].
238-
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
239+
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize)]
239240
pub struct Config {
240241
/// Maximum number of concurrent fetches per peer connection.
241242
maximum_concurrency: NonZeroUsize,
@@ -271,7 +272,7 @@ impl Default for Config {
271272
}
272273

273274
/// An active fetch represents a repository being fetched by a particular node.
274-
#[derive(Clone, Debug, PartialEq, Eq)]
275+
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
275276
pub struct ActiveFetch {
276277
pub from: NodeId,
277278
pub refs: RefsToFetch,
@@ -290,7 +291,7 @@ impl ActiveFetch {
290291
}
291292

292293
/// A fetch that is waiting to be processed, in the fetch queue.
293-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
294+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize)]
294295
pub struct QueuedFetch {
295296
/// The repository that will be fetched.
296297
pub rid: RepoId,
@@ -304,14 +305,15 @@ pub struct QueuedFetch {
304305
///
305306
/// It ensures that the queue contains unique items for fetching, and does not
306307
/// exceed the provided maximum capacity.
307-
#[derive(Clone, Debug, PartialEq, Eq)]
308+
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
308309
pub struct Queue {
309310
queue: VecDeque<QueuedFetch>,
310311
max_queue_size: MaxQueueSize,
311312
}
312313

313314
/// The maximum number of fetches that can be queued for a single node.
314-
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
315+
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
316+
#[serde(transparent)]
315317
pub struct MaxQueueSize(usize);
316318

317319
impl MaxQueueSize {

crates/radicle-protocol/src/service/limiter.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ use std::collections::{HashMap, HashSet};
22

33
use localtime::LocalTime;
44
use radicle::node::{address, config, HostName, NodeId};
5+
use serde::Serialize;
56

67
/// Peer rate limiter.
78
///
89
/// Uses a token bucket algorithm, where each address starts with a certain amount of tokens,
910
/// and every request from that address consumes one token. Tokens refill at a predefined
1011
/// rate. This mechanism allows for consistent request rates with potential bursts up to the
1112
/// bucket's capacity.
12-
#[derive(Debug, Default)]
13+
#[derive(Debug, Default, Serialize)]
1314
pub struct RateLimiter {
1415
pub buckets: HashMap<HostName, TokenBucket>,
1516
pub bypass: HashSet<NodeId>,

0 commit comments

Comments
 (0)