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

chore(gateway, http): small code nits #1103

Merged
merged 6 commits into from
Aug 17, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions gateway/src/shard/impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,9 +461,10 @@ impl Shard {
/// [`shutdown_resumable`]: Self::shutdown_resumable
/// [`shutdown`]: Self::shutdown
pub async fn start(&self) -> Result<(), ShardStartError> {
let url = if let Some(u) = self.0.config.gateway_url.clone() {
u.into_string()
let url = if let Some(u) = &self.0.config.gateway_url {
u.to_string()
} else {
// Validates the bot token
self.0
.config
.http_client()
Expand Down
39 changes: 21 additions & 18 deletions gateway/src/shard/processor/impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,12 @@ impl ShardProcessor {
let properties = IdentifyProperties::new("twilight.rs", "twilight.rs", OS, "", "");

url.push_str("?v=8");

// Discord docs state:
// "Generally, it is a good idea to explicitly pass the gateway version
// and encoding".
// <https://discord.com/developers/docs/topics/gateway#connecting-gateway-url-query-string-params>
url.push_str("&encoding=json");
vilgotf marked this conversation as resolved.
Show resolved Hide resolved
compression::add_url_feature(&mut url);

emitter.event(Event::ShardConnecting(Connecting {
Expand Down Expand Up @@ -346,29 +352,26 @@ impl ShardProcessor {

pub async fn run(mut self) {
loop {
match self.next_payload().await {
Ok(v) => v,
Err(source) => {
#[cfg(feature = "tracing")]
tracing::warn!("{}", source);

self.emit_disconnected(None, None).await;
if let Err(source) = self.next_payload().await {
#[cfg(feature = "tracing")]
tracing::warn!("{}", source);

if source.fatal() {
break;
}
self.emit_disconnected(None, None).await;

if source.reconnectable() {
self.reconnect().await;
}
if source.fatal() {
break;
}

if source.resumable() {
self.resume().await;
}
if source.reconnectable() {
self.reconnect().await;
}

continue;
if source.resumable() {
self.resume().await;
}
};

continue;
}

if let Err(source) = self.process().await {
#[cfg(feature = "tracing")]
Expand Down
9 changes: 5 additions & 4 deletions http/src/response/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ use std::{
error::Error,
fmt::{Display, Formatter, Result as FmtResult},
future::Future,
iter::{self, FusedIterator},
iter::FusedIterator,
marker::PhantomData,
pin::Pin,
task::{Context, Poll},
Expand Down Expand Up @@ -242,9 +242,10 @@ impl<T> Response<T> {

// Create a buffer filled with zeroes so we can copy the aggregate
// body into it.
let mut buf = iter::repeat(0)
.take(aggregate.remaining())
.collect::<Vec<_>>();
//
// Using `vec!` is the fastest way to do this, despite it being a
// macro and having unsafe internals.
let mut buf = vec![0; aggregate.remaining()];
vilgotf marked this conversation as resolved.
Show resolved Hide resolved
aggregate.copy_to_slice(&mut buf);

Ok(buf)
Expand Down