Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weekly open source sync #37

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .bleep
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3c6f56ba2c6cd48aa5f4edd442090e95739d15fc
c54d1250ab2ee9777401b0f8296d30890344cb37
13 changes: 9 additions & 4 deletions pingora-core/src/upstreams/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<Self> {
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
Expand Down
55 changes: 55 additions & 0 deletions pingora-error/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,35 @@ impl<T, E> OrErr<T, E> for Result<T, E> {
}
}

/// Helper trait to convert an [Option] to an [Error] with context.
pub trait OkOrErr<T> {
fn or_err(self, et: ErrorType, context: &'static str) -> Result<T, BError>;

fn or_err_with<C: Into<ImmutStr>, F: FnOnce() -> C>(
self,
et: ErrorType,
context: F,
) -> Result<T, BError>;
}

impl<T> OkOrErr<T> for Option<T> {
/// 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<T, BError> {
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<C: Into<ImmutStr>, F: FnOnce() -> C>(
self,
et: ErrorType,
context: F,
) -> Result<T, BError> {
self.ok_or_else(|| Error::explain(et, context()))
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -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<i32> = 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!"
);
}
}
6 changes: 3 additions & 3 deletions pingora-load-balancing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn ServiceDiscovery + Send + Sync + 'static>,
Expand Down Expand Up @@ -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};
Expand Down
6 changes: 5 additions & 1 deletion pingora-load-balancing/src/selection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<fnv::FnvHasher>;

/// Alias of [`FNVHash`] for backwards compatibility until the next breaking change
#[doc(hidden)]
pub type FVNHash = Weighted<fnv::FnvHasher>;
/// Random selection on weighted backends
pub type Random = Weighted<algorithms::Random>;
Expand Down
2 changes: 1 addition & 1 deletion pingora-proxy/tests/utils/server_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ impl ProxyHttp for ExampleProxyHttp {
"/tmp/nginx-test.sock",
false,
"".to_string(),
)));
)?));
}
let port = req
.headers
Expand Down