2018-03-07 23:56:53 +01:00
|
|
|
use std::{fmt, net};
|
2018-01-12 03:35:05 +01:00
|
|
|
use std::rc::Rc;
|
2018-03-07 23:56:53 +01:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::cell::{Cell, RefCell, RefMut, UnsafeCell};
|
|
|
|
use futures_cpupool::{Builder, CpuPool};
|
2018-01-12 03:35:05 +01:00
|
|
|
|
|
|
|
use helpers;
|
|
|
|
use super::channel::Node;
|
2018-01-15 02:00:28 +01:00
|
|
|
use super::shared::{SharedBytes, SharedBytesPool};
|
2018-01-12 03:35:05 +01:00
|
|
|
|
|
|
|
/// Various server settings
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct ServerSettings {
|
|
|
|
addr: Option<net::SocketAddr>,
|
|
|
|
secure: bool,
|
|
|
|
host: String,
|
2018-03-07 23:56:53 +01:00
|
|
|
cpu_pool: Arc<InnerCpuPool>,
|
2018-01-12 03:35:05 +01:00
|
|
|
}
|
|
|
|
|
2018-03-07 23:56:53 +01:00
|
|
|
struct InnerCpuPool {
|
|
|
|
cpu_pool: UnsafeCell<Option<CpuPool>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for InnerCpuPool {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "CpuPool")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl InnerCpuPool {
|
|
|
|
fn new() -> Self {
|
|
|
|
InnerCpuPool {
|
|
|
|
cpu_pool: UnsafeCell::new(None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn cpu_pool(&self) -> &CpuPool {
|
|
|
|
unsafe {
|
|
|
|
let val = &mut *self.cpu_pool.get();
|
|
|
|
if val.is_none() {
|
|
|
|
*val = Some(Builder::new().create());
|
|
|
|
}
|
|
|
|
val.as_ref().unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl Sync for InnerCpuPool {}
|
|
|
|
|
2018-01-12 03:35:05 +01:00
|
|
|
impl Default for ServerSettings {
|
|
|
|
fn default() -> Self {
|
|
|
|
ServerSettings {
|
|
|
|
addr: None,
|
|
|
|
secure: false,
|
|
|
|
host: "localhost:8080".to_owned(),
|
2018-03-07 23:56:53 +01:00
|
|
|
cpu_pool: Arc::new(InnerCpuPool::new()),
|
2018-01-12 03:35:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ServerSettings {
|
|
|
|
/// Crate server settings instance
|
|
|
|
pub(crate) fn new(addr: Option<net::SocketAddr>, host: &Option<String>, secure: bool)
|
|
|
|
-> ServerSettings
|
|
|
|
{
|
|
|
|
let host = if let Some(ref host) = *host {
|
|
|
|
host.clone()
|
|
|
|
} else if let Some(ref addr) = addr {
|
|
|
|
format!("{}", addr)
|
|
|
|
} else {
|
|
|
|
"localhost".to_owned()
|
|
|
|
};
|
2018-03-07 23:56:53 +01:00
|
|
|
let cpu_pool = Arc::new(InnerCpuPool::new());
|
|
|
|
ServerSettings { addr, secure, host, cpu_pool }
|
2018-01-12 03:35:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the socket address of the local half of this TCP connection
|
|
|
|
pub fn local_addr(&self) -> Option<net::SocketAddr> {
|
|
|
|
self.addr
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if connection is secure(https)
|
|
|
|
pub fn secure(&self) -> bool {
|
|
|
|
self.secure
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns host header value
|
|
|
|
pub fn host(&self) -> &str {
|
|
|
|
&self.host
|
|
|
|
}
|
2018-03-07 23:56:53 +01:00
|
|
|
|
|
|
|
/// Returns default `CpuPool` for server
|
|
|
|
pub fn cpu_pool(&self) -> &CpuPool {
|
|
|
|
self.cpu_pool.cpu_pool()
|
|
|
|
}
|
2018-01-12 03:35:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub(crate) struct WorkerSettings<H> {
|
|
|
|
h: RefCell<Vec<H>>,
|
|
|
|
enabled: bool,
|
|
|
|
keep_alive: u64,
|
2018-01-15 02:00:28 +01:00
|
|
|
bytes: Rc<SharedBytesPool>,
|
2018-01-12 03:35:05 +01:00
|
|
|
messages: Rc<helpers::SharedMessagePool>,
|
|
|
|
channels: Cell<usize>,
|
2018-03-03 20:16:55 +01:00
|
|
|
node: Box<Node<()>>,
|
2018-01-12 03:35:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<H> WorkerSettings<H> {
|
|
|
|
pub(crate) fn new(h: Vec<H>, keep_alive: Option<u64>) -> WorkerSettings<H> {
|
|
|
|
WorkerSettings {
|
|
|
|
h: RefCell::new(h),
|
|
|
|
enabled: if let Some(ka) = keep_alive { ka > 0 } else { false },
|
|
|
|
keep_alive: keep_alive.unwrap_or(0),
|
2018-01-15 02:00:28 +01:00
|
|
|
bytes: Rc::new(SharedBytesPool::new()),
|
2018-01-12 03:35:05 +01:00
|
|
|
messages: Rc::new(helpers::SharedMessagePool::new()),
|
|
|
|
channels: Cell::new(0),
|
2018-03-03 20:16:55 +01:00
|
|
|
node: Box::new(Node::head()),
|
2018-01-12 03:35:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn num_channels(&self) -> usize {
|
|
|
|
self.channels.get()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn head(&self) -> &Node<()> {
|
|
|
|
&self.node
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn handlers(&self) -> RefMut<Vec<H>> {
|
|
|
|
self.h.borrow_mut()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn keep_alive(&self) -> u64 {
|
|
|
|
self.keep_alive
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn keep_alive_enabled(&self) -> bool {
|
|
|
|
self.enabled
|
|
|
|
}
|
|
|
|
|
2018-01-15 02:00:28 +01:00
|
|
|
pub fn get_shared_bytes(&self) -> SharedBytes {
|
|
|
|
SharedBytes::new(self.bytes.get_bytes(), Rc::clone(&self.bytes))
|
2018-01-12 03:35:05 +01:00
|
|
|
}
|
|
|
|
|
2018-02-28 00:03:28 +01:00
|
|
|
pub fn get_http_message(&self) -> helpers::SharedHttpInnerMessage {
|
|
|
|
helpers::SharedHttpInnerMessage::new(self.messages.get(), Rc::clone(&self.messages))
|
2018-01-12 03:35:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_channel(&self) {
|
|
|
|
self.channels.set(self.channels.get() + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn remove_channel(&self) {
|
|
|
|
let num = self.channels.get();
|
|
|
|
if num > 0 {
|
|
|
|
self.channels.set(num-1);
|
|
|
|
} else {
|
|
|
|
error!("Number of removed channels is bigger than added channel. Bug in actix-web");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|