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
27 changes: 7 additions & 20 deletions src/runtime/server/WebSocketServerContext.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use core::cell::Cell;
use core::ffi::c_void;

use crate::server::jsc::{JSGlobalObject, JSValue, JsResult, VirtualMachine};
Expand Down Expand Up @@ -34,7 +35,7 @@ pub struct Handler {
// LIFETIMES.tsv = STATIC (vm) / JSC_BORROW (global_object) — both outlive the handler.
pub vm: bun_ptr::BackRef<VirtualMachine>,
pub global_object: bun_ptr::BackRef<JSGlobalObject>,
pub active_connections: usize,
pub active_connections: Cell<usize>,

/// used by publish()
pub flags: HandlerFlags,
Expand Down Expand Up @@ -70,30 +71,16 @@ impl Handler {
self.vm.get()
}

/// Zig: `handler.active_connections +|= n` through a `*Handler`.
/// PORT NOTE: takes `&self` and casts away const — the field is owned by
/// `ServerConfig.websocket` and only ever touched on the JS thread, so the
/// data race the borrow-checker would flag here is a false positive.
/// TODO(port): convert `active_connections` to `Cell<usize>`.
#[inline]
pub fn active_connections_saturating_add(&self, n: usize) {
// SAFETY: single-threaded JS heap; see PORT NOTE above. `addr_of!` avoids
// materializing an intermediate `&usize` (invalid_reference_casting lint).
unsafe {
let p = core::ptr::addr_of!(self.active_connections).cast_mut();
*p = (*p).saturating_add(n);
}
self.active_connections
.set(self.active_connections.get().saturating_add(n));
}

/// Zig: `handler.active_connections -|= n` — see `active_connections_saturating_add`.
#[inline]
pub fn active_connections_saturating_sub(&self, n: usize) {
// SAFETY: single-threaded JS heap; see PORT NOTE above. `addr_of!` avoids
// materializing an intermediate `&usize` (invalid_reference_casting lint).
unsafe {
let p = core::ptr::addr_of!(self.active_connections).cast_mut();
*p = (*p).saturating_sub(n);
}
self.active_connections
.set(self.active_connections.get().saturating_sub(n));
}

pub fn run_error_callback(
Expand Down Expand Up @@ -134,7 +121,7 @@ impl Handler {
app: None,
vm: bun_ptr::BackRef::new(VirtualMachine::get()),
global_object: bun_ptr::BackRef::new(global_object),
active_connections: 0,
active_connections: Cell::new(0),
flags: HandlerFlags::empty(),
};

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1486,7 +1486,7 @@ impl<const SSL: bool, const DEBUG: bool> NewServer<SSL, DEBUG> {
self.config
.websocket
.as_ref()
.map_or(0, |ws| ws.handler.active_connections as u32)
.map_or(0, |ws| ws.handler.active_connections.get() as u32)
}

pub fn has_active_web_sockets(&self) -> bool {
Expand Down