Skip to content

Commit

Permalink
move server to separate crate
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Dec 10, 2018
1 parent ffb07c8 commit 8ad93f4
Show file tree
Hide file tree
Showing 21 changed files with 308 additions and 149 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ script:
if [[ "$TRAVIS_RUST_VERSION" != "nightly" ]]; then
cargo clean
cargo test --features="ssl,tls,rust-tls" -- --nocapture
cd actix-codec && cargo test
cd actix-service && cargo test
cd actix-server && cargo test --features="ssl,tls,rust-tls" -- --nocapture
cd actix-rt && cargo test
fi
- |
if [[ "$TRAVIS_RUST_VERSION" == "nightly" ]]; then
Expand Down
18 changes: 2 additions & 16 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ members = [
"./",
"actix-codec",
"actix-service",
"actix-server",
"actix-rt",
]

Expand All @@ -36,15 +37,9 @@ path = "src/lib.rs"
[features]
default = []

# tls
tls = ["native-tls"]

# openssl
ssl = ["openssl", "tokio-openssl"]

# rustls
rust-tls = ["rustls", "tokio-rustls", "webpki", "webpki-roots"]

cell = []

[dependencies]
Expand All @@ -70,18 +65,9 @@ tokio-signal = "0.2"
trust-dns-proto = "^0.5.0"
trust-dns-resolver = "^0.10.0"

# native-tls
native-tls = { version="0.2", optional = true }

# openssl
openssl = { version="0.10", optional = true }
tokio-openssl = { version="0.2", optional = true }

#rustls
rustls = { version = "^0.14", optional = true }
tokio-rustls = { version = "^0.8", optional = true }
webpki = { version = "0.18", optional = true }
webpki-roots = { version = "0.15", optional = true }
tokio-openssl = { version="0.3", optional = true }

[dev-dependencies]
env_logger = "0.5"
Expand Down
2 changes: 1 addition & 1 deletion actix-rt/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

## [0.1.0] - 2018-12-09

* Move codec to separate crate
* Initial release
6 changes: 1 addition & 5 deletions actix-rt/src/arbiter.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
#![allow(
clippy::borrow_interior_mutable_const,
clippy::declare_interior_mutable_const
)]
use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use std::sync::atomic::{AtomicUsize, Ordering};
Expand All @@ -21,7 +17,7 @@ thread_local!(
static Q: RefCell<Vec<Box<Future<Item = (), Error = ()>>>> = RefCell::new(Vec::new());
);

pub(crate) const COUNT: AtomicUsize = AtomicUsize::new(0);
pub(crate) static COUNT: AtomicUsize = AtomicUsize::new(0);

