Skip to content

Commit 28216b3

Browse files
committed
Auto merge of #1864 - jtgeibel:update/conduit-hyper, r=carols10cents
Switch to async/await for bin/server and tests This PR series converts the future combinations to async/await syntax in the server binary (when `USE_HYPER=1` is set) and the recording HTTP proxy used in tests. This change requires the update of several dependencies at once (see the list below). It is not strictly necessary to update `reqwest` at this time, but I'm doing so in this series to avoid the build time regression of pulling in `hyper 0.12`, `hyper 0.13` and their associated transitive dependencies. Blockers: - [x] async/await is stable in 1.39.0 🎉 - [x] Bump the rust release in `RustConfig` - Stable release of underlying stack - [x] `futures 0.3.1` - [x] `tokio 0.2.9` - [x] `hyper 0.13.1` and `hyper-tls 0.4.1` - [x] `reqwest 0.10.1` - [x] `conduit-hyper 0.2.0`
2 parents b46f672 + f724f05 commit 28216b3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+787
-964
lines changed

Cargo.lock

Lines changed: 591 additions & 506 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ ammonia = "3.0.0"
5757
docopt = "1.0"
5858
scheduled-thread-pool = "0.2.0"
5959
derive_deref = "1.0.0"
60-
reqwest = "0.9.1"
60+
reqwest = { version = "0.10", features = ["blocking", "gzip", "json"] }
6161
tempfile = "3"
6262
parking_lot = "0.7.1"
6363
jemallocator = { version = "0.3", features = ['unprefixed_malloc_on_supported_platforms', 'profiling'] }
@@ -75,21 +75,22 @@ conduit-router = "0.8"
7575
conduit-static = "0.8"
7676
conduit-git-http-backend = "0.8"
7777
civet = "0.9"
78-
conduit-hyper = "0.1.3"
78+
conduit-hyper = "0.2.0"
7979

80-
futures = "0.1"
81-
tokio = "0.1"
82-
hyper = "0.12"
80+
futures = "0.3"
81+
tokio = { version = "0.2", default-features = false, features = ["net", "signal", "io-std"]}
82+
hyper = "0.13"
8383
ctrlc = { version = "3.0", features = ["termination"] }
8484
indexmap = "1.0.2"
8585
handlebars = "2.0.1"
8686

8787
[dev-dependencies]
8888
conduit-test = "0.8"
89-
hyper-tls = "0.3"
89+
hyper-tls = "0.4"
9090
lazy_static = "1.0"
91-
tokio-core = "0.1"
9291
diesel_migrations = { version = "1.3.0", features = ["postgres"] }
92+
tower-service = "0.3.0"
93+
tokio = { version = "0.2", default-features = false, features = ["stream"]}
9394

9495
[build-dependencies]
9596
dotenv = "0.15"

src/app.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{path::PathBuf, sync::Arc, time::Duration};
55

66
use diesel::r2d2;
77
use oauth2::basic::BasicClient;
8-
use reqwest::Client;
8+
use reqwest::blocking::Client;
99
use scheduled_thread_pool::ScheduledThreadPool;
1010

1111
/// The `App` struct holds the main components of the application like

src/background_jobs.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use reqwest::blocking::Client;
12
use std::panic::AssertUnwindSafe;
23
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
34

@@ -26,7 +27,7 @@ pub struct Environment {
2627
// FIXME: https://github.com/sfackler/r2d2/pull/70
2728
pub connection_pool: AssertUnwindSafe<DieselPool>,
2829
pub uploader: Uploader,
29-
http_client: AssertUnwindSafe<reqwest::Client>,
30+
http_client: AssertUnwindSafe<Client>,
3031
}
3132

