Skip to content

RUST-793 Reduce size of returned futures #417

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

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
72 changes: 39 additions & 33 deletions src/client/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,42 +69,45 @@ impl Client {
op: T,
session: impl Into<Option<&mut ClientSession>>,
) -> Result<T::O> {
// TODO RUST-9: allow unacknowledged write concerns
if !op.is_acknowledged() {
return Err(ErrorKind::InvalidArgument {
message: "Unacknowledged write concerns are not supported".to_string(),
Box::pin(async {
// TODO RUST-9: allow unacknowledged write concerns
if !op.is_acknowledged() {
return Err(ErrorKind::InvalidArgument {
message: "Unacknowledged write concerns are not supported".to_string(),
}
.into());
}
.into());
}
match session.into() {
Some(session) => {
if !Arc::ptr_eq(&self.inner, &session.client().inner) {
return Err(ErrorKind::InvalidArgument {
message: "the session provided to an operation must be created from the \
same client as the collection/database"
.into(),
match session.into() {
Some(session) => {
if !Arc::ptr_eq(&self.inner, &session.client().inner) {
return Err(ErrorKind::InvalidArgument {
message: "the session provided to an operation must be created from \
the same client as the collection/database"
.into(),
}
.into());
}
.into());
}

if let Some(SelectionCriteria::ReadPreference(read_preference)) =
op.selection_criteria()
{
if session.in_transaction() && read_preference != &ReadPreference::Primary {
return Err(ErrorKind::Transaction {
message: "read preference in a transaction must be primary".into(),
if let Some(SelectionCriteria::ReadPreference(read_preference)) =
op.selection_criteria()
{
if session.in_transaction() && read_preference != &ReadPreference::Primary {
return Err(ErrorKind::Transaction {
message: "read preference in a transaction must be primary".into(),
}
.into());
}
.into());
}
self.execute_operation_with_retry(op, Some(session)).await
}
None => {
let mut implicit_session = self.start_implicit_session(&op).await?;
self.execute_operation_with_retry(op, implicit_session.as_mut())
.await
}
self.execute_operation_with_retry(op, Some(session)).await
}
None => {
let mut implicit_session = self.start_implicit_session(&op).await?;
self.execute_operation_with_retry(op, implicit_session.as_mut())
.await
}
}
})
.await
}

/// Execute the given operation, returning the implicit session created for it if one was.
Expand All @@ -114,10 +117,13 @@ impl Client {
&self,
op: T,
) -> Result<(T::O, Option<ClientSession>)> {
let mut implicit_session = self.start_implicit_session(&op).await?;
self.execute_operation_with_retry(op, implicit_session.as_mut())
.await
.map(|result| (result, implicit_session))
Box::pin(async {
let mut implicit_session = self.start_implicit_session(&op).await?;
self.execute_operation_with_retry(op, implicit_session.as_mut())
.await
.map(|result| (result, implicit_session))
})
.await
}

/// Selects a server and executes the given operation on it, optionally using a provided
Expand Down
4 changes: 2 additions & 2 deletions src/operation/find/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use super::CursorResponse;
pub(crate) struct Find<T> {
ns: Namespace,
filter: Option<Document>,
options: Option<FindOptions>,
options: Option<Box<FindOptions>>,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

find_one was one of the bigger ones and FindOptions itself is pretty big, so I opted to box it here too for some additional reduction in size.

_phantom: PhantomData<T>,
}

Expand All @@ -46,7 +46,7 @@ impl<T> Find<T> {
Self {
ns,
filter,
options,
options: options.map(Box::new),
_phantom: Default::default(),
}
}
Expand Down
6 changes: 1 addition & 5 deletions src/test/spec/retryable_reads.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::{sync::Arc, time::Duration};

use bson::doc;
use futures::FutureExt;
use tokio::sync::RwLockWriteGuard;

use crate::{
Expand Down Expand Up @@ -63,10 +62,7 @@ async fn retry_releases_connection() {
let _fp_guard = client.enable_failpoint(failpoint, None).await.unwrap();

RUNTIME
.timeout(
Duration::from_secs(1),
collection.find_one(doc! {}, None).boxed(),
)
.timeout(Duration::from_secs(1), collection.find_one(doc! {}, None))
.await
.expect("operation should not time out")
.expect("find should succeed");
Expand Down