Rc and cell instead of arc atomic

This commit is contained in:
Filip Tibell 2025-04-28 11:35:53 +02:00
parent 49001bf6d6
commit 03c013cc3c
No known key found for this signature in database

View file

@ -1,10 +1,4 @@
use std::{ use std::{cell::Cell, net::SocketAddr, rc::Rc};
net::SocketAddr,
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
};
use async_net::TcpListener; use async_net::TcpListener;
use futures_lite::pin; use futures_lite::pin;
@ -45,10 +39,10 @@ pub async fn serve(lua: Lua, port: u16, config: ServeConfig) -> LuaResult<ServeH
lua.spawn_local({ lua.spawn_local({
let lua = lua.clone(); let lua = lua.clone();
async move { async move {
let handle_dropped = Arc::new(AtomicBool::new(false)); let handle_dropped = Rc::new(Cell::new(false));
loop { loop {
// 1. Keep accepting new connections until we should shutdown // 1. Keep accepting new connections until we should shutdown
let (conn, addr) = if handle_dropped.load(Ordering::SeqCst) { let (conn, addr) = if handle_dropped.get() {
// 1a. Handle has been dropped, and we don't need to listen for shutdown // 1a. Handle has been dropped, and we don't need to listen for shutdown
match listener.accept().await { match listener.accept().await {
Ok(acc) => acc, Ok(acc) => acc,
@ -65,7 +59,7 @@ pub async fn serve(lua: Lua, port: u16, config: ServeConfig) -> LuaResult<ServeH
// NOTE #1: We will only get a RecvError if the serve handle is dropped, // NOTE #1: We will only get a RecvError if the serve handle is dropped,
// this means lua has garbage collected it and the user does not want // this means lua has garbage collected it and the user does not want
// to manually stop the server using the serve handle. Run forever. // to manually stop the server using the serve handle. Run forever.
handle_dropped.store(true, Ordering::SeqCst); handle_dropped.set(true);
continue; continue;
} }
Either::Right(Ok(acc)) => acc, Either::Right(Ok(acc)) => acc,
@ -84,14 +78,14 @@ pub async fn serve(lua: Lua, port: u16, config: ServeConfig) -> LuaResult<ServeH
let mut svc = service.clone(); let mut svc = service.clone();
svc.address = addr; svc.address = addr;
let handle_dropped = Arc::clone(&handle_dropped); let handle_dropped = Rc::clone(&handle_dropped);
async move { async move {
let conn = Http1Builder::new() let conn = Http1Builder::new()
.timer(HyperTimer) .timer(HyperTimer)
.keep_alive(true) .keep_alive(true)
.serve_connection(io, svc) .serve_connection(io, svc)
.with_upgrades(); .with_upgrades();
if handle_dropped.load(Ordering::SeqCst) { if handle_dropped.get() {
if let Err(_err) = conn.await { if let Err(_err) = conn.await {
// TODO: Propagate error somehow // TODO: Propagate error somehow
} }
@ -105,7 +99,7 @@ pub async fn serve(lua: Lua, port: u16, config: ServeConfig) -> LuaResult<ServeH
Either::Left(Ok(())) => conn.as_mut().graceful_shutdown(), Either::Left(Ok(())) => conn.as_mut().graceful_shutdown(),
Either::Left(Err(_)) => { Either::Left(Err(_)) => {
// Same as note #1 // Same as note #1
handle_dropped.store(true, Ordering::SeqCst); handle_dropped.set(true);
if let Err(_err) = conn.await { if let Err(_err) = conn.await {
// TODO: Propagate error somehow // TODO: Propagate error somehow
} }