Skip to content

Commit f1e6ae0

Browse files
perf: serve API responses strictly from cache, never from RPC
The cache read paths did a TTL check and, when stale, performed the full Dash Core load synchronously inside the request. Combined with the background refresher sleeping before loading (so last_update drifts later each cycle) and with failures latching (last_update is only set on success), requests took the cold path far more often than once per TTL, paying 25+ sequential cross-instance RPCs. There was also no single-flight, so concurrent requests each launched an independent load. get_quorums, get_previous_quorums and get_masternodes are now pure cache reads. The background refresh task is the only writer, and startup populates via an explicit refresh() rather than by relying on a read to trigger a load. An unpopulated cache now returns an error instead of blocking on RPC, so a request can no longer be slower than a cache read. Verified: with Dash Core unreachable, all three endpoints respond in ~0.4ms and the server still binds and serves.
1 parent 9eb45ee commit f1e6ae0

3 files changed

Lines changed: 60 additions & 101 deletions

File tree

‎src/main.rs‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
3434

3535
// Populate quorum cache on startup
3636
println!("Loading initial quorums...");
37-
match quorum_cache.get_quorums().await {
38-
Ok(quorums) => {
39-
println!("Successfully loaded {} quorums into cache", quorums.len());
40-
}
37+
match quorum_cache.refresh().await {
38+
Ok(()) => match quorum_cache.get_quorums().await {
39+
Ok(quorums) => println!("Successfully loaded {} quorums into cache", quorums.len()),
40+
Err(e) => eprintln!("Warning: quorum cache unreadable after refresh: {}", e),
41+
},
4142
Err(e) => {
42-
eprintln!("Warning: Failed to load initial quorums: {}. Cache will populate on first request.", e);
43+
eprintln!("Warning: Failed to load initial quorums: {}. Endpoints will error until the background refresh succeeds.", e);
4344
}
4445
}
4546

@@ -51,12 +52,15 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
5152

5253
// Populate masternode cache on startup
5354
println!("Loading initial masternode list...");
54-
match masternode_cache.get_masternodes().await {
55-
Ok(masternodes) => {
56-
println!("Successfully loaded {} masternodes into cache", masternodes.len());
57-
}
55+
match masternode_cache.refresh().await {
56+
Ok(()) => match masternode_cache.get_masternodes().await {
57+
Ok(masternodes) => {
58+
println!("Successfully loaded {} masternodes into cache", masternodes.len())
59+
}
60+
Err(e) => eprintln!("Warning: masternode cache unreadable after refresh: {}", e),
61+
},
5862
Err(e) => {
59-
eprintln!("Warning: Failed to load initial masternodes: {}. Cache will populate on first request.", e);
63+
eprintln!("Warning: Failed to load initial masternodes: {}. /masternodes will error until the background refresh succeeds.", e);
6064
}
6165
}
6266

‎src/masternode_cache.rs‎

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,37 +24,25 @@ impl MasternodeCache {
2424
}
2525
}
2626

27+
/// Read the cached masternode list.
28+
///
29+
/// This never contacts Dash Core or probes any masternode: the background
30+
/// refresh task is the only writer.
2731
pub async fn get_masternodes(
2832
&self,
2933
) -> Result<EvoMasternodeList, Box<dyn std::error::Error + Send + Sync>> {
30-
// Check if we need to update the cache
31-
let should_update = {
32-
let last_update_guard = self.last_update.lock().await;
33-
match *last_update_guard {
34-
None => true,
35-
Some(last) => last.elapsed() >= self.update_interval,
36-
}
37-
};
38-
39-
if should_update {
40-
self.update_cache().await?;
41-
}
34+
let data = self.data.read().map_err(|_| "Failed to read cache")?;
35+
Ok(data
36+
.as_ref()
37+
.ok_or("Masternode cache is not populated yet")?
38+
.clone())
39+
}
4240

43-
// Return the cached data
44-
let cached_data = {
45-
let data = self.data.read().map_err(|_| "Failed to read cache")?;
46-
data.clone()
47-
};
48-
49-
match cached_data {
50-
Some(masternodes) => Ok(masternodes),
51-
None => {
52-
// This shouldn't happen as we just updated, but handle it gracefully
53-
self.update_cache().await?;
54-
let data = self.data.read().map_err(|_| "Failed to read cache")?;
55-
Ok(data.as_ref().ok_or("No masternode data available")?.clone())
56-
}
57-
}
41+
/// Refresh the masternode cache from Dash Core.
42+
///
43+
/// Called once at startup and then only by the background refresh task.
44+
pub async fn refresh(&self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
45+
self.update_cache().await
5846
}
5947

6048
async fn update_cache(&self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {

‎src/quorum_cache.rs‎

Lines changed: 31 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -28,79 +28,46 @@ impl QuorumCache {
2828
}
2929
}
3030

31+
/// Read the cached current quorums.
32+
///
33+
/// This never contacts Dash Core: the background refresh task is the only
34+
/// writer. Serving a request must not depend on RPC latency.
3135
pub async fn get_quorums(
3236
&self,
3337
) -> Result<QuorumList, Box<dyn std::error::Error + Send + Sync>> {
34-
let should_update = {
35-
let last_update_guard = self.current_last_update.lock().await;
36-
match *last_update_guard {
37-
None => true,
38-
Some(last) => last.elapsed() >= self.update_interval,
39-
}
40-
};
41-
42-
if should_update {
43-
self.update_current_cache().await?;
44-
}
45-
46-
let cached_data = {
47-
let data = self
48-
.current_quorums
49-
.read()
50-
.map_err(|_| "Failed to read quorum cache")?;
51-
data.clone()
52-
};
53-
54-
match cached_data {
55-
Some(quorums) => Ok(quorums),
56-
None => {
57-
self.update_current_cache().await?;
58-
let data = self
59-
.current_quorums
60-
.read()
61-
.map_err(|_| "Failed to read quorum cache")?;
62-
Ok(data.as_ref().ok_or("No quorum data available")?.clone())
63-
}
64-
}
38+
let data = self
39+
.current_quorums
40+
.read()
41+
.map_err(|_| "Failed to read quorum cache")?;
42+
Ok(data
43+
.as_ref()
44+
.ok_or("Quorum cache is not populated yet")?
45+
.clone())
6546
}
6647

48+
/// Read the cached previous quorums along with the height they were taken at.
49+
///
50+
/// As with [`Self::get_quorums`], this is a pure cache read.
6751
pub async fn get_previous_quorums(
6852
&self,
6953
) -> Result<(u32, QuorumList), Box<dyn std::error::Error + Send + Sync>> {
70-
let should_update = {
71-
let last_update_guard = self.previous_last_update.lock().await;
72-
match *last_update_guard {
73-
None => true,
74-
Some(last) => last.elapsed() >= self.update_interval,
75-
}
76-
};
77-
78-
if should_update {
79-
self.update_previous_cache().await?;
80-
}
81-
82-
let cached_data = {
83-
let data = self
84-
.previous_quorums
85-
.read()
86-
.map_err(|_| "Failed to read previous quorum cache")?;
87-
data.clone()
88-
};
54+
let data = self
55+
.previous_quorums
56+
.read()
57+
.map_err(|_| "Failed to read previous quorum cache")?;
58+
Ok(data
59+
.as_ref()
60+
.ok_or("Previous quorum cache is not populated yet")?
61+
.clone())
62+
}
8963

90-
match cached_data {
91-
Some(quorums) => Ok(quorums),
92-
None => {
93-
self.update_previous_cache().await?;
94-
let data = self
95-
.previous_quorums
96-
.read()
97-
.map_err(|_| "Failed to read previous quorum cache")?;
98-
Ok(data
99-
.as_ref()
100-
.ok_or("No previous quorum data available")?
101-
.clone())
102-
}
103-
}
64+
/// Refresh both quorum caches from Dash Core.
65+
///
66+
/// Called once at startup and then only by the background refresh task.
67+
pub async fn refresh(&self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
68+
self.update_current_cache().await?;
69+
self.update_previous_cache().await?;
70+
Ok(())
10471
}
10572

10673
async fn update_current_cache(&self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {

0 commit comments

Comments
 (0)