-
Notifications
You must be signed in to change notification settings - Fork 17
/
mod.rs
340 lines (302 loc) · 10.6 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license OR Apache 2.0
#![allow(clippy::type_complexity)]
use downcast_rs::{impl_downcast, Downcast, DowncastSync};
use futures::{
future::{BoxFuture, FutureExt},
TryFutureExt,
};
use std::{
fmt::{Debug, Display},
sync::Arc,
};
use crate::common::protocol::tunnel::{TunnelId, TunnelName};
pub mod local;
pub mod serialized;
#[derive(PartialEq, Eq, Hash)]
pub struct TunnelRecord<TTunnel: ?Sized, TMetadata> {
pub id: TunnelId,
pub name: Option<TunnelName>,
pub tunnel: Option<Arc<TTunnel>>,
pub metadata: TMetadata,
}
impl<TTunnel: ?Sized, TMetadata> Clone for TunnelRecord<TTunnel, TMetadata>
where
TMetadata: Clone,
{
fn clone(&self) -> Self {
Self {
id: self.id,
name: self.name.clone(),
tunnel: self.tunnel.clone(),
metadata: self.metadata.clone(),
}
}
}
impl<TTunnel: ?Sized, TMetadata> TunnelRecord<TTunnel, TMetadata> {
pub fn map_metadata<TOutputMetadata, F: FnOnce(TMetadata) -> TOutputMetadata>(
self,
f: F,
) -> TunnelRecord<TTunnel, TOutputMetadata> {
TunnelRecord {
id: self.id,
name: self.name,
tunnel: self.tunnel,
metadata: f(self.metadata),
}
}
// I'd use Into/From but apparently this clashes because you can't assert that TMetadata != TOutputMetadata
pub fn convert_metadata<TOutputMetadata>(self) -> TunnelRecord<TTunnel, TOutputMetadata>
where
TOutputMetadata: From<TMetadata>,
{
self.map_metadata(From::from)
}
}
impl<TTunnel: ?Sized, TMetadata> Debug for TunnelRecord<TTunnel, TMetadata> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct(stringify!(TunnelRecord))
.field("id", &self.id)
.field("name", &self.name)
.finish_non_exhaustive()
}
}
#[derive(thiserror::Error, Debug)]
pub enum TunnelRegistrationError<ApplicationError: Debug + Display> {
#[error("Tunnel ID was already occupied")]
IdOccupied(TunnelId),
#[error("Application error in tunnel registration")]
ApplicationError(ApplicationError),
}
impl<ApplicationError: Debug + Display> From<ApplicationError>
for TunnelRegistrationError<ApplicationError>
{
fn from(e: ApplicationError) -> Self {
TunnelRegistrationError::ApplicationError(e)
}
}
#[derive(thiserror::Error, Debug)]
pub enum TunnelNamingError<ApplicationError: Debug + Display> {
#[error("The tunnel to be named was not found")]
TunnelNotRegistered(TunnelId),
#[error("Application error in tunnel naming")]
ApplicationError(ApplicationError),
}
impl<ApplicationError: Debug + Display> From<ApplicationError>
for TunnelNamingError<ApplicationError>
{
fn from(e: ApplicationError) -> Self {
TunnelNamingError::ApplicationError(e)
}
}
pub trait TunnelRegistry<TTunnel: ?Sized>: Downcast + DowncastSync {
/// Metadata attached to each registered tunnel, uniquely per tunnel
///
/// Created at registration time, and readable by all record holders. Not guaranteed to be
/// the same instance for all readers. Any edit to this object via internal mutability is not
/// guaranteed to propagate back, except when the implementor is notified via a side channel.
type Metadata;
// Requires std::fmt::Debug constraint thanks to https://github.com/dtolnay/thiserror/issues/79
type Error: Debug + Display;
/// Looks up a tunnel by ID
///
/// Note that if a TunnelRecord contains None for the tunnel reference, that does not mean that
/// the same will hold true for other readers; The tunnel may be scoped to within that context.
fn lookup_by_id(
&self,
tunnel_id: TunnelId,
) -> BoxFuture<Result<Option<TunnelRecord<TTunnel, Self::Metadata>>, Self::Error>>;
/// Looks up a tunnel by Name
///
/// Note that if a TunnelRecord contains None for the tunnel reference, that does not mean that
/// the same will hold true for other readers; The tunnel may be scoped to within that context.
fn lookup_by_name(
&self,
tunnel_name: TunnelName,
) -> BoxFuture<Result<Option<TunnelRecord<TTunnel, Self::Metadata>>, Self::Error>>;
/// Called prior to authentication, a tunnel is not yet trusted and has no name,
/// but the ID is guaranteed to remain stable throughout its lifetime.
///
/// Upon disconnection, [Self::deregister_tunnel] will be called with the given [TunnelId].
///
/// Returns Some(Metadata) if a tunnel was created with that ID, otherwise it had a conflict.
fn register_tunnel(
&self,
tunnel_id: TunnelId,
tunnel: Arc<TTunnel>,
) -> BoxFuture<Result<Self::Metadata, TunnelRegistrationError<Self::Error>>>;
/// Called after authentication, when a tunnel is given an official designation
/// May also be called later to allow a reconnecting tunnel to replace its old
/// record until that record is removed.
///
/// Returns Some if a tunnel by that ID existed and was renamed.
fn name_tunnel(
&self,
tunnel_id: TunnelId,
name: Option<TunnelName>,
) -> BoxFuture<Result<(), TunnelNamingError<Self::Error>>>;
/// Called to remove a tunnel from the registry after it is disconnected.
/// Does not immediately destroy the Tunnel; previous consumers can hold
/// an Arc containing the Tunnel instance, which will extend its lifetime.
///
/// Completion of deregistration implies that lookups may no longer
/// produce a tunnel record, but this may occur after a delay.
fn deregister_tunnel(
&self,
tunnel_id: TunnelId,
) -> BoxFuture<Result<Option<TunnelRecord<TTunnel, Self::Metadata>>, Self::Error>>;
}
impl_downcast!(sync TunnelRegistry<TTunnel> assoc Metadata, Error);
/// An extension to the Registry trait which allows registration of unowned tunnels.
/// Intended to allow mirroring of another registry across processes or hosts.
pub trait TunnelRegistryMirroring<TTunnel: ?Sized>: TunnelRegistry<TTunnel> {
/// A superset of functionality over TunnelRegistry::register_tunnel
/// in that it also allows passing unowned tunnels / "None", and can
/// specify the exact metadata used in the remote tunnel record.
fn register_tunnel_mirror(
&self,
tunnel_id: TunnelId,
tunnel: Option<Arc<TTunnel>>,
metadata: Self::Metadata,
) -> BoxFuture<Result<Self::Metadata, TunnelRegistrationError<Self::Error>>>;
}
impl<T, TTunnel: ?Sized> TunnelRegistry<TTunnel> for Arc<T>
where
T: TunnelRegistry<TTunnel> + Send + Sync + 'static,
TTunnel: Send + Sync + 'static,
{
type Metadata = <T as TunnelRegistry<TTunnel>>::Metadata;
type Error = <T as TunnelRegistry<TTunnel>>::Error;
fn lookup_by_id(
&self,
tunnel_id: TunnelId,
) -> BoxFuture<'_, Result<Option<TunnelRecord<TTunnel, Self::Metadata>>, Self::Error>> {
self.as_ref().lookup_by_id(tunnel_id)
}
fn lookup_by_name(
&self,
tunnel_name: TunnelName,
) -> BoxFuture<'_, Result<Option<TunnelRecord<TTunnel, Self::Metadata>>, Self::Error>> {
self.as_ref().lookup_by_name(tunnel_name)
}
fn register_tunnel(
&self,
tunnel_id: TunnelId,
tunnel: Arc<TTunnel>,
) -> BoxFuture<'_, Result<Self::Metadata, TunnelRegistrationError<Self::Error>>> {
self.as_ref().register_tunnel(tunnel_id, tunnel)
}
fn name_tunnel(
&self,
tunnel_id: TunnelId,
name: Option<TunnelName>,
) -> BoxFuture<'_, Result<(), TunnelNamingError<Self::Error>>> {
self.as_ref().name_tunnel(tunnel_id, name)
}
fn deregister_tunnel(
&self,
tunnel_id: TunnelId,
) -> BoxFuture<'_, Result<Option<TunnelRecord<TTunnel, Self::Metadata>>, Self::Error>> {
self.as_ref().deregister_tunnel(tunnel_id)
}
}
pub trait AnyError: std::any::Any + std::fmt::Display + std::fmt::Debug {}
impl<T> AnyError for T where T: std::any::Any + std::fmt::Display + std::fmt::Debug {}
/// Any/Boxed/Opaque tunnel registry wrapper which translates outputs to std::any::Any
#[repr(transparent)]
pub struct BoxedTunnelRegistryInner<TTunnelRegistry>(TTunnelRegistry);
impl<T, TTunnel: ?Sized> TunnelRegistry<TTunnel> for BoxedTunnelRegistryInner<T>
where
T: TunnelRegistry<TTunnel> + Send + Sync + 'static,
TTunnel: Send + Sync + 'static,
{
type Metadata = Box<dyn std::any::Any + 'static>;
type Error = Box<dyn AnyError + 'static>;
fn lookup_by_id(
&self,
tunnel_id: TunnelId,
) -> BoxFuture<Result<Option<TunnelRecord<TTunnel, Self::Metadata>>, Self::Error>> {
self
.0
.lookup_by_id(tunnel_id)
.map_ok(|ok| ok.map(|tr| tr.map_metadata(|metadata| Box::new(metadata) as _)))
.map_err(|e| Box::new(e) as _)
.boxed()
}
fn lookup_by_name(
&self,
tunnel_name: TunnelName,
) -> BoxFuture<Result<Option<TunnelRecord<TTunnel, Self::Metadata>>, Self::Error>> {
self
.0
.lookup_by_name(tunnel_name)
.map_ok(|ok| ok.map(|tr| tr.map_metadata(|metadata| Box::new(metadata) as _)))
.map_err(|e| Box::new(e) as _)
.boxed()
}
fn register_tunnel(
&self,
tunnel_id: TunnelId,
tunnel: Arc<TTunnel>,
) -> BoxFuture<Result<Self::Metadata, TunnelRegistrationError<Self::Error>>> {
self
.0
.register_tunnel(tunnel_id, tunnel)
.map_ok(|metadata| Box::new(metadata) as _)
.map_err(|e| match e {
TunnelRegistrationError::IdOccupied(id) => TunnelRegistrationError::IdOccupied(id),
TunnelRegistrationError::ApplicationError(e) => {
TunnelRegistrationError::ApplicationError(Box::new(e) as _)
}
})
.boxed()
}
fn name_tunnel(
&self,
tunnel_id: TunnelId,
name: Option<TunnelName>,
) -> BoxFuture<Result<(), TunnelNamingError<Self::Error>>> {
self
.0
.name_tunnel(tunnel_id, name)
.map_err(|e| match e {
TunnelNamingError::TunnelNotRegistered(id) => TunnelNamingError::TunnelNotRegistered(id),
TunnelNamingError::ApplicationError(e) => {
TunnelNamingError::ApplicationError(Box::new(e) as _)
}
})
.boxed()
}
fn deregister_tunnel(
&self,
tunnel_id: TunnelId,
) -> BoxFuture<Result<Option<TunnelRecord<TTunnel, Self::Metadata>>, Self::Error>> {
self
.0
.deregister_tunnel(tunnel_id)
.map_ok(|ok| ok.map(|tr| tr.map_metadata(|metadata| Box::new(metadata) as _)))
.map_err(|e| Box::new(e) as _)
.boxed()
}
}
#[repr(transparent)]
pub struct BoxedTunnelRegistry<TTunnel: ?Sized>(
Box<
dyn TunnelRegistry<
TTunnel,
Metadata = Box<dyn std::any::Any + 'static>,
Error = Box<dyn AnyError + 'static>,
> + 'static,
>,
);
impl<TTunnel: ?Sized> BoxedTunnelRegistry<TTunnel> {
pub fn into_box<T>(inner: T) -> Self
where
TTunnel: Send + Sync + 'static,
T: TunnelRegistry<TTunnel> + Send + Sync + 'static,
T::Error: AnyError,
T::Metadata: std::any::Any,
{
Self(Box::new(BoxedTunnelRegistryInner(inner)) as Box<_>)
}
}