aves 0.5.0

Typed async application runtime with domain messaging, RPC, and durable workflow automation
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
use std::any::{Any, TypeId};
use std::collections::HashMap;
use std::fmt;
use std::sync::{Arc, RwLock};

use tokio::sync::{broadcast, mpsc, oneshot};

use crate::auth::{
    AuthAdmissionRegistry, AuthProjection, AuthSudoService, AvesAuthError, Jwt, messages::VerifyJwtInput,
};

const REQUEST_BUFFER_CAPACITY: usize = 64;
const BROADCAST_BUFFER_CAPACITY: usize = 1000;

type AdmissionRejection = Box<dyn FnOnce(Box<dyn Any + Send>) + Send>;

/// Opaque request envelope owned by the generic bus.
///
/// The envelope contains only the marker-derived [`TypeId`] and the complete
/// concrete request erased behind `Any`. The request itself retains its
/// payload, typed reply channel, response/error types, and handler context.
/// No caller-provided route, raw body, string, numeric identity, or serialized
/// transport metadata can enter the envelope.
pub struct OpaqueRequestEnvelope {
    target: TypeId,
    jwt: Option<Jwt>,
    projection: Option<AuthProjection>,
    rejection: Option<AdmissionRejection>,
    body: Box<dyn Any + Send>,
}

impl OpaqueRequestEnvelope {
    /// Constructs an envelope using only the generic marker's type identity.
    pub fn new<Marker, Request>(jwt: Jwt, request: Request) -> Self
    where
        Marker: 'static,
        Request: Send + 'static,
    {
        Self {
            target: TypeId::of::<Marker>(),
            jwt: Some(jwt),
            projection: None,
            rejection: None,
            body: Box::new(request),
        }
    }

    /// Constructs an envelope with an opaque callback for admission rejection.
    pub fn new_with_rejection<Marker, Request>(
        jwt: Jwt,
        request: Request,
        rejection: impl FnOnce(Box<dyn Any + Send>) + Send + 'static,
    ) -> Self
    where
        Marker: 'static,
        Request: Send + 'static,
    {
        Self {
            target: TypeId::of::<Marker>(),
            jwt: Some(jwt),
            projection: None,
            rejection: Some(Box::new(rejection)),
            body: Box::new(request),
        }
    }

    /// Returns the marker-derived routing identity.
    pub(crate) fn target(&self) -> TypeId {
        self.target
    }

    async fn admit(mut self, auth: Option<&AuthAdmissionConfiguration>) -> Option<Self> {
        let jwt = self.jwt.take()?;
        let Some(configuration) = auth else {
            self.reject(AvesAuthError::Internal("Auth admission is unavailable".to_owned()));
            return None;
        };
        let verified = match configuration
            .verifier
            .verify_jwt(VerifyJwtInput {
                jwt,
                accepted_issuers: configuration.registry.current_issuers(),
                audience: configuration.audience,
            })
            .await
        {
            Ok(verified) => verified,
            Err(error) => {
                self.reject(error);
                return None;
            }
        };
        self.projection = Some(verified.projection);
        Some(self)
    }

    fn reject(&mut self, error: AvesAuthError) {
        if let Some(rejection) = self.rejection.take() {
            rejection(Box::new(error));
        }
    }

    /// Recovers the request and the projection produced by bus admission.
    ///
    /// The raw JWT is consumed before a request reaches a domain adapter.
    pub(crate) fn downcast_with_projection<Request>(self) -> Result<(Request, AuthProjection), Box<Self>>
    where
        Request: Send + 'static,
    {
        let Self {
            target,
            jwt,
            projection,
            rejection: _,
            body,
        } = self;
        drop(jwt);
        let Some(projection) = projection else {
            return Err(Box::new(Self {
                target,
                jwt: None,
                projection: None,
                rejection: None,
                body,
            }));
        };
        match body.downcast::<Request>() {
            Ok(request) => Ok((*request, projection)),
            Err(body) => Err(Box::new(Self {
                target,
                jwt: None,
                projection: Some(projection),
                rejection: None,
                body,
            })),
        }
    }

