From c34e47771355974c430382d2a3d0800d506cd879 Mon Sep 17 00:00:00 2001 From: Alex Severin Date: Fri, 26 Apr 2024 21:05:38 +0000 Subject: [PATCH 1/3] expose new_uds err --- .bleep | 2 +- pingora-core/src/upstreams/peer.rs | 13 +++++++++---- pingora-proxy/tests/utils/server_utils.rs | 2 +- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/.bleep b/.bleep index 398515bbe..0b4740dda 100644 --- a/.bleep +++ b/.bleep @@ -1 +1 @@ -3c6f56ba2c6cd48aa5f4edd442090e95739d15fc \ No newline at end of file +32b73315dbc9aa07ca121a5fde8fc58dde5b9e1b diff --git a/pingora-core/src/upstreams/peer.rs b/pingora-core/src/upstreams/peer.rs index 21f737b8a..7c0277cc7 100644 --- a/pingora-core/src/upstreams/peer.rs +++ b/pingora-core/src/upstreams/peer.rs @@ -15,7 +15,10 @@ //! Defines where to connect to and how to connect to a remote server use ahash::AHasher; -use pingora_error::{ErrorType::InternalError, OrErr, Result}; +use pingora_error::{ + ErrorType::{InternalError, SocketError}, + OrErr, Result, +}; use std::collections::BTreeMap; use std::fmt::{Display, Formatter, Result as FmtResult}; use std::hash::{Hash, Hasher}; @@ -431,9 +434,11 @@ impl HttpPeer { } /// Create a new [`HttpPeer`] with the given path to Unix domain socket and TLS settings. - pub fn new_uds(path: &str, tls: bool, sni: String) -> Self { - let addr = SocketAddr::Unix(UnixSocketAddr::from_pathname(Path::new(path)).unwrap()); //TODO: handle error - Self::new_from_sockaddr(addr, tls, sni) + pub fn new_uds(path: &str, tls: bool, sni: String) -> Result { + let addr = SocketAddr::Unix( + UnixSocketAddr::from_pathname(Path::new(path)).or_err(SocketError, "invalid path")?, + ); + Ok(Self::new_from_sockaddr(addr, tls, sni)) } /// Create a new [`HttpPeer`] that uses a proxy to connect to the upstream IP and port diff --git a/pingora-proxy/tests/utils/server_utils.rs b/pingora-proxy/tests/utils/server_utils.rs index 1397e2c77..84b2c12c5 100644 --- a/pingora-proxy/tests/utils/server_utils.rs +++ b/pingora-proxy/tests/utils/server_utils.rs @@ -264,7 +264,7 @@ impl ProxyHttp for ExampleProxyHttp { "/tmp/nginx-test.sock", false, "".to_string(), - ))); + )?)); } let port = req .headers From 19e4096f02766606b1fe7f43c21e66c3db905af4 Mon Sep 17 00:00:00 2001 From: Andrew Hauck Date: Fri, 31 May 2024 08:47:34 -0700 Subject: [PATCH 2/3] Add trait for converting Options to Errors --- .bleep | 2 +- pingora-error/src/lib.rs | 55 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/.bleep b/.bleep index 0b4740dda..9cc187c57 100644 --- a/.bleep +++ b/.bleep @@ -1 +1 @@ -32b73315dbc9aa07ca121a5fde8fc58dde5b9e1b +3cc5b114adb93b478fb57b8b12666c4c849d1ec8 diff --git a/pingora-error/src/lib.rs b/pingora-error/src/lib.rs index 91b03580f..438b392e1 100644 --- a/pingora-error/src/lib.rs +++ b/pingora-error/src/lib.rs @@ -539,6 +539,35 @@ impl OrErr for Result { } } +/// Helper trait to convert an [Option] to an [Error] with context. +pub trait OkOrErr { + fn or_err(self, et: ErrorType, context: &'static str) -> Result; + + fn or_err_with, F: FnOnce() -> C>( + self, + et: ErrorType, + context: F, + ) -> Result; +} + +impl OkOrErr for Option { + /// Convert the [Option] to a new [Error] with [ErrorType] and context if None, Ok otherwise. + /// + /// This is a shortcut for .ok_or(Error::explain()) + fn or_err(self, et: ErrorType, context: &'static str) -> Result { + self.ok_or(Error::explain(et, context)) + } + + /// Similar to to_err(), but takes a closure, which is useful for constructing String. + fn or_err_with, F: FnOnce() -> C>( + self, + et: ErrorType, + context: F, + ) -> Result { + self.ok_or_else(|| Error::explain(et, context())) + } +} + #[cfg(test)] mod tests { use super::*; @@ -586,4 +615,30 @@ mod tests { " HTTPStatus context: another cause: InternalError" ); } + + #[test] + fn test_option_some_ok() { + let m = Some(2); + let o = m.or_err(ErrorType::InternalError, "some is not an error!"); + assert_eq!(2, o.unwrap()); + + let o = m.or_err_with(ErrorType::InternalError, || "some is not an error!"); + assert_eq!(2, o.unwrap()); + } + + #[test] + fn test_option_none_err() { + let m: Option = None; + let e1 = m.or_err(ErrorType::InternalError, "none is an error!"); + assert_eq!( + format!("{}", e1.unwrap_err()), + " InternalError context: none is an error!" + ); + + let e1 = m.or_err_with(ErrorType::InternalError, || "none is an error!"); + assert_eq!( + format!("{}", e1.unwrap_err()), + " InternalError context: none is an error!" + ); + } } From be0b09b55f542682f0248b78d9d1440400c72d57 Mon Sep 17 00:00:00 2001 From: James Munns Date: Mon, 3 Jun 2024 18:26:25 +0000 Subject: [PATCH 3/3] Fix a couple of typos/grammar issues --- .bleep | 2 +- pingora-load-balancing/src/lib.rs | 6 +++--- pingora-load-balancing/src/selection/mod.rs | 6 +++++- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/.bleep b/.bleep index 9cc187c57..e14ebddfb 100644 --- a/.bleep +++ b/.bleep @@ -1 +1 @@ -3cc5b114adb93b478fb57b8b12666c4c849d1ec8 +c54d1250ab2ee9777401b0f8296d30890344cb37 diff --git a/pingora-load-balancing/src/lib.rs b/pingora-load-balancing/src/lib.rs index 60e30ed46..c950c85cd 100644 --- a/pingora-load-balancing/src/lib.rs +++ b/pingora-load-balancing/src/lib.rs @@ -100,7 +100,7 @@ impl std::net::ToSocketAddrs for Backend { /// [Backends] is a collection of [Backend]s. /// /// It includes a service discovery method (static or dynamic) to discover all -/// the available backends as well as an optionally health check method to probe the liveness +/// the available backends as well as an optional health check method to probe the liveness /// of each backend. pub struct Backends { discovery: Box, @@ -207,9 +207,9 @@ impl Backends { Ok(self.do_update(new_backends, enablement)) } - /// Run health check on all the backend if it is set. + /// Run health check on all backends if it is set. /// - /// When `parallel: true`, all the backends are checked in parallel instead of sequentially + /// When `parallel: true`, all backends are checked in parallel instead of sequentially pub async fn run_health_check(&self, parallel: bool) { use crate::health_check::HealthCheck; use log::{info, warn}; diff --git a/pingora-load-balancing/src/selection/mod.rs b/pingora-load-balancing/src/selection/mod.rs index d7b7e9bcf..88a6ea632 100644 --- a/pingora-load-balancing/src/selection/mod.rs +++ b/pingora-load-balancing/src/selection/mod.rs @@ -58,8 +58,12 @@ pub trait SelectionAlgorithm { fn next(&self, key: &[u8]) -> u64; } -/// [FVN](https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function) hashing +/// [FNV](https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function) hashing /// on weighted backends +pub type FNVHash = Weighted; + +/// Alias of [`FNVHash`] for backwards compatibility until the next breaking change +#[doc(hidden)] pub type FVNHash = Weighted; /// Random selection on weighted backends pub type Random = Weighted;