2018-04-14 01:02:01 +02:00
|
|
|
use std::cell::{Cell, RefCell, RefMut, UnsafeCell};
|
2018-06-24 06:30:58 +02:00
|
|
|
use std::collections::VecDeque;
|
2018-03-18 19:05:44 +01:00
|
|
|
use std::fmt::Write;
|
2018-01-12 03:35:05 +01:00
|
|
|
use std::rc::Rc;
|
2018-06-18 00:56:18 +02:00
|
|
|
use std::{env, fmt, mem, net};
|
2018-06-17 19:51:20 +02:00
|
|
|
|
|
|
|
use bytes::BytesMut;
|
2018-06-18 00:56:18 +02:00
|
|
|
use futures_cpupool::CpuPool;
|
2018-06-17 19:51:20 +02:00
|
|
|
use http::StatusCode;
|
2018-06-18 00:56:18 +02:00
|
|
|
use parking_lot::Mutex;
|
2018-03-18 19:05:44 +01:00
|
|
|
use time;
|
2018-01-12 03:35:05 +01:00
|
|
|
|
|
|
|
use super::channel::Node;
|
2018-06-25 06:58:04 +02:00
|
|
|
use super::message::{Request, RequestPool};
|
2018-04-29 07:55:47 +02:00
|
|
|
use super::KeepAlive;
|
2018-03-23 05:14:57 +01:00
|
|
|
use body::Body;
|
2018-04-14 01:02:01 +02:00
|
|
|
use httpresponse::{HttpResponse, HttpResponseBuilder, HttpResponsePool};
|
2018-01-12 03:35:05 +01:00
|
|
|
|
2018-06-18 00:56:18 +02:00
|
|
|
/// Env variable for default cpu pool size
|
|
|
|
const ENV_CPU_POOL_VAR: &str = "ACTIX_CPU_POOL";
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
pub(crate) static ref DEFAULT_CPUPOOL: Mutex<CpuPool> = {
|
|
|
|
let default = match env::var(ENV_CPU_POOL_VAR) {
|
|
|
|
Ok(val) => {
|
|
|
|
if let Ok(val) = val.parse() {
|
|
|
|
val
|
|
|
|
} else {
|
|
|
|
error!("Can not parse ACTIX_CPU_POOL value");
|
|
|
|
20
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(_) => 20,
|
|
|
|
};
|
|
|
|
Mutex::new(CpuPool::new(default))
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-01-12 03:35:05 +01:00
|
|
|
/// Various server settings
|
|
|
|
pub struct ServerSettings {
|
|
|
|
addr: Option<net::SocketAddr>,
|
|
|
|
secure: bool,
|
|
|
|
host: String,
|
2018-06-17 19:51:20 +02:00
|
|
|
cpu_pool: UnsafeCell<Option<CpuPool>>,
|
2018-06-25 05:08:28 +02:00
|
|
|
responses: &'static HttpResponsePool,
|
2018-01-12 03:35:05 +01:00
|
|
|
}
|
|
|
|
|
2018-05-12 06:19:48 +02:00
|
|
|
impl Clone for ServerSettings {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
ServerSettings {
|
|
|
|
addr: self.addr,
|
|
|
|
secure: self.secure,
|
|
|
|
host: self.host.clone(),
|
2018-03-07 23:56:53 +01:00
|
|
|
cpu_pool: UnsafeCell::new(None),
|
2018-06-25 05:08:28 +02:00
|
|
|
responses: HttpResponsePool::get_pool(),
|
2018-03-07 23:56:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-06-25 05:08:28 +02:00
|
|
|
responses: HttpResponsePool::get_pool(),
|
2018-06-17 19:51:20 +02:00
|
|
|
cpu_pool: UnsafeCell::new(None),
|
2018-01-12 03:35:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ServerSettings {
|
|
|
|
/// Crate server settings instance
|
2018-04-14 01:02:01 +02:00
|
|
|
pub(crate) fn new(
|
2018-04-29 07:55:47 +02:00
|
|
|
addr: Option<net::SocketAddr>, host: &Option<String>, secure: bool,
|
2018-04-14 01:02:01 +02:00
|
|
|
) -> ServerSettings {
|
2018-01-12 03:35:05 +01:00
|
|
|
let host = if let Some(ref host) = *host {
|
|
|
|
host.clone()
|
|
|
|
} else if let Some(ref addr) = addr {
|
|
|
|
format!("{}", addr)
|
|
|
|
} else {
|
|
|
|
"localhost".to_owned()
|
|
|
|
};
|
2018-06-17 19:51:20 +02:00
|
|
|
let cpu_pool = UnsafeCell::new(None);
|
2018-06-25 05:08:28 +02:00
|
|
|
let responses = HttpResponsePool::get_pool();
|
2018-04-14 01:02:01 +02:00
|
|
|
ServerSettings {
|
|
|
|
addr,
|
|
|
|
secure,
|
|
|
|
host,
|
|
|
|
cpu_pool,
|
|
|
|
responses,
|
|
|
|
}
|
2018-01-12 03:35:05 +01:00
|
|
|
}
|
|
|
|
|
2018-06-17 19:51:20 +02:00
|
|
|
pub(crate) fn parts(&self) -> (Option<net::SocketAddr>, String, bool) {
|
|
|
|
(self.addr, self.host.clone(), self.secure)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn from_parts(parts: (Option<net::SocketAddr>, String, bool)) -> Self {
|
|
|
|
let (addr, host, secure) = parts;
|
|
|
|
ServerSettings {
|
|
|
|
addr,
|
|
|
|
host,
|
|
|
|
secure,
|
|
|
|
cpu_pool: UnsafeCell::new(None),
|
2018-06-25 05:08:28 +02:00
|
|
|
responses: HttpResponsePool::get_pool(),
|
2018-06-17 19:51:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-06-24 18:21:04 +02:00
|
|
|
// Unsafe: ServerSetting is !Sync, DEFAULT_CPUPOOL is protected by Mutex
|
2018-06-17 19:51:20 +02:00
|
|
|
unsafe {
|
|
|
|
let val = &mut *self.cpu_pool.get();
|
|
|
|
if val.is_none() {
|
2018-06-18 00:56:18 +02:00
|
|
|
let pool = DEFAULT_CPUPOOL.lock().clone();
|
|
|
|
*val = Some(pool);
|
2018-06-17 19:51:20 +02:00
|
|
|
}
|
|
|
|
val.as_ref().unwrap()
|
|
|
|
}
|
2018-03-07 23:56:53 +01:00
|
|
|
}
|
2018-03-23 05:14:57 +01:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub(crate) fn get_response(&self, status: StatusCode, body: Body) -> HttpResponse {
|
|
|
|
HttpResponsePool::get_response(&self.responses, status, body)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2018-04-14 01:02:01 +02:00
|
|
|
pub(crate) fn get_response_builder(
|
2018-04-29 07:55:47 +02:00
|
|
|
&self, status: StatusCode,
|
2018-04-14 01:02:01 +02:00
|
|
|
) -> HttpResponseBuilder {
|
2018-03-23 05:14:57 +01:00
|
|
|
HttpResponsePool::get_builder(&self.responses, status)
|
|
|
|
}
|
2018-01-12 03:35:05 +01:00
|
|
|
}
|
|
|
|
|
2018-03-18 19:05:44 +01:00
|
|
|
// "Sun, 06 Nov 1994 08:49:37 GMT".len()
|
|
|
|
const DATE_VALUE_LENGTH: usize = 29;
|
2018-01-12 03:35:05 +01:00
|
|
|
|
|
|
|
pub(crate) struct WorkerSettings<H> {
|
|
|
|
h: RefCell<Vec<H>>,
|
|
|
|
keep_alive: u64,
|
2018-03-10 01:21:14 +01:00
|
|
|
ka_enabled: bool,
|
2018-01-15 02:00:28 +01:00
|
|
|
bytes: Rc<SharedBytesPool>,
|
2018-06-25 06:58:04 +02:00
|
|
|
messages: &'static RequestPool,
|
2018-01-12 03:35:05 +01:00
|
|
|
channels: Cell<usize>,
|
2018-03-03 20:16:55 +01:00
|
|
|
node: Box<Node<()>>,
|
2018-03-18 19:05:44 +01:00
|
|
|
date: UnsafeCell<Date>,
|
2018-01-12 03:35:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<H> WorkerSettings<H> {
|
2018-06-25 06:58:04 +02:00
|
|
|
pub(crate) fn new(
|
|
|
|
h: Vec<H>, keep_alive: KeepAlive, settings: ServerSettings,
|
|
|
|
) -> WorkerSettings<H> {
|
2018-03-10 01:21:14 +01:00
|
|
|
let (keep_alive, ka_enabled) = match keep_alive {
|
|
|
|
KeepAlive::Timeout(val) => (val as u64, true),
|
|
|
|
KeepAlive::Os | KeepAlive::Tcp(_) => (0, true),
|
|
|
|
KeepAlive::Disabled => (0, false),
|
|
|
|
};
|
|
|
|
|
2018-01-12 03:35:05 +01:00
|
|
|
WorkerSettings {
|
|
|
|
h: RefCell::new(h),
|
2018-01-15 02:00:28 +01:00
|
|
|
bytes: Rc::new(SharedBytesPool::new()),
|
2018-07-04 17:01:27 +02:00
|
|
|
messages: RequestPool::pool(settings),
|
2018-01-12 03:35:05 +01:00
|
|
|
channels: Cell::new(0),
|
2018-03-03 20:16:55 +01:00
|
|
|
node: Box::new(Node::head()),
|
2018-03-18 19:05:44 +01:00
|
|
|
date: UnsafeCell::new(Date::new()),
|
2018-06-25 06:58:04 +02:00
|
|
|
keep_alive,
|
|
|
|
ka_enabled,
|
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 {
|
2018-03-10 01:21:14 +01:00
|
|
|
self.ka_enabled
|
2018-01-12 03:35:05 +01:00
|
|
|
}
|
|
|
|
|
2018-06-24 06:30:58 +02:00
|
|
|
pub fn get_bytes(&self) -> BytesMut {
|
|
|
|
self.bytes.get_bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn release_bytes(&self, bytes: BytesMut) {
|
|
|
|
self.bytes.release_bytes(bytes)
|
2018-01-12 03:35:05 +01:00
|
|
|
}
|
|
|
|
|
2018-07-04 18:52:49 +02:00
|
|
|
pub fn get_request(&self) -> Request {
|
|
|
|
RequestPool::get(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 {
|
2018-04-14 01:02:01 +02:00
|
|
|
self.channels.set(num - 1);
|
2018-01-12 03:35:05 +01:00
|
|
|
} else {
|
|
|
|
error!("Number of removed channels is bigger than added channel. Bug in actix-web");
|
|
|
|
}
|
|
|
|
}
|
2018-03-18 19:05:44 +01:00
|
|
|
|
|
|
|
pub fn update_date(&self) {
|
2018-06-24 18:21:04 +02:00
|
|
|
// Unsafe: WorkerSetting is !Sync and !Send
|
2018-04-14 01:02:01 +02:00
|
|
|
unsafe { &mut *self.date.get() }.update();
|
2018-03-18 19:05:44 +01:00
|
|
|
}
|
|
|
|
|
2018-06-23 06:13:09 +02:00
|
|
|
pub fn set_date(&self, dst: &mut BytesMut, full: bool) {
|
2018-06-24 18:21:04 +02:00
|
|
|
// Unsafe: WorkerSetting is !Sync and !Send
|
2018-06-23 07:51:02 +02:00
|
|
|
unsafe {
|
|
|
|
if full {
|
|
|
|
let mut buf: [u8; 39] = mem::uninitialized();
|
|
|
|
buf[..6].copy_from_slice(b"date: ");
|
|
|
|
buf[6..35].copy_from_slice(&(*self.date.get()).bytes);
|
|
|
|
buf[35..].copy_from_slice(b"\r\n\r\n");
|
|
|
|
dst.extend_from_slice(&buf);
|
|
|
|
} else {
|
|
|
|
dst.extend_from_slice(&(*self.date.get()).bytes);
|
|
|
|
}
|
2018-06-23 06:13:09 +02:00
|
|
|
}
|
2018-03-20 19:40:05 +01:00
|
|
|
}
|
2018-03-18 19:05:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Date {
|
|
|
|
bytes: [u8; DATE_VALUE_LENGTH],
|
|
|
|
pos: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Date {
|
|
|
|
fn new() -> Date {
|
2018-04-14 01:02:01 +02:00
|
|
|
let mut date = Date {
|
|
|
|
bytes: [0; DATE_VALUE_LENGTH],
|
|
|
|
pos: 0,
|
|
|
|
};
|
2018-03-18 19:05:44 +01:00
|
|
|
date.update();
|
|
|
|
date
|
|
|
|
}
|
|
|
|
fn update(&mut self) {
|
|
|
|
self.pos = 0;
|
|
|
|
write!(self, "{}", time::at_utc(time::get_time()).rfc822()).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Write for Date {
|
|
|
|
fn write_str(&mut self, s: &str) -> fmt::Result {
|
|
|
|
let len = s.len();
|
|
|
|
self.bytes[self.pos..self.pos + len].copy_from_slice(s.as_bytes());
|
|
|
|
self.pos += len;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-24 06:30:58 +02:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub(crate) struct SharedBytesPool(RefCell<VecDeque<BytesMut>>);
|
|
|
|
|
|
|
|
impl SharedBytesPool {
|
|
|
|
pub fn new() -> SharedBytesPool {
|
|
|
|
SharedBytesPool(RefCell::new(VecDeque::with_capacity(128)))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_bytes(&self) -> BytesMut {
|
|
|
|
if let Some(bytes) = self.0.borrow_mut().pop_front() {
|
|
|
|
bytes
|
|
|
|
} else {
|
|
|
|
BytesMut::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn release_bytes(&self, mut bytes: BytesMut) {
|
|
|
|
let v = &mut self.0.borrow_mut();
|
|
|
|
if v.len() < 128 {
|
|
|
|
bytes.clear();
|
|
|
|
v.push_front(bytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-18 19:05:44 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_date_len() {
|
2018-05-17 21:20:20 +02:00
|
|
|
assert_eq!(DATE_VALUE_LENGTH, "Sun, 06 Nov 1994 08:49:37 GMT".len());
|
2018-03-18 19:05:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_date() {
|
2018-06-25 06:58:04 +02:00
|
|
|
let settings = WorkerSettings::<()>::new(
|
|
|
|
Vec::new(),
|
|
|
|
KeepAlive::Os,
|
|
|
|
ServerSettings::default(),
|
|
|
|
);
|
2018-03-18 19:05:44 +01:00
|
|
|
let mut buf1 = BytesMut::with_capacity(DATE_VALUE_LENGTH + 10);
|
2018-06-23 06:13:09 +02:00
|
|
|
settings.set_date(&mut buf1, true);
|
2018-03-18 19:05:44 +01:00
|
|
|
let mut buf2 = BytesMut::with_capacity(DATE_VALUE_LENGTH + 10);
|
2018-06-23 06:13:09 +02:00
|
|
|
settings.set_date(&mut buf2, true);
|
2018-03-18 19:05:44 +01:00
|
|
|
assert_eq!(buf1, buf2);
|
|
|
|
}
|
2018-01-12 03:35:05 +01:00
|
|
|
}
|