Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Format code to make CI happy #38

Merged
merged 1 commit into from
Jan 26, 2024
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
21 changes: 12 additions & 9 deletions examples/benches/http-server-bench.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion, Throughput, BenchmarkId};
use nuclei::block_on;
use criterion::async_executor::FuturesExecutor;

use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
use nuclei::block_on;

use nuclei::*;
use std::net::TcpListener;
Expand Down Expand Up @@ -42,7 +41,7 @@ async fn listen(listener: Handle<TcpListener>) -> Result<()> {
println!("Connection error: {:#?}", err);
}
})
.detach();
.detach();
}
}

Expand Down Expand Up @@ -89,15 +88,19 @@ pub fn http_server_bench(c: &mut Criterion) {
let mut group = c.benchmark_group("http_server_bench");
for i in [1_u64, 10_u64, 25_u64].iter() {
group.throughput(Throughput::Bytes(DATA.len() as u64 * i));
group.bench_function(BenchmarkId::from_parameter(i), |b| b.to_async(FuturesExecutor).iter(|| async {
let tasks = (0..*i).map(|e| surf::get(uri).recv_string()).collect::<Vec<_>>();
join_all(tasks).await;
}));
group.bench_function(BenchmarkId::from_parameter(i), |b| {
b.to_async(FuturesExecutor).iter(|| async {
let tasks = (0..*i)
.map(|e| surf::get(uri).recv_string())
.collect::<Vec<_>>();
join_all(tasks).await;
})
});
}
group.finish();

nuclei::block_on(x.cancel());
}

criterion_group!(benches, http_server_bench);
criterion_main!(benches);
criterion_main!(benches);
6 changes: 1 addition & 5 deletions examples/fwrite-vect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ use std::fs::{File, OpenOptions};
use std::io;
use std::path::PathBuf;



use futures::{AsyncSeekExt, AsyncWriteExt};
use futures::AsyncReadExt;
use futures::{AsyncSeekExt, AsyncWriteExt};
use std::io::{IoSlice, SeekFrom};


const IOVEC_WIDTH: usize = 1 << 10;

#[nuclei::main]
Expand Down Expand Up @@ -39,7 +36,6 @@ async fn main() -> io::Result<()> {
assert!(file.seek(SeekFrom::Start(0)).await.is_ok());
file.read_to_string(&mut bufv).await.unwrap();


assert_eq!(bufv.matches('A').count(), IOVEC_WIDTH);
assert_eq!(bufv.matches('B').count(), IOVEC_WIDTH);
assert_eq!(bufv.matches('C').count(), IOVEC_WIDTH);
Expand Down
2 changes: 0 additions & 2 deletions examples/fwrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::fs::{File, OpenOptions};
use std::io;
use std::path::PathBuf;


use futures::io::SeekFrom;
use futures::{AsyncReadExt, AsyncSeekExt, AsyncWriteExt};

Expand Down Expand Up @@ -44,7 +43,6 @@ async fn main() -> io::Result<()> {
assert_eq!(file.read_to_end(&mut buf).await.unwrap(), dark_matter.len());
assert_eq!(&buf[0..dark_matter.len()], dark_matter.as_bytes());


println!("Length of file is {}", buf.len());

Ok(())
Expand Down
9 changes: 4 additions & 5 deletions examples/h1-server-multishot.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#[cfg(target_os = "linux")]
#[nuclei::main]
async fn main() -> anyhow::Result<()> {
async fn main() -> anyhow::Result<()> {
use nuclei::*;
use std::net::TcpListener;

use futures::stream::StreamExt;
use anyhow::Result;
use async_dup::Arc;
use futures::stream::StreamExt;

use futures::prelude::*;
use http_types::{Request, Response, StatusCode};
Expand All @@ -17,7 +17,6 @@ async fn main() -> anyhow::Result<()> {
////// Reason is: Multishot based IO is only part of io_uring backend.
/////////////////////////////////////////////////////////////////////////


static DATA: &'static str = include_str!("data/quark-gluon-plasma");

/// Serves a request and returns a response.
Expand Down Expand Up @@ -46,7 +45,7 @@ async fn main() -> anyhow::Result<()> {
println!("Connection error: {:#?}", err);
}
})
.detach();
.detach();
}

Ok(())
Expand All @@ -61,4 +60,4 @@ async fn main() -> anyhow::Result<()> {
#[cfg(target_os = "macos")]
fn main() {
panic!("This example can only be run by IO_URING backend.");
}
}
2 changes: 2 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
edition = "2021"
reorder_imports = true
6 changes: 2 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@

///
/// Nuclei's proactor configuration.
#[derive(Clone, Debug, Default)]
pub struct NucleiConfig {
/// **IO_URING Configuration** allows you to configure [io_uring](https://unixism.net/loti/what_is_io_uring.html) backend.
pub iouring: IoUringConfiguration
pub iouring: IoUringConfiguration,
}

/// **IO_URING Configuration**
Expand Down Expand Up @@ -56,7 +55,6 @@ pub struct IoUringConfiguration {
/// If [None] passed unbounded workers will be limited by the process task limit,
/// as indicated by the rlimit [RLIMIT_NPROC](https://man7.org/linux/man-pages/man2/getrlimit.2.html) limit.
pub per_numa_unbounded_worker_count: Option<u32>,

// XXX: `redrive_kthread_wake` = bool, syncs queue changes so kernel threads got awakened. increased cpu usage.
}

Expand All @@ -69,4 +67,4 @@ impl Default for IoUringConfiguration {
per_numa_unbounded_worker_count: Some(1 << 9),
}
}
}
}
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@
#![allow(dead_code, unused_variables)]

