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    /// Flushes the transport queue if there is one.
18    ///
19    /// If the queue was successfully drained, the return value should be
20    /// `true` or `false` if events were left in it.
21    fn flush(&self, timeout: Duration) -> bool {
22        let _timeout = timeout;
23        true
24    }
25
26    /// Instructs the Transport to shut down.
27    fn shutdown(&self, timeout: Duration) -> bool {
28        self.flush(timeout)
29    }
30}
31
32/// A factory creating transport instances.
33///
34/// Because options are potentially reused between different clients the
35/// options do not actually contain a transport but a factory object that
36/// can create transports instead.
37///
38/// The factory has a single method that creates a new arced transport.
39/// Because transports can be wrapped in `Arc`s and those are clonable
40/// any `Arc<Transport>` is also a valid transport factory.  This for
41/// instance lets you put a `Arc<TestTransport>` directly into the options.
42///
43/// This is automatically implemented for all closures optionally taking
44/// options and returning a boxed factory.
45pub trait TransportFactory: Send + Sync {
46    /// Given some options creates a transport.
47    fn create_transport(&self, options: &ClientOptions) -> Arc<dyn Transport>;
48}
49
50impl<F> TransportFactory for F
51where
52    F: Fn(&ClientOptions) -> Arc<dyn Transport> + Clone + Send + Sync + 'static,
53{
54    fn create_transport(&self, options: &ClientOptions) -> Arc<dyn Transport> {
55        (*self)(options)
56    }
57}
58
59impl<T: Transport> Transport for Arc<T> {
60    fn send_envelope(&self, envelope: Envelope) {
61        (**self).send_envelope(envelope)
62    }
63
64    fn shutdown(&self, timeout: Duration) -> bool {
65        (**self).shutdown(timeout)
66    }
67}
68
69impl<T: Transport> TransportFactory for Arc<T> {
70    fn create_transport(&self, options: &ClientOptions) -> Arc<dyn Transport> {
71        let _options = options;
72        self.clone()
73    }
74}