Skip to content

Commit

Permalink
Improve event deserialization by sacrificing GuildUnavailable & `Un…
Browse files Browse the repository at this point in the history
…known` events (serenity-rs#1938, serenity-rs#1951)

BREAKING CHANGES: Removes the `guild_unavailable` & `unknown` methods
from the `EventHandler` trait.
  • Loading branch information
GnomedDev authored and arqunis committed Nov 13, 2022
1 parent ffcf8f4 commit fc52767
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 344 deletions.
19 changes: 7 additions & 12 deletions src/cache/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use crate::model::event::{
GuildRoleDeleteEvent,
GuildRoleUpdateEvent,
GuildStickersUpdateEvent,
GuildUnavailableEvent,
GuildUpdateEvent,
MessageCreateEvent,
MessageUpdateEvent,
Expand Down Expand Up @@ -214,6 +213,13 @@ impl CacheUpdate for GuildDeleteEvent {
type Output = Guild;

fn update(&mut self, cache: &Cache) -> Option<Self::Output> {
if self.guild.unavailable {
cache.unavailable_guilds.insert(self.guild.id);
cache.guilds.remove(&self.guild.id);

return None;
}

match cache.guilds.remove(&self.guild.id) {
Some(guild) => {
for (channel_id, channel) in &guild.1.channels {
Expand Down Expand Up @@ -397,17 +403,6 @@ impl CacheUpdate for GuildStickersUpdateEvent {
}
}

impl CacheUpdate for GuildUnavailableEvent {
type Output = ();

fn update(&mut self, cache: &Cache) -> Option<()> {
cache.unavailable_guilds.insert(self.guild_id);
cache.guilds.remove(&self.guild_id);

None
}
}

impl CacheUpdate for GuildUpdateEvent {
type Output = ();

Expand Down
8 changes: 3 additions & 5 deletions src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,8 @@ pub struct Cache {
/// A map of channels in [`Guild`]s that the current user has received data
/// for.
///
/// When a [`Event::GuildDelete`] or [`Event::GuildUnavailable`] is
/// received and processed by the cache, the relevant channels are also
/// removed from this map.
/// When a [`Event::GuildDelete`] is received and processed by the cache,
/// the relevant channels are also removed from this map.
pub(crate) channels: DashMap<ChannelId, GuildChannel>,
/// Cache of channels that have been fetched via to_channel.
///
Expand All @@ -182,8 +181,7 @@ pub struct Cache {
pub(crate) private_channels: DashMap<ChannelId, PrivateChannel>,
/// The total number of shards being used by the bot.
pub(crate) shard_count: RwLock<u64>,
/// A list of guilds which are "unavailable". Refer to the documentation for
/// [`Event::GuildUnavailable`] for more information on when this can occur.
/// A list of guilds which are "unavailable".
///
/// Additionally, guilds are always unavailable for bot users when a Ready
/// is received. Guilds are "sent in" over time through the receiving of
Expand Down
4 changes: 1 addition & 3 deletions src/client/bridge/gateway/shard_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use async_tungstenite::tungstenite;
use async_tungstenite::tungstenite::error::Error as TungsteniteError;
use async_tungstenite::tungstenite::protocol::frame::CloseFrame;
use futures::channel::mpsc::{self, UnboundedReceiver as Receiver, UnboundedSender as Sender};
use serde::Deserialize;
use tokio::sync::RwLock;
use tracing::{debug, error, info, instrument, trace, warn};
use typemap_rev::TypeMap;
Expand Down Expand Up @@ -529,8 +528,7 @@ impl ShardRunner {
#[instrument(skip(self))]
async fn recv_event(&mut self) -> Result<(Option<Event>, Option<ShardAction>, bool)> {
let gw_event = match self.shard.client.recv_json().await {
Ok(Some(value)) => GatewayEvent::deserialize(value).map(Some).map_err(From::from),
Ok(None) => Ok(None),
Ok(inner) => Ok(inner),
Err(Error::Tungstenite(TungsteniteError::Io(_))) => {
debug!("Attempting to auto-reconnect");

Expand Down
15 changes: 2 additions & 13 deletions src/client/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::sync::Arc;
use futures::channel::mpsc::UnboundedSender as Sender;
use futures::future::{BoxFuture, FutureExt};
use tokio::sync::RwLock;
use tracing::instrument;
use tracing::{instrument, warn};
use typemap_rev::TypeMap;

#[cfg(feature = "gateway")]
Expand Down Expand Up @@ -597,13 +597,6 @@ async fn handle_event(
event_handler.guild_stickers_update(context, event.guild_id, event.stickers).await;
});
},
Event::GuildUnavailable(mut event) => {
update(&cache_and_http, &mut event);

spawn_named("dispatch::event_handler::guild_unavailable", async move {
event_handler.guild_unavailable(context, event.guild_id).await;
});
},
Event::GuildUpdate(mut event) => {
spawn_named("dispatch::event_handler::guild_update", async move {
feature_cache! {{
Expand Down Expand Up @@ -704,11 +697,7 @@ async fn handle_event(
event_handler.typing_start(context, event).await;
});
},
Event::Unknown(event) => {
spawn_named("dispatch::event_handler::unknown", async move {
event_handler.unknown(context, event.kind, event.value).await;
});
},
Event::Unknown => warn!("An unknown event was received"),
Event::UserUpdate(mut event) => {
let _before = update(&cache_and_http, &mut event);

Expand Down
11 changes: 0 additions & 11 deletions src/client/event_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use async_trait::async_trait;
use super::context::Context;
use crate::client::bridge::gateway::event::*;
use crate::http::ratelimiting::RatelimitInfo;
use crate::json::Value;
use crate::model::application::command::CommandPermission;
use crate::model::application::interaction::Interaction;
use crate::model::guild::automod::{ActionExecution, Rule};
Expand Down Expand Up @@ -281,11 +280,6 @@ pub trait EventHandler: Send + Sync {
) {
}

/// Dispatched when a guild became unavailable.
///
/// Provides the guild's id.
async fn guild_unavailable(&self, _ctx: Context, _guild_id: GuildId) {}

/// Dispatched when the guild is updated.
///
/// Provides the guild's old full data (if available) and the new, albeit partial data.
Expand Down Expand Up @@ -412,11 +406,6 @@ pub trait EventHandler: Send + Sync {
/// Dispatched when a user starts typing.
async fn typing_start(&self, _ctx: Context, _: TypingStartEvent) {}

/// Dispatched when an unknown event was sent from discord.
///
/// Provides the event's name and its unparsed data.
async fn unknown(&self, _ctx: Context, _name: String, _raw: Value) {}

/// Dispatched when the bot's data is updated.
///
/// Provides the old and new data.
Expand Down
3 changes: 2 additions & 1 deletion src/gateway/ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::client::bridge::gateway::ChunkGuildFilter;
use crate::constants::{self, OpCode};
use crate::gateway::{CurrentPresence, GatewayError};
use crate::json::{from_str, json, to_string, Value};
use crate::model::event::GatewayEvent;
use crate::model::gateway::GatewayIntents;
use crate::model::id::GuildId;
use crate::{Error, Result};
Expand All @@ -38,7 +39,7 @@ impl WsClient {
Ok(Self(stream))
}

pub(crate) async fn recv_json(&mut self) -> Result<Option<Value>> {
pub(crate) async fn recv_json(&mut self) -> Result<Option<GatewayEvent>> {
let message = match timeout(TIMEOUT, self.0.next()).await {
Ok(Some(Ok(msg))) => msg,
Ok(Some(Err(e))) => return Err(e.into()),
Expand Down
Loading

0 comments on commit fc52767

Please sign in to comment.