ckb_sentry_core/
transport.rs

1use std::sync::Arc;
2use std::time::Duration;
3
4use crate::{ClientOptions, Envelope};
5
6/// The trait for transports.
7///
8/// A transport is responsible for sending events to Sentry.  Custom implementations
9/// can be created to use a different abstraction to send events.  This is for instance
10/// used for the test system.
11pub trait Transport: Send + Sync + 'static {
12    /// Sends an [`Envelope`].
13    ///
14    /// [`Envelope`]: struct.Envelope.html
15    fn send_envelope(&self, envelope: Envelope);
16
17    /// Drains the queue if there is one.
18    ///
19    /// The default implementation does nothing.  If the queue was successfully
20    /// shutdowned the return value should be `true` or `false` if events were
21    /// left in it.
22    fn shutdown(&self, timeout: Duration) -> bool {
23        let _timeout = timeout;
24        true
25    }
26}
27
28/// A factory creating transport instances.
29///
30/// Because options are potentially reused between different clients the
31/// options do not actually contain a transport but a factory object that
32/// can create transports instead.
33///
34/// The factory has a single method that creates a new arced transport.
35/// Because transports can be wrapped in `Arc`s and those are clonable
36/// any `Arc<Transport>` is also a valid transport factory.  This for
37/// instance lets you put a `Arc<TestTransport>` directly into the options.
38///
39/// This is automatically implemented for all closures optionally taking
40/// options and returning a boxed factory.
41pub trait TransportFactory: Send + Sync {
42    /// Given some options creates a transport.
43    fn create_transport(&self, options: &ClientOptions) -> Arc<dyn Transport>;
44}
45
46impl<F> TransportFactory for F
47where
48    F: Fn(&ClientOptions) -> Arc<dyn Transport> + Clone + Send + Sync + 'static,
49{
50    fn create_transport(&self, options: &ClientOptions) -> Arc<dyn Transport> {
51        (*self)(options)
52    }
53}
54
55impl<T: Transport> Transport for Arc<T> {
56    fn send_envelope(&self, envelope: Envelope) {
57        (**self).send_envelope(envelope)
58    }
59
60    fn shutdown(&self, timeout: Duration) -> bool {
61        (**self).shutdown(timeout)
62    }
63}
64
65impl<T: Transport> TransportFactory for Arc<T> {
66    fn create_transport(&self, options: &ClientOptions) -> Arc<dyn Transport> {
67        let _options = options;
68        self.clone()
69    }
70}