public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [pve-devel] [PATCH proxmox 1/2] http: sync client: add HTTP request timeout option
@ 2024-11-22 10:08 Lukas Wagner
  2024-11-22 10:08 ` [pve-devel] [PATCH proxmox 2/2] notify: webhook: gotify: set HTTP request timeout Lukas Wagner
  0 siblings, 1 reply; 2+ messages in thread
From: Lukas Wagner @ 2024-11-22 10:08 UTC (permalink / raw)
  To: pve-devel

This commits adds the possibility to set a HTTP request timeout for the
sync client.

For now, I've opted to add this as a separate option than can be set via
a separate new_with_timeout method as compared to adding it to the HttpOptions
struct. While it of course would make a lot of sense to add it to the
latter, this would require adding support for request timeouts to the
async client as well. Some users of the async client handle request
timeouts externally via tokio::time::timeout, so these would need to
modified as well. I don't want to touch this at the moment,
so I've opted to introduce the timeout to the sync client only for now.
We can always revisit this at a later time and move the option to the
HttpOptions struct.

Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
---
 proxmox-http/src/client/sync.rs | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/proxmox-http/src/client/sync.rs b/proxmox-http/src/client/sync.rs
index fb10f5be..955a6fce 100644
--- a/proxmox-http/src/client/sync.rs
+++ b/proxmox-http/src/client/sync.rs
@@ -1,6 +1,7 @@
 use std::collections::HashMap;
 use std::io::Read;
 use std::sync::Arc;
+use std::time::Duration;
 
 use anyhow::Error;
 use http::Response;
@@ -12,16 +13,31 @@ use crate::HttpOptions;
 /// Blocking HTTP client for usage with [`HttpClient`].
 pub struct Client {
     options: HttpOptions,
+    timeout: Option<Duration>,
 }
 
 impl Client {
     pub fn new(options: HttpOptions) -> Self {
-        Self { options }
+        Self {
+            options,
+            timeout: None,
+        }
+    }
+
+    pub fn new_with_timeout(options: HttpOptions, timeout: Duration) -> Self {
+        Self {
+            options,
+            timeout: Some(timeout),
+        }
     }
 
     fn agent(&self) -> Result<ureq::Agent, Error> {
         let mut builder = ureq::AgentBuilder::new();
 
+        if let Some(timeout) = self.timeout {
+            builder = builder.timeout(timeout);
+        };
+
         let connector = Arc::new(native_tls::TlsConnector::new()?);
         builder = builder.tls_connector(connector);
 
-- 
2.39.5



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [pve-devel] [PATCH proxmox 2/2] notify: webhook: gotify: set HTTP request timeout
  2024-11-22 10:08 [pve-devel] [PATCH proxmox 1/2] http: sync client: add HTTP request timeout option Lukas Wagner
@ 2024-11-22 10:08 ` Lukas Wagner
  0 siblings, 0 replies; 2+ messages in thread
From: Lukas Wagner @ 2024-11-22 10:08 UTC (permalink / raw)
  To: pve-devel

By default, the sync client from proxmox-http (powered by ureq) does not
have any request timeout. To avoid blocking the current thread for a
prolonged period of time, we now make use of the previously added
`Client::new_with_timeout` function to create a new HTTP client with a
default timout of 5 seconds.

Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
---
 proxmox-notify/src/endpoints/gotify.rs  | 5 ++++-
 proxmox-notify/src/endpoints/webhook.rs | 6 +++++-
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/proxmox-notify/src/endpoints/gotify.rs b/proxmox-notify/src/endpoints/gotify.rs
index fae036cd..ea13d5d0 100644
--- a/proxmox-notify/src/endpoints/gotify.rs
+++ b/proxmox-notify/src/endpoints/gotify.rs
@@ -1,4 +1,5 @@
 use std::collections::HashMap;
+use std::time::Duration;
 
 use serde::{Deserialize, Serialize};
 use serde_json::json;
@@ -13,6 +14,8 @@ use crate::renderer::TemplateType;
 use crate::schema::ENTITY_NAME_SCHEMA;
 use crate::{renderer, Content, Endpoint, Error, Notification, Origin, Severity};
 
+const HTTP_TIMEOUT: Duration = Duration::from_secs(5);
+
 fn severity_to_priority(level: Severity) -> u32 {
     match level {
         Severity::Info => 1,
@@ -146,7 +149,7 @@ impl Endpoint for GotifyEndpoint {
             ..Default::default()
         };
 
-        let client = Client::new(options);
+        let client = Client::new_with_timeout(options, HTTP_TIMEOUT);
         let uri = format!("{}/message", self.config.server);
 
         client
diff --git a/proxmox-notify/src/endpoints/webhook.rs b/proxmox-notify/src/endpoints/webhook.rs
index 4ad9cb2f..efb70637 100644
--- a/proxmox-notify/src/endpoints/webhook.rs
+++ b/proxmox-notify/src/endpoints/webhook.rs
@@ -7,6 +7,8 @@
 //! Secrets are kept in a private configuration file, accessible only by root, and are not retrievable via the API.
 //! Within templates, secrets can be referenced using `{{ secrets.<name> }}`.
 //! Additionally, we take measures to prevent secrets from appearing in logs or error messages.
+use std::time::Duration;
+
 use handlebars::{
     Context as HandlebarsContext, Handlebars, Helper, HelperResult, Output, RenderContext,
     RenderError as HandlebarsRenderError,
@@ -30,6 +32,8 @@ use crate::{renderer, Content, Endpoint, Error, Notification, Origin};
 /// This will be used as a section type in the public/private configuration file.
 pub(crate) const WEBHOOK_TYPENAME: &str = "webhook";
 
+const HTTP_TIMEOUT: Duration = Duration::from_secs(5);
+
 #[api]
 #[derive(Serialize, Deserialize, Clone, Copy, Default)]
 #[serde(rename_all = "kebab-case")]
@@ -270,7 +274,7 @@ impl WebhookEndpoint {
             ..Default::default()
         };
 
-        Ok(Client::new(options))
+        Ok(Client::new_with_timeout(options, HTTP_TIMEOUT))
     }
 
     fn build_request(&self, notification: &Notification) -> Result<Request<String>, Error> {
-- 
2.39.5



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-11-22 10:08 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-11-22 10:08 [pve-devel] [PATCH proxmox 1/2] http: sync client: add HTTP request timeout option Lukas Wagner
2024-11-22 10:08 ` [pve-devel] [PATCH proxmox 2/2] notify: webhook: gotify: set HTTP request timeout Lukas Wagner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal