Skip to content

Commit 2b3196a

Browse files
committed
f make runtime optional at startup
1 parent 4a2014f commit 2b3196a

File tree

1 file changed

+17
-6
lines changed
  • lightning-dns-resolver/src

1 file changed

+17
-6
lines changed

lightning-dns-resolver/src/lib.rs

+17-6
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ where
3434
{
3535
state: Arc<OMResolverState>,
3636
proof_handler: Option<PH>,
37-
runtime_handle: Handle,
37+
runtime_handle: Mutex<Option<Handle>>,
3838
}
3939

4040
const MAX_PENDING_RESPONSES: usize = 1024;
@@ -69,19 +69,20 @@ where
6969
/// requests but some other handler is making proof requests of remote nodes and wants to get
7070
/// results.
7171
pub fn new(resolver: SocketAddr, proof_handler: Option<PH>) -> Self {
72-
Self::with_runtime(resolver, proof_handler, Handle::current())
72+
Self::with_runtime(resolver, proof_handler, Some(Handle::current()))
7373
}
7474

7575
/// Creates a new [`OMDomainResolver`] given the [`SocketAddr`] of a DNS resolver listening on
7676
/// TCP (e.g. 8.8.8.8:53, 1.1.1.1:53 or your local DNS resolver) and a `tokio` runtime
77-
/// [`Handle`] on which futures will be spawned.
77+
/// [`Handle`] on which futures will be spawned. If no runtime is provided, `set_runtime` must
78+
/// be called before any queries will be handled.
7879
///
7980
/// The optional `proof_handler` can be provided to pass proofs coming back to us to the
8081
/// underlying handler. This is useful when this resolver is handling incoming resolution
8182
/// requests but some other handler is making proof requests of remote nodes and wants to get
8283
/// results.
8384
pub fn with_runtime(
84-
resolver: SocketAddr, proof_handler: Option<PH>, runtime_handle: Handle,
85+
resolver: SocketAddr, proof_handler: Option<PH>, runtime_handle: Option<Handle>,
8586
) -> Self {
8687
Self {
8788
state: Arc::new(OMResolverState {
@@ -90,9 +91,14 @@ where
9091
pending_query_count: AtomicUsize::new(0),
9192
}),
9293
proof_handler,
93-
runtime_handle,
94+
runtime_handle: Mutex::new(runtime_handle),
9495
}
9596
}
97+
98+
/// Sets the runtime on which futures will be spawned.
99+
pub fn set_runtime(&self, runtime_handle: Handle) {
100+
*self.runtime_handle.lock().unwrap() = Some(runtime_handle);
101+
}
96102
}
97103

98104
impl<PH: Deref> DNSResolverMessageHandler for OMDomainResolver<PH>
@@ -112,12 +118,17 @@ where
112118
Some(responder) => responder,
113119
None => return None,
114120
};
121+
let runtime = if let Some(runtime) = self.runtime_handle.lock().unwrap().clone() {
122+
runtime
123+
} else {
124+
return None;
125+
};
115126
if self.state.pending_query_count.fetch_add(1, Ordering::Relaxed) > MAX_PENDING_RESPONSES {
116127
self.state.pending_query_count.fetch_sub(1, Ordering::Relaxed);
117128
return None;
118129
}
119130
let us = Arc::clone(&self.state);
120-
self.runtime_handle.spawn(async move {
131+
runtime.spawn(async move {
121132
if let Ok((proof, _ttl)) = build_txt_proof_async(us.resolver, &q.0).await {
122133
let contents = DNSResolverMessage::DNSSECProof(DNSSECProof { name: q.0, proof });
123134
let instructions = responder.respond().into_instructions();

0 commit comments

Comments
 (0)