refactor(tests): reformat code and update dependency management
- Reorganized import statements in adapter tests for better readability - Replaced or_insert_with(Vec::new) with or_default() in test closures - Updated Cargo.lock with new dependency versions and checksums - Added TLS features to tonic dependency configuration - Included sqlx, chrono, and uuid dependencies with specific features - Added jsonwebtoken and arc-swap as project dependencies - Reformatted assertion statements to comply with line length limits - Adjusted base64 import order in engine codec module - Updated protobuf include statement formatting
This commit is contained in:
+90
-32
@@ -5,7 +5,9 @@ use async_trait::async_trait;
|
||||
use dashmap::DashMap;
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
use crate::socket::adapter::{Adapter, AdapterError, BroadcastOptions, BusMessage, SocketInfo};
|
||||
use crate::socket::adapter::{
|
||||
Adapter, AdapterError, BroadcastOptions, BusMessage, LocalBroadcastFn, SocketInfo,
|
||||
};
|
||||
use crate::socket::message_bus::MessageBus;
|
||||
use crate::socket::packet::Packet;
|
||||
use crate::socket::parser;
|
||||
@@ -15,11 +17,16 @@ use crate::socket::socket::Socket;
|
||||
/// Only performs local dispatch — no remote state writes needed.
|
||||
async fn handle_bus_message(
|
||||
msg: BusMessage,
|
||||
on_local_broadcast: &Arc<dyn Fn(&Packet, &BroadcastOptions) + Send + Sync + 'static>,
|
||||
on_local_broadcast: &LocalBroadcastFn,
|
||||
server_id: &str,
|
||||
) {
|
||||
match msg {
|
||||
BusMessage::Broadcast { namespace: _, packet, opts, server_id: sender_id } => {
|
||||
BusMessage::Broadcast {
|
||||
namespace: _,
|
||||
packet,
|
||||
opts,
|
||||
server_id: sender_id,
|
||||
} => {
|
||||
if sender_id == server_id {
|
||||
return;
|
||||
}
|
||||
@@ -29,13 +36,18 @@ async fn handle_bus_message(
|
||||
}
|
||||
// NATS adapter manages room state locally; cross-server join/leave/disconnect
|
||||
// are informational only and don't require duplicate state writes.
|
||||
BusMessage::SocketJoin { server_id: sender_id, .. }
|
||||
| BusMessage::SocketLeave { server_id: sender_id, .. }
|
||||
| BusMessage::SocketDisconnect { server_id: sender_id, .. } => {
|
||||
if sender_id == server_id {
|
||||
return;
|
||||
}
|
||||
BusMessage::SocketJoin {
|
||||
server_id: sender_id,
|
||||
..
|
||||
}
|
||||
| BusMessage::SocketLeave {
|
||||
server_id: sender_id,
|
||||
..
|
||||
}
|
||||
| BusMessage::SocketDisconnect {
|
||||
server_id: sender_id,
|
||||
..
|
||||
} => if sender_id == server_id {},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,7 +63,7 @@ pub struct NatsAdapter {
|
||||
sockets: DashMap<String, Arc<Socket>>,
|
||||
server_id: String,
|
||||
namespace: String,
|
||||
on_local_broadcast: Arc<dyn Fn(&Packet, &BroadcastOptions) + Send + Sync + 'static>,
|
||||
on_local_broadcast: LocalBroadcastFn,
|
||||
}
|
||||
|
||||
impl NatsAdapter {
|
||||
@@ -59,7 +71,7 @@ impl NatsAdapter {
|
||||
message_bus: Arc<dyn MessageBus>,
|
||||
server_id: String,
|
||||
namespace: String,
|
||||
on_local_broadcast: Arc<dyn Fn(&Packet, &BroadcastOptions) + Send + Sync + 'static>,
|
||||
on_local_broadcast: LocalBroadcastFn,
|
||||
) -> Self {
|
||||
Self {
|
||||
message_bus,
|
||||
@@ -133,7 +145,11 @@ impl NatsAdapter {
|
||||
|
||||
#[async_trait]
|
||||
impl Adapter for NatsAdapter {
|
||||
async fn broadcast(&self, packet: &Packet, opts: &BroadcastOptions) -> Result<(), AdapterError> {
|
||||
async fn broadcast(
|
||||
&self,
|
||||
packet: &Packet,
|
||||
opts: &BroadcastOptions,
|
||||
) -> Result<(), AdapterError> {
|
||||
if opts.flags.local_only {
|
||||
(self.on_local_broadcast)(packet, opts);
|
||||
return Ok(());
|
||||
@@ -146,8 +162,8 @@ impl Adapter for NatsAdapter {
|
||||
server_id: self.server_id.clone(),
|
||||
};
|
||||
|
||||
let payload = serde_json::to_vec(&msg)
|
||||
.map_err(|e| AdapterError::Serialization(e.to_string()))?;
|
||||
let payload =
|
||||
serde_json::to_vec(&msg).map_err(|e| AdapterError::Serialization(e.to_string()))?;
|
||||
|
||||
self.message_bus
|
||||
.publish(&format!("socket.io:{}:broadcast", self.namespace), &payload)
|
||||
@@ -158,20 +174,30 @@ impl Adapter for NatsAdapter {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn register(&self, socket_sid: &str, engine_sid: &str, _ns: &str) -> Result<(), AdapterError> {
|
||||
self.socket_sids.insert(socket_sid.to_string(), engine_sid.to_string());
|
||||
async fn register(
|
||||
&self,
|
||||
socket_sid: &str,
|
||||
engine_sid: &str,
|
||||
_ns: &str,
|
||||
) -> Result<(), AdapterError> {
|
||||
self.socket_sids
|
||||
.insert(socket_sid.to_string(), engine_sid.to_string());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn add(&self, sid: &str, room: &str, _ns: &str) -> Result<(), AdapterError> {
|
||||
self.socket_rooms
|
||||
.entry(sid.to_string())
|
||||
.and_modify(|set| { set.insert(room.to_string()); })
|
||||
.and_modify(|set| {
|
||||
set.insert(room.to_string());
|
||||
})
|
||||
.or_insert_with(|| HashSet::from([room.to_string()]));
|
||||
|
||||
self.rooms
|
||||
.entry(room.to_string())
|
||||
.and_modify(|set| { set.insert(sid.to_string()); })
|
||||
.and_modify(|set| {
|
||||
set.insert(sid.to_string());
|
||||
})
|
||||
.or_insert_with(|| HashSet::from([sid.to_string()]));
|
||||
|
||||
let msg = BusMessage::SocketJoin {
|
||||
@@ -181,8 +207,8 @@ impl Adapter for NatsAdapter {
|
||||
server_id: self.server_id.clone(),
|
||||
};
|
||||
|
||||
let payload = serde_json::to_vec(&msg)
|
||||
.map_err(|e| AdapterError::Serialization(e.to_string()))?;
|
||||
let payload =
|
||||
serde_json::to_vec(&msg).map_err(|e| AdapterError::Serialization(e.to_string()))?;
|
||||
|
||||
self.message_bus
|
||||
.publish(&format!("socket.io:{}:join", self.namespace), &payload)
|
||||
@@ -196,14 +222,24 @@ impl Adapter for NatsAdapter {
|
||||
if let Some(mut entry) = self.socket_rooms.get_mut(sid) {
|
||||
entry.value_mut().remove(room);
|
||||
}
|
||||
if self.socket_rooms.get(sid).map(|e| e.value().is_empty()).unwrap_or(true) {
|
||||
if self
|
||||
.socket_rooms
|
||||
.get(sid)
|
||||
.map(|e| e.value().is_empty())
|
||||
.unwrap_or(true)
|
||||
{
|
||||
self.socket_rooms.remove(sid);
|
||||
}
|
||||
|
||||
if let Some(mut entry) = self.rooms.get_mut(room) {
|
||||
entry.value_mut().remove(sid);
|
||||
}
|
||||
if self.rooms.get(room).map(|e| e.value().is_empty()).unwrap_or(true) {
|
||||
if self
|
||||
.rooms
|
||||
.get(room)
|
||||
.map(|e| e.value().is_empty())
|
||||
.unwrap_or(true)
|
||||
{
|
||||
self.rooms.remove(room);
|
||||
}
|
||||
|
||||
@@ -214,8 +250,8 @@ impl Adapter for NatsAdapter {
|
||||
server_id: self.server_id.clone(),
|
||||
};
|
||||
|
||||
let payload = serde_json::to_vec(&msg)
|
||||
.map_err(|e| AdapterError::Serialization(e.to_string()))?;
|
||||
let payload =
|
||||
serde_json::to_vec(&msg).map_err(|e| AdapterError::Serialization(e.to_string()))?;
|
||||
|
||||
self.message_bus
|
||||
.publish(&format!("socket.io:{}:leave", self.namespace), &payload)
|
||||
@@ -231,7 +267,12 @@ impl Adapter for NatsAdapter {
|
||||
if let Some(mut entry) = self.rooms.get_mut(room) {
|
||||
entry.value_mut().remove(sid);
|
||||
}
|
||||
if self.rooms.get(room).map(|e| e.value().is_empty()).unwrap_or(true) {
|
||||
if self
|
||||
.rooms
|
||||
.get(room)
|
||||
.map(|e| e.value().is_empty())
|
||||
.unwrap_or(true)
|
||||
{
|
||||
self.rooms.remove(room);
|
||||
}
|
||||
}
|
||||
@@ -246,18 +287,24 @@ impl Adapter for NatsAdapter {
|
||||
server_id: self.server_id.clone(),
|
||||
};
|
||||
|
||||
let payload = serde_json::to_vec(&msg)
|
||||
.map_err(|e| AdapterError::Serialization(e.to_string()))?;
|
||||
let payload =
|
||||
serde_json::to_vec(&msg).map_err(|e| AdapterError::Serialization(e.to_string()))?;
|
||||
|
||||
self.message_bus
|
||||
.publish(&format!("socket.io:{}:disconnect", self.namespace), &payload)
|
||||
.publish(
|
||||
&format!("socket.io:{}:disconnect", self.namespace),
|
||||
&payload,
|
||||
)
|
||||
.await
|
||||
.map_err(|e| AdapterError::MessageBus(e.to_string()))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn fetch_sockets(&self, opts: &BroadcastOptions) -> Result<Vec<SocketInfo>, AdapterError> {
|
||||
async fn fetch_sockets(
|
||||
&self,
|
||||
opts: &BroadcastOptions,
|
||||
) -> Result<Vec<SocketInfo>, AdapterError> {
|
||||
let mut result = Vec::new();
|
||||
|
||||
let target_sids: HashSet<String> = if opts.rooms.is_empty() {
|
||||
@@ -276,7 +323,11 @@ impl Adapter for NatsAdapter {
|
||||
if opts.except.contains(&sid) {
|
||||
continue;
|
||||
}
|
||||
let rooms = self.socket_rooms.get(&sid).map(|e| e.value().clone()).unwrap_or_default();
|
||||
let rooms = self
|
||||
.socket_rooms
|
||||
.get(&sid)
|
||||
.map(|e| e.value().clone())
|
||||
.unwrap_or_default();
|
||||
result.push(SocketInfo {
|
||||
sid: sid.clone(),
|
||||
namespace: self.namespace.clone(),
|
||||
@@ -288,7 +339,11 @@ impl Adapter for NatsAdapter {
|
||||
}
|
||||
|
||||
async fn socket_rooms(&self, sid: &str) -> Result<HashSet<String>, AdapterError> {
|
||||
Ok(self.socket_rooms.get(sid).map(|e| e.value().clone()).unwrap_or_default())
|
||||
Ok(self
|
||||
.socket_rooms
|
||||
.get(sid)
|
||||
.map(|e| e.value().clone())
|
||||
.unwrap_or_default())
|
||||
}
|
||||
|
||||
fn server_id(&self) -> &str {
|
||||
@@ -296,7 +351,10 @@ impl Adapter for NatsAdapter {
|
||||
}
|
||||
|
||||
async fn close(&self) -> Result<(), AdapterError> {
|
||||
self.message_bus.close().await.map_err(|e| AdapterError::MessageBus(e.to_string()))?;
|
||||
self.message_bus
|
||||
.close()
|
||||
.await
|
||||
.map_err(|e| AdapterError::MessageBus(e.to_string()))?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user