    /// Returns the marker-derived routing identity for test support.
    #[cfg(any(test, feature = "test-support"))]
    #[doc(hidden)]
    pub fn test_target(&self) -> TypeId {
        self.target()
    }

    /// Recovers a concrete request for test support.
    #[cfg(any(test, feature = "test-support"))]
    #[doc(hidden)]
    pub fn test_downcast<Request>(self) -> Result<Request, Self>
    where
        Request: Send + 'static,
    {
        let Self { target, body, .. } = self;
        match body.downcast::<Request>() {
            Ok(request) => Ok(*request),
            Err(body) => Err(Self {
                target,
                jwt: None,
                projection: None,
                rejection: None,
                body,
            }),
        }
    }
}

impl fmt::Debug for OpaqueRequestEnvelope {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("OpaqueRequestEnvelope")
            .field("target", &self.target)
            .field("body", &"<opaque>")
            .finish()
    }
}

/// Typed publisher for the root broadcast channel.
pub struct BroadcastAdapter<B, Root>
where
    B: Into<Root> + Clone + Send + 'static,
    Root: Clone + Send + 'static,
{
    sender: broadcast::Sender<Root>,
    _marker: std::marker::PhantomData<B>,
}

impl<B, Root> BroadcastAdapter<B, Root>
where
    B: Into<Root> + Clone + Send + 'static,
    Root: Clone + Send + 'static,
{
    pub fn new(sender: broadcast::Sender<Root>) -> Self {
        Self {
            sender,
            _marker: std::marker::PhantomData,
        }
    }

    pub fn send(&self, event: B) -> Result<usize, broadcast::error::SendError<Root>> {
        self.sender.send(event.into())
    }

    pub fn subscribe(&self) -> broadcast::Receiver<Root> {
        self.sender.subscribe()
    }
}

impl<B, Root> Clone for BroadcastAdapter<B, Root>
where
    B: Into<Root> + Clone + Send + 'static,
    Root: Clone + Send + 'static,
{
    fn clone(&self) -> Self {
        Self {
            sender: self.sender.clone(),
            _marker: std::marker::PhantomData,
        }
    }
}

/// The channels produced when a marker is registered with the bus.
pub struct DomainChannels<Request, Broadcast, Root>
where
    Request: Send + 'static,
    Broadcast: Into<Root> + Clone + Send + 'static,
    Root: Clone + Send + 'static,
{
    pub broadcasts_tx: BroadcastAdapter<Broadcast, Root>,
    pub broadcasts_rx: broadcast::Receiver<Root>,
    pub requests_tx: mpsc::Sender<OpaqueRequestEnvelope>,
    pub requests_rx: mpsc::Receiver<(Request, AuthProjection)>,
}

#[derive(Clone)]
pub(crate) struct AuthAdmissionConfiguration {
    pub(crate) registry: AuthAdmissionRegistry,
    pub(crate) verifier: Arc<dyn AuthSudoService>,
    pub(crate) audience: uuid::Uuid,
}

/// A marker-keyed, type-erased request and root-broadcast bus.
pub struct EventBus<Root>
where
    Root: Clone + Send + 'static,
{
    input_sender: mpsc::Sender<OpaqueRequestEnvelope>,
    adapters: Arc<RwLock<HashMap<TypeId, mpsc::Sender<OpaqueRequestEnvelope>>>>,
    auth_configuration: Arc<RwLock<Option<AuthAdmissionConfiguration>>>,
    broadcast_sender: broadcast::Sender<Root>,
    routing_shutdown: Option<oneshot::Sender<()>>,
    routing_task: Option<tokio::task::JoinHandle<()>>,
}

impl<Root> Default for EventBus<Root>
where
    Root: Clone + Send + 'static,
{
    fn default() -> Self {
        Self::new()
    }
}

