Skip to content

Commit 4bed129

Browse files
committed
Auto merge of #1646 - jtgeibel:conduit-hyper-round3, r=sgrif
Add `hyper` support as a boot-time option If the `USE_HYPER` environment variable is set then `hyper` is used as the backend server. If the variable is not set, the default remains a `civet` based server. This has a trivial impact on build times and binary sizes, as `hyper` is already pulled in (as a `Client`) on production via `reqwest`. This will also aid in profiling performance differences, as backend instances can be run in parallel in two terminals: ``` $ PORT=8888 cargo run --release --bin server $ PORT=8889 USE_HYPER=1 cargo run --release --bin server ```
2 parents c93f3c2 + 914939b commit 4bed129

File tree

7 files changed

+49
-30
lines changed

7 files changed

+49
-30
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ rustdoc-args = [
2929

3030
[dependencies]
3131
cargo-registry-s3 = { path = "src/s3", version = "0.2.0" }
32-
old_semver = { path = "src/old_semver", version = "0.1.0" }
3332
rand = "0.6"
3433
git2 = "0.6.4"
3534
flate2 = "1.0"
@@ -76,6 +75,7 @@ conduit-router = "0.8"
7675
conduit-static = "0.8"
7776
conduit-git-http-backend = "0.8"
7877
civet = "0.9"
78+
conduit-hyper = "0.1.3"
7979

8080
[dev-dependencies]
8181
conduit-test = "0.8"

docs/BACKEND.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ The server does the following things:
1212
3. Reads values from environment variables to configure a new instance of `cargo_registry::App`
1313
4. Adds middleware to the app by calling `cargo_registry::middleware`
1414
5. Syncs the categories defined in *src/categories.toml* with the categories in the database
15-
6. Starts a [civet][] `Server` that uses the `cargo_registry::App` instance
15+
6. Starts either a [conduit] or a [hyper] server that uses the `cargo_registry::App` instance
1616
7. Tells Nginx on Heroku that the application is ready to receive requests, if running on Heroku
17-
8. Blocks forever (or until the process is killed) waiting to receive messages on a channel that no
18-
messages are ever sent to, in order to outive the civet `Server` threads
17+
8. Blocks forever (or until the process is killed)
1918

2019
[civet]: https://crates.io/crates/civet
20+
[hyper]: https://crates.io/crates/hyper
2121

2222
## Routes
2323

src/bin/server.rs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@ use std::{
88
sync::{mpsc::channel, Arc},
99
};
1010

11-
use civet::Server;
11+
use civet::Server as CivetServer;
12+
use conduit_hyper::Service as HyperService;
13+
14+
enum Server {
15+
Civet(CivetServer),
16+
Hyper(HyperService<conduit_middleware::MiddlewareBuilder>),
17+
}
18+
19+
use Server::*;
1220

1321
fn main() {
1422
let _ = jemalloc_ctl::set_background_thread(true);
@@ -66,9 +74,16 @@ fn main() {
6674
} else {
6775
50
6876
};
69-
let mut cfg = civet::Config::new();
70-
cfg.port(port).threads(threads).keep_alive(true);
71-
let _a = Server::start(cfg, app);
77+
78+
let server = if env::var("USE_HYPER").is_ok() {
79+
println!("Booting with a hyper based server");
80+
Hyper(HyperService::new(app, threads as usize))
81+
} else {
82+
println!("Booting with a civet based server");
83+
let mut cfg = civet::Config::new();
84+
cfg.port(port).threads(threads).keep_alive(true);
85+
Civet(CivetServer::start(cfg, app).unwrap())
86+
};
7287

7388
println!("listening on port {}", port);
7489

@@ -78,7 +93,13 @@ fn main() {
7893
File::create("/tmp/app-initialized").unwrap();
7994
}
8095

81-
// TODO: handle a graceful shutdown by just waiting for a SIG{INT,TERM}
82-
let (_tx, rx) = channel::<()>();
83-
rx.recv().unwrap();
96+
if let Hyper(server) = server {
97+
let addr = ([127, 0, 0, 1], port).into();
98+
server.run(addr);
99+
} else {
100+
// Civet server is already running, but we need to block the main thread forever
101+
// TODO: handle a graceful shutdown by just waiting for a SIG{INT,TERM}
102+
let (_tx, rx) = channel::<()>();
103+
rx.recv().unwrap();
104+
}
84105
}

src/old_semver/Cargo.toml

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/old_semver/src/lib.rs

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/util/request_proxy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{io::Read, net::SocketAddr};
22

33
use conduit::Request;
4-
use old_semver::semver;
4+
use conduit_hyper::semver;
55

66
// Can't derive Debug because of Request.
77
#[allow(missing_debug_implementations)]

0 commit comments

Comments
 (0)