Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor(node): dedupe client plumbing and tighten resolver visibility
Simplify pass findings, all behavior-preserving: share the git
subprocess tail between the plain and network runners, drop the
double OnceLock over the operator client, box the resolve iterator
directly, sort the pin list in place, and unexport the resolver
internals nothing outside the module calls. Two comments that
restated the code came out, and a comment that named the old
GITLAWB_IPFS_API spelling now says TWIGPINE_IPFS_API.
  • Loading branch information
beardthelion committed Nov 18, 2026
commit 95f401e8398d10a766f8ef87f148fddb16736fcc
5 changes: 2 additions & 3 deletions crates/twigpine-node/src/ipfs_pin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1580,11 +1580,10 @@ pub(crate) const PIN_READ_FLOOR: Duration = Duration::from_millis(1100);
/// operator client from `crate::operator_http_client`, not the shared peer client:
/// the Kubo endpoint is operator-configured and may be a hostname that resolves
/// to a loopback or private address, which the shared client refuses. Both sinks
/// only ever send to the configured `GITLAWB_IPFS_API` URL, so no remote party
/// only ever send to the configured `TWIGPINE_IPFS_API` URL, so no remote party
/// picks a destination here. Keep it that way.
fn http_client() -> &'static reqwest::Client {
static CLIENT: std::sync::OnceLock<&'static reqwest::Client> = std::sync::OnceLock::new();
CLIENT.get_or_init(crate::operator_http_client)
crate::operator_http_client()
}

/// Pin a single git object to the local IPFS/Kubo node.
Expand Down
7 changes: 5 additions & 2 deletions crates/twigpine-node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1844,7 +1844,7 @@ fn build_operator_http_client() -> reqwest::Result<reqwest::Client> {
.build()
}

