Skip to content
Open
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
1 change: 1 addition & 0 deletions examples/from_std_listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ async fn main() {
let listener = TcpListener::bind(addr).unwrap();
println!("listening on {}", addr);
axum_server::from_tcp(listener)
.unwrap()
.serve(app.into_make_service())
.await
.unwrap();
Expand Down
1 change: 1 addition & 0 deletions examples/from_std_listener_rustls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ async fn main() {
let listener = TcpListener::bind(addr).unwrap();
println!("listening on {}", addr);
axum_server::from_tcp_rustls(listener, config)
.unwrap()
.serve(app.into_make_service())
.await
.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion examples/graceful_shutdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async fn main() {
println!("server is shut down");
}

async fn graceful_shutdown(handle: Handle) {
async fn graceful_shutdown(handle: Handle<SocketAddr>) {
// Wait 10 seconds.
sleep(Duration::from_secs(10)).await;

Expand Down
18 changes: 18 additions & 0 deletions examples/hello_world_unix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//! Run with `cargo run --example hello_world_unix` command.
//!
//! To make a request using curl, try `curl --unix-socket /tmp/axum-server.sock http:/localhost`

use axum::{routing::get, Router};
use std::os::unix::net::SocketAddr;

#[tokio::main]
async fn main() {
let app = Router::new().route("/", get(|| async { "Hello, world!" }));

let addr = SocketAddr::from_pathname("/tmp/axum-server.sock").unwrap();
println!("listening on {}", addr.as_pathname().unwrap().display());
axum_server::bind(addr)
.serve(app.into_make_service())
.await
.unwrap();
}
2 changes: 1 addition & 1 deletion examples/shutdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async fn main() {
println!("server is shut down");
}

async fn shutdown(handle: Handle) {
async fn shutdown(handle: Handle<SocketAddr>) {
// Wait 20 seconds.
sleep(Duration::from_secs(20)).await;

Expand Down
60 changes: 41 additions & 19 deletions src/handle.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::notify_once::NotifyOnce;
use crate::{notify_once::NotifyOnce, server::Address};
use std::{
net::SocketAddr,
sync::{
atomic::{AtomicUsize, Ordering},
Arc, Mutex,
Expand All @@ -10,14 +9,22 @@ use std::{
use tokio::{sync::Notify, time::sleep};

/// A handle for [`Server`](crate::server::Server).
#[derive(Clone, Debug, Default)]
pub struct Handle {
inner: Arc<HandleInner>,
#[derive(Clone, Debug)]
pub struct Handle<A: Address> {
inner: Arc<HandleInner<A>>,
}

#[derive(Debug, Default)]
struct HandleInner {
addr: Mutex<Option<SocketAddr>>,
impl<A: Address> Default for Handle<A> {
fn default() -> Self {
Self {
inner: Default::default(),
}
}
}

#[derive(Debug)]
struct HandleInner<A: Address> {
addr: Mutex<Option<A>>,
addr_notify: Notify,
conn_count: AtomicUsize,
shutdown: NotifyOnce,
Expand All @@ -26,7 +33,22 @@ struct HandleInner {
conn_end: NotifyOnce,
}

impl Handle {
// Manually implemented as the derive macro will want A to be Default.
impl<A: Address> Default for HandleInner<A> {
fn default() -> Self {
Self {
addr: Default::default(),
addr_notify: Default::default(),
conn_count: Default::default(),
shutdown: Default::default(),
graceful: Default::default(),
graceful_dur: Default::default(),
conn_end: Default::default(),
}
}
}

impl<A: Address> Handle<A> {
/// Create a new handle.
pub fn new() -> Self {
Self::default()
Expand Down Expand Up @@ -54,25 +76,25 @@ impl Handle {
/// Returns local address and port when server starts listening.
///
/// Returns `None` if server fails to bind.
pub async fn listening(&self) -> Option<SocketAddr> {
pub async fn listening(&self) -> Option<A> {
let notified = self.inner.addr_notify.notified();

if let Some(addr) = *self.inner.addr.lock().unwrap() {
if let Some(addr) = self.inner.addr.lock().unwrap().clone() {
return Some(addr);
}

notified.await;

*self.inner.addr.lock().unwrap()
self.inner.addr.lock().unwrap().clone()
}

pub(crate) fn notify_listening(&self, addr: Option<SocketAddr>) {
pub(crate) fn notify_listening(&self, addr: Option<A>) {
*self.inner.addr.lock().unwrap() = addr;

self.inner.addr_notify.notify_waiters();
}

pub(crate) fn watcher(&self) -> Watcher {
pub(crate) fn watcher(&self) -> Watcher<A> {
Watcher::new(self.clone())
}

Expand Down Expand Up @@ -102,12 +124,12 @@ impl Handle {
}
}

pub(crate) struct Watcher {
handle: Handle,
pub(crate) struct Watcher<A: Address> {
handle: Handle<A>,
}

impl Watcher {
fn new(handle: Handle) -> Self {
impl<A: Address> Watcher<A> {
fn new(handle: Handle<A>) -> Self {
handle.inner.conn_count.fetch_add(1, Ordering::SeqCst);

Self { handle }
Expand All @@ -122,7 +144,7 @@ impl Watcher {
}
}

impl Drop for Watcher {
impl<A: Address> Drop for Watcher<A> {
fn drop(&mut self) {
let count = self.handle.inner.conn_count.fetch_sub(1, Ordering::SeqCst) - 1;

Expand Down
9 changes: 8 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,12 @@ pub use self::{
// addr_incoming_config::AddrIncomingConfig,
handle::Handle,
// http_config::HttpConfig,
server::{bind, from_tcp, Server},
server::{bind, from_tcp, AddrListener, Address, Server},
};

#[cfg(unix)]
pub use self::server::from_unix;

#[cfg(feature = "tls-rustls-no-provider")]
#[cfg_attr(docsrs, doc(cfg(feature = "tls-rustls")))]
pub mod tls_rustls;
Expand All @@ -116,6 +119,10 @@ pub mod tls_rustls;
#[cfg(feature = "tls-rustls-no-provider")]
pub use self::tls_rustls::export::{bind_rustls, from_tcp_rustls};

#[doc(inline)]
#[cfg(all(feature = "tls-rustls-no-provider", unix))]
pub use self::tls_rustls::export::from_unix_rustls;

#[cfg(feature = "tls-openssl")]
#[cfg_attr(docsrs, doc(cfg(feature = "tls-openssl")))]
pub mod tls_openssl;
Expand Down
Loading