Skip to content

Commit

Permalink
Merge branch 'main' into release/v0.15.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Totodore authored Nov 2, 2024
2 parents 3ec10ab + 160f7af commit dbeaa0b
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 19 deletions.
2 changes: 1 addition & 1 deletion crates/engineioxide/src/service/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! ## A tower [`Service`](tower::Service) for engine.io so it can be used with frameworks supporting tower services
//! ## A tower [`Service`](tower_service::Service) for engine.io so it can be used with frameworks supporting tower services
//!
//!
//! #### Example with a `hyper` standalone service :
Expand Down
24 changes: 17 additions & 7 deletions crates/engineioxide/src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,28 +330,38 @@ where
.expect("Pong rx should be locked only once");

let instant = tokio::time::Instant::now();
let mut interval_tick = tokio::time::interval(interval);
interval_tick.tick().await;
// Sleep for an interval minus the time it took to get here
tokio::time::sleep(interval.saturating_sub(Duration::from_millis(
15 + instant.elapsed().as_millis() as u64,
)))
.await;

#[cfg(feature = "tracing")]
tracing::debug!("[sid={}] heartbeat sender routine started", self.id);
tracing::debug!(sid = ?self.id, "heartbeat sender routine started");

let mut interval_tick = tokio::time::interval(interval);
interval_tick.tick().await;
// Some clients send the pong packet in first. If that happens, we should consume it.
heartbeat_rx.try_recv().ok();
loop {
// Some clients send the pong packet in first. If that happens, we should consume it.
heartbeat_rx.try_recv().ok();
#[cfg(feature = "tracing")]
tracing::trace!(sid = ?self.id, "emitting ping");

self.internal_tx
.try_send(smallvec![Packet::Ping])
.map_err(|_| Error::HeartbeatTimeout)?;

#[cfg(feature = "tracing")]
tracing::trace!(sid = ?self.id, "waiting for pong");

tokio::time::timeout(timeout, heartbeat_rx.recv())
.await
.map_err(|_| Error::HeartbeatTimeout)?
.ok_or(Error::HeartbeatTimeout)?;

#[cfg(feature = "tracing")]
tracing::trace!(sid = ?self.id, "pong received");

interval_tick.tick().await;
}
}
Expand All @@ -364,7 +374,7 @@ where
.expect("Pong rx should be locked only once");

#[cfg(feature = "tracing")]
tracing::debug!("[sid={}] heartbeat receiver routine started", self.id);
tracing::debug!(sid = ?self.id, "heartbeat receiver routine started");

