Switch to Redis from MongoDB for event broker.

Closes #1.
This commit is contained in:
Paul
2021-09-19 22:13:25 +01:00
parent e5e0031cad
commit 2ef6b28029
9 changed files with 176 additions and 15 deletions

View File

@@ -17,6 +17,7 @@ extern crate ctrlc;
pub mod database;
pub mod notifications;
pub mod routes;
pub mod redis;
pub mod util;
pub mod version;
@@ -47,6 +48,7 @@ async fn main() {
util::variables::preflight_checks();
database::connect().await;
redis::connect().await;
notifications::hive::init_hive().await;
ctrlc::set_handler(move || {
@@ -56,7 +58,7 @@ async fn main() {
.expect("Error setting Ctrl-C handler");
let web_task = task::spawn(launch_web());
let hive_task = task::spawn(notifications::hive::listen());
let hive_task = task::spawn_local(notifications::hive::listen());
join!(
web_task,

View File

@@ -187,7 +187,7 @@ impl ClientboundNotification {
pub fn publish(self, topic: String) {
async_std::task::spawn(async move {
prehandle_hook(&self).await.ok(); // ! FIXME: this should be moved to pubsub
hive_pubsub::backend::mongo::publish(get_hive(), &topic, self)
hive_pubsub::backend::redis::publish(get_hive(), topic, self)
.await
.ok();
});

View File

@@ -1,18 +1,23 @@
use std::sync::{Arc, Mutex};
use super::{events::ClientboundNotification, websocket};
use crate::database::*;
use crate::redis::get_pool;
use crate::util::variables::REDIS_URI;
use futures::FutureExt;
use hive_pubsub::backend::mongo::MongodbPubSub;
use hive_pubsub::backend::redis::RedisPubSub;
use hive_pubsub::PubSub;
use log::{debug, error};
use once_cell::sync::OnceCell;
use serde_json::to_string;
type Hive = MongodbPubSub<String, String, ClientboundNotification>;
static HIVE: OnceCell<Hive> = OnceCell::new();
type Hive<'a> = RedisPubSub<'a, String, String, ClientboundNotification>;
static HIVE: OnceCell<Hive<'static>> = OnceCell::new();
pub async fn init_hive() {
let hive = MongodbPubSub::new(
let pubsub_con = redis::Client::open(REDIS_URI.to_string()).unwrap().get_async_connection().await.unwrap().into_pubsub();
let hive = RedisPubSub::new(
|ids, notification: ClientboundNotification| {
let notif = notification.clone();
async_std::task::spawn(async move {
@@ -26,7 +31,8 @@ pub async fn init_hive() {
error!("Failed to serialise notification.");
}
},
get_collection("pubsub"),
get_pool(),
Arc::new(Mutex::new(pubsub_con))
);
if HIVE.set(hive).is_err() {
@@ -37,6 +43,7 @@ pub async fn init_hive() {
pub async fn listen() {
HIVE.get()
.unwrap()
.clone()
.listen()
.fuse()
.await
@@ -61,6 +68,6 @@ pub fn subscribe_if_exists(user: String, topic: String) -> Result<(), String> {
Ok(())
}
pub fn get_hive() -> &'static Hive {
pub fn get_hive() -> &'static Hive<'static> {
HIVE.get().unwrap()
}

18
src/redis/mod.rs Normal file
View File

@@ -0,0 +1,18 @@
use crate::util::variables::REDIS_URI;
use mobc::Pool;
use mobc_redis::RedisConnectionManager;
use once_cell::sync::OnceCell;
static REDISPOOL: OnceCell<Pool<RedisConnectionManager>> = OnceCell::new();
pub async fn connect() {
let client = mobc_redis::redis::Client::open(REDIS_URI.to_string()).unwrap();
let manager = mobc_redis::RedisConnectionManager::new(client);
let pool = mobc::Pool::builder().max_open(100).build(manager);
REDISPOOL.set(pool).ok().unwrap();
}
pub fn get_pool() -> &'static Pool<RedisConnectionManager> {
REDISPOOL.get().unwrap()
}

View File

@@ -7,6 +7,8 @@ lazy_static! {
// Application Settings
pub static ref MONGO_URI: String =
env::var("REVOLT_MONGO_URI").expect("Missing REVOLT_MONGO_URI environment variable.");
pub static ref REDIS_URI: String =
env::var("REVOLT_REDIS_URI").expect("Missing REVOLT_REDIS_URI environment variable.");
pub static ref WS_HOST: String =
env::var("REVOLT_WS_HOST").unwrap_or_else(|_| "0.0.0.0:9000".to_string());
pub static ref PUBLIC_URL: String =

View File

@@ -1 +1 @@
pub const VERSION: &str = "0.5.3-alpha.4-patch.0";
pub const VERSION: &str = "0.5.3-alpha.5";