Skip to content

Commit 136b6f5

Browse files
KixironJoshua Nelson
authored and
Joshua Nelson
committed
Switch to one single runtime for S3 storage
Changed S3Backend::start_storage_transaction from creating a new Runtime on each call to using a lazily-initialized global runtime for all S3 instances to use. Since Tokio v0.1's Runtime has no mechanism to allow for blocking that doesn't require an &mut self, the runtime is stored inside of a mutex to allow for mutable access. We're already transitively dependent on parking_lot v0.10.2, so this is just adding it as a direct dependency (Removing tokio v0.2 will remove the other) version of parking_lot we depend on). When upgrading to tokio v0.2 this should be changed since v0.2 has the Runtime::handle method which allows runtime access from only &self.
1 parent 7d5092c commit 136b6f5

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ path-slash = "0.1.3"
4646
once_cell = { version = "1.4.0", features = ["parking_lot"] }
4747
base64 = "0.12.1"
4848
strum = { version = "0.18.0", features = ["derive"] }
49+
parking_lot = "0.10.2"
4950

5051
# Data serialization and deserialization
5152
serde = { version = "1.0", features = ["derive"] }

src/storage/s3.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use failure::Error;
44
use futures::stream::{FuturesUnordered, Stream};
55
use futures::Future;
66
use log::{error, warn};
7+
use once_cell::sync::Lazy;
8+
use parking_lot::Mutex;
79
use rusoto_core::region::Region;
810
use rusoto_credential::DefaultCredentialsProvider;
911
use rusoto_s3::{GetObjectRequest, PutObjectRequest, S3Client, S3};
@@ -16,17 +18,21 @@ mod test;
1618
pub(crate) use test::TestS3;
1719

1820
pub(crate) static S3_BUCKET_NAME: &str = "rust-docs-rs";
21+
static S3_RUNTIME: Lazy<Mutex<Runtime>> =
22+
Lazy::new(|| Mutex::new(Runtime::new().expect("Failed to create S3 runtime")));
1923

2024
pub(crate) struct S3Backend {
2125
client: S3Client,
2226
bucket: String,
27+
runtime: &'static Mutex<Runtime>,
2328
}
2429

2530
impl S3Backend {
2631
pub(crate) fn new(client: S3Client, bucket: &str) -> Self {
2732
Self {
2833
client,
2934
bucket: bucket.into(),
35+
runtime: &*S3_RUNTIME,
3036
}
3137
}
3238

@@ -63,16 +69,12 @@ impl S3Backend {
6369
}
6470

6571
pub(super) fn start_storage_transaction(&self) -> Result<S3StorageTransaction, Error> {
66-
Ok(S3StorageTransaction {
67-
s3: self,
68-
runtime: Runtime::new()?,
69-
})
72+
Ok(S3StorageTransaction { s3: self })
7073
}
7174
}
7275

7376
pub(super) struct S3StorageTransaction<'a> {
7477
s3: &'a S3Backend,
75-
runtime: Runtime,
7678
}
7779

7880
impl<'a> StorageTransaction for S3StorageTransaction<'a> {
@@ -100,7 +102,7 @@ impl<'a> StorageTransaction for S3StorageTransaction<'a> {
100102
}
101103
attempts += 1;
102104

103-
match self.runtime.block_on(futures.map(drop).collect()) {
105+
match self.s3.runtime.lock().block_on(futures.map(drop).collect()) {
104106
// this batch was successful, start another batch if there are still more files
105107
Ok(_) => break,
106108
Err(err) => {

0 commit comments

Comments
 (0)