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
use super::{private, GetRawSocket, Period, RawSocket};
use crate::error::Error;
use Period::*;

use serde::{Deserialize, Serialize};

use std::{sync::MutexGuard, time::Duration};

fn set_heartbeat(
    raw_socket: &RawSocket,
    maybe: Option<Heartbeat>,
    mut mutex: MutexGuard<Option<Heartbeat>>,
) -> Result<(), Error> {
    if *mutex == maybe {
        return Ok(());
    }

    if let Some(heartbeat) = &maybe {
        raw_socket.set_heartbeat_interval(heartbeat.interval)?;
        if let Finite(timeout) = heartbeat.timeout {
            raw_socket.set_heartbeat_timeout(timeout)?;
        }
        if let Finite(ttl) = heartbeat.ttl {
            raw_socket.set_heartbeat_timeout(ttl)?;
        }
    } else {
        raw_socket.set_heartbeat_interval(Duration::from_millis(0))?;
        raw_socket.set_heartbeat_timeout(Duration::from_millis(0))?;
        raw_socket.set_heartbeat_ttl(Duration::from_millis(0))?;
    }

    *mutex = maybe;
    Ok(())
}

/// Socket heartbeating configuration.
///
/// # Example
/// ```
/// use libzmq::Heartbeat;
/// use std::time::Duration;
///
/// let duration = Duration::from_millis(300);
///
/// let hb = Heartbeat::new(duration)
///     .add_timeout(2 * duration);
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Heartbeat {
    #[serde(with = "humantime_serde")]
    pub(crate) interval: Duration,
    pub(crate) timeout: Period,
    pub(crate) ttl: Period,
}

impl Heartbeat {
    /// Create a new `Heartbeat` from the given interval.
    ///
    /// This interval specifies the duration between each heartbeat.
    pub fn new<D>(interval: D) -> Self
    where
        D: Into<Duration>,
    {
        Self {
            interval: interval.into(),
            timeout: Infinite,
            ttl: Infinite,
        }
    }

    /// Returns the interval between each heartbeat.
    pub fn interval(&self) -> Duration {
        self.interval
    }

    /// Set a timeout for the `Heartbeat`.
    ///
    /// This timeout specifies how long to wait before timing out a connection
    /// with a peer for not receiving any traffic.
    pub fn add_timeout<D>(mut self, timeout: D) -> Self
    where
        D: Into<Duration>,
    {
        self.timeout = Finite(timeout.into());
        self
    }

    /// Returns the heartbeat timeout.
    pub fn timeout(&self) -> Period {
        self.timeout
    }

    /// Set a ttl for the `Heartbeat`
    ///
    /// This ttl is equivalent to a `heartbeat_timeout` for the remote
    /// side for this specific connection.
    pub fn add_ttl<D>(mut self, ttl: D) -> Self
    where
        D: Into<Duration>,
    {
        self.ttl = Finite(ttl.into());
        self
    }

    /// Returns the heartbeat ttl.
    pub fn ttl(&self) -> Period {
        self.ttl
    }
}

impl<'a> From<&'a Heartbeat> for Heartbeat {
    fn from(hb: &'a Heartbeat) -> Self {
        hb.to_owned()
    }
}

/// A trait that indicates that the socket supports configurable
/// heartbeating.
///
/// The actual heartbeating will be done by the engine in the background.
///
/// Only applies to connection based transports such as `TCP`.
///
/// # Contract
/// * timeout and interval duration in ms cannot exceed i32::MAX
/// * ttl duration in ms cannot exceed 6553599
///
/// # Default value
/// `None` (no heartbeating)
///
/// # Return Errors
/// * [`InvalidInput`]: (if contract is not respected)
///
/// # Example
/// ```
/// # fn main() -> Result<(), anyhow::Error> {
/// use libzmq::{prelude::*, Client, Heartbeat, auth::*};
/// use std::time::Duration;
///
/// let client = Client::new()?;
/// assert_eq!(client.heartbeat(), None);
///
/// let duration = Duration::from_millis(300);
/// let hb = Heartbeat::new(duration)
///     .add_timeout(2 * duration);
/// let expected = hb.clone();
///
/// client.set_heartbeat(Some(hb))?;
/// assert_eq!(client.heartbeat(), Some(expected));
/// #
/// #     Ok(())
/// # }
/// ```
///
/// [`Mechanism`]: ../auth/enum.Mechanism.html
pub trait Heartbeating: GetRawSocket {
    /// Returns a the socket's heartbeating configuration.
    fn heartbeat(&self) -> Option<Heartbeat> {
        self.raw_socket().heartbeat().lock().unwrap().to_owned()
    }

    /// Sets the socket's heartbeating configuration.
    fn set_heartbeat(&self, maybe: Option<Heartbeat>) -> Result<(), Error> {
        let raw_socket = self.raw_socket();
        let mutex = raw_socket.heartbeat().lock().unwrap();

        set_heartbeat(raw_socket, maybe, mutex)
    }
}

#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)]
#[doc(hidden)]
pub struct HeartbeatingConfig {
    pub(crate) heartbeat: Option<Heartbeat>,
}

impl HeartbeatingConfig {
    pub(crate) fn apply<S: Heartbeating>(
        &self,
        socket: &S,
    ) -> Result<(), Error> {
        socket.set_heartbeat(self.heartbeat.clone())
    }
}

#[doc(hidden)]
pub trait GetHeartbeatingConfig: private::Sealed {
    fn heartbeat_config(&self) -> &HeartbeatingConfig;

    fn heartbeat_config_mut(&mut self) -> &mut HeartbeatingConfig;
}

impl GetHeartbeatingConfig for HeartbeatingConfig {
    fn heartbeat_config(&self) -> &HeartbeatingConfig {
        self
    }

    fn heartbeat_config_mut(&mut self) -> &mut HeartbeatingConfig {
        self
    }
}

/// A set of provided methods for a socket configuration.
pub trait ConfigureHeartbeating: GetHeartbeatingConfig {
    fn heartbeat(&self) -> Option<&Heartbeat> {
        self.heartbeat_config().heartbeat.as_ref()
    }

    fn set_heartbeat(&mut self, maybe: Option<Heartbeat>) {
        self.heartbeat_config_mut().heartbeat = maybe;
    }
}

impl ConfigureHeartbeating for HeartbeatingConfig {}

/// A set of provided methods for a socket builder.
pub trait BuildHeartbeating: GetHeartbeatingConfig + Sized {
    fn heartbeat<H>(&mut self, heartbeat: H) -> &mut Self
    where
        H: Into<Heartbeat>,
    {
        self.heartbeat_config_mut()
            .set_heartbeat(Some(heartbeat.into()));
        self
    }
}