Skip to content

Commit 212e50f

Browse files
committed
Avoid allocating String in CertificateOrder::cache_key.
Also, prefer first available DNS name.
1 parent ce4a2f6 commit 212e50f

File tree

1 file changed

+35
-9
lines changed

1 file changed

+35
-9
lines changed

src/conf/order.rs

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,22 @@ where
4343
}
4444

4545
/// Generates a stable unique identifier for this order.
46-
pub fn cache_key(&self) -> std::string::String
46+
pub fn cache_key(&self) -> PrintableOrderId<'_, S, A>
4747
where
4848
S: fmt::Display + hash::Hash,
4949
{
50-
if self.identifiers.is_empty() {
51-
return "".into();
52-
}
53-
54-
let name = self.identifiers[0].value();
50+
PrintableOrderId(self)
51+
}
5552

56-
let mut hasher = SipHasher::default();
57-
self.hash(&mut hasher);
53+
/// Attempts to find the first DNS identifier, with fallback to a first identifier of any kind.
54+
pub fn first_name(&self) -> Option<&S> {
55+
let dns = self
56+
.identifiers
57+
.iter()
58+
.find(|x| matches!(x, Identifier::Dns(_)));
5859

59-
std::format!("{name}-{hash:x}", hash = hasher.finish())
60+
dns.or_else(|| self.identifiers.first())
61+
.map(Identifier::value)
6062
}
6163

6264
pub fn to_str_order<NewA>(&self, alloc: NewA) -> CertificateOrder<&str, NewA>
@@ -247,6 +249,30 @@ impl CertificateOrder<ngx_str_t, Pool> {
247249
}
248250
}
249251

252+
/// Unique identifier for the CertificateOrder.
253+
///
254+
/// This identifier should be suitable for logs, file names or cache keys.
255+
pub struct PrintableOrderId<'a, S, A>(&'a CertificateOrder<S, A>)
256+
where
257+
A: ngx::allocator::Allocator;
258+
259+
impl<S, A> fmt::Display for PrintableOrderId<'_, S, A>
260+
where
261+
A: ngx::allocator::Allocator,
262+
S: fmt::Display + hash::Hash,
263+
{
264+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
265+
let Some(name) = self.0.first_name() else {
266+
return Ok(());
267+
};
268+
269+
let mut hasher = SipHasher::default();
270+
self.0.hash(&mut hasher);
271+
272+
write!(f, "{name}-{hash:x}", hash = hasher.finish())
273+
}
274+
}
275+
250276
fn validate_host(pool: &Pool, mut host: ngx_str_t) -> Result<ngx_str_t, Status> {
251277
let mut pool = pool.clone();
252278
let rc = Status(unsafe { nginx_sys::ngx_http_validate_host(&mut host, pool.as_mut(), 1) });

0 commit comments

Comments
 (0)