Skip to content

Commit 173bea7

Browse files
committed
Update dependencies and refactor shard handling in dispatch
1 parent 1849fae commit 173bea7

File tree

4 files changed

+46
-29
lines changed

4 files changed

+46
-29
lines changed

Cargo.lock

Lines changed: 13 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ halfbrown = { version = "0.2", features = ["serde"] }
1313
hyper = { version = "0.14", default-features = false, features = [
1414
"server",
1515
"http1",
16-
"http2"
16+
"http2",
17+
"tcp"
1718
] }
1819
inotify = { version = "0.10", default-features = false, features = ["stream"] }
1920
itoa = "1.0"

src/dispatch.rs

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ use itoa::Buffer;
22
#[cfg(feature = "simd-json")]
33
use simd_json::Mutable;
44
use tokio::{sync::broadcast, time::Instant};
5+
use futures_util::StreamExt;
56
use tracing::{info, trace};
6-
use twilight_gateway::{parse, ConnectionStatus, Event, EventType, EventTypeFlags, Message, Shard};
7+
use twilight_gateway::{parse, Event, EventType, EventTypeFlags, Message, Shard};
78
use twilight_model::gateway::event::GatewayEvent as TwilightGatewayEvent;
89

910
use std::{
@@ -25,13 +26,13 @@ pub type BroadcastMessage = (String, Option<SequenceInfo>);
2526
const TEN_SECONDS: Duration = Duration::from_secs(10);
2627

2728
pub async fn events(
28-
mut shard: Shard,
29+
mut shard: Shard<twilight_gateway_queue::InMemoryQueue>,
2930
shard_state: Arc<ShardState>,
3031
shard_id: u32,
3132
shard_count: u32,
3233
broadcast_tx: broadcast::Sender<BroadcastMessage>,
3334
client: Arc<twilight_http::Client>,
34-
) {
35+
){
3536
// This method only wants to relay events while the shard is in a READY state
3637
// Therefore, we only put events in the queue while we are connected and READY
3738
let mut is_ready = false;
@@ -49,24 +50,20 @@ pub async fn events(
4950

5051
if now.duration_since(last_metrics_update) > TEN_SECONDS {
5152
let latencies = shard.latency().recent();
52-
let info = shard.status();
53-
update_shard_statistics(&shard_id_str, &shard_state, info, latencies);
53+
let info = shard.state();
54+
update_shard_statistics(&shard_id_str, &shard_state, &info, latencies);
5455
last_metrics_update = now;
5556
}
5657

57-
let payload = match shard.next_message().await {
58-
Ok(Message::Text(payload)) => payload,
59-
Ok(Message::Close(_)) if SHUTDOWN.load(Ordering::Relaxed) => return,
60-
Ok(Message::Close(_)) => continue,
61-
Err(e) => {
58+
let payload = match shard.next().await {
59+
Some(Ok(Message::Text(payload))) => payload,
60+
Some(Ok(Message::Close(_))) if SHUTDOWN.load(Ordering::Relaxed) => return,
61+
Some(Ok(Message::Close(_))) => continue,
62+
Some(Err(e)) => {
6263
tracing::error!("Error receiving message: {e}");
63-
64-
if e.is_fatal() {
65-
return;
66-
}
67-
6864
continue;
6965
}
66+
None => return,
7067
};
7168

7269
// NOTE: payload cannot be modified because we have to do optional event parsing
@@ -166,16 +163,25 @@ pub async fn events(
166163
pub fn update_shard_statistics(
167164
shard_id: &str,
168165
shard_state: &Arc<ShardState>,
169-
connection_status: &ConnectionStatus,
166+
connection_status: &impl std::fmt::Debug,
170167
latencies: &[Duration],
171168
) {
172-
// There is no way around this, sadly
173-
let connection_status = match connection_status {
174-
ConnectionStatus::Connected => 4.0,
175-
ConnectionStatus::Disconnected { .. } => 1.0,
176-
ConnectionStatus::Identifying => 2.0,
177-
ConnectionStatus::Resuming => 3.0,
178-
ConnectionStatus::FatallyClosed { .. } => 0.0,
169+
// We don't want to depend on a specific twilight type name here. Use the
170+
// Debug representation to determine a rough numeric status mapping.
171+
let status_str = format!("{:?}", connection_status);
172+
let connection_status = if status_str.contains("Connected") {
173+
4.0
174+
} else if status_str.contains("Identifying") {
175+
2.0
176+
} else if status_str.contains("Resuming") {
177+
3.0
178+
} else if status_str.contains("Disconnected") {
179+
1.0
180+
} else if status_str.contains("FatallyClosed") {
181+
0.0
182+
} else {
183+
// Unknown state
184+
f64::NAN
179185
};
180186

181187
let latency = latencies.first().map_or(f64::NAN, Duration::as_secs_f64);

src/main.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,8 @@ async fn run() -> Result<(), Box<dyn Error + Send + Sync>> {
112112
"",
113113
);
114114

115-
let config = Config::builder(CONFIG.token.clone(), CONFIG.intents)
116-
.queue(Arc::new(queue))
117-
.event_types(CONFIG.cache.clone().into())
115+
let config = ConfigBuilder::new(CONFIG.token.clone(), CONFIG.intents)
116+
.queue(queue.clone())
118117
.build();
119118

120119
let mut dispatch_tasks = JoinSet::new();

0 commit comments

Comments
 (0)