/// Process-wide operator client, built once. See `build_operator_http_client`.
/// Process-wide operator client. See `build_operator_http_client`.
pub(crate) fn operator_http_client() -> &'static reqwest::Client {
static CLIENT: std::sync::OnceLock<reqwest::Client> = std::sync::OnceLock::new();
CLIENT
Expand Down Expand Up @@ -2547,7 +2547,10 @@ mod gossip_ssrf_tests {
let proxy = format!("http://127.0.0.1:{proxy_port}");
run_isolated(
"gossip_ssrf_tests::shared_client_ignores_env_proxy",
&[("HTTP_PROXY", proxy.as_str()), ("ALL_PROXY", proxy.as_str())],
&[
("HTTP_PROXY", proxy.as_str()),
("ALL_PROXY", proxy.as_str()),
],
);
assert_eq!(
proxy_hits.load(Ordering::SeqCst),
Expand Down
18 changes: 3 additions & 15 deletions crates/twigpine-node/src/resolver_policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const MAX_ANSWERS: usize = 32;
type LookupFuture = Pin<Box<dyn Future<Output = std::io::Result<Vec<IpAddr>>> + Send>>;
type LookupFn = dyn Fn(String) -> LookupFuture + Send + Sync;

/// Why an answer was refused. Each variant has a fixed log reason.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Denial {
NonPublic,
Expand All @@ -36,7 +35,7 @@ pub(crate) enum Denial {
}

impl Denial {
pub(crate) fn reason(self) -> &'static str {
fn reason(self) -> &'static str {
match self {
Denial::NonPublic => "non-public answer",
Denial::Empty => "empty answer",
Expand All @@ -55,9 +54,7 @@ impl std::fmt::Display for Denial {

impl std::error::Error for Denial {}

/// Deduplicate `answers` and accept them only when there are 1 to `max`
/// distinct addresses and every one is public.
pub(crate) fn check_answers(answers: &[IpAddr], max: usize) -> Result<Vec<IpAddr>, Denial> {
fn check_answers(answers: &[IpAddr], max: usize) -> Result<Vec<IpAddr>, Denial> {
let mut seen = HashSet::new();
let distinct: Vec<IpAddr> = answers
.iter()
Expand Down Expand Up @@ -95,7 +92,6 @@ impl ResolverPolicy {
}
}

/// The system resolver under the production deadline and answer cap.
pub(crate) fn production() -> Self {
Self::new(
|host| {
Expand All @@ -109,8 +105,6 @@ impl ResolverPolicy {
)
}

/// Look `host` up and return the checked addresses, or the reason for
/// refusal. Logs each refusal once.
pub(crate) async fn resolve_host(&self, host: &str) -> Result<Vec<IpAddr>, Denial> {
let result =
match tokio::time::timeout(self.deadline, (self.lookup)(host.to_string())).await {
Expand All @@ -131,13 +125,7 @@ impl Resolve for ResolverPolicy {
Box::pin(async move {
let addrs = this.resolve_host(name.as_str()).await?;
// reqwest replaces the port with the URL's port.
let addrs: Addrs = Box::new(
addrs
.into_iter()
.map(|ip| SocketAddr::new(ip, 0))
.collect::<Vec<_>>()
.into_iter(),
);
let addrs: Addrs = Box::new(addrs.into_iter().map(|ip| SocketAddr::new(ip, 0)));
Ok(addrs)
})
}
Expand Down
30 changes: 13 additions & 17 deletions crates/twigpine-node/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ fn sync_lag_from_origin_timestamp(
(lag <= MAX_SYNC_LAG_OBSERVATION).then_some(lag)
}

/// Where `process_batch` gets the git handle for a peer row.
enum GitSource {
Hardened {
resolver: crate::resolver_policy::ResolverPolicy,
Expand Down Expand Up @@ -462,7 +461,8 @@ impl NetworkGit {
.await
.map_err(|d| anyhow::anyhow!("peer host {host} refused: {d}"))?;
// Same entry format as the upstream mirror worker's pin.
let addrs: std::collections::BTreeSet<std::net::IpAddr> = addrs.into_iter().collect();
let mut addrs = addrs;
addrs.sort_unstable();
let list = addrs
.iter()
.map(|ip| match ip {
Expand Down Expand Up @@ -986,8 +986,8 @@ async fn replicate_encrypted_blobs(
}

/// Run a git subprocess, returning an error with stderr on non-zero exit.
async fn git_run(args: &[&str]) -> anyhow::Result<()> {
let out = tokio::process::Command::new("git")
async fn git_output(mut cmd: tokio::process::Command, args: &[&str]) -> anyhow::Result<()> {
let out = cmd
.args(args)
.output()
.await
Expand All @@ -999,19 +999,12 @@ async fn git_run(args: &[&str]) -> anyhow::Result<()> {
Ok(())
}

/// Run a git subprocess that contacts the peer, through `net`.
async fn git_run(args: &[&str]) -> anyhow::Result<()> {
git_output(tokio::process::Command::new("git"), args).await
}

async fn git_run_net(net: &NetworkGit, args: &[&str]) -> anyhow::Result<()> {
let out = net
.command()
.args(args)
.output()
.await
.map_err(|e| anyhow::anyhow!("git failed to spawn: {e}"))?;
if !out.status.success() {
let stderr = String::from_utf8_lossy(&out.stderr);
return Err(anyhow::anyhow!("git {args:?} failed: {stderr}"));
}
Ok(())
git_output(net.command(), args).await
}

/// Observe `gitlawb_sync_lag_seconds` for one dequeued row, but only when the
Expand Down Expand Up @@ -3561,7 +3554,10 @@ mod tests {
let proxy = format!("http://127.0.0.1:{proxy_port}");
run_isolated(
"sync::tests::sync_client_ignores_env_proxy",
&[("HTTP_PROXY", proxy.as_str()), ("ALL_PROXY", proxy.as_str())],
&[
("HTTP_PROXY", proxy.as_str()),
("ALL_PROXY", proxy.as_str()),
],
);
assert_eq!(
proxy_hits.load(std::sync::atomic::Ordering::SeqCst),
Expand Down