global: snapshot

This commit is contained in:
nym21
2026-02-13 13:54:09 +01:00
parent b779edc0d6
commit 80b2c636b0
53 changed files with 1819 additions and 1184 deletions

View File

@@ -91,8 +91,8 @@ impl AddressRoutes for ApiRouter<AppState> {
Path(path): Path<AddressParam>,
State(state): State<AppState>
| {
// Mempool txs for an address - use MaxAge since it's volatile
state.cached_json(&headers, CacheStrategy::MaxAge(5), move |q| q.address_mempool_txids(path.address)).await
let hash = state.sync(|q| q.address_mempool_hash(&path.address));
state.cached_json(&headers, CacheStrategy::MempoolHash(hash), move |q| q.address_mempool_txids(path.address)).await
}, |op| op
.id("get_address_mempool_txs")
.addresses_tag()

View File

@@ -1,8 +1,8 @@
use aide::axum::{ApiRouter, routing::get_with};
use axum::{extract::State, http::HeaderMap, response::Redirect, routing::get};
use brk_types::{MempoolBlock, MempoolInfo, RecommendedFees, Txid};
use brk_types::{Dollars, MempoolBlock, MempoolInfo, RecommendedFees, Txid};
use crate::{CacheStrategy, extended::TransformResponseExtended};
use crate::extended::TransformResponseExtended;
use super::AppState;
@@ -18,7 +18,7 @@ impl MempoolRoutes for ApiRouter<AppState> {
"/api/mempool/info",
get_with(
async |headers: HeaderMap, State(state): State<AppState>| {
state.cached_json(&headers, CacheStrategy::MaxAge(5), |q| q.mempool_info()).await
state.cached_json(&headers, state.mempool_cache(), |q| q.mempool_info()).await
},
|op| {
op.id("get_mempool")
@@ -34,7 +34,7 @@ impl MempoolRoutes for ApiRouter<AppState> {
"/api/mempool/txids",
get_with(
async |headers: HeaderMap, State(state): State<AppState>| {
state.cached_json(&headers, CacheStrategy::MaxAge(5), |q| q.mempool_txids()).await
state.cached_json(&headers, state.mempool_cache(), |q| q.mempool_txids()).await
},
|op| {
op.id("get_mempool_txids")
@@ -50,7 +50,7 @@ impl MempoolRoutes for ApiRouter<AppState> {
"/api/v1/fees/recommended",
get_with(
async |headers: HeaderMap, State(state): State<AppState>| {
state.cached_json(&headers, CacheStrategy::MaxAge(3), |q| q.recommended_fees()).await
state.cached_json(&headers, state.mempool_cache(), |q| q.recommended_fees()).await
},
|op| {
op.id("get_recommended_fees")
@@ -67,7 +67,7 @@ impl MempoolRoutes for ApiRouter<AppState> {
get_with(
async |headers: HeaderMap, State(state): State<AppState>| {
state
.cached_json(&headers, CacheStrategy::MaxAge(5), |q| q.live_price())
.cached_json(&headers, state.mempool_cache(), |q| q.live_price())
.await
},
|op| {
@@ -75,11 +75,11 @@ impl MempoolRoutes for ApiRouter<AppState> {
.mempool_tag()
.summary("Live BTC/USD price")
.description(
"Returns the current BTC/USD price in cents, derived from \
"Returns the current BTC/USD price in dollars, derived from \
on-chain round-dollar output patterns in the last 12 blocks \
plus mempool.",
)
.ok_response::<u64>()
.ok_response::<Dollars>()
.server_error()
},
),
@@ -88,7 +88,7 @@ impl MempoolRoutes for ApiRouter<AppState> {
"/api/v1/fees/mempool-blocks",
get_with(
async |headers: HeaderMap, State(state): State<AppState>| {
state.cached_json(&headers, CacheStrategy::MaxAge(5), |q| q.mempool_blocks()).await
state.cached_json(&headers, state.mempool_cache(), |q| q.mempool_blocks()).await
},
|op| {
op.id("get_mempool_blocks")

View File

@@ -12,9 +12,9 @@ pub enum CacheStrategy {
/// Etag = VERSION only, Cache-Control: must-revalidate
Static,
/// Volatile data (mempool) - no etag, just max-age
/// Cache-Control: max-age={seconds}
MaxAge(u64),
/// Mempool data - etag from next projected block hash + short max-age
/// Etag = VERSION-m{hash:x}, Cache-Control: max-age=1, must-revalidate
MempoolHash(u64),
}
/// Resolved cache parameters
@@ -50,9 +50,9 @@ impl CacheParams {
etag: Some(VERSION.to_string()),
cache_control: "public, max-age=1, must-revalidate".into(),
},
MaxAge(secs) => Self {
etag: None,
cache_control: format!("public, max-age={secs}"),
MempoolHash(hash) => Self {
etag: Some(format!("{VERSION}-m{hash:x}")),
cache_control: "public, max-age=1, must-revalidate".into(),
},
}
}

View File

@@ -30,6 +30,11 @@ pub struct AppState {
}
impl AppState {
pub fn mempool_cache(&self) -> CacheStrategy {
let hash = self.sync(|q| q.mempool().map(|m| m.next_block_hash()).unwrap_or(0));
CacheStrategy::MempoolHash(hash)
}
/// JSON response with caching
pub async fn cached_json<T, F>(
&self,