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
use std::sync::{
    atomic::{AtomicBool, Ordering::Relaxed},
    Arc,
};

use crate::{SharedStats, SmartChannelSource, SmartMessage, TryRecvError};

pub struct Receiver<T: Send> {
    pub(crate) rx: crossbeam::channel::Receiver<SmartMessage<T>>,
    stats: Arc<SharedStats>,
    pub(crate) source: Arc<SmartChannelSource>,
    connected: AtomicBool,
}

impl<T: Send> Receiver<T> {
    pub(crate) fn new(
        rx: crossbeam::channel::Receiver<SmartMessage<T>>,
        stats: Arc<SharedStats>,
        source: Arc<SmartChannelSource>,
    ) -> Self {
        Self {
            rx,
            stats,
            source,
            connected: AtomicBool::new(true),
        }
    }

    /// Are we still connected?
    ///
    /// Once false, we will never be connected again: the source has run dry.
    ///
    /// This is only updated once one of the receive methods fails.
    pub fn is_connected(&self) -> bool {
        self.connected.load(Relaxed)
    }

    #[cfg(not(target_arch = "wasm32"))] // Cannot block on web
    pub fn recv(&self) -> Result<SmartMessage<T>, crate::RecvError> {
        let msg = match self.rx.recv() {
            Ok(x) => x,
            Err(crate::RecvError) => {
                self.connected.store(false, Relaxed);
                return Err(crate::RecvError);
            }
        };

        let latency_ns = msg.time.elapsed().as_nanos() as u64;
        self.stats.latency_ns.store(latency_ns, Relaxed);

        Ok(msg)
    }

    pub fn try_recv(&self) -> Result<SmartMessage<T>, TryRecvError> {
        let msg = match self.rx.try_recv() {
            Ok(x) => x,
            Err(err) => {
                if err == TryRecvError::Disconnected {
                    self.connected.store(false, Relaxed);
                }
                return Err(err);
            }
        };

        let latency_ns = msg.time.elapsed().as_nanos() as u64;
        self.stats.latency_ns.store(latency_ns, Relaxed);

        Ok(msg)
    }

    #[cfg(not(target_arch = "wasm32"))] // Cannot block on web
    pub fn recv_timeout(
        &self,
        timeout: std::time::Duration,
    ) -> Result<SmartMessage<T>, crate::RecvTimeoutError> {
        let msg = match self.rx.recv_timeout(timeout) {
            Ok(x) => x,
            Err(err) => {
                if err == crate::RecvTimeoutError::Disconnected {
                    self.connected.store(false, Relaxed);
                }
                return Err(err);
            }
        };

        let latency_ns = msg.time.elapsed().as_nanos() as u64;
        self.stats.latency_ns.store(latency_ns, Relaxed);

        Ok(msg)
    }

    /// Receives without registering the latency.
    ///
    /// This is for use with [`crate::Sender::send_at`] when chaining to another channel
    /// created with [`Self::chained_channel`].
    #[cfg(not(target_arch = "wasm32"))] // Cannot block on web
    pub fn recv_with_send_time(&self) -> Result<SmartMessage<T>, crate::RecvError> {
        self.rx.recv()
    }

    /// Where is the data coming from?
    #[inline]
    pub fn source(&self) -> &SmartChannelSource {
        &self.source
    }

    /// Is the channel currently empty of messages?
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.rx.is_empty()
    }

    /// Number of messages in the channel right now.
    #[inline]
    pub fn len(&self) -> usize {
        self.rx.len()
    }

    /// Latest known latency from sending a message to receiving it, it nanoseconds.
    pub fn latency_ns(&self) -> u64 {
        self.stats.latency_ns.load(Relaxed)
    }

    /// Latest known latency from sending a message to receiving it,
    /// in seconds
    pub fn latency_sec(&self) -> f32 {
        self.latency_ns() as f32 / 1e9
    }

    /// Create a new channel that use the same stats as this one.
    ///
    /// This means both channels will see the same latency numbers.
    ///
    /// Care must be taken to use [`Self::recv_with_send_time`] and [`crate::Sender::send_at`].
    /// This is a very leaky abstraction, and it would be nice with a refactor.
    pub fn chained_channel(&self) -> (crate::Sender<T>, Receiver<T>) {
        crate::smart_channel_with_stats(
            // NOTE: We cannot know yet, and it doesn't matter as the new sender will only be used
            // to forward existing messages.
            crate::SmartMessageSource::Unknown,
            self.source.clone(),
            self.stats.clone(),
        )
    }
}