2019-07-08 13:31:42 +02:00
|
|
|
//! Receives data from Redis, sorts it by `ClientAgent`, and stores it until
|
|
|
|
//! polled by the correct `ClientAgent`. Also manages sububscriptions and
|
|
|
|
//! unsubscriptions to/from Redis.
|
2020-04-01 21:35:24 +02:00
|
|
|
mod err;
|
2019-10-09 20:46:56 +02:00
|
|
|
mod message_queues;
|
2020-03-27 17:00:48 +01:00
|
|
|
|
2020-04-01 21:35:24 +02:00
|
|
|
pub use err::ReceiverErr;
|
2020-03-27 17:00:48 +01:00
|
|
|
pub use message_queues::{MessageQueues, MsgQueue};
|
|
|
|
|
2020-04-01 21:35:24 +02:00
|
|
|
use super::redis::{redis_connection::RedisCmd, RedisConn};
|
|
|
|
|
2019-10-09 20:46:56 +02:00
|
|
|
use crate::{
|
2020-03-27 17:00:48 +01:00
|
|
|
config,
|
2020-03-25 22:50:32 +01:00
|
|
|
messages::Event,
|
2020-04-01 21:35:24 +02:00
|
|
|
parse_client_request::{Stream, Subscription, Timeline},
|
2019-10-09 02:35:26 +02:00
|
|
|
};
|
2020-04-01 21:35:24 +02:00
|
|
|
|
2019-04-30 15:44:51 +02:00
|
|
|
use futures::{Async, Poll};
|
2020-04-01 21:35:24 +02:00
|
|
|
use std::{
|
|
|
|
collections::HashMap,
|
|
|
|
result,
|
|
|
|
sync::{Arc, Mutex},
|
|
|
|
};
|
2019-05-10 07:47:29 +02:00
|
|
|
use uuid::Uuid;
|
2019-05-09 05:02:01 +02:00
|
|
|
|
2020-04-01 21:35:24 +02:00
|
|
|
type Result<T> = result::Result<T, ReceiverErr>;
|
|
|
|
|
2019-07-08 21:21:02 +02:00
|
|
|
/// The item that streams from Redis and is polled by the `ClientAgent`
|
2019-04-30 15:44:51 +02:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Receiver {
|
2020-03-31 00:54:00 +02:00
|
|
|
redis_connection: RedisConn,
|
2019-10-09 20:46:56 +02:00
|
|
|
pub msg_queues: MessageQueues,
|
2020-03-19 01:37:10 +01:00
|
|
|
clients_per_timeline: HashMap<Timeline, i32>,
|
|
|
|
}
|
2020-03-27 17:00:48 +01:00
|
|
|
|
2019-04-30 15:44:51 +02:00
|
|
|
impl Receiver {
|
2019-07-06 02:08:50 +02:00
|
|
|
/// Create a new `Receiver`, with its own Redis connections (but, as yet, no
|
|
|
|
/// active subscriptions).
|
2020-04-01 21:35:24 +02:00
|
|
|
pub fn try_from(redis_cfg: config::RedisConfig) -> Result<Self> {
|
|
|
|
let redis_connection = RedisConn::new(redis_cfg)?;
|
2019-10-03 06:34:41 +02:00
|
|
|
|
2020-04-01 21:35:24 +02:00
|
|
|
Ok(Self {
|
2020-03-31 00:54:00 +02:00
|
|
|
redis_connection,
|
2020-03-19 01:37:10 +01:00
|
|
|
msg_queues: MessageQueues(HashMap::new()),
|
|
|
|
clients_per_timeline: HashMap::new(),
|
2020-04-01 21:35:24 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn into_arc(self) -> Arc<Mutex<Self>> {
|
|
|
|
Arc::new(Mutex::new(self))
|
2019-04-30 15:44:51 +02:00
|
|
|
}
|
2019-05-10 07:47:29 +02:00
|
|
|
|
2019-07-06 02:08:50 +02:00
|
|
|
/// Assigns the `Receiver` a new timeline to monitor and runs other
|
|
|
|
/// first-time setup.
|
|
|
|
///
|
2019-07-08 13:31:42 +02:00
|
|
|
/// Note: this method calls `subscribe_or_unsubscribe_as_needed`,
|
2019-07-06 02:08:50 +02:00
|
|
|
/// so Redis PubSub subscriptions are only updated when a new timeline
|
|
|
|
/// comes under management for the first time.
|
2020-04-01 21:35:24 +02:00
|
|
|
pub fn add_subscription(&mut self, subscription: &Subscription) -> Result<()> {
|
|
|
|
let (tag, tl) = (subscription.hashtag_name.clone(), subscription.timeline);
|
2020-03-19 01:37:10 +01:00
|
|
|
|
2020-04-01 21:35:24 +02:00
|
|
|
if let (Some(hashtag), Timeline(Stream::Hashtag(id), _, _)) = (tag, tl) {
|
|
|
|
self.redis_connection.update_cache(hashtag, id);
|
2019-09-28 23:57:37 +02:00
|
|
|
};
|
2020-04-01 21:35:24 +02:00
|
|
|
self.msg_queues.insert(subscription.id, MsgQueue::new(tl));
|
|
|
|
self.subscribe_or_unsubscribe_as_needed(tl)?;
|
|
|
|
Ok(())
|
2019-07-06 02:08:50 +02:00
|
|
|
}
|
2019-04-30 15:44:51 +02:00
|
|
|
|
2019-07-08 13:31:42 +02:00
|
|
|
/// Returns the oldest message in the `ClientAgent`'s queue (if any).
|
|
|
|
///
|
|
|
|
/// Note: This method does **not** poll Redis every time, because polling
|
2020-03-25 22:50:32 +01:00
|
|
|
/// Redis is significantly more time consuming that simply returning the
|
2019-07-08 13:31:42 +02:00
|
|
|
/// message already in a queue. Thus, we only poll Redis if it has not
|
|
|
|
/// been polled lately.
|
2020-04-01 21:35:24 +02:00
|
|
|
pub fn poll_for(&mut self, id: Uuid, timeline: Timeline) -> Poll<Option<Event>, ReceiverErr> {
|
2020-03-31 00:54:00 +02:00
|
|
|
loop {
|
|
|
|
match self.redis_connection.poll_redis() {
|
|
|
|
Ok(Async::Ready(Some((timeline, event)))) => self
|
|
|
|
.msg_queues
|
|
|
|
.values_mut()
|
|
|
|
.filter(|msg_queue| msg_queue.timeline == timeline)
|
|
|
|
.for_each(|msg_queue| {
|
|
|
|
msg_queue.messages.push_back(event.clone());
|
|
|
|
}),
|
|
|
|
Ok(Async::NotReady) => break,
|
|
|
|
Ok(Async::Ready(None)) => (),
|
|
|
|
Err(err) => Err(err)?,
|
2020-03-27 17:00:48 +01:00
|
|
|
}
|
2019-07-08 13:31:42 +02:00
|
|
|
}
|
2019-04-30 15:44:51 +02:00
|
|
|
|
2019-05-10 07:47:29 +02:00
|
|
|
// If the `msg_queue` being polled has any new messages, return the first (oldest) one
|
2020-04-01 21:35:24 +02:00
|
|
|
match self.msg_queues.get_mut(&id) {
|
|
|
|
Some(msg_q) => {
|
|
|
|
msg_q.update_polled_at_time();
|
|
|
|
match msg_q.messages.pop_front() {
|
|
|
|
Some(event) => Ok(Async::Ready(Some(event))),
|
|
|
|
None => Ok(Async::NotReady),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
log::error!("Polled a MsgQueue that had not been set up. Setting it up now.");
|
|
|
|
self.msg_queues.insert(id, MsgQueue::new(timeline));
|
|
|
|
Ok(Async::NotReady)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Drop any PubSub subscriptions that don't have active clients and check
|
|
|
|
/// that there's a subscription to the current one. If there isn't, then
|
|
|
|
/// subscribe to it.
|
|
|
|
fn subscribe_or_unsubscribe_as_needed(&mut self, tl: Timeline) -> Result<()> {
|
|
|
|
let timelines_to_modify = self.msg_queues.calculate_timelines_to_add_or_drop(tl);
|
|
|
|
|
|
|
|
// Record the lower number of clients subscribed to that channel
|
|
|
|
for change in timelines_to_modify {
|
|
|
|
let timeline = change.timeline;
|
|
|
|
|
|
|
|
let count_of_subscribed_clients = self
|
|
|
|
.clients_per_timeline
|
|
|
|
.entry(timeline)
|
|
|
|
.and_modify(|n| *n += change.in_subscriber_number)
|
|
|
|
.or_insert_with(|| 1);
|
|
|
|
|
|
|
|
// If no clients, unsubscribe from the channel
|
|
|
|
use RedisCmd::*;
|
|
|
|
if *count_of_subscribed_clients <= 0 {
|
|
|
|
self.redis_connection.send_cmd(Unsubscribe, &timeline)?;
|
|
|
|
} else if *count_of_subscribed_clients == 1 && change.in_subscriber_number == 1 {
|
|
|
|
self.redis_connection.send_cmd(Subscribe, &timeline)?
|
|
|
|
}
|
2019-04-30 15:44:51 +02:00
|
|
|
}
|
2020-04-01 21:35:24 +02:00
|
|
|
Ok(())
|
2019-04-30 15:44:51 +02:00
|
|
|
}
|
|
|
|
}
|