1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-27 17:22:57 +01:00

Reduce copies in ping/pong

This commit is contained in:
asonix 2024-11-04 18:02:41 -06:00
parent b3c5934b00
commit a66f20a106
2 changed files with 6 additions and 6 deletions

View File

@ -82,7 +82,7 @@ async fn ws(
loop { loop {
interval.tick().await; interval.tick().await;
if session2.ping(b"").await.is_err() { if session2.ping(&b""[..]).await.is_err() {
break; break;
} }
@ -97,7 +97,7 @@ async fn ws(
while let Some(Ok(msg)) = stream.recv().await { while let Some(Ok(msg)) = stream.recv().await {
match msg { match msg {
AggregatedMessage::Ping(bytes) => { AggregatedMessage::Ping(bytes) => {
if session.pong(&bytes).await.is_err() { if session.pong(bytes).await.is_err() {
return; return;
} }
} }

View File

@ -103,11 +103,11 @@ impl Session {
/// } /// }
/// # } /// # }
/// ``` /// ```
pub async fn ping(&mut self, msg: &[u8]) -> Result<(), Closed> { pub async fn ping(&mut self, msg: impl Into<Bytes>) -> Result<(), Closed> {
self.pre_check(); self.pre_check();
if let Some(inner) = self.inner.as_mut() { if let Some(inner) = self.inner.as_mut() {
inner inner
.send(Message::Ping(Bytes::copy_from_slice(msg))) .send(Message::Ping(msg.into()))
.await .await
.map_err(|_| Closed) .map_err(|_| Closed)
} else { } else {
@ -127,11 +127,11 @@ impl Session {
/// _ => (), /// _ => (),
/// } /// }
/// # } /// # }
pub async fn pong(&mut self, msg: &[u8]) -> Result<(), Closed> { pub async fn pong(&mut self, msg: impl Into<Bytes>) -> Result<(), Closed> {
self.pre_check(); self.pre_check();
if let Some(inner) = self.inner.as_mut() { if let Some(inner) = self.inner.as_mut() {
inner inner
.send(Message::Pong(Bytes::copy_from_slice(msg))) .send(Message::Pong(msg.into()))
.await .await
.map_err(|_| Closed) .map_err(|_| Closed)
} else { } else {