Skip to content

feat(core): async adapter - #419

Merged
Totodore merged 15 commits into
mainfrom
feat-adapter-rework
Jan 12, 2025
Merged

feat(core): async adapter#419
Totodore merged 15 commits into
mainfrom
feat-adapter-rework

Conversation

@Totodore

@TotodoreTotodore commented Jan 1, 2025

Copy link
Copy Markdown
Owner

Full rework of the async adapter.
The majority of the adapter code is now in the socketioxide-core crate. It contains a LocalAdapter with the following type def:

pubtraitCoreAdapter<E:SocketEmitter>:Sized + Send + Sync + 'static{/// An error that can occur when using the adapter.typeError:StdError + Into<AdapterError> + Send + 'static;/// A shared state between all the namespace [`CoreAdapter`]./// This can be used to share a connection for example.typeState:Send + Sync + 'static;/// A stream that emits the acknowledgments of multiple sockets.typeAckStream:Stream<Item = AckStreamItem<E::AckError>> + FusedStream + Send + 'static;/// Creates a new adapter with the given state and local adapter.////// The state is used to share a common state between all your adapters. E.G. a connection to a remote system./// The local adapter is used to manipulate the local sockets.fnnew(state:&Self::State,local:CoreLocalAdapter<E>) -> Self;/// Initializes the adapter.fninit(self:Arc<Self>) -> implFuture<Output = Result<(),Self::Error>> + Send;/// Closes the adapter.fnclose(&self) -> implFuture<Output = Result<(),Self::Error>> + Send;/// Returns the number of servers.fnserver_count(&self) -> implFuture<Output = Result<u16,Self::Error>> + Send;/// Broadcasts the packet to the sockets that match the [`BroadcastOptions`].fnbroadcast(&self,packet:Packet,opts:BroadcastOptions) -> implFuture<Output = Result<(),BroadcastError>> + Send;/// Broadcasts the packet to the sockets that match the [`BroadcastOptions`]/// and return a stream of ack responses.fnbroadcast_with_ack(&self,packet:Packet,opts:BroadcastOptions,timeout:Option<Duration>) -> implFuture<Output = Result<Self::AckStream,Self::Error>> + Send;/// Adds the sockets that match the [`BroadcastOptions`] to the rooms.fnadd_sockets(&self,opts:BroadcastOptions,rooms:implRoomParam) -> implFuture<Output = Result<(),Self::Error>> + Send;/// Removes the sockets that match the [`BroadcastOptions`] from the rooms.fndel_sockets(&self,opts:BroadcastOptions,rooms:implRoomParam) -> implFuture<Output = Result<(),Self::Error>> + Send;/// Disconnects the sockets that match the [`BroadcastOptions`].fndisconnect_socket(&self,opts:BroadcastOptions) -> implFuture<Output = Result<(),BroadcastError>> + Send;/// Fetches rooms that match the [`BroadcastOptions`]fnrooms(&self,opts:BroadcastOptions) -> implFuture<Output = Result<Vec<Room>,Self::Error>> + Send;/// Fetches remote sockets that match the [`BroadcastOptions`].fnfetch_sockets(&self,opts:BroadcastOptions) -> implFuture<Output = Result<Vec<RemoteSocketData>,Self::Error>> + Send;/// Returns the local adapter. Used to enable default behaviors.fnget_local(&self) -> &CoreLocalAdapter<E>;}

The SocketEmitter is an internal interface implemented by the socketioxide crate to execute actions on the sockets:

/// A item yield by the ack stream.pubtypeAckStreamItem<E> = (Sid,Result<Value,E>);/// The [`SocketEmitter`] will be implemented by the socketioxide library./// It is simply used as an abstraction to allow the adapter to communicate/// with the socket server without the need to depend on the socketioxide lib.pubtraitSocketEmitter:Send + Sync + 'static{/// An error that can occur when sending data an acknowledgment.typeAckError:StdError + Send + Serialize + DeserializeOwned + 'static;/// A stream that emits the acknowledgments of multiple sockets.typeAckStream:Stream<Item = AckStreamItem<Self::AckError>> + FusedStream + Send + 'static;/// Get all the socket ids in the namespace.fnget_all_sids(&self,filter:implFn(&Sid) -> bool) -> Vec<Sid>;/// Get the socket data that match the list of socket ids.fnget_remote_sockets(&self,sids:BroadcastIter<'_>) -> Vec<RemoteSocketData>;/// Send data to the list of socket ids.fnsend_many(&self,sids:BroadcastIter<'_>,data:Value) -> Result<(),Vec<SocketError>>;/// Send data to the list of socket ids and get a stream of acks and the number of expected acks.fnsend_many_with_ack(&self,sids:BroadcastIter<'_>,packet:Packet,timeout:Option<Duration>,) -> (Self::AckStream,u32);/// Disconnect all the sockets in the list.fndisconnect_many(&self,sids:Vec<Sid>) -> Result<(),Vec<SocketError>>;/// Get the path of the namespace.fnpath(&self) -> &Str;/// Get the parser of the namespace.fnparser(&self) -> implParse;/// Get the unique server id.fnserver_id(&self) -> Uid;}

The Adapter default implementation is the local adapter. Any other implementation that uses remote systems (redis, mongo, ...) can propagate requests and then execute the default behavior with the CoreLocalAdapter passed to the adapter implementation.

@TotodoreTotodore added A-core Area related to socketioxide-core C-Feature-request Request for a feature labels Jan 1, 2025
@TotodoreTotodore self-assigned this Jan 1, 2025
@TotodoreTotodore linked an issue Jan 1, 2025 that may be closed by this pull request
@TotodoreTotodore linked an issue Jan 12, 2025 that may be closed by this pull request
@Totodore
Totodore enabled auto-merge (rebase) January 12, 2025 18:03
auto-merge was automatically disabled January 12, 2025 18:06

Rebase failed

@Totodore
Totodore merged commit 4ce3966 into mainJan 12, 2025
@Totodore
Totodore deleted the feat-adapter-rework branch January 17, 2025 16:59
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-coreArea related to socketioxide-coreC-Feature-requestRequest for a feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Redis Adapter Feature: Adapter trait with optional async

1 participant

@Totodore