Skip to content

Commit

Permalink
Make threadpool optional (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
petoknm authored Jan 30, 2025
1 parent 14ecc47 commit c2359a5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
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 Server<'_> {
/// })
/// # }
/// ```
#[cfg(feature = "threadpool")]
pub fn serve<S>(self, service: S) -> io::Result<()>
where
S: Service,
Expand Down Expand Up @@ -218,6 +221,7 @@ impl Server<'_> {
/// })
/// # }
/// ```
#[cfg(feature = "threadpool")]
pub fn make_service<M>(self, make_service: M) -> io::Result<()>
where
M: MakeService + 'static,
Expand All @@ -236,13 +240,15 @@ impl Server<'_> {
}

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 @@ -266,6 +272,7 @@ impl ServerBuilder {
/// })
/// # }
/// ```
#[cfg(feature = "threadpool")]
pub fn max_threads(self, max_threads: usize) -> Self {
Self {
max_threads,
Expand Down Expand Up @@ -359,6 +366,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

0 comments on commit c2359a5

Please sign in to comment.