3233
// FIXME: AssertUnwindSafe should be `Clone`, this can be replaced with
@@ -47,7 +48,7 @@ impl Environment {
4748
index: Repository,
4849
connection_pool: DieselPool,
4950
uploader: Uploader,
50-
http_client: reqwest::Client,
51+
http_client: Client,
5152
) -> Self {
5253
Self {
5354
index: Arc::new(Mutex::new(index)),
@@ -68,7 +69,7 @@ impl Environment {
6869
}
6970

7071
/// Returns a client for making HTTP requests to upload crate files.
71-
pub(crate) fn http_client(&self) -> &reqwest::Client {
72+
pub(crate) fn http_client(&self) -> &Client {
7273
&self.http_client
7374
}
7475
}

src/bin/background-worker.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use cargo_registry::git::{Repository, RepositoryConfig};
1616
use cargo_registry::{background_jobs::*, db};
1717
use diesel::r2d2;
18+
use reqwest::blocking::Client;
1819
use std::thread::sleep;
1920
use std::time::Duration;
2021

@@ -43,12 +44,7 @@ fn main() {
4344
let repository = Repository::open(&repository_config).expect("Failed to clone index");
4445
println!("Index cloned");
4546

46-
let environment = Environment::new(
47-
repository,
48-
db_pool.clone(),
49-
config.uploader,
50-
reqwest::Client::new(),
51-
);
47+
let environment = Environment::new(repository, db_pool.clone(), config.uploader, Client::new());
5248

5349
let build_runner = || {
5450
swirl::Runner::builder(db_pool.clone(), environment.clone())

src/bin/on_call/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use cargo_registry::util::Error;
22

3-
use reqwest::{header, StatusCode as Status};
3+
use reqwest::{blocking::Client, header, StatusCode as Status};
44

55
#[derive(serde::Serialize, Debug)]
66
#[serde(rename_all = "snake_case", tag = "event_type")]
@@ -29,7 +29,7 @@ impl Event {
2929
let api_token = dotenv::var("PAGERDUTY_API_TOKEN")?;
3030
let service_key = dotenv::var("PAGERDUTY_INTEGRATION_KEY")?;
3131

32-
let mut response = reqwest::Client::new()
32+
let response = Client::new()
3333
.post("https://events.pagerduty.com/generic/2010-04-15/create_event.json")
3434
.header(header::ACCEPT, "application/vnd.pagerduty+json;version=2")
3535
.header(header::AUTHORIZATION, format!("Token token={}", api_token))

src/bin/render-readmes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use chrono::{TimeZone, Utc};
2121
use diesel::{dsl::any, prelude::*};
2222
use docopt::Docopt;
2323
use flate2::read::GzDecoder;
24-
use reqwest::{header, Client};
24+
use reqwest::{blocking::Client, header};
2525
use tar::{self, Archive};
2626

2727
const CACHE_CONTROL_README: &str = "public,max-age=604800";
@@ -170,7 +170,7 @@ fn get_readme(
170170
.uploader
171171
.crate_location(krate_name, &version.num.to_string());
172172

173-
let mut response = match client.get(&location).send() {
173+
let response = match client.get(&location).send() {
174174
Ok(r) => r,
175175
Err(err) => {
176176
println!(

src/bin/server.rs

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ use std::{
99
};
1010

1111
use civet::Server as CivetServer;
12-
use conduit_hyper::Service as HyperService;
13-
use futures::Future;
14-
use reqwest::Client;
12+
use conduit_hyper::Service;
13+
use futures::prelude::*;
14+
use reqwest::blocking::Client;
1515

1616
enum Server {
1717
Civet(CivetServer),
18-
Hyper(tokio::runtime::Runtime),
18+
Hyper(tokio::runtime::Runtime, tokio::task::JoinHandle<()>),
1919
}
2020

2121
use Server::*;
2222

23-
fn main() {
23+
fn main() -> Result<(), Box<dyn std::error::Error>> {
2424
// Initialize logging
2525
env_logger::init();
2626

@@ -57,35 +57,43 @@ fn main() {
5757
});
5858

5959
let server = if dotenv::var("USE_HYPER").is_ok() {
60-
println!("Booting with a hyper based server");
61-
let addr = ([127, 0, 0, 1], port).into();
62-
let service = HyperService::new(app, threads as usize);
63-
let server = hyper::Server::bind(&addr).serve(service);
64-
65-
let (tx, rx) = futures::sync::oneshot::channel::<()>();
66-
let server = server
67-
.with_graceful_shutdown(rx)
68-
.map_err(|e| log::error!("Server error: {}", e));
60+
use tokio::io::AsyncWriteExt;
61+
use tokio::signal::unix::{signal, SignalKind};
6962

70-
ctrlc_handler(move || tx.send(()).unwrap_or(()));
63+
println!("Booting with a hyper based server");
7164

7265
let mut rt = tokio::runtime::Builder::new()
73-
.core_threads(4)
74-
.name_prefix("hyper-server-worker-")
75-
.after_start(|| {
76-
log::debug!("Stared thread {}", thread::current().name().unwrap_or("?"))
77-
})
78-
.before_stop(|| {
79-
log::debug!(
80-
"Stopping thread {}",
81-
thread::current().name().unwrap_or("?")
82-
)
83-
})
66+
.threaded_scheduler()
67+
.enable_all()
8468
.build()
8569
.unwrap();
86-
rt.spawn(server);
8770

88-
Hyper(rt)
71+
let handler = Arc::new(conduit_hyper::BlockingHandler::new(app, threads as usize));
72+
let make_service =
73+
hyper::service::make_service_fn(move |socket: &hyper::server::conn::AddrStream| {
74+
let addr = socket.remote_addr();
75+
let handler = handler.clone();
76+
async move { Service::from_blocking(handler, addr) }
77+
});
78+
79+
let addr = ([127, 0, 0, 1], port).into();
80+
let server = rt.block_on(async { hyper::Server::bind(&addr).serve(make_service) });
81+
82+
let mut sig_int = rt.block_on(async { signal(SignalKind::interrupt()) })?;
83+
let mut sig_term = rt.block_on(async { signal(SignalKind::terminate()) })?;
84+
85+
let server = server.with_graceful_shutdown(async move {
86+
// Wait for either signal
87+
futures::select! {
88+
_ = sig_int.recv().fuse() => (),
89+
_ = sig_term.recv().fuse() => (),
90+
};
91+
let mut stdout = tokio::io::stdout();
92+
stdout.write_all(b"Starting graceful shutdown\n").await.ok();
93+
});
94+
95+
let server = rt.spawn(async { server.await.unwrap() });
96+
Hyper(rt, server)
8997
} else {
9098
println!("Booting with a civet based server");
9199
let mut cfg = civet::Config::new();
@@ -112,7 +120,9 @@ fn main() {
112120

113121
// Block the main thread until the server has shutdown
114122
match server {
115-
Hyper(rt) => rt.shutdown_on_idle().wait().unwrap(),
123+
Hyper(mut rt, server) => {
124+
rt.block_on(async { server.await.unwrap() });
125+
}
116126
Civet(server) => {
117127
let (tx, rx) = channel::<()>();
118128
ctrlc_handler(move || tx.send(()).unwrap_or(()));
@@ -122,6 +132,7 @@ fn main() {
122132
}
123133

124134
println!("Server has gracefully shutdown!");
135+
Ok(())
125136
}
126137

127138
fn ctrlc_handler<F>(f: F)

src/github.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ where
2424
.get(&url)
2525
.header(header::ACCEPT, "application/vnd.github.v3+json")
2626
.header(header::AUTHORIZATION, format!("token {}", auth.secret()))
27+
.header(header::USER_AGENT, "crates.io (https://crates.io)")
2728
.send()?
2829
.error_for_status()
2930
.map_err(|e| handle_error_response(&e))?

src/s3/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ path = "lib.rs"
1717
base64 = "0.6"
1818
chrono = "0.4"
1919
openssl = "0.10.13"
20-
reqwest = "0.9.1"
20+
reqwest = { version = "0.10", features = ["blocking"] }

src/s3/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ use openssl::error::ErrorStack;
66
use openssl::hash::MessageDigest;
77
use openssl::pkey::PKey;
88
use openssl::sign::Signer;
9-
use reqwest::{header, Body, Client, Response};
9+
use reqwest::{
10+
blocking::{Body, Client, Response},
11+
header,
12+
};
1013

1114
mod error;
1215
pub use error::Error;
@@ -60,6 +63,7 @@ impl Bucket {
6063
.header(header::AUTHORIZATION, auth)
6164
.header(header::CONTENT_TYPE, content_type)
6265
.header(header::DATE, date)
66+
.header(header::USER_AGENT, "crates.io (https://crates.io)")
6367
.headers(extra_headers)
6468
.body(Body::sized(content, content_length))
6569
.send()?

src/tasks/dump_db.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ impl DumpTarball {
152152
}
153153

154154
fn upload(&self, target_name: &str, uploader: &Uploader) -> Result<u64, PerformError> {
155-
let client = reqwest::Client::new();
155+
let client = reqwest::blocking::Client::new();
156156
let tarfile = File::open(&self.tarball_path)?;
157157
let content_length = tarfile.metadata()?.len();
158158
// TODO Figure out the correct content type.

src/tests/all.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use std::{
2929

3030
use conduit_test::MockRequest;
3131
use diesel::prelude::*;
32-
use reqwest::{Client, Proxy};
32+
use reqwest::{blocking::Client, Proxy};
3333

3434
macro_rules! t {
3535
($e:expr) => {

src/tests/http-data/krate_good_badges

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@
2020
"authorization",
2121
"AWS AKIAICL5IWUZYWWKA7JA:kDm23yhf8YuOKpTcbHhNBa6BtQw="
2222
],
23-
[
24-
"user-agent",
25-
"reqwest/0.9.1"
26-
],
2723
[
2824
"host",
2925
"alexcrichton-test.s3.amazonaws.com"

src/tests/http-data/krate_good_categories

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@
2828
"date",
2929
"Fri, 15 Sep 2017 07:53:05 -0700"
3030
],
31-
[
32-
"user-agent",
33-
"reqwest/0.9.1"
34-
],
3531
[
3632
"accept",
3733
"*/*"

src/tests/http-data/krate_ignored_badges

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@
2020
"content-length",
2121
"35"
2222
],
23-
[
24-
"user-agent",
25-
"reqwest/0.9.1"
26-
],
2723
[
2824
"accept-encoding",
2925
"gzip"

src/tests/http-data/krate_ignored_categories

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
"content-length",
99
"35"
1010
],
11-
[
12-
"user-agent",
13-
"reqwest/0.9.1"
14-
],
1511
[
1612
"authorization",
1713
"AWS AKIAICL5IWUZYWWKA7JA:V37kbEzeh57sB4yTSZIOJACPoP4="

src/tests/http-data/krate_new_krate

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@
2020
"accept-encoding",
2121
"gzip"
2222
],
23-
[
24-
"user-agent",
25-
"reqwest/0.9.1"
26-
],
2723
[
2824
"content-type",
2925
"application/x-tar"

src/tests/http-data/krate_new_krate_git_upload

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@
3131
[
3232
"accept",
3333
"*/*"
34-
],
35-
[
36-
"user-agent",
37-
"reqwest/0.9.1"
3834
]
3935
],
4036
"body": "H4sIAAAAAAAA/+3AAQEAAACCIP+vbkhQwKsBLq+17wAEAAA="

src/tests/http-data/krate_new_krate_git_upload_appends

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@
2020
"accept",
2121
"*/*"
2222
],
23-
[
24-
"user-agent",
25-
"reqwest/0.9.1"
26-
],
2723
[
2824
"authorization",
2925
"AWS AKIAICL5IWUZYWWKA7JA:UgUqqHJ9cQAZDdbcsxpnC0BI2eE="
@@ -91,10 +87,6 @@
9187
"accept",
9288
"*/*"
9389
],
94-
[
95-
"user-agent",
96-
"reqwest/0.9.1"
97-
],
9890
[
9991
"authorization",
10092
"AWS AKIAICL5IWUZYWWKA7JA:UgUqqHJ9cQAZDdbcsxpnC0BI2eE="

src/tests/http-data/krate_new_krate_git_upload_with_conflicts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@
2424
"authorization",
2525
"AWS AKIAICL5IWUZYWWKA7JA:241ftMxnamoj94RBOB/al86Xwjk="
2626
],
27-
[
28-
"user-agent",
29-
"reqwest/0.9.1"
30-
],
3127
[
3228
"accept",
3329
"*/*"

0 commit comments

Comments
 (0)