impl<Root> EventBus<Root>
where
    Root: Clone + Send + 'static,
{
    pub fn new() -> Self {
        let (input_sender, mut input_receiver) = mpsc::channel::<OpaqueRequestEnvelope>(REQUEST_BUFFER_CAPACITY);

        let auth_configuration = Arc::new(RwLock::new(None::<AuthAdmissionConfiguration>));
        let routing_auth = Arc::clone(&auth_configuration);

        let adapters: Arc<RwLock<HashMap<TypeId, mpsc::Sender<OpaqueRequestEnvelope>>>> =
            Arc::new(RwLock::new(HashMap::new()));
        let routing_adapters = Arc::clone(&adapters);

        let (routing_shutdown, mut shutdown_receiver) = oneshot::channel();

        let routing_task = tokio::spawn(async move {
            loop {
                tokio::select! {
                    _ = &mut shutdown_receiver => break,
                    envelope = input_receiver.recv() => {
                        let Some(envelope) = envelope else { break };
                        let auth = routing_auth
                            .read()
                            .expect("auth registry lock poisoned")
                            .clone();
                        let Some(envelope) = envelope.admit(auth.as_ref()).await else { continue };
                        let target = envelope.target();
                        let adapter = routing_adapters
                            .read()
                            .expect("event bus registry lock poisoned")
                            .get(&target)
                            .cloned()
                            .unwrap_or_else(|| panic!("unregistered domain marker: {target:?}"));
                        tokio::select! {
                            _ = &mut shutdown_receiver => break,
                            result = adapter.send(envelope) => {
                                result.expect("registered domain mailbox unexpectedly closed");
                            }
                        }
                    }
                }
            }
        });

        let (broadcast_sender, _) = broadcast::channel(BROADCAST_BUFFER_CAPACITY);
        Self {
            input_sender,
            adapters,
            auth_configuration,
            broadcast_sender,
            routing_shutdown: Some(routing_shutdown),
            routing_task: Some(routing_task),
        }
    }

    pub(crate) fn install_auth(&mut self, configuration: AuthAdmissionConfiguration) {
        let mut auth = self
            .auth_configuration
            .write()
            .expect("auth registry lock poisoned");
        assert!(
            auth.replace(configuration).is_none(),
            "Auth admission is already installed"
        );
    }

    /// Registers a marker and returns its opaque input sender plus typed mailbox.
    pub fn register<Marker, Request, Broadcast>(&mut self) -> DomainChannels<Request, Broadcast, Root>
    where
        Marker: 'static,
        Request: Send + 'static,
        Broadcast: Into<Root> + Clone + Send + 'static,
    {
        let (adapter_tx, mut adapter_rx) = mpsc::channel(REQUEST_BUFFER_CAPACITY);
        let (request_tx, request_rx) = mpsc::channel(REQUEST_BUFFER_CAPACITY);
        let marker = TypeId::of::<Marker>();
        let mut registry = self
            .adapters
            .write()
            .expect("event bus registry lock poisoned");
        assert!(
            registry.insert(marker, adapter_tx).is_none(),
            "domain marker already registered"
        );
        drop(registry);

        tokio::spawn(async move {
            while let Some(envelope) = adapter_rx.recv().await {
                match envelope.downcast_with_projection::<Request>() {
                    Ok((request, projection)) => {
                        if request_tx.send((request, projection)).await.is_err() {
                            break;
                        }
                    }
                    Err(envelope) => {
                        panic!("opaque request body does not match its marker-derived route: {envelope:?}");
                    }
                }
            }
        });

        DomainChannels {
            broadcasts_tx: BroadcastAdapter::new(self.broadcast_sender.clone()),
            broadcasts_rx: self.broadcast_sender.subscribe(),
            requests_tx: self.input_sender.clone(),
            requests_rx: request_rx,
        }
    }

    pub fn broadcast_sender(&self) -> broadcast::Sender<Root> {
        self.broadcast_sender.clone()
    }

    /// Returns the single opaque ingress shared by all registered domains.
    pub fn input_sender(&self) -> mpsc::Sender<OpaqueRequestEnvelope> {
        self.input_sender.clone()
    }

    pub fn subscribe(&self) -> broadcast::Receiver<Root> {
        self.broadcast_sender.subscribe()
    }

    pub(crate) fn into_routing_parts(
        self,
    ) -> (
        mpsc::Sender<OpaqueRequestEnvelope>,
        broadcast::Sender<Root>,
        oneshot::Sender<()>,
        tokio::task::JoinHandle<()>,
    ) {
        (
            self.input_sender,
            self.broadcast_sender,
            self.routing_shutdown
                .expect("event bus routing shutdown missing"),
            self.routing_task.expect("event bus routing task missing"),
        )
    }

    pub(crate) fn abort_routing(&mut self) {
        if let Some(shutdown) = self.routing_shutdown.take() {
            let _ = shutdown.send(());
        }
        if let Some(task) = self.routing_task.take() {
            task.abort();
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    struct FirstMarker;
    struct SecondMarker;
    struct FirstRequest;
    struct CompleteRequest {
        payload: u8,
        reply_to: std::sync::mpsc::Sender<u8>,
    }

    #[test]
    fn test_downcast_recovers_the_complete_request_without_projection() {
        let (reply_to, reply_receiver) = std::sync::mpsc::channel();
        let envelope = OpaqueRequestEnvelope::new::<FirstMarker, _>(
            Jwt::from_opaque("test-jwt".to_owned()),
            CompleteRequest { payload: 7, reply_to },
        );
        let request = envelope
            .test_downcast::<CompleteRequest>()
            .expect("request should downcast to its original concrete type");

        assert_eq!(request.payload, 7);
        request
            .reply_to
            .send(11)
            .expect("reply channel remains intact");
        assert_eq!(reply_receiver.recv().expect("reply should arrive"), 11);
    }

    #[test]
    fn route_identity_is_derived_only_from_the_marker() {
        let request = CompleteRequest {
            payload: 7,
            reply_to: std::sync::mpsc::channel().0,
        };
        let envelope = OpaqueRequestEnvelope::new::<FirstMarker, _>(Jwt::from_opaque("test-jwt".to_owned()), request);

        assert_eq!(envelope.target(), TypeId::of::<FirstMarker>());
        assert_ne!(envelope.target(), TypeId::of::<SecondMarker>());
    }

    #[tokio::test]
    async fn admission_requires_auth_configuration() {
        let envelope =
            OpaqueRequestEnvelope::new::<FirstMarker, _>(Jwt::from_opaque("test-jwt".to_owned()), FirstRequest);

        assert!(envelope.admit(None).await.is_none());
    }

    #[test]
    fn debug_output_keeps_the_erased_request_opaque() {
        let debug_for = |payload| {
            let envelope = OpaqueRequestEnvelope::new::<FirstMarker, _>(
                Jwt::from_opaque("test-jwt".to_owned()),
                CompleteRequest {
                    payload,
                    reply_to: std::sync::mpsc::channel().0,
                },
            );
            format!("{envelope:?}")
        };
        let debug = debug_for(99);

        assert!(debug.contains("OpaqueRequestEnvelope"));
        assert!(debug.contains("<opaque>"));
        assert_eq!(debug, debug_for(1));
    }

    #[tokio::test]
    async fn duplicate_marker_registration_is_rejected() {
        let mut bus = EventBus::<u8>::new();
        let _ = bus.register::<FirstMarker, CompleteRequest, u8>();

        let duplicate = std::panic::AssertUnwindSafe(|| {
            bus.register::<FirstMarker, CompleteRequest, u8>();
        });
        assert!(std::panic::catch_unwind(duplicate).is_err());
    }
}