pub(crate) enum ArbiterCommand {
Stop,
Expand Down
5 changes: 5 additions & 0 deletions actix-server/CHANGES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changes

## [0.1.0] - 2018-12-09

* Move server to separate crate
74 changes: 74 additions & 0 deletions actix-server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
[package]
name = "actix-server"
version = "0.1.0"
authors = ["Nikolay Kim <[email protected]>"]
description = "Actix server - General purpose tcp server"
readme = "README.md"
keywords = ["network", "framework", "async", "futures"]
homepage = "https://actix.rs"
repository = "https://github.com/actix/actix-net.git"
documentation = "https://docs.rs/actix-server/"
categories = ["network-programming", "asynchronous"]
license = "MIT/Apache-2.0"
exclude = [".gitignore", ".travis.yml", ".cargo/config", "appveyor.yml"]
edition = "2018"
workspace = "../"

[package.metadata.docs.rs]
features = ["ssl", "tls", "rust-tls"]

[lib]
name = "actix_server"
path = "src/lib.rs"

[features]
default = []

# tls
tls = ["native-tls"]

# openssl
ssl = ["openssl", "tokio-openssl"]

# rustls
rust-tls = ["rustls", "tokio-rustls", "webpki", "webpki-roots"]

[dependencies]
actix-service = "0.1.1"
actix-rt = { path = "../actix-rt" }

log = "0.4"
num_cpus = "1.0"

# io
mio = "^0.6.13"
net2 = "0.2"
bytes = "0.4"
futures = "0.1"
slab = "0.4"
tokio-io = "0.1"
tokio-tcp = "0.1"
tokio-timer = "0.2"
tokio-reactor = "0.1"
tokio-signal = "0.2"

# native-tls
native-tls = { version="0.2", optional = true }

# openssl
openssl = { version="0.10", optional = true }
tokio-openssl = { version="0.3", optional = true }

#rustls
rustls = { version = "^0.14", optional = true }
tokio-rustls = { version = "^0.8", optional = true }
webpki = { version = "0.18", optional = true }
webpki-roots = { version = "0.15", optional = true }

[dev-dependencies]
env_logger = "0.5"

[profile.release]
lto = true
opt-level = 3
codegen-units = 1
4 changes: 2 additions & 2 deletions src/server/accept.rs → actix-server/src/accept.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ impl Accept {
match self.workers[self.next].send(msg) {
Ok(_) => (),
Err(tmp) => {
let _ = self.srv.worker_died(self.workers[self.next].idx);
self.srv.worker_died(self.workers[self.next].idx);
msg = tmp;
self.workers.swap_remove(self.next);
if self.workers.is_empty() {
Expand All @@ -397,7 +397,7 @@ impl Accept {
return;
}
Err(tmp) => {
let _ = self.srv.worker_died(self.workers[self.next].idx);
self.srv.worker_died(self.workers[self.next].idx);
msg = tmp;
self.workers.swap_remove(self.next);
if self.workers.is_empty() {
Expand Down
File renamed without changes.
File renamed without changes.
78 changes: 78 additions & 0 deletions actix-server/src/counter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use std::cell::Cell;
use std::rc::Rc;

use futures::task::AtomicTask;

#[derive(Clone)]
/// Simple counter with ability to notify task on reaching specific number
///
/// Counter could be cloned, total ncount is shared across all clones.
pub struct Counter(Rc<CounterInner>);

struct CounterInner {
count: Cell<usize>,
capacity: usize,
task: AtomicTask,
}

impl Counter {
/// Create `Counter` instance and set max value.
pub fn new(capacity: usize) -> Self {
Counter(Rc::new(CounterInner {
capacity,
count: Cell::new(0),
task: AtomicTask::new(),
}))
}

pub fn get(&self) -> CounterGuard {
CounterGuard::new(self.0.clone())
}

/// Check if counter is not at capacity
pub fn available(&self) -> bool {
self.0.available()
}

/// Get total number of acquired counts
pub fn total(&self) -> usize {
self.0.count.get()
}
}

pub struct CounterGuard(Rc<CounterInner>);

impl CounterGuard {
fn new(inner: Rc<CounterInner>) -> Self {
inner.inc();
CounterGuard(inner)
}
}

impl Drop for CounterGuard {
fn drop(&mut self) {
self.0.dec();
}
}

impl CounterInner {
fn inc(&self) {
let num = self.count.get() + 1;
self.count.set(num);
if num == self.capacity {
self.task.register();
}
}

fn dec(&self) {
let num = self.count.get();
self.count.set(num - 1);
if num == self.capacity {
self.task.notify();
}
}

fn available(&self) -> bool {
self.count.get() < self.capacity
}
}
2 changes: 2 additions & 0 deletions src/server/mod.rs → actix-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
mod accept;
mod builder;
mod config;
mod counter;
mod server;
mod services;
pub mod ssl;
mod worker;

pub use self::builder::ServerBuilder;
Expand Down
File renamed without changes.
File renamed without changes.
35 changes: 35 additions & 0 deletions actix-server/src/ssl/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//! SSL Services
use std::sync::atomic::{AtomicUsize, Ordering};

use crate::counter::Counter;

#[cfg(feature = "ssl")]
mod openssl;
#[cfg(feature = "ssl")]
pub use self::openssl::OpensslAcceptor;

#[cfg(feature = "tls")]
mod nativetls;
#[cfg(feature = "tls")]
pub use self::nativetls::{NativeTlsAcceptor, TlsStream};

#[cfg(feature = "rust-tls")]
mod rustls;
#[cfg(feature = "rust-tls")]
pub use self::rustls::RustlsAcceptor;

/// Sets the maximum per-worker concurrent ssl connection establish process.
///
/// All listeners will stop accepting connections when this limit is
/// reached. It can be used to limit the global SSL CPU usage.
///
/// By default max connections is set to a 256.
pub fn max_concurrent_ssl_connect(num: usize) {
MAX_CONN.store(num, Ordering::Relaxed);
}

pub(crate) static MAX_CONN: AtomicUsize = AtomicUsize::new(256);

thread_local! {
static MAX_CONN_COUNTER: Counter = Counter::new(MAX_CONN.load(Ordering::Relaxed));
}
2 changes: 1 addition & 1 deletion src/ssl/nativetls.rs → actix-server/src/ssl/nativetls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl<T: AsyncRead + AsyncWrite> NativeTlsAcceptor<T> {
/// Create `NativeTlsAcceptor` instance
pub fn new(acceptor: TlsAcceptor) -> Self {
NativeTlsAcceptor {
acceptor: acceptor.into(),
acceptor,
io: PhantomData,
}
}
Expand Down
Loading

0 comments on commit 8ad93f4

Please sign in to comment.