Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resume pending identify requests #2260

Merged
merged 8 commits into from
Aug 26, 2023
27 changes: 20 additions & 7 deletions twilight-gateway/src/shard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,7 @@ impl Shard {
.as_mut()
.map_or(false, |handle| Pin::new(handle).poll(cx).is_ready())
{
self.identify_handle = None;
ByteAlex marked this conversation as resolved.
Show resolved Hide resolved
return Poll::Ready(Action::Identify);
}

Expand Down Expand Up @@ -1164,14 +1165,14 @@ impl Shard {
} else {
// Can not use `MessageSender` since it is only polled after
// the shard is identified.
self.identify_handle = Some(tokio::spawn({
let shard_id = self.id();
let queue = self.config().queue().clone();

async move {
queue.request([shard_id.number(), shard_id.total()]).await;
if let Some(handle) = &self.identify_handle {
// if the "old handle" is ready, we must assume it's no longer valid
if handle.is_finished() {
ByteAlex marked this conversation as resolved.
Show resolved Hide resolved
self.identify_handle = Some(self.create_identify_handle());
}
}));
} else {
self.identify_handle = Some(self.create_identify_handle());
}
}
}
Some(OpCode::InvalidSession) => {
Expand All @@ -1193,6 +1194,18 @@ impl Shard {
Ok(())
}

/// Create a new identify handle for the current shard
fn create_identify_handle(&self) -> JoinHandle<()> {
tokio::spawn({
let shard_id = self.id();
let queue = self.config().queue().clone();

async move {
queue.request([shard_id.number(), shard_id.total()]).await;
}
})
}

/// Establishes a Websocket connection, sets the [status] to [`Resuming`] or
/// [`Identifying`] if holding an active [`Session`] or not, and resets the
/// [inflater].
Expand Down
Loading