mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-04-02 18:55:32 +00:00
aquatic_http: split into mio and glommio modules
This commit is contained in:
parent
40eff52f1c
commit
ad7e464788
12 changed files with 196 additions and 153 deletions
3
Cargo.lock
generated
3
Cargo.lock
generated
|
|
@ -91,8 +91,11 @@ dependencies = [
|
||||||
"aquatic_cli_helpers",
|
"aquatic_cli_helpers",
|
||||||
"aquatic_common",
|
"aquatic_common",
|
||||||
"aquatic_http_protocol",
|
"aquatic_http_protocol",
|
||||||
|
"cfg-if",
|
||||||
"crossbeam-channel",
|
"crossbeam-channel",
|
||||||
"either",
|
"either",
|
||||||
|
"futures-lite",
|
||||||
|
"glommio",
|
||||||
"hashbrown 0.11.2",
|
"hashbrown 0.11.2",
|
||||||
"histogram",
|
"histogram",
|
||||||
"indexmap",
|
"indexmap",
|
||||||
|
|
|
||||||
|
|
@ -15,28 +15,40 @@ path = "src/lib/lib.rs"
|
||||||
name = "aquatic_http"
|
name = "aquatic_http"
|
||||||
path = "src/bin/main.rs"
|
path = "src/bin/main.rs"
|
||||||
|
|
||||||
|
[features]
|
||||||
|
default = ["with-mio"]
|
||||||
|
with-glommio = ["glommio", "futures-lite"]
|
||||||
|
with-mio = ["crossbeam-channel", "histogram", "mio", "native-tls", "socket2"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "1"
|
anyhow = "1"
|
||||||
aquatic_cli_helpers = "0.1.0"
|
aquatic_cli_helpers = "0.1.0"
|
||||||
aquatic_common = "0.1.0"
|
aquatic_common = "0.1.0"
|
||||||
aquatic_http_protocol = "0.1.0"
|
aquatic_http_protocol = "0.1.0"
|
||||||
crossbeam-channel = "0.5"
|
cfg-if = "1"
|
||||||
either = "1"
|
either = "1"
|
||||||
hashbrown = "0.11.2"
|
hashbrown = "0.11.2"
|
||||||
histogram = "0.6"
|
|
||||||
indexmap = "1"
|
indexmap = "1"
|
||||||
itoa = "0.4"
|
itoa = "0.4"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
mimalloc = { version = "0.1", default-features = false }
|
mimalloc = { version = "0.1", default-features = false }
|
||||||
memchr = "2"
|
memchr = "2"
|
||||||
mio = { version = "0.7", features = ["tcp", "os-poll", "os-util"] }
|
|
||||||
native-tls = "0.2"
|
|
||||||
parking_lot = "0.11"
|
parking_lot = "0.11"
|
||||||
privdrop = "0.5"
|
privdrop = "0.5"
|
||||||
rand = { version = "0.8", features = ["small_rng"] }
|
rand = { version = "0.8", features = ["small_rng"] }
|
||||||
serde = { version = "1", features = ["derive"] }
|
serde = { version = "1", features = ["derive"] }
|
||||||
smartstring = "0.2"
|
smartstring = "0.2"
|
||||||
socket2 = { version = "0.4.1", features = ["all"] }
|
|
||||||
|
# mio
|
||||||
|
crossbeam-channel = { version = "0.5", optional = true }
|
||||||
|
histogram = { version = "0.6", optional = true }
|
||||||
|
mio = { version = "0.7", features = ["tcp", "os-poll", "os-util"], optional = true }
|
||||||
|
native-tls = { version = "0.2", optional = true }
|
||||||
|
socket2 = { version = "0.4.1", features = ["all"], optional = true }
|
||||||
|
|
||||||
|
# glommio
|
||||||
|
glommio = { git = "https://github.com/DataDog/glommio.git", rev = "4e6b14772da2f4325271fbcf12d24cf91ed466e5", optional = true }
|
||||||
|
futures-lite = { version = "1", optional = true }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
quickcheck = "1.0"
|
quickcheck = "1.0"
|
||||||
|
|
|
||||||
10
aquatic_http/src/lib/glommio/mod.rs
Normal file
10
aquatic_http/src/lib/glommio/mod.rs
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
use glommio::prelude::*;
|
||||||
|
|
||||||
|
use crate::config::Config;
|
||||||
|
|
||||||
|
pub fn run(
|
||||||
|
config: Config,
|
||||||
|
) -> anyhow::Result<()> {
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
@ -1,153 +1,20 @@
|
||||||
use std::sync::Arc;
|
use cfg_if::cfg_if;
|
||||||
use std::thread::Builder;
|
|
||||||
use std::time::Duration;
|
|
||||||
|
|
||||||
use anyhow::Context;
|
|
||||||
use mio::{Poll, Waker};
|
|
||||||
use parking_lot::Mutex;
|
|
||||||
use privdrop::PrivDrop;
|
|
||||||
|
|
||||||
pub mod common;
|
|
||||||
pub mod config;
|
pub mod config;
|
||||||
pub mod handler;
|
|
||||||
pub mod network;
|
|
||||||
pub mod tasks;
|
|
||||||
|
|
||||||
use common::*;
|
#[cfg(feature = "with-mio")]
|
||||||
use config::Config;
|
pub mod mio;
|
||||||
use network::utils::create_tls_acceptor;
|
#[cfg(all(feature = "with-glommio", target_os = "linux"))]
|
||||||
|
pub mod glommio;
|
||||||
|
|
||||||
pub const APP_NAME: &str = "aquatic_http: HTTP/TLS BitTorrent tracker";
|
pub const APP_NAME: &str = "aquatic_http: HTTP/TLS BitTorrent tracker";
|
||||||
|
|
||||||
pub fn run(config: Config) -> anyhow::Result<()> {
|
pub fn run(config: config::Config) -> ::anyhow::Result<()> {
|
||||||
let state = State::default();
|
cfg_if! {
|
||||||
|
if #[cfg(all(feature = "with-glommio", target_os = "linux"))] {
|
||||||
tasks::update_access_list(&config, &state);
|
glommio::run(config)
|
||||||
|
} else {
|
||||||
start_workers(config.clone(), state.clone())?;
|
mio::run(config)
|
||||||
|
}
|
||||||
loop {
|
|
||||||
::std::thread::sleep(Duration::from_secs(config.cleaning.interval));
|
|
||||||
|
|
||||||
tasks::update_access_list(&config, &state);
|
|
||||||
|
|
||||||
state
|
|
||||||
.torrent_maps
|
|
||||||
.lock()
|
|
||||||
.clean(&config, &state.access_list.load_full());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn start_workers(config: Config, state: State) -> anyhow::Result<()> {
|
|
||||||
let opt_tls_acceptor = create_tls_acceptor(&config.network.tls)?;
|
|
||||||
|
|
||||||
let (request_channel_sender, request_channel_receiver) = ::crossbeam_channel::unbounded();
|
|
||||||
|
|
||||||
let mut out_message_senders = Vec::new();
|
|
||||||
let mut wakers = Vec::new();
|
|
||||||
|
|
||||||
let socket_worker_statuses: SocketWorkerStatuses = {
|
|
||||||
let mut statuses = Vec::new();
|
|
||||||
|
|
||||||
for _ in 0..config.socket_workers {
|
|
||||||
statuses.push(None);
|
|
||||||
}
|
|
||||||
|
|
||||||
Arc::new(Mutex::new(statuses))
|
|
||||||
};
|
|
||||||
|
|
||||||
for i in 0..config.socket_workers {
|
|
||||||
let config = config.clone();
|
|
||||||
let state = state.clone();
|
|
||||||
let socket_worker_statuses = socket_worker_statuses.clone();
|
|
||||||
let request_channel_sender = request_channel_sender.clone();
|
|
||||||
let opt_tls_acceptor = opt_tls_acceptor.clone();
|
|
||||||
let poll = Poll::new().expect("create poll");
|
|
||||||
let waker = Arc::new(Waker::new(poll.registry(), CHANNEL_TOKEN).expect("create waker"));
|
|
||||||
|
|
||||||
let (response_channel_sender, response_channel_receiver) = ::crossbeam_channel::unbounded();
|
|
||||||
|
|
||||||
out_message_senders.push(response_channel_sender);
|
|
||||||
wakers.push(waker);
|
|
||||||
|
|
||||||
Builder::new()
|
|
||||||
.name(format!("socket-{:02}", i + 1))
|
|
||||||
.spawn(move || {
|
|
||||||
network::run_socket_worker(
|
|
||||||
config,
|
|
||||||
state,
|
|
||||||
i,
|
|
||||||
socket_worker_statuses,
|
|
||||||
request_channel_sender,
|
|
||||||
response_channel_receiver,
|
|
||||||
opt_tls_acceptor,
|
|
||||||
poll,
|
|
||||||
);
|
|
||||||
})?;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Wait for socket worker statuses. On error from any, quit program.
|
|
||||||
// On success from all, drop privileges if corresponding setting is set
|
|
||||||
// and continue program.
|
|
||||||
loop {
|
|
||||||
::std::thread::sleep(::std::time::Duration::from_millis(10));
|
|
||||||
|
|
||||||
if let Some(statuses) = socket_worker_statuses.try_lock() {
|
|
||||||
for opt_status in statuses.iter() {
|
|
||||||
if let Some(Err(err)) = opt_status {
|
|
||||||
return Err(::anyhow::anyhow!(err.to_owned()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if statuses.iter().all(Option::is_some) {
|
|
||||||
if config.privileges.drop_privileges {
|
|
||||||
PrivDrop::default()
|
|
||||||
.chroot(config.privileges.chroot_path.clone())
|
|
||||||
.user(config.privileges.user.clone())
|
|
||||||
.apply()
|
|
||||||
.context("Couldn't drop root privileges")?;
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let response_channel_sender = ResponseChannelSender::new(out_message_senders);
|
|
||||||
|
|
||||||
for i in 0..config.request_workers {
|
|
||||||
let config = config.clone();
|
|
||||||
let state = state.clone();
|
|
||||||
let request_channel_receiver = request_channel_receiver.clone();
|
|
||||||
let response_channel_sender = response_channel_sender.clone();
|
|
||||||
let wakers = wakers.clone();
|
|
||||||
|
|
||||||
Builder::new()
|
|
||||||
.name(format!("request-{:02}", i + 1))
|
|
||||||
.spawn(move || {
|
|
||||||
handler::run_request_worker(
|
|
||||||
config,
|
|
||||||
state,
|
|
||||||
request_channel_receiver,
|
|
||||||
response_channel_sender,
|
|
||||||
wakers,
|
|
||||||
);
|
|
||||||
})?;
|
|
||||||
}
|
|
||||||
|
|
||||||
if config.statistics.interval != 0 {
|
|
||||||
let state = state.clone();
|
|
||||||
let config = config.clone();
|
|
||||||
|
|
||||||
Builder::new()
|
|
||||||
.name("statistics".to_string())
|
|
||||||
.spawn(move || loop {
|
|
||||||
::std::thread::sleep(Duration::from_secs(config.statistics.interval));
|
|
||||||
|
|
||||||
tasks::print_statistics(&state);
|
|
||||||
})
|
|
||||||
.expect("spawn statistics thread");
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -13,8 +13,8 @@ use aquatic_common::extract_response_peers;
|
||||||
use aquatic_http_protocol::request::*;
|
use aquatic_http_protocol::request::*;
|
||||||
use aquatic_http_protocol::response::*;
|
use aquatic_http_protocol::response::*;
|
||||||
|
|
||||||
use crate::common::*;
|
|
||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
|
use super::common::*;
|
||||||
|
|
||||||
pub fn run_request_worker(
|
pub fn run_request_worker(
|
||||||
config: Config,
|
config: Config,
|
||||||
150
aquatic_http/src/lib/mio/mod.rs
Normal file
150
aquatic_http/src/lib/mio/mod.rs
Normal file
|
|
@ -0,0 +1,150 @@
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::thread::Builder;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use anyhow::Context;
|
||||||
|
use mio::{Poll, Waker};
|
||||||
|
use parking_lot::Mutex;
|
||||||
|
use privdrop::PrivDrop;
|
||||||
|
|
||||||
|
pub mod common;
|
||||||
|
pub mod handler;
|
||||||
|
pub mod network;
|
||||||
|
pub mod tasks;
|
||||||
|
|
||||||
|
use crate::config::Config;
|
||||||
|
use common::*;
|
||||||
|
use network::utils::create_tls_acceptor;
|
||||||
|
|
||||||
|
pub fn run(config: Config) -> anyhow::Result<()> {
|
||||||
|
let state = State::default();
|
||||||
|
|
||||||
|
tasks::update_access_list(&config, &state);
|
||||||
|
|
||||||
|
start_workers(config.clone(), state.clone())?;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
::std::thread::sleep(Duration::from_secs(config.cleaning.interval));
|
||||||
|
|
||||||
|
tasks::update_access_list(&config, &state);
|
||||||
|
|
||||||
|
state
|
||||||
|
.torrent_maps
|
||||||
|
.lock()
|
||||||
|
.clean(&config, &state.access_list.load_full());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn start_workers(config: Config, state: State) -> anyhow::Result<()> {
|
||||||
|
let opt_tls_acceptor = create_tls_acceptor(&config.network.tls)?;
|
||||||
|
|
||||||
|
let (request_channel_sender, request_channel_receiver) = ::crossbeam_channel::unbounded();
|
||||||
|
|
||||||
|
let mut out_message_senders = Vec::new();
|
||||||
|
let mut wakers = Vec::new();
|
||||||
|
|
||||||
|
let socket_worker_statuses: SocketWorkerStatuses = {
|
||||||
|
let mut statuses = Vec::new();
|
||||||
|
|
||||||
|
for _ in 0..config.socket_workers {
|
||||||
|
statuses.push(None);
|
||||||
|
}
|
||||||
|
|
||||||
|
Arc::new(Mutex::new(statuses))
|
||||||
|
};
|
||||||
|
|
||||||
|
for i in 0..config.socket_workers {
|
||||||
|
let config = config.clone();
|
||||||
|
let state = state.clone();
|
||||||
|
let socket_worker_statuses = socket_worker_statuses.clone();
|
||||||
|
let request_channel_sender = request_channel_sender.clone();
|
||||||
|
let opt_tls_acceptor = opt_tls_acceptor.clone();
|
||||||
|
let poll = Poll::new().expect("create poll");
|
||||||
|
let waker = Arc::new(Waker::new(poll.registry(), CHANNEL_TOKEN).expect("create waker"));
|
||||||
|
|
||||||
|
let (response_channel_sender, response_channel_receiver) = ::crossbeam_channel::unbounded();
|
||||||
|
|
||||||
|
out_message_senders.push(response_channel_sender);
|
||||||
|
wakers.push(waker);
|
||||||
|
|
||||||
|
Builder::new()
|
||||||
|
.name(format!("socket-{:02}", i + 1))
|
||||||
|
.spawn(move || {
|
||||||
|
network::run_socket_worker(
|
||||||
|
config,
|
||||||
|
state,
|
||||||
|
i,
|
||||||
|
socket_worker_statuses,
|
||||||
|
request_channel_sender,
|
||||||
|
response_channel_receiver,
|
||||||
|
opt_tls_acceptor,
|
||||||
|
poll,
|
||||||
|
);
|
||||||
|
})?;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait for socket worker statuses. On error from any, quit program.
|
||||||
|
// On success from all, drop privileges if corresponding setting is set
|
||||||
|
// and continue program.
|
||||||
|
loop {
|
||||||
|
::std::thread::sleep(::std::time::Duration::from_millis(10));
|
||||||
|
|
||||||
|
if let Some(statuses) = socket_worker_statuses.try_lock() {
|
||||||
|
for opt_status in statuses.iter() {
|
||||||
|
if let Some(Err(err)) = opt_status {
|
||||||
|
return Err(::anyhow::anyhow!(err.to_owned()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if statuses.iter().all(Option::is_some) {
|
||||||
|
if config.privileges.drop_privileges {
|
||||||
|
PrivDrop::default()
|
||||||
|
.chroot(config.privileges.chroot_path.clone())
|
||||||
|
.user(config.privileges.user.clone())
|
||||||
|
.apply()
|
||||||
|
.context("Couldn't drop root privileges")?;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let response_channel_sender = ResponseChannelSender::new(out_message_senders);
|
||||||
|
|
||||||
|
for i in 0..config.request_workers {
|
||||||
|
let config = config.clone();
|
||||||
|
let state = state.clone();
|
||||||
|
let request_channel_receiver = request_channel_receiver.clone();
|
||||||
|
let response_channel_sender = response_channel_sender.clone();
|
||||||
|
let wakers = wakers.clone();
|
||||||
|
|
||||||
|
Builder::new()
|
||||||
|
.name(format!("request-{:02}", i + 1))
|
||||||
|
.spawn(move || {
|
||||||
|
handler::run_request_worker(
|
||||||
|
config,
|
||||||
|
state,
|
||||||
|
request_channel_receiver,
|
||||||
|
response_channel_sender,
|
||||||
|
wakers,
|
||||||
|
);
|
||||||
|
})?;
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.statistics.interval != 0 {
|
||||||
|
let state = state.clone();
|
||||||
|
let config = config.clone();
|
||||||
|
|
||||||
|
Builder::new()
|
||||||
|
.name("statistics".to_string())
|
||||||
|
.spawn(move || loop {
|
||||||
|
::std::thread::sleep(Duration::from_secs(config.statistics.interval));
|
||||||
|
|
||||||
|
tasks::print_statistics(&state);
|
||||||
|
})
|
||||||
|
.expect("spawn statistics thread");
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
@ -10,7 +10,7 @@ use native_tls::{MidHandshakeTlsStream, TlsAcceptor};
|
||||||
|
|
||||||
use aquatic_http_protocol::request::{Request, RequestParseError};
|
use aquatic_http_protocol::request::{Request, RequestParseError};
|
||||||
|
|
||||||
use crate::common::*;
|
use crate::mio::common::*;
|
||||||
|
|
||||||
use super::stream::Stream;
|
use super::stream::Stream;
|
||||||
|
|
||||||
|
|
@ -13,7 +13,7 @@ use native_tls::TlsAcceptor;
|
||||||
|
|
||||||
use aquatic_http_protocol::response::*;
|
use aquatic_http_protocol::response::*;
|
||||||
|
|
||||||
use crate::common::*;
|
use crate::mio::common::*;
|
||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
|
|
||||||
pub mod connection;
|
pub mod connection;
|
||||||
|
|
@ -2,7 +2,8 @@ use histogram::Histogram;
|
||||||
|
|
||||||
use aquatic_common::access_list::{AccessListMode, AccessListQuery};
|
use aquatic_common::access_list::{AccessListMode, AccessListQuery};
|
||||||
|
|
||||||
use crate::{common::*, config::Config};
|
use crate::config::Config;
|
||||||
|
use super::common::*;
|
||||||
|
|
||||||
pub fn update_access_list(config: &Config, state: &State) {
|
pub fn update_access_list(config: &Config, state: &State) {
|
||||||
match config.access_list.mode {
|
match config.access_list.mode {
|
||||||
Loading…
Add table
Add a link
Reference in a new issue