Skip to content

Commit 2f8f1db

Browse files
committed
Remove civet server option
1 parent 094188b commit 2f8f1db

File tree

4 files changed

+39
-110
lines changed

4 files changed

+39
-110
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ anyhow = "1.0"
3434
base64 = "0.13"
3535
cargo-registry-s3 = { path = "src/s3", version = "0.2.0" }
3636
chrono = { version = "0.4.0", features = ["serde"] }
37-
civet = "0.12.0-alpha.5"
3837
clap = "=3.0.0-beta.2"
3938
comrak = { version = "0.9", default-features = false }
4039

docs/BACKEND.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@ 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 either a [conduit] or a [hyper] server that uses the `cargo_registry::App` instance
15+
6. Starts 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
1717
8. Blocks forever (or until the process is killed)
1818

19-
[civet]: https://crates.io/crates/civet
2019
[hyper]: https://crates.io/crates/hyper
2120

2221
## Routes

src/bin/server.rs

Lines changed: 38 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,17 @@
22
#![allow(unknown_lints)]
33

44
use cargo_registry::{boot, App, Env};
5-
use std::{
6-
borrow::Cow,
7-
fs::File,
8-
process::Command,
9-
sync::{mpsc::channel, Arc, Mutex},
10-
time::Duration,
11-
};
12-
13-
use civet::Server as CivetServer;
5+
use std::{borrow::Cow, fs::File, process::Command, sync::Arc, time::Duration};
6+
147
use conduit_hyper::Service;
158
use futures_util::future::FutureExt;
169
use reqwest::blocking::Client;
1710
use sentry::{ClientOptions, IntoDsn};
11+
use tokio::io::AsyncWriteExt;
12+
use tokio::signal::unix::{signal, SignalKind};
1813

1914
const CORE_THREADS: usize = 4;
2015

21-
#[allow(clippy::large_enum_variant)]
22-
enum Server {
23-
Civet(CivetServer),
24-
Hyper(tokio::runtime::Runtime, tokio::task::JoinHandle<()>),
25-
}
26-
27-
use Server::*;
28-
2916
fn main() -> Result<(), Box<dyn std::error::Error>> {
3017
let _sentry = dotenv::var("SENTRY_DSN_API")
3118
.ok()
@@ -90,52 +77,41 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
9077
}
9178
});
9279

93-
let server = if dotenv::var("WEB_USE_CIVET").is_err() {
94-
use tokio::io::AsyncWriteExt;
95-
use tokio::signal::unix::{signal, SignalKind};
96-
97-
println!("Booting with a hyper based server");
98-
99-
let rt = tokio::runtime::Builder::new_multi_thread()
100-
.enable_all()
101-
.worker_threads(CORE_THREADS)
102-
.max_blocking_threads(threads as usize)
103-
.build()
104-
.unwrap();
105-
106-
let handler = Arc::new(conduit_hyper::BlockingHandler::new(handler));
107-
let make_service =
108-
hyper::service::make_service_fn(move |socket: &hyper::server::conn::AddrStream| {
109-
let addr = socket.remote_addr();
110-
let handler = handler.clone();
111-
async move { Service::from_blocking(handler, addr) }
112-
});
113-
114-
let addr = (ip, port).into();
115-
#[allow(clippy::async_yields_async)]
116-
let server = rt.block_on(async { hyper::Server::bind(&addr).serve(make_service) });
117-
118-
let mut sig_int = rt.block_on(async { signal(SignalKind::interrupt()) })?;
119-
let mut sig_term = rt.block_on(async { signal(SignalKind::terminate()) })?;
120-
121-
let server = server.with_graceful_shutdown(async move {
122-
// Wait for either signal
123-
futures_util::select! {
124-
_ = sig_int.recv().fuse() => {},
125-
_ = sig_term.recv().fuse() => {},
126-
};
127-
let mut stdout = tokio::io::stdout();
128-
stdout.write_all(b"Starting graceful shutdown\n").await.ok();
80+
println!("Booting with a hyper based server");
81+
82+
let rt = tokio::runtime::Builder::new_multi_thread()
83+
.enable_all()
84+
.worker_threads(CORE_THREADS)
85+
.max_blocking_threads(threads as usize)
86+
.build()
87+
.unwrap();
88+
89+
let handler = Arc::new(conduit_hyper::BlockingHandler::new(handler));
90+
let make_service =
91+
hyper::service::make_service_fn(move |socket: &hyper::server::conn::AddrStream| {
92+
let addr = socket.remote_addr();
93+
let handler = handler.clone();
94+
async move { Service::from_blocking(handler, addr) }
12995
});
13096

131-
let server = rt.spawn(async { server.await.unwrap() });
132-
Hyper(rt, server)
133-
} else {
134-
println!("Booting with a civet based server");
135-
let mut cfg = civet::Config::new();
136-
cfg.port(port).threads(threads).keep_alive(true);
137-
Civet(CivetServer::start(cfg, handler).unwrap())
138-
};
97+
let addr = (ip, port).into();
98+
#[allow(clippy::async_yields_async)]
99+
let server = rt.block_on(async { hyper::Server::bind(&addr).serve(make_service) });
100+
101+
let mut sig_int = rt.block_on(async { signal(SignalKind::interrupt()) })?;
102+
let mut sig_term = rt.block_on(async { signal(SignalKind::terminate()) })?;
103+
104+
let server = server.with_graceful_shutdown(async move {
105+
// Wait for either signal
106+
futures_util::select! {
107+
_ = sig_int.recv().fuse() => {},
108+
_ = sig_term.recv().fuse() => {},
109+
};
110+
let mut stdout = tokio::io::stdout();
111+
stdout.write_all(b"Starting graceful shutdown\n").await.ok();
112+
});
113+
114+
let server = rt.spawn(async { server.await.unwrap() });
139115

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

@@ -159,17 +135,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
159135
}
160136

161137
// Block the main thread until the server has shutdown
162-
match server {
163-
Hyper(rt, server) => {
164-
rt.block_on(async { server.await.unwrap() });
165-
}
166-
Civet(server) => {
167-
let (tx, rx) = channel::<()>();
168-
ctrlc_handler(move || tx.send(()).unwrap_or(()));
169-
rx.recv().unwrap();
170-
drop(server);
171-
}
172-
}
138+
rt.block_on(async { server.await.unwrap() });
173139

174140
println!("Persisting remaining downloads counters");
175141
if let Err(err) = app.downloads_counter.persist_all_shards(&app) {
@@ -180,23 +146,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
180146
Ok(())
181147
}
182148

183-
fn ctrlc_handler<F>(f: F)
184-
where
185-
F: FnOnce() + Send + 'static,
186-
{
187-
let call_once = Mutex::new(Some(f));
188-
189-
ctrlc::set_handler(move || {
190-
if let Some(f) = call_once.lock().unwrap().take() {
191-
println!("Starting graceful shutdown");
192-
f();
193-
} else {
194-
println!("Already sent signal to start graceful shutdown");
195-
}
196-
})
197-
.unwrap();
198-
}
199-
200149
fn downloads_counter_thread(app: Arc<App>) {
201150
let interval = Duration::from_millis(
202151
(app.config.downloads_persist_interval_ms / app.downloads_counter.shards_count()) as u64,

0 commit comments

Comments
 (0)