Skip to content

Commit f085918

Browse files
committed
Solve low hanging fruit causing large future sizes (#3357)
1 parent 2459d39 commit f085918

File tree

9 files changed

+28
-45
lines changed

9 files changed

+28
-45
lines changed

src/builder/create_message.rs

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,6 @@ pub struct CreateMessage<'a> {
7171
enforce_nonce: bool,
7272
#[serde(skip_serializing_if = "Option::is_none")]
7373
poll: Option<CreatePoll<'a, Ready>>,
74-
75-
// The following fields are handled separately.
76-
#[serde(skip)]
77-
reactions: Cow<'a, [ReactionType]>,
7874
}
7975

8076
impl<'a> CreateMessage<'a> {
@@ -140,12 +136,6 @@ impl<'a> CreateMessage<'a> {
140136
self
141137
}
142138

143-
/// Adds a list of reactions to create after the message's sent.
144-
pub fn reactions(mut self, reactions: impl Into<Cow<'a, [ReactionType]>>) -> Self {
145-
self.reactions = reactions.into();
146-
self
147-
}
148-
149139
/// Appends a file to the message.
150140
///
151141
/// **Note**: Requires the [Attach Files] permission.
@@ -287,31 +277,14 @@ impl<'a> CreateMessage<'a> {
287277
/// [Send Messages]: Permissions::SEND_MESSAGES
288278
/// [Attach Files]: Permissions::ATTACH_FILES
289279
#[cfg(feature = "http")]
290-
pub async fn execute(
291-
mut self,
292-
http: &Http,
293-
channel_id: GenericChannelId,
294-
guild_id: Option<GuildId>,
295-
) -> Result<Message> {
280+
pub async fn execute(mut self, http: &Http, channel_id: GenericChannelId) -> Result<Message> {
296281
self.check_length()?;
297282

298283
let files = self.attachments.new_attachments();
299284
if self.allowed_mentions.is_none() {
300285
self.allowed_mentions.clone_from(&http.default_allowed_mentions);
301286
}
302287

303-
let mut message = http.send_message(channel_id, files, &self).await?;
304-
305-
for reaction in self.reactions.iter() {
306-
http.create_reaction(channel_id, message.id, reaction).await?;
307-
}
308-
309-
// HTTP sent Messages don't have guild_id set, so we fill it in ourselves by best effort
310-
if message.guild_id.is_none() {
311-
// If we were called from GuildChannel, we can fill in the GuildId ourselves.
312-
message.guild_id = guild_id;
313-
}
314-
315-
Ok(message)
288+
http.send_message(channel_id, files, &self).await
316289
}
317290
}

src/http/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4415,7 +4415,7 @@ impl Http {
44154415
pub async fn request(&self, req: Request<'_>) -> Result<ReqwestResponse> {
44164416
let method = req.method.reqwest_method();
44174417
let response = if let Some(ratelimiter) = &self.ratelimiter {
4418-
ratelimiter.perform(req).await?
4418+
ratelimiter.perform(&req).await?
44194419
} else {
44204420
let request = req
44214421
.build(

src/http/multipart.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ impl Multipart<'_> {
4949
},
5050
MultipartUpload::Attachments(attachment_files) => {
5151
for (idx, file) in attachment_files.into_iter().enumerate() {
52-
multipart = multipart.part(format!("files[{idx}]"), file.into_part().await?);
52+
let part = file.into_part().await?;
53+
multipart = multipart.part(format!("files[{idx}]"), part);
5354
}
5455
},
5556
}

src/http/ratelimiting.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl Ratelimiter {
171171
///
172172
/// Only error kind that may be returned is [`Error::Http`].
173173
#[cfg_attr(feature = "tracing_instrument", instrument)]
174-
pub async fn perform(&self, req: Request<'_>) -> Result<Response> {
174+
pub async fn perform(&self, req: &Request<'_>) -> Result<Response> {
175175
loop {
176176
// This will block if another thread hit the global ratelimit.
177177
drop(self.global.lock().await);
@@ -185,7 +185,7 @@ impl Ratelimiter {
185185
let ratelimiting_bucket = req.route.ratelimiting_bucket();
186186
let delay_time = {
187187
let mut bucket = self.routes.entry(ratelimiting_bucket).or_default();
188-
bucket.pre_hook(&req, &*self.ratelimit_callback.read())
188+
bucket.pre_hook(req, &*self.ratelimit_callback.read())
189189
};
190190

191191
if let Some(delay_time) = delay_time {
@@ -244,7 +244,7 @@ impl Ratelimiter {
244244
{
245245
bucket.post_hook(
246246
&response,
247-
&req,
247+
req,
248248
&*self.ratelimit_callback.read(),
249249
self.absolute_ratelimits,
250250
)

src/http/request.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ impl<'a> Request<'a> {
7575
token: Option<&str>,
7676
proxy: Option<&str>,
7777
) -> Result<ReqwestRequestBuilder> {
78+
// Build the multipart first, as to keep future size small.
79+
let multipart = if let Some(multipart) = self.multipart {
80+
Some(multipart.build_form().await?)
81+
} else {
82+
None
83+
};
84+
7885
let mut path = self.route.path().into_owned();
7986

8087
if let Some(proxy) = proxy {
@@ -100,9 +107,9 @@ impl<'a> Request<'a> {
100107
);
101108
}
102109

103-
if let Some(multipart) = self.multipart {
110+
if let Some(multipart) = multipart {
104111
// Setting multipart adds the content-length header.
105-
builder = builder.multipart(multipart.build_form().await?);
112+
builder = builder.multipart(multipart);
106113
} else if let Some(bytes) = self.body {
107114
headers.insert(CONTENT_LENGTH, bytes.len().into());
108115
headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));

src/model/channel/channel_id.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,7 @@ impl GenericChannelId {
923923
/// See [`CreateMessage::execute`] for a list of possible errors, and their corresponding
924924
/// reasons.
925925
pub async fn send_message(self, http: &Http, builder: CreateMessage<'_>) -> Result<Message> {
926-
builder.execute(http, self, None).await
926+
builder.execute(http, self).await
927927
}
928928

929929
/// Starts typing in the channel for an indefinite period of time.

src/model/channel/guild_channel.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,9 @@ impl GuildChannel {
340340
/// See [`CreateMessage::execute`] for a list of possible errors, and their corresponding
341341
/// reasons.
342342
pub async fn send_message(&self, http: &Http, builder: CreateMessage<'_>) -> Result<Message> {
343-
builder.execute(http, self.id.widen(), Some(self.base.guild_id)).await
343+
let mut message = self.id.widen().send_message(http, builder).await?;
344+
message.guild_id = Some(self.base.guild_id);
345+
Ok(message)
344346
}
345347

346348
/// Retrieves [`Member`]s from the current channel.

src/model/channel/thread.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,9 @@ impl GuildThread {
210210
/// See [`CreateMessage::execute`] for a list of possible errors, and their corresponding
211211
/// reasons.
212212
pub async fn send_message(&self, http: &Http, builder: CreateMessage<'_>) -> Result<Message> {
213-
builder.execute(http, self.id.widen(), Some(self.base.guild_id)).await
213+
let mut message = self.id.widen().send_message(http, builder).await?;
214+
message.guild_id = Some(self.base.guild_id);
215+
Ok(message)
214216
}
215217
}
216218

src/model/user.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -618,12 +618,10 @@ impl UserId {
618618
cache_http: impl CacheHttp,
619619
builder: CreateMessage<'_>,
620620
) -> Result<Message> {
621-
self.create_dm_channel(&cache_http)
622-
.await?
623-
.id
624-
.widen()
625-
.send_message(cache_http.http(), builder)
626-
.await
621+
// Do not refactor this to a one liner. The PrivateChannel from `create_dm_channel`
622+
// should be dropped before the `send_message` call to avoid bloating future sizes.
623+
let dm_channel_id = self.create_dm_channel(&cache_http).await?.id;
624+
dm_channel_id.widen().send_message(cache_http.http(), builder).await
627625
}
628626

629627
/// This is an alias of [`Self::direct_message`].

0 commit comments

Comments
 (0)