Skip to content

Add hyper support as a boot-time option #1646

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 1 commit into from
Mar 7, 2019
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
24 changes: 16 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ rustdoc-args = [

[dependencies]
cargo-registry-s3 = { path = "src/s3", version = "0.2.0" }
old_semver = { path = "src/old_semver", version = "0.1.0" }
rand = "0.6"
git2 = "0.6.4"
flate2 = "1.0"
Expand Down Expand Up @@ -76,6 +75,7 @@ conduit-router = "0.8"
conduit-static = "0.8"
conduit-git-http-backend = "0.8"
civet = "0.9"
conduit-hyper = "0.1.3"

[dev-dependencies]
conduit-test = "0.8"
Expand Down
6 changes: 3 additions & 3 deletions docs/BACKEND.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ The server does the following things:
3. Reads values from environment variables to configure a new instance of `cargo_registry::App`
4. Adds middleware to the app by calling `cargo_registry::middleware`
5. Syncs the categories defined in *src/categories.toml* with the categories in the database
6. Starts a [civet][] `Server` that uses the `cargo_registry::App` instance
6. Starts either a [conduit] or a [hyper] server that uses the `cargo_registry::App` instance
7. Tells Nginx on Heroku that the application is ready to receive requests, if running on Heroku
8. Blocks forever (or until the process is killed) waiting to receive messages on a channel that no
messages are ever sent to, in order to outive the civet `Server` threads
8. Blocks forever (or until the process is killed)

[civet]: https://crates.io/crates/civet
[hyper]: https://crates.io/crates/hyper

## Routes

Expand Down
35 changes: 28 additions & 7 deletions src/bin/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ use std::{
sync::{mpsc::channel, Arc},
};

use civet::Server;
use civet::Server as CivetServer;
use conduit_hyper::Service as HyperService;

enum Server {
Civet(CivetServer),
Hyper(HyperService<conduit_middleware::MiddlewareBuilder>),
}

use Server::*;

fn main() {
let _ = jemalloc_ctl::set_background_thread(true);
Expand Down Expand Up @@ -66,9 +74,16 @@ fn main() {
} else {
50
};
let mut cfg = civet::Config::new();
cfg.port(port).threads(threads).keep_alive(true);
let _a = Server::start(cfg, app);

let server = if env::var("USE_HYPER").is_ok() {
println!("Booting with a hyper based server");
Hyper(HyperService::new(app, threads as usize))
} else {
println!("Booting with a civet based server");
let mut cfg = civet::Config::new();
cfg.port(port).threads(threads).keep_alive(true);
Civet(CivetServer::start(cfg, app).unwrap())
};

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

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

// TODO: handle a graceful shutdown by just waiting for a SIG{INT,TERM}
let (_tx, rx) = channel::<()>();
rx.recv().unwrap();
if let Hyper(server) = server {
let addr = ([127, 0, 0, 1], port).into();
server.run(addr);
} else {
// Civet server is already running, but we need to block the main thread forever
// TODO: handle a graceful shutdown by just waiting for a SIG{INT,TERM}
let (_tx, rx) = channel::<()>();
rx.recv().unwrap();
}
}
7 changes: 0 additions & 7 deletions src/old_semver/Cargo.toml

This file was deleted.

3 changes: 0 additions & 3 deletions src/old_semver/src/lib.rs

This file was deleted.

2 changes: 1 addition & 1 deletion src/util/request_proxy.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{io::Read, net::SocketAddr};

use conduit::Request;
use old_semver::semver;
use conduit_hyper::semver;

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