Skip to content

Commit 65c928a

Browse files
committed
fix: wip
1 parent f87d11d commit 65c928a

File tree

4 files changed

+50
-15
lines changed

4 files changed

+50
-15
lines changed

src/api/tls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use anyhow::{anyhow, Error, Result};
22
use futures_util::stream::{repeat, Stream};
3-
use futures_util::{StreamExt, TryStreamExt, TryFutureExt};
3+
use futures_util::{StreamExt, TryFutureExt, TryStreamExt};
44
use parking_lot::RwLock;
55
use rustls::internal::pemfile::{certs, pkcs8_private_keys};
66
use rustls::{NoClientAuth, ServerConfig};

src/config/listener.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ impl<'de> Deserialize<'de> for ProxyProtocol {
1414
D: Deserializer<'de>,
1515
{
1616
match bool::deserialize(deserializer)? {
17-
true => Ok(ProxyProtocol::Enabled),
18-
false => Ok(ProxyProtocol::Disabled),
17+
true => Ok(Self::Enabled),
18+
false => Ok(Self::Disabled),
1919
}
2020
}
2121
}
2222

2323
impl Default for ProxyProtocol {
2424
fn default() -> Self {
25-
ProxyProtocol::Disabled
25+
Self::Disabled
2626
}
2727
}
2828

src/config/records.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,7 @@ where
2727
{
2828
let mut res = HashMap::new();
2929
while let Some(key) = map.next_key::<&str>()? {
30-
let name = match Name::from_str(key) {
31-
Err(e) => return Err(DeError::custom(e)),
32-
Ok(name) => name,
33-
};
34-
30+
let name = Name::from_str(key).map_err(DeError::custom)?;
3531
let (record_type, record_set) = map.next_value_seed(RecordDataSeed(name))?;
3632

3733
res.insert(record_type, record_set);

src/dns/handler.rs

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use futures_util::future::{Join, Map, Ready};
22
use futures_util::FutureExt;
33
use lazy_static::lazy_static;
4-
use prometheus::{register_histogram_vec, HistogramTimer, HistogramVec};
4+
use prometheus::{register_histogram_vec, Histogram, HistogramTimer, HistogramVec};
55
use std::future::Future;
6+
use std::pin::Pin;
7+
use std::task::{Context, Poll};
68
use tracing::field::Empty;
79
use tracing::instrument::Instrumented;
810
use tracing::{info_span, Instrument, Span};
@@ -24,8 +26,10 @@ type ResponseFutureOutput = <ResponseFuture as Future>::Output;
2426

2527
type MapFn = fn((ResponseFutureOutput, HistogramTimer));
2628

27-
type JoinedFuture = Join<ResponseFuture, Ready<HistogramTimer>>;
28-
type MappedFuture = Map<Instrumented<JoinedFuture>, MapFn>;
29+
type StartTimer = fn(Histogram) -> HistogramTimer;
30+
// change type as join could maybe not start both futures
31+
type JoinedFuture = Join<ResponseFuture, Lazy<Histogram, StartTimer>>;
32+
type MappedFuture = Instrumented<Map<JoinedFuture, MapFn>>;
2933

3034
pub(super) struct TraceRequestHandler {
3135
catalog: Catalog,
@@ -66,18 +70,53 @@ impl RequestHandler for TraceRequestHandler {
6670
let addr = request.src;
6771
let span = info_span!(parent: &self.span, "request", remote.addr = %addr, name = Empty, query_type = Empty);
6872

69-
let timer =
70-
futures_util::future::ready(DNS_REQ_HISTOGRAM.with_label_values(name).start_timer());
73+
let timer = DNS_REQ_HISTOGRAM.with_label_values(name);
74+
let timer = Lazy::new(timer, start_timer);
7175
let handle_request = self.catalog.handle_request(request, response_handle);
7276

7377
futures_util::future::join(handle_request, timer)
74-
.instrument(span)
7578
.map(end_timer)
79+
.instrument(span)
7680
}
7781
}
7882

83+
fn start_timer(timer: Histogram) -> HistogramTimer {
84+
timer.start_timer()
85+
}
86+
7987
fn end_timer((res, timer): (ResponseFutureOutput, HistogramTimer)) {
8088
timer.observe_duration();
8189

8290
res
8391
}
92+
93+
// todo: lazy is not needed should be possible with map and then
94+
struct Lazy<T, F> {
95+
data: Option<T>,
96+
fun: Option<F>,
97+
}
98+
99+
impl<T, F> Lazy<T, F> {
100+
fn new(data: T, fun: F) -> Self {
101+
Lazy {
102+
data: Some(data),
103+
fun: Some(fun),
104+
}
105+
}
106+
}
107+
108+
impl<T, F, R> Future for Lazy<T, F>
109+
where
110+
T: Unpin,
111+
F: FnOnce(T) -> R + Unpin,
112+
{
113+
type Output = R;
114+
115+
fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
116+
let this = self.get_mut();
117+
let fun = this.fun.take().expect("Polled after completion");
118+
let data = this.data.take().expect("Polled after completion");
119+
120+
Poll::Ready(fun(data))
121+
}
122+
}

0 commit comments

Comments
 (0)