Skip to content

Commit

Permalink
barely working async impl
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateo Cabanal committed Aug 12, 2023
1 parent 6c888c7 commit 2da3f1d
Show file tree
Hide file tree
Showing 7 changed files with 471 additions and 63 deletions.
112 changes: 110 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions tinyhttp-internal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ num_cpus = "1.16.0"
rusty_pool = "0.7.0"
thiserror = "1"


#async-std = { version = "1.12.0", features = ["attributes"], optional = true }
#futures = { version = "0.3.21", features = ["thread-pool"], optional = true }

[dependencies.flate2]
features = ["miniz-sys"]
version = "1"
Expand All @@ -31,10 +27,16 @@ features = ["std"]
version = "0.4"
optional = true

[dependencies.tokio]
features = ["rt-multi-thread", "io-util", "net", "macros", "fs"]
version = "1.3"
optional = true

[features]
default = ["sys", "log"]
default = ["sys", "log", "async"]
openssl_vendor = ["ssl", "openssl?/vendored"]
#async = ["dep:async-std", "dep:futures"]
middleware = []
async = ["dep:tokio"]
ssl = ["dep:openssl"]
sys = ["ssl", "dep:flate2"]
log = ["dep:log"]
36 changes: 7 additions & 29 deletions tinyhttp-internal/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,7 @@ use std::fmt::Debug;
#[cfg(feature = "ssl")]
use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};

#[cfg(feature = "async")]
use crate::async_http::start_http;

#[cfg(feature = "async")]
use async_std::net::{Incoming, TcpListener};

#[cfg(feature = "async")]
use futures::executor::{ThreadPool, ThreadPoolBuilder};

#[cfg(not(feature = "async"))]
use crate::http::start_http;

use crate::response::Response;
Expand All @@ -26,6 +17,9 @@ use rusty_pool::{Builder, ThreadPool};
#[cfg(not(feature = "async"))]
use std::net::{Incoming, TcpListener};

#[cfg(feature = "async")]
use tokio::{net::TcpListener};

use std::sync::Mutex;

#[cfg(test)]
Expand Down Expand Up @@ -60,7 +54,7 @@ impl Clone for Box<dyn Route> {
}

pub struct HttpListener {
socket: TcpListener,
pub(crate) socket: TcpListener,
pub config: Config,
pub pool: ThreadPool,
pub use_pool: bool,
Expand All @@ -82,13 +76,7 @@ impl HttpListener {
HttpListener {
socket: socket.into(),
config,
#[cfg(not(feature = "async"))]
pool: ThreadPool::default(),
#[cfg(feature = "async")]
pool: ThreadPoolBuilder::new()
.pool_size(num_cpus::get())
.create()
.unwrap(),
#[cfg(feature = "ssl")]
ssl_acpt,
use_pool: true,
Expand All @@ -97,13 +85,7 @@ impl HttpListener {
HttpListener {
socket: socket.into(),
config,
#[cfg(not(feature = "async"))]
pool: ThreadPool::default(),
#[cfg(feature = "async")]
pool: ThreadPoolBuilder::new()
.pool_size(num_cpus::get())
.create()
.unwrap(),
#[cfg(feature = "ssl")]
ssl_acpt: None,
use_pool: true,
Expand All @@ -112,14 +94,8 @@ impl HttpListener {
}

pub fn threads(mut self, threads: usize) -> Self {
#[cfg(not(feature = "async"))]
let pool = Builder::new().core_size(threads).build();

#[cfg(feature = "async")]
let pool = ThreadPoolBuilder::new()
.pool_size(threads)
.create()
.unwrap();

self.pool = pool;
self
Expand All @@ -136,13 +112,15 @@ impl HttpListener {
}

#[cfg(feature = "async")]
pub async fn start_async(self) {
pub async fn start(self) {
start_http(self).await;
}

#[cfg(not(feature = "async"))]
pub fn get_stream(&self) -> Incoming<'_> {
self.socket.incoming()
}

}

#[derive(Clone)]
Expand Down
Loading

0 comments on commit 2da3f1d

Please sign in to comment.