From 209299267ca3114c6b082adfb7dc431fe7525ca9 Mon Sep 17 00:00:00 2001 From: fakeshadow <24548779@qq.com> Date: Wed, 28 Feb 2024 03:09:28 +0800 Subject: [PATCH] add ResponseSender::close shortcut. --- http-ws/CHANGES.md | 1 + http-ws/src/stream.rs | 10 ++++++++++ http/src/util/service/route.rs | 4 ++-- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/http-ws/CHANGES.md b/http-ws/CHANGES.md index 343e5aa1..751981e7 100644 --- a/http-ws/CHANGES.md +++ b/http-ws/CHANGES.md @@ -2,6 +2,7 @@ ## Add - add `RequestStream::inner_mut` method for accessing inner stream type. - add `RequestStream::codec_mut` method for accessing `Codec` type. +- add `ResponseSender::close` method for sending close message. ## Change - reduce `stream::RequestStream`'s generic type params. diff --git a/http-ws/src/stream.rs b/http-ws/src/stream.rs index c20a3675..c0492fd2 100644 --- a/http-ws/src/stream.rs +++ b/http-ws/src/stream.rs @@ -17,6 +17,7 @@ use tokio::sync::mpsc::{channel, Receiver, Sender}; use super::{ codec::{Codec, Message}, error::ProtocolError, + proto::CloseReason, }; pin_project! { @@ -239,6 +240,15 @@ impl ResponseSender { pub fn binary(&self, bin: impl Into) -> impl Future> + '_ { self.send(Message::Binary(bin.into())) } + + /// encode [Message::Close] variant and add to [ResponseStream]. + /// take ownership of Self as after close message no more message can be sent to client. + pub fn close( + self, + reason: Option + 'static>, + ) -> impl Future> + 'static { + async move { self.send(Message::Close(reason.map(Into::into))).await } + } } /// [Weak] version of [ResponseSender]. diff --git a/http/src/util/service/route.rs b/http/src/util/service/route.rs index 27761860..b8f7936d 100644 --- a/http/src/util/service/route.rs +++ b/http/src/util/service/route.rs @@ -176,7 +176,7 @@ impl ReadyService for RouteService { } /// Error type of Method not allow for route. -pub struct MethodNotAllowed(pub Vec); +pub struct MethodNotAllowed(pub Box>); impl MethodNotAllowed { /// slice of allowed methods of current route. @@ -229,7 +229,7 @@ where type Error = RouterError; async fn call(&self, _: Req) -> Result { - Err(RouterError::NotAllowed(MethodNotAllowed(Vec::new()))) + Err(RouterError::NotAllowed(MethodNotAllowed(Box::new(Vec::new())))) } }