Skip to content

Commit fbcb056

Browse files
chore(iroh-dns-server): cleanup some code (#2941)
## Description Just some things I found while reading the code - avoid spawn-blocking - remove unused imports ## Breaking Changes None ## Notes & open questions <!-- Any notes, remarks or open questions you have to make about the PR. --> ## Change checklist - [x] Self-review. - [x] Documentation updates following the [style guide](https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html#appendix-a-full-conventions-text), if relevant. - [x] Tests if relevant. - [x] All breaking changes documented.
1 parent 38bfe5e commit fbcb056

File tree

2 files changed

+12
-25
lines changed

2 files changed

+12
-25
lines changed

iroh-dns-server/src/http/tls.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,8 @@ impl TlsAcceptor {
9090
let cert_path = dir.join(format!("{keyname}.crt"));
9191
let key_path = dir.join(format!("{keyname}.key"));
9292

93-
let (certs, secret_key) = tokio::task::spawn_blocking(move || {
94-
let certs = load_certs(cert_path)?;
95-
let key = load_secret_key(key_path)?;
96-
anyhow::Ok((certs, key))
97-
})
98-
.await??;
93+
let certs = load_certs(cert_path).await?;
94+
let secret_key = load_secret_key(key_path).await?;
9995

10096
let config = config.with_single_cert(certs, secret_key)?;
10197
let config = RustlsConfig::from_config(Arc::new(config));
@@ -136,23 +132,24 @@ impl TlsAcceptor {
136132
}
137133
}
138134

139-
fn load_certs(
135+
async fn load_certs(
140136
filename: impl AsRef<Path>,
141137
) -> Result<Vec<rustls::pki_types::CertificateDer<'static>>> {
142-
let certfile = std::fs::File::open(filename).context("cannot open certificate file")?;
143-
let mut reader = std::io::BufReader::new(certfile);
144-
138+
let certfile = tokio::fs::read(filename)
139+
.await
140+
.context("cannot open certificate file")?;
141+
let mut reader = std::io::Cursor::new(certfile);
145142
let certs: Result<Vec<_>, std::io::Error> = rustls_pemfile::certs(&mut reader).collect();
146143
let certs = certs?;
147144

148145
Ok(certs)
149146
}
150147

151-
fn load_secret_key(
148+
async fn load_secret_key(
152149
filename: impl AsRef<Path>,
153150
) -> Result<rustls::pki_types::PrivateKeyDer<'static>> {
154-
let keyfile = std::fs::File::open(filename.as_ref()).context("cannot open secret key file")?;
155-
let mut reader = std::io::BufReader::new(keyfile);
151+
let keyfile = std::fs::read(filename.as_ref()).context("cannot open secret key file")?;
152+
let mut reader = std::io::Cursor::new(keyfile);
156153

157154
loop {
158155
match rustls_pemfile::read_one(&mut reader).context("cannot parse secret key .pem file")? {

iroh-dns-server/src/main.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
1-
#![allow(unused_imports)]
2-
3-
use std::{
4-
future::Future,
5-
net::{Ipv4Addr, SocketAddr},
6-
path::PathBuf,
7-
};
1+
use std::path::PathBuf;
82

93
use anyhow::Result;
10-
use axum::{routing::get, Router};
114
use clap::Parser;
12-
use futures_lite::FutureExt;
135
use iroh_dns_server::{
146
config::Config, metrics::init_metrics, server::run_with_config_until_ctrl_c,
157
};
16-
use tokio::task::JoinSet;
17-
use tokio_util::sync::CancellationToken;
18-
use tracing::{debug, debug_span, error, error_span, Instrument, Span};
8+
use tracing::debug;
199

2010
#[derive(Parser, Debug)]
2111
struct Cli {

0 commit comments

Comments
 (0)