Skip to content

Commit 5bcb080

Browse files
committed
Send GUILD_DELETE for unavailable guilds again, avoid large box
Signed-off-by: Jens Reidel <[email protected]>
1 parent 72a9fb0 commit 5bcb080

File tree

2 files changed

+37
-40
lines changed

2 files changed

+37
-40
lines changed

src/cache.rs

+32-30
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
use halfbrown::hashmap;
33
use serde::Serialize;
44
#[cfg(not(feature = "simd-json"))]
5-
use serde_json::Value as OwnedValue;
5+
use serde_json::{to_string, Value as OwnedValue};
66
#[cfg(feature = "simd-json")]
7-
use simd_json::OwnedValue;
7+
use simd_json::{to_string, OwnedValue};
88
use twilight_cache_inmemory::{DefaultCacheModels, InMemoryCache, InMemoryCacheStats, UpdateCache};
99
use twilight_model::{
1010
channel::{message::Sticker, Channel, StageInstance},
1111
gateway::{
12-
payload::incoming::GuildCreate,
12+
payload::incoming::GuildDelete,
1313
presence::{Presence, UserOrId},
1414
OpCode,
1515
},
16-
guild::{scheduled_event::GuildScheduledEvent, Emoji, Guild, Member, Role, UnavailableGuild},
16+
guild::{scheduled_event::GuildScheduledEvent, Emoji, Guild, Member, Role},
1717
id::{
1818
marker::{GuildMarker, UserMarker},
1919
Id,
@@ -26,20 +26,13 @@ use std::sync::Arc;
2626
use crate::model::JsonObject;
2727

2828
#[derive(Serialize)]
29-
pub struct Payload {
30-
pub d: Event,
29+
pub struct Payload<T> {
30+
pub d: T,
3131
pub op: OpCode,
32-
pub t: String,
32+
pub t: &'static str,
3333
pub s: usize,
3434
}
3535

36-
#[derive(Serialize, Clone)]
37-
#[serde(untagged)]
38-
pub enum Event {
39-
Ready(JsonObject),
40-
GuildCreate(Box<GuildCreate>),
41-
}
42-
4336
pub struct Guilds(Arc<InMemoryCache>);
4437

4538
impl Guilds {
@@ -55,7 +48,11 @@ impl Guilds {
5548
self.0.stats()
5649
}
5750

58-
pub fn get_ready_payload(&self, mut ready: JsonObject, sequence: &mut usize) -> Payload {
51+
pub fn get_ready_payload(
52+
&self,
53+
mut ready: JsonObject,
54+
sequence: &mut usize,
55+
) -> Payload<JsonObject> {
5956
*sequence += 1;
6057

6158
let guild_id_to_json = |guild_id: Id<GuildMarker>| {
@@ -94,9 +91,9 @@ impl Guilds {
9491
ready.insert(String::from("guilds"), OwnedValue::Array(guilds));
9592

9693
Payload {
97-
d: Event::Ready(ready),
94+
d: ready,
9895
op: OpCode::Dispatch,
99-
t: String::from("READY"),
96+
t: "READY",
10097
s: *sequence,
10198
}
10299
}
@@ -323,15 +320,21 @@ impl Guilds {
323320
pub fn get_guild_payloads<'a>(
324321
&'a self,
325322
sequence: &'a mut usize,
326-
) -> impl Iterator<Item = Payload> + 'a {
323+
) -> impl Iterator<Item = String> + 'a {
327324
self.0.iter().guilds().map(move |guild| {
328325
*sequence += 1;
329326

330-
let guild_create = if guild.unavailable() {
331-
GuildCreate::Unavailable(UnavailableGuild {
332-
id: guild.id(),
333-
unavailable: true,
327+
if guild.unavailable() {
328+
to_string(&Payload {
329+
d: GuildDelete {
330+
id: guild.id(),
331+
unavailable: true,
332+
},
333+
op: OpCode::Dispatch,
334+
t: "GUILD_DELETE",
335+
s: *sequence,
334336
})
337+
.unwrap()
335338
} else {
336339
let guild_channels = self.channels_in_guild(guild.id());
337340
let presences = self.presences_in_guild(guild.id());
@@ -397,14 +400,13 @@ impl Guilds {
397400
widget_enabled: guild.widget_enabled(),
398401
};
399402

400-
GuildCreate::Available(new_guild)
401-
};
402-
403-
Payload {
404-
d: Event::GuildCreate(Box::new(guild_create)),
405-
op: OpCode::Dispatch,
406-
t: String::from("GUILD_CREATE"),
407-
s: *sequence,
403+
to_string(&Payload {
404+
d: new_guild,
405+
op: OpCode::Dispatch,
406+
t: "GUILD_CREATE",
407+
s: *sequence,
408+
})
409+
.unwrap()
408410
}
409411
})
410412
}

src/server.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ use tracing::{debug, error, info, trace, warn};
2828
use std::{convert::Infallible, future::ready, net::SocketAddr, sync::Arc};
2929

3030
use crate::{
31-
cache::Event,
3231
config::CONFIG,
3332
deserializer::{GatewayEvent, SequenceInfo},
3433
model::{Identify, Resume},
@@ -135,9 +134,9 @@ async fn forward_shard(
135134
.get_ready_payload(ready_payload, &mut seq);
136135

137136
// Overwrite the session ID in the READY
138-
if let Event::Ready(payload) = &mut ready_payload.d {
139-
payload.insert(String::from("session_id"), OwnedValue::String(session_id));
140-
}
137+
ready_payload
138+
.d
139+
.insert(String::from("session_id"), OwnedValue::String(session_id));
141140

142141
if let Ok(serialized) = to_string(&ready_payload) {
143142
debug!("[Shard {shard_id}] Sending newly created READY");
@@ -146,12 +145,8 @@ async fn forward_shard(
146145

147146
// Send GUILD_CREATE/GUILD_DELETEs based on guild availability
148147
for payload in shard_status.guilds.get_guild_payloads(&mut seq) {
149-
if let Ok(serialized) = to_string(&payload) {
150-
trace!(
151-
"[Shard {shard_id}] Sending newly created GUILD_CREATE/GUILD_DELETE payload",
152-
);
153-
let _res = stream_writer.send(Message::text(serialized));
154-
};
148+
trace!("[Shard {shard_id}] Sending newly created GUILD_CREATE/GUILD_DELETE payload");
149+
let _res = stream_writer.send(Message::text(payload));
155150
}
156151
} else {
157152
let _res = stream_writer.send(Message::text(RESUMED.to_string()));

0 commit comments

Comments
 (0)