[pbs-devel] [PATCH proxmox-backup 3/9] implement Servive for RateLimitedStream

Dietmar Maurer dietmar at proxmox.com
Tue Nov 9 07:52:43 CET 2021


Signed-off-by: Dietmar Maurer <dietmar at proxmox.com>
---
 proxmox-rest-server/Cargo.toml  |  1 +
 proxmox-rest-server/src/rest.rs | 28 ++++++++++++++++++++++++++++
 2 files changed, 29 insertions(+)

diff --git a/proxmox-rest-server/Cargo.toml b/proxmox-rest-server/Cargo.toml
index 1fa76f21..b88e5d12 100644
--- a/proxmox-rest-server/Cargo.toml
+++ b/proxmox-rest-server/Cargo.toml
@@ -33,6 +33,7 @@ url = "2.1"
 proxmox = "0.15.0"
 proxmox-io = "1"
 proxmox-lang = "1"
+proxmox-http = { version = "0.5.0", features = [ "client" ] }
 proxmox-router = "1.1"
 proxmox-schema = { version = "1", features = [ "api-macro", "upid-api-impl" ] }
 proxmox-time = "1"
diff --git a/proxmox-rest-server/src/rest.rs b/proxmox-rest-server/src/rest.rs
index 74bc8bb1..f27f703d 100644
--- a/proxmox-rest-server/src/rest.rs
+++ b/proxmox-rest-server/src/rest.rs
@@ -31,6 +31,8 @@ use proxmox_schema::{
     ParameterSchema,
 };
 
+use proxmox_http::client::RateLimitedStream;
+
 use pbs_tools::compression::{DeflateEncoder, Level};
 use pbs_tools::stream::AsyncReaderStream;
 
@@ -73,6 +75,32 @@ impl RestServer {
     }
 }
 
+impl Service<&Pin<Box<tokio_openssl::SslStream<RateLimitedStream<tokio::net::TcpStream>>>>>
+    for RestServer
+{
+    type Response = ApiService;
+    type Error = Error;
+    type Future = Pin<Box<dyn Future<Output = Result<ApiService, Error>> + Send>>;
+
+    fn poll_ready(&mut self, _cx: &mut Context) -> Poll<Result<(), Self::Error>> {
+        Poll::Ready(Ok(()))
+    }
+
+    fn call(
+        &mut self,
+        ctx: &Pin<Box<tokio_openssl::SslStream<RateLimitedStream<tokio::net::TcpStream>>>>,
+    ) -> Self::Future {
+        match ctx.get_ref().peer_addr() {
+            Err(err) => future::err(format_err!("unable to get peer address - {}", err)).boxed(),
+            Ok(peer) => future::ok(ApiService {
+                peer,
+                api_config: self.api_config.clone(),
+            })
+            .boxed(),
+        }
+    }
+}
+
 impl Service<&Pin<Box<tokio_openssl::SslStream<tokio::net::TcpStream>>>>
     for RestServer
 {
-- 
2.30.2





More information about the pbs-devel mailing list