use std::collections::HashMap;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering};
use ahash::AHashSet;
use crossbeam_channel::{Receiver as CBReceiver, Sender as CBSender, unbounded};
use dashmap::{DashMap, DashSet};
use tokio::sync::mpsc;
use tokio_tungstenite::tungstenite::protocol::Message;
use super::signing::{ClientKey, ServerSigner};
use super::{BroadcastQueue, Fanout, TopicIndex, WsFrame};
use crate::cluster::{ClusterCommand, ClusterDlq, ClusterMetrics};
use crate::jwt::JwtConfig;
pub(crate) const CLOSE_NORMAL: u16 = 1000; pub(crate) const CLOSE_GOING_AWAY: u16 = 1001; pub(crate) const CLOSE_INTERNAL_ERROR: u16 = 1011; pub(crate) const CLOSE_TRY_AGAIN_LATER: u16 = 1013; pub(crate) const CLOSE_AUTH_FAILED: u16 = 4401; pub(crate) const CLOSE_TOKEN_EXPIRED: u16 = 4403; pub(crate) const CLOSE_IDLE: u16 = 4408; pub(crate) const CLOSE_RATE_LIMITED: u16 = 4429; pub(crate) const CLOSE_E2E_UNSUPPORTED: u16 = 4800; pub(crate) const CLOSE_E2E_BAD_FRAME: u16 = 4802; pub(crate) const CLOSE_E2E_EXHAUSTED: u16 = 4803;
pub(crate) const CLOSE_GRACE: std::time::Duration = std::time::Duration::from_secs(5);
#[derive(Clone)]
pub(crate) struct Closer {
token: tokio_util::sync::CancellationToken,
closing: Arc<AtomicBool>,
stops: Arc<std::sync::Mutex<Vec<futures_util::future::AbortHandle>>>,
}
impl Closer {
pub(crate) fn new() -> Self {
Self {
token: tokio_util::sync::CancellationToken::new(),
closing: Arc::new(AtomicBool::new(false)),
stops: Arc::new(std::sync::Mutex::new(Vec::new())),
}
}
pub(crate) fn stop_registration(&self) -> futures_util::future::AbortRegistration {
let (handle, registration) = futures_util::future::AbortHandle::new_pair();
let mut stops = self.stops.lock().unwrap_or_else(|e| e.into_inner());
if self.token.is_cancelled() {
handle.abort();
} else {
stops.push(handle);
}
registration
}
fn cancel(&self) {
let mut stops = self.stops.lock().unwrap_or_else(|e| e.into_inner());
self.token.cancel();
for stop in stops.drain(..) {
stop.abort();
}
}
pub(crate) fn token(&self) -> &tokio_util::sync::CancellationToken {
&self.token
}
pub(crate) fn is_closing(&self) -> bool {
self.closing.load(Ordering::Acquire)
}
pub(crate) fn abort(&self) {
self.closing.store(true, Ordering::Release);
self.cancel();
}
pub(crate) fn close(
&self,
tx: &mpsc::UnboundedSender<WsFrame>,
pending: &AtomicUsize,
code: u16,
reason: &str,
) {
use tokio_tungstenite::tungstenite::protocol::frame::coding::CloseCode;
use tokio_tungstenite::tungstenite::protocol::{CloseFrame, Message};
if self.closing.swap(true, Ordering::AcqRel) {
return;
}
let frame = Message::Close(Some(CloseFrame {
code: CloseCode::from(code),
reason: reason.to_owned().into(),
}));
let bytes = 8 + reason.len();
pending.fetch_add(bytes, Ordering::Relaxed);
if tx.send(WsFrame::Msg(frame)).is_err() {
pending.fetch_sub(bytes, Ordering::Relaxed);
}
let closer = self.clone();
tokio::spawn(async move {
tokio::select! {
() = tokio::time::sleep(CLOSE_GRACE) => closer.cancel(),
() = closer.token.cancelled() => {}
}
});
}
}
#[derive(Clone)]
pub(crate) struct ConnectionHandle {
pub(crate) tx: mpsc::UnboundedSender<WsFrame>,
pub(crate) out: Arc<Outbox>,
pub(crate) closer: Closer,
pub(crate) msgpack: bool,
pub(crate) lane: u8,
}
#[derive(Default)]
pub(crate) struct Outbox {
pub(crate) pending: AtomicUsize,
pub(crate) closed: AtomicBool,
pub(crate) admitted: AtomicBool,
pub(crate) cipher: std::sync::OnceLock<Seal>,
pub(crate) frames: parking_lot::Mutex<BroadcastQueue>,
pub(crate) notify: tokio::sync::Notify,
}
pub(crate) struct CloseOutbox(pub(crate) Arc<Outbox>);
impl Drop for CloseOutbox {
fn drop(&mut self) {
self.0.closed.store(true, Ordering::Release);
}
}
pub(crate) enum Seal {
Legacy(Arc<crate::crypto::Aes256Gcm>),
Session,
}
impl ConnectionHandle {
pub(crate) fn close(&self, code: u16, reason: &str) {
self.closer.close(&self.tx, &self.out.pending, code, reason);
}
}
pub(crate) struct QueueGroup {
pub(crate) members: Vec<(String, ConnectionHandle)>,
pub(crate) next: AtomicUsize,
}
#[cfg_attr(not(feature = "python"), allow(dead_code))]
pub(super) enum InboundEvent {
Connect {
conn_id: String,
cookies: String,
},
AuthConnect {
conn_id: String,
user_id: String,
},
Message {
conn_id: String,
value: serde_json::Value,
},
Signed {
conn_id: String,
value: serde_json::Value,
},
RawText {
conn_id: String,
text: String,
},
Binary {
conn_id: String,
data: Vec<u8>,
},
Disconnect {
conn_id: String,
},
PresenceJoin {
topic: String,
user_id: String,
data: serde_json::Value,
},
PresenceLeave {
topic: String,
user_id: String,
data: serde_json::Value,
},
}
#[cfg_attr(not(feature = "python"), allow(dead_code))]
pub(super) struct Queued {
pub(super) event: InboundEvent,
bytes: Option<usize>,
}
#[repr(align(128))]
#[derive(Default)]
struct InboundRoom {
messages: AtomicUsize,
bytes: AtomicUsize,
}
#[derive(Default)]
#[cfg_attr(not(feature = "python"), allow(dead_code))]
pub(super) struct Taken {
messages: usize,
bytes: usize,
lifecycle: usize,
}
impl Taken {
#[cfg_attr(not(feature = "python"), allow(dead_code))]
pub(super) fn take(&mut self, queued: Queued) -> InboundEvent {
if let Some(bytes) = queued.bytes {
self.messages += 1;
self.bytes += bytes;
} else if queued.event.is_lifecycle() {
self.lifecycle += 1;
}
queued.event
}
}
#[cfg_attr(not(feature = "python"), allow(dead_code))]
pub(super) struct CallbackRoom {
state: Arc<SharedState>,
bytes: usize,
}
impl Drop for CallbackRoom {
fn drop(&mut self) {
self.state.release_message(self.bytes);
}
}
pub(crate) struct DeduplicationState {
pub(super) seen: AHashSet<String>,
pub(super) queue: std::collections::VecDeque<String>,
pub(super) max_entries: usize,
}
pub(crate) const COMMAND_QUEUE: usize = 65_536;
pub(crate) type PresenceEvents = Box<dyn Fn(super::handler::Presence) + Send + Sync>;
pub(crate) enum Bind {
Proved(Vec<u8>),
Expected { point: Vec<u8>, user_id: String },
Unproved,
}
#[derive(Clone)]
pub(crate) struct Commands {
tx: mpsc::UnboundedSender<ServerCommand>,
queued: Arc<AtomicUsize>,
}
impl Commands {
pub(crate) fn new(tx: mpsc::UnboundedSender<ServerCommand>, queued: Arc<AtomicUsize>) -> Self {
Self { tx, queued }
}
pub(crate) fn send(
&self,
cmd: ServerCommand,
) -> Result<(), mpsc::error::SendError<ServerCommand>> {
self.queued.fetch_add(1, Ordering::Relaxed);
self.tx.send(cmd).inspect_err(|_| {
self.queued.fetch_sub(1, Ordering::Relaxed);
})
}
pub(crate) fn is_full(&self) -> bool {
self.queued.load(Ordering::Relaxed) >= COMMAND_QUEUE
}
pub(crate) fn is_closed(&self) -> bool {
self.tx.is_closed()
}
pub(crate) fn wait_for_room(&self) {
while self.is_full() && !self.tx.is_closed() {
std::thread::sleep(std::time::Duration::from_micros(50));
}
}
}
#[cfg_attr(not(feature = "python"), allow(dead_code))]
pub(crate) fn inbound_cost(wire: usize, value: &serde_json::Value) -> usize {
let decoded = decoded_size(value);
if decoded > wire.saturating_mul(DECODED_SIZE_RATIO) {
decoded
} else {
wire
}
}
#[cfg_attr(not(feature = "python"), allow(dead_code))]
const DECODED_SIZE_RATIO: usize = 4;
#[cfg_attr(not(feature = "python"), allow(dead_code))]
fn decoded_size(value: &serde_json::Value) -> usize {
use serde_json::Value;
const NODE: usize = std::mem::size_of::<Value>();
match value {
Value::String(s) => NODE + s.len(),
Value::Array(items) => NODE + items.iter().map(decoded_size).sum::<usize>(),
Value::Object(map) => {
NODE + map
.iter()
.map(|(k, v)| NODE + k.len() + decoded_size(v))
.sum::<usize>()
}
_ => NODE,
}
}
pub(crate) struct Tally {
shards: Box<[Shard]>,
}
#[repr(align(128))]
#[derive(Default)]
struct Shard(AtomicU64);
impl Tally {
pub(crate) fn new(shards: usize) -> Self {
Self {
shards: (0..shards.max(1)).map(|_| Shard::default()).collect(),
}
}
#[inline]
pub(crate) fn shard(&self, i: usize) -> &AtomicU64 {
&self.shards.get(i).unwrap_or(&self.shards[0]).0
}
#[inline]
pub(crate) fn add(&self, i: usize, n: u64) {
self.shard(i).fetch_add(n, Ordering::Relaxed);
}
pub(crate) fn sum(&self) -> u64 {
self.shards
.iter()
.map(|shard| shard.0.load(Ordering::Relaxed))
.sum()
}
}
#[repr(align(128))]
#[derive(Default)]
pub(crate) struct Activity(AtomicU64);
impl std::ops::Deref for Activity {
type Target = AtomicU64;
fn deref(&self) -> &AtomicU64 {
&self.0
}
}
const MIN_LIFECYCLE_EVENTS: usize = 64;
impl InboundEvent {
#[cfg_attr(not(feature = "python"), allow(dead_code))]
fn is_lifecycle(&self) -> bool {
matches!(
self,
Self::Connect { .. } | Self::AuthConnect { .. } | Self::Disconnect { .. }
)
}
}
pub(crate) enum ServerCommand {
SendText {
conn_id: String,
data: String,
},
SendBytes {
conn_id: String,
data: Vec<u8>,
},
SendPrebuilt {
conn_id: String,
message: Message,
},
BroadcastText {
data: String,
},
BroadcastBytes {
data: Vec<u8>,
},
BroadcastLocal {
topic: String,
data: String,
skip_recovery: bool,
publish: bool,
},
Accept {
conn_id: String,
ready: String,
},
Disconnect {
conn_id: String,
},
Refuse {
conn_id: String,
message: String,
code: u16,
},
Shutdown,
Drain {
close_code: u16,
close_reason: String,
},
AbortAll,
}
pub(crate) struct SharedStateConfig {
pub max_connections: usize,
pub jwt_config: Option<JwtConfig>,
pub max_inbound_queue_size: usize,
pub max_inbound_queue_bytes: usize,
pub allowed_origins: Option<AHashSet<String>>,
pub client_subscriptions: bool,
pub e2e_identity: Option<crate::crypto::IdentityKey>,
pub e2e_legacy: bool,
pub recovery: Option<Arc<crate::recovery::RecoveryManager>>,
pub presence_enabled: bool,
pub presence_max_data_size: usize,
pub presence_max_members: usize,
pub rate_capacity: f64,
pub rate_refill: f64,
pub max_message_size: usize,
pub ping_interval_secs: u64,
pub idle_timeout_secs: u64,
pub max_outbound_queue_bytes: usize,
pub max_subscriptions_per_connection: usize,
pub signer: Option<ServerSigner>,
pub signature_window_secs: u64,
pub bind_sessions: bool,
}
pub(crate) struct SharedState {
pub(crate) connections: Arc<parking_lot::RwLock<HashMap<String, ConnectionHandle>>>,
pub(super) max_connections: usize,
pub(super) allowed_origins: Option<AHashSet<String>>,
pub(super) client_subscriptions: bool,
pub(super) e2e_identity: Option<crate::crypto::IdentityKey>,
pub(super) e2e_legacy: bool,
pub(super) conn_sealed: DashSet<String>,
pub(crate) signer: Option<ServerSigner>,
pub(super) signature_window_ms: u64,
pub(super) bind_sessions: bool,
pub(super) client_keys: DashMap<String, Arc<ClientKey>>,
pub(super) binds: DashMap<String, Bind>,
pub(crate) user_conns: DashMap<String, AHashSet<String>>,
pub(crate) conn_user: DashMap<String, String>,
pub(super) signed_seen: DashMap<String, u64>,
#[cfg_attr(not(feature = "python"), allow(dead_code))]
pub(super) drain_mode: AtomicBool,
#[cfg_attr(not(feature = "python"), allow(dead_code))]
inbound_tx: CBSender<Queued>,
pub(super) inbound_rx: CBReceiver<Queued>,
#[cfg_attr(not(feature = "python"), allow(dead_code))]
inbound_room: InboundRoom,
#[cfg_attr(not(feature = "python"), allow(dead_code))]
max_inbound_messages: usize,
#[cfg_attr(not(feature = "python"), allow(dead_code))]
max_inbound_bytes: usize,
#[cfg_attr(not(feature = "python"), allow(dead_code))]
inbound_lifecycle: AtomicUsize,
#[cfg_attr(not(feature = "python"), allow(dead_code))]
max_lifecycle_events: usize,
pub(super) inbound_dropped: AtomicU64,
pub(super) conn_formats: DashMap<String, bool>,
pub(super) conn_compression: DashMap<String, usize>,
pub(super) connection_count: AtomicUsize,
pub(super) jwt_config: Option<arc_swap::ArcSwap<JwtConfig>>,
pub(super) draining: AtomicBool,
pub(crate) topics: TopicIndex,
pub(crate) fanout: std::sync::RwLock<Option<Fanout>>,
pub(super) conn_topics: DashMap<String, DashSet<String>>,
pub(super) cluster_cmd_tx: arc_swap::ArcSwapOption<mpsc::UnboundedSender<ClusterCommand>>,
pub(crate) cluster_metrics: Arc<ClusterMetrics>,
pub(super) cluster_dlq: Arc<std::sync::Mutex<ClusterDlq>>,
pub(crate) cluster_instance_id: std::sync::Mutex<Option<String>>,
pub(crate) local_topic_refcount: Arc<std::sync::Mutex<HashMap<String, usize>>>,
interest_linger: std::sync::Mutex<HashMap<String, std::time::Instant>>,
pub(super) cluster_main_port: std::sync::RwLock<
Option<mpsc::UnboundedSender<(tokio::net::TcpStream, std::net::SocketAddr)>>,
>,
pub(crate) cluster_tls_holder:
std::sync::RwLock<Option<Arc<arc_swap::ArcSwap<crate::cluster::ClusterTlsConfig>>>>,
pub(crate) recovery: Option<Arc<crate::recovery::RecoveryManager>>,
pub(super) conn_last_activity: DashMap<String, Arc<Activity>>,
pub(super) clock_base: std::time::Instant,
pub(super) conn_encryption: DashMap<String, Arc<crate::crypto::Aes256Gcm>>,
pub(crate) presence: Option<Arc<crate::presence::PresenceManager>>,
pub(crate) queue_groups: Arc<DashMap<String, DashMap<String, QueueGroup>>>,
pub(super) conn_queue_groups: DashMap<String, Vec<(String, String)>>,
pub(crate) commands: arc_swap::ArcSwapOption<Commands>,
pub(crate) presence_events: arc_swap::ArcSwapOption<PresenceEvents>,
pub(super) rate_capacity: f64,
pub(super) rate_refill: f64,
pub(super) max_message_size: usize,
pub(super) ping_interval_secs: u64,
pub(super) idle_timeout_secs: u64,
pub(crate) max_outbound_queue_bytes: usize,
pub(super) max_subscriptions_per_connection: usize,
pub(crate) slow_consumer_drops: Arc<Tally>,
pub(super) messages_received_total: Tally,
pub(super) messages_sent_total: AtomicU64,
pub(super) commands_queued: Arc<AtomicUsize>,
pub(super) connections_accepted_total: AtomicU64,
pub(super) connections_rejected_total: AtomicU64,
pub(super) bytes_received_total: Tally,
pub(super) bytes_sent_total: AtomicU64,
pub(super) auth_failures_total: AtomicU64,
pub(super) rate_limited_total: AtomicU64,
pub(crate) started_at: parking_lot::Mutex<Option<std::time::Instant>>,
pub(crate) dedup: std::sync::Mutex<DeduplicationState>,
pub(crate) topic_message_counts: Arc<DashMap<String, AtomicU64>>,
}
impl SharedState {
pub(crate) fn register_user(&self, conn_id: &str, user_id: &str) {
if let Some(previous) = self
.conn_user
.insert(conn_id.to_owned(), user_id.to_owned())
{
self.forget_user_conn(&previous, conn_id);
}
self.user_conns
.entry(user_id.to_owned())
.or_default()
.insert(conn_id.to_owned());
}
pub(crate) fn unregister_user(&self, conn_id: &str) {
if let Some((_, user_id)) = self.conn_user.remove(conn_id) {
self.forget_user_conn(&user_id, conn_id);
}
}
fn forget_user_conn(&self, user_id: &str, conn_id: &str) {
if let Some(mut conns) = self.user_conns.get_mut(user_id) {
conns.remove(conn_id);
}
self.user_conns
.remove_if(user_id, |_, conns| conns.is_empty());
}
pub(super) fn activity_ms(&self, at: std::time::Instant) -> u64 {
at.saturating_duration_since(self.clock_base).as_millis() as u64
}
pub(crate) fn add_local_interest<'a>(&self, topics: impl IntoIterator<Item = &'a String>) {
let cluster_tx = self.cluster_cmd_tx.load_full();
let mut refcounts = self
.local_topic_refcount
.lock()
.unwrap_or_else(|e| e.into_inner());
for topic in topics {
let count = refcounts.entry(topic.clone()).or_insert(0);
*count += 1;
if *count != 1 {
continue;
}
let lingered = self
.interest_linger
.lock()
.unwrap_or_else(|e| e.into_inner())
.remove(topic)
.is_some();
if lingered {
continue;
}
if let Some(recovery) = &self.recovery {
recovery.interest_began(topic);
}
if let Some(tx) = &cluster_tx {
let _ = tx.send(ClusterCommand::Sub {
topic: topic.clone(),
});
}
}
}
pub(crate) fn remove_local_interest<'a>(&self, topics: impl IntoIterator<Item = &'a String>) {
let cluster_tx = self.cluster_cmd_tx.load_full();
let mut refcounts = self
.local_topic_refcount
.lock()
.unwrap_or_else(|e| e.into_inner());
for topic in topics {
let Some(count) = refcounts.get_mut(topic) else {
continue;
};
*count = count.saturating_sub(1);
if *count != 0 {
continue;
}
if let Some(recovery) = &self.recovery {
self.interest_linger
.lock()
.unwrap_or_else(|e| e.into_inner())
.insert(
topic.clone(),
std::time::Instant::now() + recovery.interest_linger(),
);
continue;
}
refcounts.remove(topic);
if let Some(tx) = &cluster_tx {
let _ = tx.send(ClusterCommand::Unsub {
topic: topic.clone(),
});
}
}
}
pub(crate) fn sweep_interest_linger(&self) {
let now = std::time::Instant::now();
let cluster_tx = self.cluster_cmd_tx.load_full();
let mut refcounts = self
.local_topic_refcount
.lock()
.unwrap_or_else(|e| e.into_inner());
let mut linger = self
.interest_linger
.lock()
.unwrap_or_else(|e| e.into_inner());
linger.retain(|topic, until| {
if *until > now {
return true;
}
if refcounts.get(topic) == Some(&0) {
refcounts.remove(topic);
if let Some(recovery) = &self.recovery {
recovery.interest_ended(topic);
}
if let Some(tx) = &cluster_tx {
let _ = tx.send(ClusterCommand::Unsub {
topic: topic.clone(),
});
}
}
false
});
}
pub(super) fn new(cfg: SharedStateConfig) -> Self {
let (tx, rx) = unbounded();
Self {
connections: Arc::new(parking_lot::RwLock::new(HashMap::new())),
max_connections: cfg.max_connections,
allowed_origins: cfg.allowed_origins,
client_subscriptions: cfg.client_subscriptions,
e2e_identity: cfg.e2e_identity,
e2e_legacy: cfg.e2e_legacy,
conn_sealed: DashSet::new(),
signer: cfg.signer,
signature_window_ms: cfg.signature_window_secs.saturating_mul(1000),
bind_sessions: cfg.bind_sessions,
client_keys: DashMap::new(),
binds: DashMap::new(),
user_conns: DashMap::new(),
conn_user: DashMap::new(),
signed_seen: DashMap::new(),
drain_mode: AtomicBool::new(false),
inbound_tx: tx,
inbound_rx: rx,
inbound_room: InboundRoom::default(),
max_inbound_messages: cfg.max_inbound_queue_size,
max_inbound_bytes: cfg.max_inbound_queue_bytes,
inbound_lifecycle: AtomicUsize::new(0),
max_lifecycle_events: cfg
.max_connections
.saturating_mul(2)
.max(MIN_LIFECYCLE_EVENTS),
inbound_dropped: AtomicU64::new(0),
conn_formats: DashMap::new(),
conn_compression: DashMap::new(),
connection_count: AtomicUsize::new(0),
jwt_config: cfg.jwt_config.map(arc_swap::ArcSwap::from_pointee),
draining: AtomicBool::new(false),
topics: TopicIndex::new(super::fanout::lane_count()),
fanout: std::sync::RwLock::new(None),
conn_topics: DashMap::new(),
cluster_cmd_tx: arc_swap::ArcSwapOption::empty(),
cluster_metrics: Arc::new(ClusterMetrics::new()),
cluster_dlq: Arc::new(std::sync::Mutex::new(ClusterDlq::new(1000))),
cluster_instance_id: std::sync::Mutex::new(None),
local_topic_refcount: Arc::new(std::sync::Mutex::new(HashMap::new())),
interest_linger: std::sync::Mutex::new(HashMap::new()),
cluster_main_port: std::sync::RwLock::new(None),
cluster_tls_holder: std::sync::RwLock::new(None),
recovery: cfg.recovery,
conn_last_activity: DashMap::new(),
clock_base: std::time::Instant::now(),
conn_encryption: DashMap::new(),
presence: if cfg.presence_enabled {
Some(Arc::new(crate::presence::PresenceManager::new(
cfg.presence_max_data_size,
cfg.presence_max_members,
)))
} else {
None
},
queue_groups: Arc::new(DashMap::new()),
conn_queue_groups: DashMap::new(),
commands: arc_swap::ArcSwapOption::empty(),
presence_events: arc_swap::ArcSwapOption::empty(),
rate_capacity: cfg.rate_capacity,
rate_refill: cfg.rate_refill,
max_message_size: cfg.max_message_size,
ping_interval_secs: cfg.ping_interval_secs,
idle_timeout_secs: cfg.idle_timeout_secs,
max_outbound_queue_bytes: cfg.max_outbound_queue_bytes,
max_subscriptions_per_connection: cfg.max_subscriptions_per_connection,
slow_consumer_drops: Arc::new(Tally::new(super::fanout::lane_count())),
messages_received_total: Tally::new(super::fanout::lane_count()),
messages_sent_total: AtomicU64::new(0),
commands_queued: Arc::default(),
connections_accepted_total: AtomicU64::new(0),
connections_rejected_total: AtomicU64::new(0),
bytes_received_total: Tally::new(super::fanout::lane_count()),
bytes_sent_total: AtomicU64::new(0),
auth_failures_total: AtomicU64::new(0),
rate_limited_total: AtomicU64::new(0),
started_at: parking_lot::Mutex::new(None),
dedup: std::sync::Mutex::new(DeduplicationState {
seen: AHashSet::with_capacity(50_000),
queue: std::collections::VecDeque::with_capacity(50_000),
max_entries: 50_000,
}),
topic_message_counts: Arc::new(DashMap::new()),
}
}
#[cfg_attr(not(feature = "python"), allow(dead_code))]
pub(super) fn push_inbound(&self, event: InboundEvent) {
if event.is_lifecycle() {
self.inbound_lifecycle.fetch_add(1, Ordering::AcqRel);
}
let _ = self.inbound_tx.send(Queued { event, bytes: None });
}
#[cfg_attr(not(feature = "python"), allow(dead_code))]
pub(super) fn push_connect(&self, event: InboundEvent) -> bool {
if self.inbound_lifecycle.load(Ordering::Acquire) >= self.max_lifecycle_events {
return false;
}
self.push_inbound(event);
true
}
#[cfg_attr(not(feature = "python"), allow(dead_code))]
fn reserve_message(&self, bytes: usize) -> bool {
let queued = self.inbound_room.messages.fetch_add(1, Ordering::AcqRel);
let held = self.inbound_room.bytes.fetch_add(bytes, Ordering::AcqRel);
if queued >= self.max_inbound_messages
|| (held > 0 && held.saturating_add(bytes) > self.max_inbound_bytes)
{
self.release_message(bytes);
self.inbound_dropped.fetch_add(1, Ordering::Relaxed);
return false;
}
true
}
#[cfg_attr(not(feature = "python"), allow(dead_code))]
fn release_message(&self, bytes: usize) {
self.inbound_room.messages.fetch_sub(1, Ordering::AcqRel);
self.inbound_room.bytes.fetch_sub(bytes, Ordering::AcqRel);
}
#[cfg_attr(not(feature = "python"), allow(dead_code))]
pub(super) fn push_message(&self, event: InboundEvent, bytes: usize) -> bool {
let bytes = match &event {
InboundEvent::Message { value, .. } | InboundEvent::Signed { value, .. } => {
inbound_cost(bytes, value)
}
_ => bytes,
};
if !self.reserve_message(bytes) {
return false;
}
let queued = Queued {
event,
bytes: Some(bytes),
};
match self.inbound_tx.send(queued) {
Ok(()) => true,
Err(_) => {
self.release_message(bytes);
false
}
}
}
#[cfg_attr(not(feature = "python"), allow(dead_code))]
pub(super) fn give_back(&self, taken: Taken) {
if taken.messages > 0 {
self.inbound_room
.messages
.fetch_sub(taken.messages, Ordering::AcqRel);
self.inbound_room
.bytes
.fetch_sub(taken.bytes, Ordering::AcqRel);
}
if taken.lifecycle > 0 {
self.inbound_lifecycle
.fetch_sub(taken.lifecycle, Ordering::AcqRel);
}
}
#[cfg_attr(not(feature = "python"), allow(dead_code))]
pub(super) fn callback_room(state: &Arc<Self>, bytes: usize) -> Option<CallbackRoom> {
state.reserve_message(bytes).then(|| CallbackRoom {
state: Arc::clone(state),
bytes,
})
}
}