From 136eb2e8a73cd520352c4a1346b1054f80b5ba6a Mon Sep 17 00:00:00 2001 From: kana-rus Date: Wed, 21 Aug 2024 18:36:27 +0900 Subject: [PATCH 1/5] reuse Request in session --- ohkami/src/header/map.rs | 7 +++++++ ohkami/src/request/headers.rs | 9 +++++++++ ohkami/src/request/memory.rs | 6 ++++++ ohkami/src/request/mod.rs | 12 ++++++++++++ ohkami/src/session/mod.rs | 15 +++++++++------ 5 files changed, 43 insertions(+), 6 deletions(-) diff --git a/ohkami/src/header/map.rs b/ohkami/src/header/map.rs index 4d23b8a3..58f1fce6 100644 --- a/ohkami/src/header/map.rs +++ b/ohkami/src/header/map.rs @@ -14,6 +14,13 @@ impl IndexMap { } } + #[cfg(any(feature="rt_tokio",feature="rt_async-std"))] + #[inline] + pub(crate) fn clear(&mut self) { + for idx in &mut self.index {*idx = Self::NULL} + self.values.clear(); + } + #[inline(always)] pub(crate) unsafe fn get(&self, index: usize) -> Option<&Value> { match *self.index.get_unchecked(index) { diff --git a/ohkami/src/request/headers.rs b/ohkami/src/request/headers.rs index 109ad5b9..627a302b 100644 --- a/ohkami/src/request/headers.rs +++ b/ohkami/src/request/headers.rs @@ -359,6 +359,15 @@ impl Headers { Self::init() } + #[cfg(any(feature="rt_tokio",feature="rt_async-std"))] + #[inline] + pub(crate) fn clear(&mut self) { + self.standard.clear(); + if let Some(map) = &mut self.custom { + map.clear() + } + } + #[inline] pub(crate) fn get_raw(&self, name: Header) -> Option<&CowSlice> { unsafe {self.standard.get(name as usize)} } diff --git a/ohkami/src/request/memory.rs b/ohkami/src/request/memory.rs index 64e3b1a8..9e228fbd 100644 --- a/ohkami/src/request/memory.rs +++ b/ohkami/src/request/memory.rs @@ -33,6 +33,12 @@ impl Store { pub(super) const fn init() -> Self { Self(None) } + #[cfg(any(feature="rt_tokio",feature="rt_async-std"))] + pub(super) fn clear(&mut self) { + if let Some(map) = &mut self.0 { + map.clear() + } + } #[inline] pub fn insert(&mut self, value: Data) { self.0.get_or_insert_with(|| Box::new(HashMap::default())) diff --git a/ohkami/src/request/mod.rs b/ohkami/src/request/mod.rs index 1aa4c466..1cccadf7 100644 --- a/ohkami/src/request/mod.rs +++ b/ohkami/src/request/mod.rs @@ -186,6 +186,18 @@ impl Request { store: Store::init(), } } + #[cfg(any(feature="rt_tokio",feature="rt_async-std"))] + #[inline] + pub(crate) fn clear(&mut self) { + for b in &mut *self.__buf__ { + match b {0 => break, _ => *b = 0} + } + self.path = Path::uninit(); + self.query = None; + self.headers.clear(); + self.payload = None; + self.store.clear(); + } #[cfg(any(feature="rt_tokio",feature="rt_async-std"))] #[inline] diff --git a/ohkami/src/session/mod.rs b/ohkami/src/session/mod.rs index bfb1a77d..025b7bab 100644 --- a/ohkami/src/session/mod.rs +++ b/ohkami/src/session/mod.rs @@ -64,23 +64,26 @@ impl Session { } match timeout_in(Duration::from_secs(env::OHKAMI_KEEPALIVE_TIMEOUT()), async { + let mut req = Request::init(); + let mut req = unsafe {Pin::new_unchecked(&mut req)}; loop { - let mut req = Request::init(); - let mut req = unsafe {Pin::new_unchecked(&mut req)}; match req.as_mut().read(&mut self.connection).await { Ok(Some(())) => { let close = matches!(req.headers.Connection(), Some("close" | "Close")); - let res = match catch_unwind(AssertUnwindSafe(|| self.router.handle(req.get_mut()))) { + let res = match catch_unwind(AssertUnwindSafe({ + let req = req.as_mut(); + || self.router.handle(req.get_mut()) + })) { Ok(future) => future.await, Err(panic) => panicking(panic), }; let upgrade = res.send(&mut self.connection).await; - if !upgrade.is_none() { - break upgrade - } + if !upgrade.is_none() {break upgrade} if close {break Upgrade::None} + + req.clear(); } Ok(None) => break Upgrade::None, Err(res) => {res.send(&mut self.connection).await;}, From d712ab30fb3c72062321d4e2e47216ff14fa5865 Mon Sep 17 00:00:00 2001 From: kana-rus Date: Wed, 21 Aug 2024 19:21:21 +0900 Subject: [PATCH 2/5] session: not call explicit shutdown --- ohkami/src/session/mod.rs | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/ohkami/src/session/mod.rs b/ohkami/src/session/mod.rs index 025b7bab..0dff233e 100644 --- a/ohkami/src/session/mod.rs +++ b/ohkami/src/session/mod.rs @@ -92,20 +92,6 @@ impl Session { }).await { Some(Upgrade::None) | None => { crate::DEBUG!("about to shutdown connection"); - - if let Some(err) = { - #[cfg(feature="rt_tokio")] {use crate::__rt__::AsyncWriter; - self.connection.shutdown().await - } - #[cfg(feature="rt_async-std")] { - self.connection.shutdown(std::net::Shutdown::Both) - } - }.err() { - match err.kind() { - std::io::ErrorKind::NotConnected => (), - _ => panic!("Failed to shutdown stream: {err}") - } - } } #[cfg(all(feature="ws", any(feature="rt_tokio",feature="rt_async-std")))] From ae9734f4abc4d81ecae84139a9d03014f0f1eeab Mon Sep 17 00:00:00 2001 From: kana-rus Date: Wed, 21 Aug 2024 20:08:36 +0900 Subject: [PATCH 3/5] remove unnecessary 'static --- ohkami/src/response/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ohkami/src/response/mod.rs b/ohkami/src/response/mod.rs index aca7d11b..457f95b2 100644 --- a/ohkami/src/response/mod.rs +++ b/ohkami/src/response/mod.rs @@ -172,7 +172,7 @@ impl Upgrade { impl Response { #[cfg_attr(not(feature="sse"), inline)] pub(crate) async fn send(mut self, - conn: &mut (impl AsyncWriter + Unpin + 'static) + conn: &mut (impl AsyncWriter + Unpin) ) -> Upgrade { self.complete(); From 14756d9a732dbd56d65e8f6fc063e30e6052640b Mon Sep 17 00:00:00 2001 From: kana-rus Date: Thu, 22 Aug 2024 00:52:14 +0900 Subject: [PATCH 4/5] refactor to make clear `req` lifecycle --- ohkami/src/session/mod.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/ohkami/src/session/mod.rs b/ohkami/src/session/mod.rs index 0dff233e..fb6ff4c3 100644 --- a/ohkami/src/session/mod.rs +++ b/ohkami/src/session/mod.rs @@ -82,12 +82,11 @@ impl Session { if !upgrade.is_none() {break upgrade} if close {break Upgrade::None} - - req.clear(); } Ok(None) => break Upgrade::None, Err(res) => {res.send(&mut self.connection).await;}, - }; + } + req.clear() } }).await { Some(Upgrade::None) | None => { From 894d467f3a1442753b0066023aaaa4fc48d9b5b3 Mon Sep 17 00:00:00 2001 From: kana-rus Date: Thu, 22 Aug 2024 01:56:26 +0900 Subject: [PATCH 5/5] refactor `req.clear` aand its position --- ohkami/src/request/mod.rs | 18 ++++++++++-------- ohkami/src/session/mod.rs | 2 +- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/ohkami/src/request/mod.rs b/ohkami/src/request/mod.rs index 1cccadf7..0209b053 100644 --- a/ohkami/src/request/mod.rs +++ b/ohkami/src/request/mod.rs @@ -189,14 +189,16 @@ impl Request { #[cfg(any(feature="rt_tokio",feature="rt_async-std"))] #[inline] pub(crate) fn clear(&mut self) { - for b in &mut *self.__buf__ { - match b {0 => break, _ => *b = 0} - } - self.path = Path::uninit(); - self.query = None; - self.headers.clear(); - self.payload = None; - self.store.clear(); + if self.__buf__[0] != 0 { + for b in &mut *self.__buf__ { + match b {0 => break, _ => *b = 0} + } + self.path = Path::uninit(); + self.query = None; + self.headers.clear(); + self.payload = None; + self.store.clear(); + } /* else: just after `init`ed or `clear`ed */ } #[cfg(any(feature="rt_tokio",feature="rt_async-std"))] diff --git a/ohkami/src/session/mod.rs b/ohkami/src/session/mod.rs index fb6ff4c3..4b31c375 100644 --- a/ohkami/src/session/mod.rs +++ b/ohkami/src/session/mod.rs @@ -67,6 +67,7 @@ impl Session { let mut req = Request::init(); let mut req = unsafe {Pin::new_unchecked(&mut req)}; loop { + req.clear(); match req.as_mut().read(&mut self.connection).await { Ok(Some(())) => { let close = matches!(req.headers.Connection(), Some("close" | "Close")); @@ -86,7 +87,6 @@ impl Session { Ok(None) => break Upgrade::None, Err(res) => {res.send(&mut self.connection).await;}, } - req.clear() } }).await { Some(Upgrade::None) | None => {