loop {
tokio::time::timeout(interval + timeout, heartbeat_rx.recv())
Expand All @@ -373,7 +383,7 @@ where
.ok_or(Error::HeartbeatTimeout)?;

#[cfg(feature = "tracing")]
tracing::debug!("[sid={}] ping received, sending pong", self.id);
tracing::trace!(sid = ?self.id, "ping received, sending pong");
self.internal_tx
.try_send(smallvec![Packet::Pong])
.map_err(|_| Error::HeartbeatTimeout)?;
Expand Down
1 change: 0 additions & 1 deletion crates/socketioxide/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ tower-layer.workspace = true
http.workspace = true
http-body.workspace = true
thiserror.workspace = true
smallvec.workspace = true
hyper.workspace = true
matchit.workspace = true
pin-project-lite.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/socketioxide/src/extract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
//! that it is dropped at least when the socket is disconnected.
//! Otherwise it will create a memory leak. It is why the [`SocketRef`] extractor is used instead of cloning
//! the socket for common usage.
//! If you want to deserialize the [`Value`] data you must manually call the `Data` extractor to deserialize it.
//! If you want to deserialize the [`Value`](socketioxide_core::Value) data you must manually call the `Data` extractor to deserialize it.
//!
//! [`FromConnectParts`]: crate::handler::FromConnectParts
//! [`FromMessageParts`]: crate::handler::FromMessageParts
Expand Down
8 changes: 4 additions & 4 deletions crates/socketioxide/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl Default for SocketIoConfig {

/// A builder to create a [`SocketIo`] instance.
/// It contains everything to configure the socket.io server with a [`SocketIoConfig`].
/// It can be used to build either a Tower [`Layer`](tower::layer::Layer) or a [`Service`](tower::Service).
/// It can be used to build either a Tower [`Layer`](tower_layer::Layer) or a [`Service`](tower_service::Service).
pub struct SocketIoBuilder<A: Adapter = LocalAdapter> {
config: SocketIoConfig,
engine_config_builder: EngineIoConfigBuilder,
Expand Down Expand Up @@ -291,21 +291,21 @@ impl SocketIo<LocalAdapter> {

/// Creates a new [`SocketIoService`] and a [`SocketIo`] instance with a default config.
/// This service will be a _standalone_ service that return a 404 error for every non-socket.io request.
/// It can be used as a [`Service`](tower::Service) (see hyper example)
/// It can be used as a [`Service`](tower_service::Service) (see hyper example)
#[inline(always)]
pub fn new_svc() -> (SocketIoService<NotFoundService>, SocketIo) {
Self::builder().build_svc()
}

/// Creates a new [`SocketIoService`] and a [`SocketIo`] instance with a default config.
/// It can be used as a [`Service`](tower::Service) with an inner service
/// It can be used as a [`Service`](tower_service::Service) with an inner service
#[inline(always)]
pub fn new_inner_svc<S: Clone>(svc: S) -> (SocketIoService<S>, SocketIo) {
Self::builder().build_with_inner_svc(svc)
}

/// Builds a [`SocketIoLayer`] and a [`SocketIo`] instance with a default config.
/// It can be used as a tower [`Layer`](tower::layer::Layer) (see axum example)
/// It can be used as a tower [`Layer`](tower_layer::Layer) (see axum example)
#[inline(always)]
pub fn new_layer() -> (SocketIoLayer, SocketIo) {
Self::builder().build_layer()
Expand Down
8 changes: 4 additions & 4 deletions crates/socketioxide/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
nonstandard_style,
missing_docs
)]
//! Socketioxide is a socket.io server implementation that works as a [`tower`] layer/service.
//! It integrates nicely with the rest of the [`tower`]/[`tokio`]/[`hyper`](https://docs.rs/hyper/latest/hyper/) ecosystem.
//! Socketioxide is a socket.io server implementation that works as a tower layer/service.
//! It integrates nicely with the rest of the [`tower`](https://docs.rs/tower/latest/tower/)/[`tokio`]/[`hyper`](https://docs.rs/hyper/latest/hyper/) ecosystem.
//!
//! ## Table of contents
//! * [Features](#features)
Expand Down Expand Up @@ -60,7 +60,7 @@
//! * Polling & Websocket transports
//!
//! ## Compatibility
//! Because it works as a tower [`layer`](tower::layer)/[`service`](tower::Service) or an hyper [`service`](hyper::service::Service)
//! Because it works as a tower [`layer`](tower_layer::Layer)/[`service`](tower_service::Service) or an hyper [`service`](hyper::service::Service)
//! you can use it with any http server frameworks that works with tower/hyper:
//! * [Axum](https://docs.rs/axum/latest/axum/)
//! * [Warp](https://docs.rs/warp/latest/warp/) (Not supported with socketioxide >= 0.9.0 as long as warp doesn't migrate to hyper v1)
Expand Down Expand Up @@ -105,7 +105,7 @@
//! }
//! ```
//! ## Initialisation
//! The [`SocketIo`] struct is the main entry point of the library. It is used to create a [`Layer`](tower::layer) or a [`Service`](tower::Service).
//! The [`SocketIo`] struct is the main entry point of the library. It is used to create a [`Layer`](tower_layer::Layer) or a [`Service`](tower_service::Service).
//! Later it can be used as the equivalent of the `io` object in the JS API.
//!
//! When creating your [`SocketIo`] instance, you can use the builder pattern to configure it with the [`SocketIoBuilder`] struct.
Expand Down
2 changes: 1 addition & 1 deletion crates/socketioxide/src/service.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! ## A Tower [`Service`](tower::Service) and Hyper [`Service`](hyper::service::Service) for socket.io so it
//! ## A Tower [`Service`](tower_service::Service) and Hyper [`Service`](hyper::service::Service) for socket.io so it
//! can be used with frameworks supporting tower and hyper services.
//!
//! #### Example with a raw `hyper` standalone service (most of the time it easier to use a framework like `axum` or `salvo`):
Expand Down

0 comments on commit dbeaa0b

Please sign in to comment.