Skip to content

Commit

Permalink
Use stretto instead of moka. (#9)
Browse files Browse the repository at this point in the history
* put entire bot in single Arc

* moka -> stretto

* Remove unecessary Arcs from cache

* Update Dockerfile

* simplify stretto cache creation
  • Loading branch information
circuitsacul authored Jul 10, 2022
1 parent 6828001 commit 36c306a
Show file tree
Hide file tree
Showing 21 changed files with 169 additions and 373 deletions.
393 changes: 82 additions & 311 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ serde_json = "1.0"
serde = "1.0.137"
emojis = "0.4.0"
dashmap = "5.3.4"
moka = { version = "0.9.0", features = ["future"] }
stretto = { version = "0.5.1", features = ["async"] }
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM rust:1.62.0 as builder
FROM rust:1.62.0-slim-buster as builder
WORKDIR /usr/src/starboard

# force cargo to update the crates.io index.
Expand Down
33 changes: 18 additions & 15 deletions src/cache/cache.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::sync::Arc;

use dashmap::{DashMap, DashSet};
use twilight_gateway::Event;
use twilight_model::id::{
Expand All @@ -11,30 +9,35 @@ use crate::constants;

use super::{models::message::CachedMessage, update::UpdateCache};

#[derive(Clone)]
pub struct Cache {
// discord side
pub guild_emojis: Arc<DashMap<Id<GuildMarker>, DashSet<Id<EmojiMarker>>>>,
pub messages: moka::future::Cache<Id<MessageMarker>, Arc<CachedMessage>>,
pub guild_emojis: DashMap<Id<GuildMarker>, DashSet<Id<EmojiMarker>>>,
pub messages: stretto::AsyncCache<Id<MessageMarker>, CachedMessage>,

// database side
pub autostar_channel_ids: Arc<DashSet<Id<ChannelMarker>>>,
pub autostar_channel_ids: DashSet<Id<ChannelMarker>>,

// autocomplete
pub guild_autostar_channel_names: moka::future::Cache<Id<GuildMarker>, Arc<Vec<String>>>,
pub guild_autostar_channel_names: stretto::AsyncCache<Id<GuildMarker>, Vec<String>>,
}

impl Cache {
pub fn new(autostar_channel_ids: DashSet<Id<ChannelMarker>>) -> Self {
Self {
guild_emojis: Arc::new(DashMap::new()),
messages: moka::future::Cache::builder()
.max_capacity(constants::MAX_MESSAGES)
.build(),
autostar_channel_ids: Arc::new(autostar_channel_ids),
guild_autostar_channel_names: moka::future::Cache::builder()
.max_capacity(constants::MAX_AUTOSTAR_NAMES)
.build(),
guild_emojis: DashMap::new(),
messages: stretto::AsyncCache::new(
(constants::MAX_MESSAGES * 10).try_into().unwrap(),
constants::MAX_MESSAGES.into(),
tokio::spawn,
)
.unwrap(),
autostar_channel_ids,
guild_autostar_channel_names: stretto::AsyncCache::new(
(constants::MAX_AUTOSTAR_NAMES * 10).try_into().unwrap(),
constants::MAX_AUTOSTAR_NAMES.into(),
tokio::spawn,
)
.unwrap(),
}
}

Expand Down
14 changes: 6 additions & 8 deletions src/cache/events/message.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::sync::Arc;

use async_trait::async_trait;
use twilight_model::gateway::payload::incoming::{
MessageCreate, MessageDelete, MessageDeleteBulk, MessageUpdate,
Expand All @@ -21,22 +19,22 @@ impl UpdateCache for MessageCreate {
embeds: self.embeds.clone(),
};

cache.messages.insert(message.id, Arc::new(message)).await;
cache.messages.insert(message.id, message, 1).await;
}
}

#[async_trait]
impl UpdateCache for MessageDelete {
async fn update_cache(&self, cache: &Cache) {
cache.messages.invalidate(&self.id).await;
cache.messages.remove(&self.id).await;
}
}

#[async_trait]
impl UpdateCache for MessageDeleteBulk {
async fn update_cache(&self, cache: &Cache) {
for id in &self.ids {
cache.messages.invalidate(id).await;
cache.messages.remove(id).await;
}
}
}
Expand All @@ -53,11 +51,11 @@ impl UpdateCache for MessageUpdate {

let attachments = match &self.attachments {
Some(attachments) => attachments.clone(),
None => cached.attachments.clone(),
None => cached.value().attachments.clone(),
};
let embeds = match &self.embeds {
Some(embeds) => embeds.clone(),
None => cached.embeds.clone(),
None => cached.value().embeds.clone(),
};

let message = CachedMessage {
Expand All @@ -66,6 +64,6 @@ impl UpdateCache for MessageUpdate {
embeds,
};

cache.messages.insert(self.id, Arc::new(message)).await;
cache.messages.insert(self.id, message, 1).await;
}
}
35 changes: 17 additions & 18 deletions src/client/bot.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fmt::Debug, sync::Arc};
use std::fmt::Debug;

use sqlx::PgPool;
use tokio::sync::RwLock;
Expand All @@ -18,17 +18,16 @@ use crate::{cache::cache::Cache, client::config::Config};

use super::cooldowns::Cooldowns;

#[derive(Clone)]
pub struct StarboardBot {
pub cluster: Arc<Cluster>,
pub http: Arc<HttpClient>,
pub cluster: Cluster,
pub http: HttpClient,
pub cache: Cache,
pub application: Arc<RwLock<Option<PartialApplication>>>,
pub pool: Arc<PgPool>,
pub errors: Arc<ErrorHandler>,
pub standby: Arc<Standby>,
pub config: Arc<Config>,
pub cooldowns: Arc<Cooldowns>,
pub application: RwLock<Option<PartialApplication>>,
pub pool: PgPool,
pub errors: ErrorHandler,
pub standby: Standby,
pub config: Config,
pub cooldowns: Cooldowns,
}

impl Debug for StarboardBot {
Expand Down Expand Up @@ -89,15 +88,15 @@ impl StarboardBot {
Ok((
events,
Self {
cluster: Arc::new(cluster),
http: Arc::new(http),
cluster: cluster,
http: http,
cache: cache,
application: Arc::new(RwLock::new(None)),
pool: Arc::new(pool),
errors: Arc::new(errors),
standby: Arc::new(Standby::new()),
config: Arc::new(config),
cooldowns: Arc::new(Cooldowns::new()),
application: RwLock::new(None),
pool: pool,
errors: errors,
standby: Standby::new(),
config: config,
cooldowns: Cooldowns::new(),
},
))
}
Expand Down
6 changes: 5 additions & 1 deletion src/client/runner.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

use futures::stream::StreamExt;
use tokio::{
signal::unix::{signal, SignalKind},
Expand All @@ -7,7 +9,7 @@ use twilight_gateway::cluster::Events;

use crate::{client::bot::StarboardBot, events::handle_event};

async fn shutdown_handler(bot: StarboardBot) {
async fn shutdown_handler(bot: Arc<StarboardBot>) {
let (tx, mut rx) = mpsc::unbounded_channel();

for kind in [SignalKind::terminate(), SignalKind::interrupt()].into_iter() {
Expand All @@ -26,6 +28,8 @@ async fn shutdown_handler(bot: StarboardBot) {
}

pub async fn run(mut events: Events, bot: StarboardBot) {
let bot = Arc::new(bot);

if bot.config.development {
println!("Running bot in development mode.");
}
Expand Down
4 changes: 2 additions & 2 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::time::Duration;
pub const BOT_COLOR: u32 = 0xFFE19C;

// Cache size
pub const MAX_MESSAGES: u64 = 10_000;
pub const MAX_AUTOSTAR_NAMES: u64 = 100;
pub const MAX_MESSAGES: u32 = 10_000;
pub const MAX_AUTOSTAR_NAMES: u32 = 100;

// Cooldowns
pub const AUTOSTAR_COOLDOWN: (u32, Duration) = (5, Duration::from_secs(20));
Expand Down
3 changes: 2 additions & 1 deletion src/core/autostar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,9 @@ async fn get_status(bot: &StarboardBot, asc: &AutoStarChannel, event: &MessageCr
let updated_msg = bot.cache.messages.get(&event.id);
let mut still_invalid = true;

// AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
if let Some(msg) = updated_msg {
if has_image(&msg.embeds, &msg.attachments) {
if has_image(&msg.value().embeds, &msg.value().attachments) {
still_invalid = false;
}
} else {
Expand Down
8 changes: 5 additions & 3 deletions src/events/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

use twilight_gateway::Event;
use twilight_model::gateway::payload::outgoing::RequestGuildMembers;

Expand All @@ -7,11 +9,11 @@ use crate::{
interactions::{commands::register::post_commands, handle::handle_interaction},
};

pub async fn handle_event(shard_id: u64, event: Event, bot: StarboardBot) {
pub async fn handle_event(shard_id: u64, event: Event, bot: Arc<StarboardBot>) {
tokio::spawn(internal_handle_event(shard_id, event, bot));
}

async fn internal_handle_event(shard_id: u64, event: Event, bot: StarboardBot) {
async fn internal_handle_event(shard_id: u64, event: Event, bot: Arc<StarboardBot>) {
bot.cache.update(&event).await;
bot.standby.process(&event);

Expand All @@ -26,7 +28,7 @@ async fn internal_handle_event(shard_id: u64, event: Event, bot: StarboardBot) {
}
}

async fn match_events(shard_id: u64, event: Event, bot: StarboardBot) -> anyhow::Result<()> {
async fn match_events(shard_id: u64, event: Event, bot: Arc<StarboardBot>) -> anyhow::Result<()> {
match event {
Event::InteractionCreate(int) => handle_interaction(shard_id, int.0, bot).await?,
Event::ShardConnected(event) => println!("Shard {} connected.", event.shard_id),
Expand Down
2 changes: 1 addition & 1 deletion src/interactions/autocomplete/autostar_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub async fn autostar_name_autocomplete(
.guild_autostar_channel_names
.get(&interaction.guild_id.unwrap())
{
Some(names) => (*names).clone(),
Some(names) => (*names.value()).clone(),
None => {
AutoStarChannel::list_by_guild(&bot.pool, unwrap_id!(interaction.guild_id.unwrap()))
.await?
Expand Down
4 changes: 3 additions & 1 deletion src/interactions/autocomplete/handle.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

use twilight_model::{
application::interaction::{
application_command_autocomplete::{
Expand Down Expand Up @@ -61,7 +63,7 @@ pub fn qualified_name(interaction: &Box<ApplicationCommandAutocomplete>) -> Stri
}

pub async fn handle_autocomplete(
bot: StarboardBot,
bot: Arc<StarboardBot>,
interaction: Box<ApplicationCommandAutocomplete>,
) -> anyhow::Result<()> {
let options = match qualified_name(&interaction).as_str() {
Expand Down
2 changes: 1 addition & 1 deletion src/interactions/commands/chat/autostar/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl CreateAutoStarChannel {
ctx.bot
.cache
.guild_autostar_channel_names
.invalidate(&guild_id)
.remove(&guild_id)
.await;

ctx.respond_str(
Expand Down
2 changes: 1 addition & 1 deletion src/interactions/commands/chat/autostar/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl DeleteAutoStarChannel {
ctx.bot
.cache
.guild_autostar_channel_names
.invalidate(&guild_id)
.remove(&guild_id)
.await;
ctx.respond_str(&format!("Deleted autostar channel '{}'.", self.name), false)
.await?;
Expand Down
2 changes: 1 addition & 1 deletion src/interactions/commands/chat/autostar/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl RenameAutoStarChannel {
ctx.bot
.cache
.guild_autostar_channel_names
.invalidate(&guild_id)
.remove(&guild_id)
.await;

ctx.respond_str(
Expand Down
10 changes: 8 additions & 2 deletions src/interactions/commands/context.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

use twilight_http::Response;
use twilight_model::{
application::interaction::ApplicationCommand,
Expand All @@ -11,15 +13,19 @@ use crate::client::bot::StarboardBot;
#[derive(Debug)]
pub struct CommandCtx {
pub shard_id: u64,
pub bot: StarboardBot,
pub bot: Arc<StarboardBot>,
pub interaction: Box<ApplicationCommand>,
responded: bool,
}

type TwResult = Result<Response<Message>, twilight_http::Error>;

impl CommandCtx {
pub fn new(shard_id: u64, bot: StarboardBot, interaction: Box<ApplicationCommand>) -> Self {
pub fn new(
shard_id: u64,
bot: Arc<StarboardBot>,
interaction: Box<ApplicationCommand>,
) -> Self {
Self {
shard_id,
bot,
Expand Down
4 changes: 3 additions & 1 deletion src/interactions/commands/handle.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

use twilight_interactions::command::CommandModel;
use twilight_model::application::interaction::ApplicationCommand;

Expand All @@ -21,7 +23,7 @@ macro_rules! match_commands {

pub async fn handle_command(
shard_id: u64,
bot: StarboardBot,
bot: Arc<StarboardBot>,
interaction: Box<ApplicationCommand>,
) -> anyhow::Result<()> {
let ctx = CommandCtx::new(shard_id, bot, interaction);
Expand Down
4 changes: 3 additions & 1 deletion src/interactions/commands/register.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

use twilight_interactions::command::CreateCommand;

use crate::{client::bot::StarboardBot, interactions::commands::chat};
Expand All @@ -12,7 +14,7 @@ macro_rules! commands_to_create {
};
}

pub async fn post_commands(bot: StarboardBot) {
pub async fn post_commands(bot: Arc<StarboardBot>) {
let inter_client = bot.interaction_client().await;

let commands = commands_to_create!(chat::ping::Ping, chat::autostar::AutoStar);
Expand Down
4 changes: 3 additions & 1 deletion src/interactions/components/dismiss.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::sync::Arc;

use twilight_model::application::interaction::MessageComponentInteraction;

use crate::client::bot::StarboardBot;

pub async fn handle_dismiss(
bot: StarboardBot,
bot: Arc<StarboardBot>,
interaction: Box<MessageComponentInteraction>,
) -> anyhow::Result<()> {
assert!(interaction.is_dm());
Expand Down
4 changes: 3 additions & 1 deletion src/interactions/components/handle.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::sync::Arc;

use twilight_model::application::interaction::MessageComponentInteraction;

use crate::client::bot::StarboardBot;

use super::dismiss::handle_dismiss;

pub async fn handle_component(
bot: StarboardBot,
bot: Arc<StarboardBot>,
interaction: Box<MessageComponentInteraction>,
) -> anyhow::Result<()> {
match interaction.data.custom_id.as_str() {
Expand Down
Loading

0 comments on commit 36c306a

Please sign in to comment.