Skip to content

Commit

Permalink
don't cache server name lookups indefinitely (girlbossceo#436)
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Volk <[email protected]>
  • Loading branch information
jevolk committed Jul 4, 2024
1 parent eaf1cf3 commit dc18f89
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/service/globals/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ impl Resolve for Hooked {
.read()
.expect("locked for reading")
.get(name.as_str())
.filter(|cached| cached.valid())
.cloned();

if let Some(cached) = cached {
Expand Down
30 changes: 28 additions & 2 deletions src/service/sending/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ use std::{
fmt,
fmt::Debug,
net::{IpAddr, SocketAddr},
time::SystemTime,
};

use hickory_resolver::{error::ResolveError, lookup::SrvLookup};
use ipaddress::IPAddress;
use ruma::{OwnedServerName, ServerName};
use tracing::{debug, error, trace};

use crate::{debug_error, debug_info, debug_warn, services, Error, Result};
use crate::{debug_error, debug_info, debug_warn, services, utils::rand, Error, Result};

/// Wraps either an literal IP address plus port, or a hostname plus complement
/// (colon-plus-port if it was specified).
Expand Down Expand Up @@ -47,12 +48,14 @@ pub(crate) struct ActualDest {
pub struct CachedDest {
pub dest: FedDest,
pub host: String,
pub expire: SystemTime,
}

#[derive(Clone, Debug)]
pub struct CachedOverride {
pub ips: Vec<IpAddr>,
pub port: u16,
pub expire: SystemTime,
}

#[tracing::instrument(skip_all, name = "resolve")]
Expand Down Expand Up @@ -125,6 +128,7 @@ pub async fn resolve_actual_dest(dest: &ServerName, cache: bool) -> Result<Cache
Ok(CachedDest {
dest: actual_dest,
host: host.into_uri_string(),
expire: CachedDest::default_expire(),
})
}

Expand Down Expand Up @@ -286,6 +290,7 @@ async fn query_and_cache_override(overname: &'_ str, hostname: &'_ str, port: u1
CachedOverride {
ips: override_ip.iter().collect(),
port,
expire: CachedOverride::default_expire(),
},
);

Expand Down Expand Up @@ -416,6 +421,7 @@ impl crate::globals::resolver::Resolver {
.read()
.expect("locked for reading")
.get(name)
.filter(|cached| cached.valid())
.cloned()
}

Expand All @@ -431,10 +437,30 @@ impl crate::globals::resolver::Resolver {
self.overrides
.read()
.expect("locked for reading")
.contains_key(name)
.get(name)
.filter(|cached| cached.valid())
.is_some()
}
}

impl CachedDest {
#[inline]
#[must_use]
pub fn valid(&self) -> bool { self.expire > SystemTime::now() }

#[must_use]
pub(crate) fn default_expire() -> SystemTime { rand::timepoint_secs(60 * 60 * 18..60 * 60 * 36) }
}

impl CachedOverride {
#[inline]
#[must_use]
pub fn valid(&self) -> bool { self.expire > SystemTime::now() }

#[must_use]
pub(crate) fn default_expire() -> SystemTime { rand::timepoint_secs(60 * 60 * 6..60 * 60 * 12) }
}

impl FedDest {
fn into_https_string(self) -> String {
match self {
Expand Down
1 change: 1 addition & 0 deletions src/service/sending/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ where
CachedDest {
dest: actual.dest.clone(),
host: actual.host.clone(),
expire: CachedDest::default_expire(),
},
);
}
Expand Down

0 comments on commit dc18f89

Please sign in to comment.