Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ categories = ["network-programming", "web-programming"]
keywords = ["web", "http", "protocol"]

[features]
default = ["server"]
default = ["server", "threadpool"]
full = ["client", "server"]
server = ["threadpool"]
threadpool = ["dep:threadpool"]
server = []
unix-sockets = []
client = []

Expand Down
8 changes: 8 additions & 0 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use std::{

use headers::{HeaderMapExt, HeaderValue};
use http::{Method, Request, Response, StatusCode, Version};
#[cfg(feature = "threadpool")]
use threadpool::ThreadPool;

use crate::{
Expand Down Expand Up @@ -118,6 +119,7 @@ where

/// A listening HTTP server that accepts HTTP 1 connections.
pub struct Server<'a> {
#[cfg(feature = "threadpool")]
thread_pool: ThreadPool,
incoming: Box<dyn Iterator<Item = Connection> + 'a>,
}
Expand Down Expand Up @@ -157,6 +159,7 @@ impl<'a> Server<'a> {
/// })
/// # }
/// ```
#[cfg(feature = "threadpool")]
pub fn serve<S>(self, service: S) -> io::Result<()>
where
S: Service,
Expand Down Expand Up @@ -219,6 +222,7 @@ impl<'a> Server<'a> {
/// })
/// # }
/// ```
#[cfg(feature = "threadpool")]
pub fn make_service<M>(self, make_service: M) -> io::Result<()>
where
M: MakeService,
Expand All @@ -239,13 +243,15 @@ impl<'a> Server<'a> {
}

pub struct ServerBuilder {
#[cfg(feature = "threadpool")]
max_threads: usize,
read_timeout: Option<Duration>,
}

impl Default for ServerBuilder {
fn default() -> Self {
Self {
#[cfg(feature = "threadpool")]
max_threads: 512,
read_timeout: None,
}
Expand All @@ -269,6 +275,7 @@ impl ServerBuilder {
/// })
/// # }
/// ```
#[cfg(feature = "threadpool")]
pub fn max_threads(self, max_threads: usize) -> Self {
Self {
max_threads,
Expand Down Expand Up @@ -362,6 +369,7 @@ impl ServerBuilder {
conns: T,
) -> Server<'a> {
Server {
#[cfg(feature = "threadpool")]
thread_pool: ThreadPool::new(self.max_threads),
incoming: Box::new(conns.into_iter().filter_map(move |conn| {
conn.set_read_timeout(self.read_timeout).ok()?;
Expand Down
Loading