mod async_io;
/// Nuclei's configuration options reside here.
pub mod config;
mod handle;
mod proactor;
mod submission_handler;
mod sys;
mod utils;
mod waker;
/// Nuclei's configuration options reside here.
pub mod config;

#[cfg(not(any(
target_os = "linux", // epoll, iouring
Expand Down Expand Up @@ -84,4 +84,4 @@ pub use async_global_executor::*;
pub use proactor::*;

#[cfg(feature = "attributes")]
pub use nuclei_attributes::*;
pub use nuclei_attributes::*;
23 changes: 15 additions & 8 deletions src/proactor.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::ops::DerefMut;

Check warning on line 1 in src/proactor.rs

View workflow job for this annotation

GitHub Actions / stable - x86_64-unknown-linux-gnu

unused import: `std::ops::DerefMut`

Check warning on line 1 in src/proactor.rs

View workflow job for this annotation

GitHub Actions / stable - x86_64-unknown-linux-gnu

unused import: `std::ops::DerefMut`

Check warning on line 1 in src/proactor.rs

View workflow job for this annotation

GitHub Actions / nightly - x86_64-unknown-linux-gnu

unused import: `std::ops::DerefMut`

Check warning on line 1 in src/proactor.rs

View workflow job for this annotation

GitHub Actions / nightly - x86_64-unknown-linux-gnu

unused import: `std::ops::DerefMut`

Check warning on line 1 in src/proactor.rs

View workflow job for this annotation

GitHub Actions / stable - x86_64-apple-darwin

unused import: `std::ops::DerefMut`

Check warning on line 1 in src/proactor.rs

View workflow job for this annotation

GitHub Actions / stable - x86_64-apple-darwin

unused import: `std::ops::DerefMut`
use std::task::{Context, Poll};
use std::time::Duration;
use std::{future::Future, io};
use std::ops::DerefMut;

use once_cell::sync::{Lazy, OnceCell};
use crate::config::NucleiConfig;
use once_cell::sync::{Lazy, OnceCell};

Check warning on line 7 in src/proactor.rs

View workflow job for this annotation

GitHub Actions / stable - x86_64-unknown-linux-gnu

unused import: `Lazy`

Check warning on line 7 in src/proactor.rs

View workflow job for this annotation

GitHub Actions / stable - x86_64-unknown-linux-gnu

unused import: `Lazy`

Check warning on line 7 in src/proactor.rs

View workflow job for this annotation

GitHub Actions / nightly - x86_64-unknown-linux-gnu

unused import: `Lazy`

Check warning on line 7 in src/proactor.rs

View workflow job for this annotation

GitHub Actions / nightly - x86_64-unknown-linux-gnu

unused import: `Lazy`

Check warning on line 7 in src/proactor.rs

View workflow job for this annotation

GitHub Actions / stable - x86_64-apple-darwin

unused import: `Lazy`

Check warning on line 7 in src/proactor.rs

View workflow job for this annotation

GitHub Actions / stable - x86_64-apple-darwin

unused import: `Lazy`

use super::syscore::*;
use super::waker::*;
Expand All @@ -26,17 +26,25 @@
pub fn get() -> &'static Proactor {
unsafe {
&PROACTOR.get_or_init(|| {
Proactor(SysProactor::new(NucleiConfig::default()).expect("cannot initialize IO backend"))
Proactor(
SysProactor::new(NucleiConfig::default())
.expect("cannot initialize IO backend"),
)
})
}
}

