From 426f006c29430e65c2cc75485e29f067b75e8d71 Mon Sep 17 00:00:00 2001 From: Totodore Date: Fri, 7 Jun 2024 00:40:23 +0200 Subject: [PATCH] feat(socket): remove config form socket --- socketioxide/src/ack.rs | 3 ++- socketioxide/src/client.rs | 13 ++++--------- socketioxide/src/io.rs | 8 ++++---- socketioxide/src/layer.rs | 4 ++-- socketioxide/src/ns.rs | 4 +--- socketioxide/src/operators.rs | 4 +++- socketioxide/src/service.rs | 2 +- socketioxide/src/socket.rs | 14 +++----------- 8 files changed, 20 insertions(+), 32 deletions(-) diff --git a/socketioxide/src/ack.rs b/socketioxide/src/ack.rs index 699f8f74..75588037 100644 --- a/socketioxide/src/ack.rs +++ b/socketioxide/src/ack.rs @@ -156,7 +156,8 @@ impl AckInnerStream { return AckInnerStream::Stream { rxs }; } - let duration = duration.unwrap_or_else(|| sockets.first().unwrap().config.ack_timeout); + let duration = + duration.unwrap_or_else(|| sockets.first().unwrap().get_io().config().ack_timeout); for socket in sockets { let rx = socket.send_with_ack(packet.clone()); rxs.push(AckResultWithId { diff --git a/socketioxide/src/client.rs b/socketioxide/src/client.rs index 4a3e99d2..97b82235 100644 --- a/socketioxide/src/client.rs +++ b/socketioxide/src/client.rs @@ -23,7 +23,7 @@ use crate::{ use crate::{ProtocolVersion, SocketIo}; pub struct Client { - pub(crate) config: Arc, + pub(crate) config: SocketIoConfig, ns: RwLock, Arc>>>, #[cfg(feature = "state")] pub(crate) state: state::TypeMap![Send + Sync], @@ -31,7 +31,7 @@ pub struct Client { impl Client { pub fn new( - config: Arc, + config: SocketIoConfig, #[cfg(feature = "state")] mut state: state::TypeMap![Send + Sync], ) -> Self { #[cfg(feature = "state")] @@ -57,13 +57,8 @@ impl Client { if let Some(ns) = self.get_ns(ns_path) { let esocket = esocket.clone(); - let config = self.config.clone(); tokio::spawn(async move { - if ns - .connect(esocket.id, esocket.clone(), auth, config) - .await - .is_ok() - { + if ns.connect(esocket.id, esocket.clone(), auth).await.is_ok() { // cancel the connect timeout task for v5 if let Some(tx) = esocket.data.connect_recv_tx.lock().unwrap().take() { tx.send(()).ok(); @@ -398,7 +393,7 @@ mod test { ..Default::default() }; let client = Client::::new( - std::sync::Arc::new(config), + config, #[cfg(feature = "state")] Default::default(), ); diff --git a/socketioxide/src/io.rs b/socketioxide/src/io.rs index 053d81c2..d85956b7 100644 --- a/socketioxide/src/io.rs +++ b/socketioxide/src/io.rs @@ -186,7 +186,7 @@ impl SocketIoBuilder { self.config.engine_config = self.engine_config_builder.build(); let (layer, client) = SocketIoLayer::from_config( - Arc::new(self.config), + self.config, #[cfg(feature = "state")] self.state, ); @@ -202,7 +202,7 @@ impl SocketIoBuilder { let (svc, client) = SocketIoService::with_config_inner( NotFoundService, - Arc::new(self.config), + self.config, #[cfg(feature = "state")] self.state, ); @@ -217,7 +217,7 @@ impl SocketIoBuilder { let (svc, client) = SocketIoService::with_config_inner( svc, - Arc::new(self.config), + self.config, #[cfg(feature = "state")] self.state, ); @@ -897,7 +897,7 @@ mod tests { let config = SocketIoConfig::default().into(); io.0.get_ns("/") .unwrap() - .connect(sid, socket, None, config) + .connect(sid, socket, None) .await .ok(); diff --git a/socketioxide/src/layer.rs b/socketioxide/src/layer.rs index d463e73c..ac7e1371 100644 --- a/socketioxide/src/layer.rs +++ b/socketioxide/src/layer.rs @@ -42,11 +42,11 @@ impl Clone for SocketIoLayer { impl SocketIoLayer { pub(crate) fn from_config( - config: Arc, + config: SocketIoConfig, #[cfg(feature = "state")] state: state::TypeMap![Send + Sync], ) -> (Self, Arc>) { let client = Arc::new(Client::new( - config.clone(), + config, #[cfg(feature = "state")] state, )); diff --git a/socketioxide/src/ns.rs b/socketioxide/src/ns.rs index 7a604167..5862e3d8 100644 --- a/socketioxide/src/ns.rs +++ b/socketioxide/src/ns.rs @@ -10,7 +10,6 @@ use crate::{ handler::{BoxedConnectHandler, ConnectHandler, MakeErasedHandler}, packet::{Packet, PacketData}, socket::{DisconnectReason, Socket}, - SocketIoConfig, }; use crate::{client::SocketData, errors::AdapterError}; use engineioxide::sid::Sid; @@ -47,9 +46,8 @@ impl Namespace { sid: Sid, esocket: Arc>>, auth: Option, - config: Arc, ) -> Result<(), ConnectFail> { - let socket: Arc> = Socket::new(sid, self.clone(), esocket.clone(), config).into(); + let socket: Arc> = Socket::new(sid, self.clone(), esocket.clone()).into(); if let Err(e) = self.handler.call_middleware(socket.clone(), &auth).await { #[cfg(feature = "tracing")] diff --git a/socketioxide/src/operators.rs b/socketioxide/src/operators.rs index 46a9754f..22a4aeb2 100644 --- a/socketioxide/src/operators.rs +++ b/socketioxide/src/operators.rs @@ -433,7 +433,9 @@ impl ConfOperators<'_, A> { return Err(e.with_value(data).into()); } }; - let timeout = self.timeout.unwrap_or(self.socket.config.ack_timeout); + let timeout = self + .timeout + .unwrap_or_else(|| self.socket.get_io().config().ack_timeout); let packet = self.get_packet(event, data)?; let rx = self.socket.send_with_ack_permit(packet, permit); let stream = AckInnerStream::send(rx, timeout, self.socket.id); diff --git a/socketioxide/src/service.rs b/socketioxide/src/service.rs index fd6ea517..6ce0ef33 100644 --- a/socketioxide/src/service.rs +++ b/socketioxide/src/service.rs @@ -119,7 +119,7 @@ impl SocketIoService { /// Creates a new [`EngineIoService`] with a custom inner service and a custom config. pub(crate) fn with_config_inner( inner: S, - config: Arc, + config: SocketIoConfig, #[cfg(feature = "state")] state: state::TypeMap![Send + Sync], ) -> (Self, Arc>) { let engine_config = config.engine_config.clone(); diff --git a/socketioxide/src/socket.rs b/socketioxide/src/socket.rs index 9732136c..9e869593 100644 --- a/socketioxide/src/socket.rs +++ b/socketioxide/src/socket.rs @@ -32,7 +32,7 @@ use crate::{ ns::Namespace, operators::{BroadcastOperators, ConfOperators, RoomParam}, packet::{BinaryPacket, Packet, PacketData}, - AckError, SocketIo, SocketIoConfig, + AckError, SocketIo, }; use crate::{ client::SocketData, @@ -128,7 +128,6 @@ impl<'a> PermitExt<'a> for Permit<'a> { /// It is used to send and receive messages from the client, join and leave rooms, etc. /// The socket struct itself should not be used directly, but through a [`SocketRef`](crate::extract::SocketRef). pub struct Socket { - pub(crate) config: Arc, pub(crate) ns: Arc>, message_handlers: RwLock, BoxedMessageHandler>>, disconnect_handler: Mutex>>, @@ -154,7 +153,6 @@ impl Socket { sid: Sid, ns: Arc>, esocket: Arc>>, - config: Arc, ) -> Self { Self { ns, @@ -166,7 +164,6 @@ impl Socket { id: sid, #[cfg(feature = "extensions")] extensions: Extensions::new(), - config, esocket, } } @@ -398,7 +395,7 @@ impl Socket { let data = serde_json::to_value(data)?; let packet = Packet::event(self.ns(), event.into(), data); let rx = self.send_with_ack_permit(packet, permit); - let stream = AckInnerStream::send(rx, self.config.ack_timeout, self.id); + let stream = AckInnerStream::send(rx, self.get_io().config().ack_timeout, self.id); Ok(AckStream::::from(stream)) } @@ -828,12 +825,7 @@ impl Socket { /// Creates a dummy socket for testing purposes pub fn new_dummy(sid: Sid, ns: Arc>) -> Socket { let close_fn = Box::new(move |_, _| ()); - let s = Socket::new( - sid, - ns, - engineioxide::Socket::new_dummy(sid, close_fn), - Arc::new(SocketIoConfig::default()), - ); + let s = Socket::new(sid, ns, engineioxide::Socket::new_dummy(sid, close_fn)); s.set_connected(true); s }