34
34
{
35
35
state : Arc < OMResolverState > ,
36
36
proof_handler : Option < PH > ,
37
- runtime_handle : Handle ,
37
+ runtime_handle : Mutex < Option < Handle > > ,
38
38
}
39
39
40
40
const MAX_PENDING_RESPONSES : usize = 1024 ;
@@ -69,19 +69,20 @@ where
69
69
/// requests but some other handler is making proof requests of remote nodes and wants to get
70
70
/// results.
71
71
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 ( ) ) )
73
73
}
74
74
75
75
/// Creates a new [`OMDomainResolver`] given the [`SocketAddr`] of a DNS resolver listening on
76
76
/// 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.
78
79
///
79
80
/// The optional `proof_handler` can be provided to pass proofs coming back to us to the
80
81
/// underlying handler. This is useful when this resolver is handling incoming resolution
81
82
/// requests but some other handler is making proof requests of remote nodes and wants to get
82
83
/// results.
83
84
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 > ,
85
86
) -> Self {
86
87
Self {
87
88
state : Arc :: new ( OMResolverState {
90
91
pending_query_count : AtomicUsize :: new ( 0 ) ,
91
92
} ) ,
92
93
proof_handler,
93
- runtime_handle,
94
+ runtime_handle : Mutex :: new ( runtime_handle ) ,
94
95
}
95
96
}
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
+ }
96
102
}
97
103
98
104
impl < PH : Deref > DNSResolverMessageHandler for OMDomainResolver < PH >
@@ -112,12 +118,17 @@ where
112
118
Some ( responder) => responder,
113
119
None => return None ,
114
120
} ;
121
+ let runtime = if let Some ( runtime) = self . runtime_handle . lock ( ) . unwrap ( ) . clone ( ) {
122
+ runtime
123
+ } else {
124
+ return None ;
125
+ } ;
115
126
if self . state . pending_query_count . fetch_add ( 1 , Ordering :: Relaxed ) > MAX_PENDING_RESPONSES {
116
127
self . state . pending_query_count . fetch_sub ( 1 , Ordering :: Relaxed ) ;
117
128
return None ;
118
129
}
119
130
let us = Arc :: clone ( & self . state ) ;
120
- self . runtime_handle . spawn ( async move {
131
+ runtime . spawn ( async move {
121
132
if let Ok ( ( proof, _ttl) ) = build_txt_proof_async ( us. resolver , & q. 0 ) . await {
122
133
let contents = DNSResolverMessage :: DNSSECProof ( DNSSECProof { name : q. 0 , proof } ) ;
123
134
let instructions = responder. respond ( ) . into_instructions ( ) ;
0 commit comments