/// Builds a proactor instance with given config and returns a reference to it.
pub fn with_config(config: NucleiConfig) -> &'static Proactor {
unsafe {
let mut proactor = Proactor(SysProactor::new(config.clone()).expect("cannot initialize IO backend"));
PROACTOR.set(proactor).map_err(|e| "Proactor instance not being able to set.").unwrap();
let proactor = Proactor(SysProactor::new(config).expect("cannot initialize IO backend"));
let mut proactor =

Check warning on line 40 in src/proactor.rs

View workflow job for this annotation

GitHub Actions / stable - x86_64-apple-darwin

variable does not need to be mutable

Check warning on line 40 in src/proactor.rs

View workflow job for this annotation

GitHub Actions / stable - x86_64-apple-darwin

variable does not need to be mutable
Proactor(SysProactor::new(config.clone()).expect("cannot initialize IO backend"));
PROACTOR
.set(proactor)
.map_err(|e| "Proactor instance not being able to set.")
.unwrap();
let proactor =
Proactor(SysProactor::new(config).expect("cannot initialize IO backend"));
&PROACTOR.get_or_init(|| proactor)
}
}
Expand Down Expand Up @@ -130,7 +138,6 @@
let new = Proactor::with_config(config);
let old = Proactor::get();


let nsq = new.0.sq.lock();
let nlen = nsq.capacity();
drop(nsq);
Expand All @@ -146,4 +153,4 @@
assert_eq!(nlen, 16);
assert_eq!(olen, 16);
}
}
}
2 changes: 1 addition & 1 deletion src/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub enum IoBackend {
/// Linux backend
Epoll,
/// Linux backend
IoUring
IoUring,
}

#[cfg(unix)]
Expand Down
4 changes: 2 additions & 2 deletions src/syscore/bsd/kqueue.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::sys::event::{kevent_ts, kqueue, KEvent};
use ahash::{HashMap, HashMapExt};
use futures::channel::oneshot;
use lever::prelude::*;
use pin_utils::unsafe_pinned;
Expand All @@ -8,7 +9,6 @@ use std::mem::MaybeUninit;
use std::os::unix::io::{AsRawFd, RawFd};
use std::pin::Pin;
use std::task::{Context, Poll};
use ahash::{HashMap, HashMapExt};
use std::{os::unix::net::UnixStream, time::Duration};

macro_rules! syscall {
Expand All @@ -25,10 +25,10 @@ macro_rules! syscall {
///////////////////
///////////////////

use crate::config::NucleiConfig;
use socket2::SockAddr;
use std::mem;
use std::os::unix::net::SocketAddr as UnixSocketAddr;
use crate::config::NucleiConfig;

fn max_len() -> usize {
// The maximum read limit on most posix-like systems is `SSIZE_MAX`,
Expand Down
2 changes: 1 addition & 1 deletion src/syscore/bsd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ pub(crate) use kqueue::*;

pub(crate) use processor::*;

pub const BACKEND: crate::sys::IoBackend = crate::sys::IoBackend::Kqueue;
pub const BACKEND: crate::sys::IoBackend = crate::sys::IoBackend::Kqueue;
4 changes: 2 additions & 2 deletions src/syscore/linux/epoll/epoll.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::sys::epoll::*;
use ahash::{HashMap, HashMapExt};
use futures::channel::oneshot;
use lever::prelude::*;
use pin_utils::unsafe_pinned;
use ahash::{HashMap, HashMapExt};
use std::future::Future;
use std::io::{self, Read, Write};
use std::mem::MaybeUninit;
Expand All @@ -25,10 +25,10 @@ macro_rules! syscall {
///////////////////
///////////////////

use crate::config::NucleiConfig;
use socket2::SockAddr;
use std::mem;
use std::os::unix::net::SocketAddr as UnixSocketAddr;
use crate::config::NucleiConfig;

fn max_len() -> usize {
// The maximum read limit on most posix-like systems is `SSIZE_MAX`,
Expand Down
2 changes: 1 addition & 1 deletion src/syscore/linux/epoll/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ pub(crate) use fs::*;

pub(crate) use processor::*;

pub const BACKEND: crate::sys::IoBackend = crate::sys::IoBackend::Epoll;
pub const BACKEND: crate::sys::IoBackend = crate::sys::IoBackend::Epoll;
1 change: 0 additions & 1 deletion src/syscore/linux/epoll/nethandle.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

use std::io;
use std::marker::Unpin;
use std::net::{SocketAddr, ToSocketAddrs};
Expand Down
2 changes: 1 addition & 1 deletion src/syscore/linux/iouring/fs/buffer.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use futures::ready;
use rustix_uring::types::Statx;
use std::alloc::{alloc, dealloc, handle_alloc_error, Layout};
use std::cmp;
use std::io;
use std::mem;
use std::ptr::NonNull;
use std::slice;
use std::task::Poll;
use rustix_uring::types::Statx;

use super::cancellation::Cancellation;

Expand Down
Loading
Loading