From 8e1a0e78d1a0c0ed616d3f8d7f3b6e78e5e6168e Mon Sep 17 00:00:00 2001 From: Jonas Berlin Date: Mon, 28 Oct 2024 11:22:46 +0200 Subject: [PATCH 1/2] feat: Promote `rpxy-lib/sticky-cookie` to top-level feature .. and fix compile warnings when disabled --- rpxy-bin/Cargo.toml | 13 ++++++------- .../src/backend/load_balance/load_balance_main.rs | 2 -- rpxy-lib/src/backend/load_balance/mod.rs | 2 ++ rpxy-lib/src/message_handler/handler_main.rs | 4 ++-- rpxy-lib/src/message_handler/utils_headers.rs | 4 +++- 5 files changed, 13 insertions(+), 12 deletions(-) diff --git a/rpxy-bin/Cargo.toml b/rpxy-bin/Cargo.toml index 50173fe..aa0b7b7 100644 --- a/rpxy-bin/Cargo.toml +++ b/rpxy-bin/Cargo.toml @@ -13,10 +13,10 @@ publish.workspace = true # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [features] -# default = ["http3-quinn", "cache", "rustls-backend", "acme", "post-quantum"] -# default = ["http3-s2n", "cache", "rustls-backend", "acme", "post-quantum"] -default = ["http3-quinn", "cache", "rustls-backend", "acme"] -# default = ["http3-s2n", "cache", "rustls-backend", "acme"] +# default = ["http3-quinn", "cache", "rustls-backend", "acme", "sticky-cookie", "post-quantum"] +# default = ["http3-s2n", "cache", "rustls-backend", "acme", "sticky-cookie", "post-quantum"] +default = ["http3-quinn", "cache", "rustls-backend", "acme", "sticky-cookie"] +# default = ["http3-s2n", "cache", "rustls-backend", "acme", "sticky-cookie"] http3-quinn = ["rpxy-lib/http3-quinn"] http3-s2n = ["rpxy-lib/http3-s2n"] native-tls-backend = ["rpxy-lib/native-tls-backend"] @@ -25,11 +25,10 @@ webpki-roots = ["rpxy-lib/webpki-roots"] cache = ["rpxy-lib/cache"] acme = ["rpxy-lib/acme", "rpxy-acme"] post-quantum = ["rpxy-lib/post-quantum"] +sticky-cookie = ["rpxy-lib/sticky-cookie"] [dependencies] -rpxy-lib = { path = "../rpxy-lib/", default-features = false, features = [ - "sticky-cookie", -] } +rpxy-lib = { path = "../rpxy-lib/", default-features = false } mimalloc = { version = "*", default-features = false } anyhow = "1.0.91" diff --git a/rpxy-lib/src/backend/load_balance/load_balance_main.rs b/rpxy-lib/src/backend/load_balance/load_balance_main.rs index 0b3eff8..9cce980 100644 --- a/rpxy-lib/src/backend/load_balance/load_balance_main.rs +++ b/rpxy-lib/src/backend/load_balance/load_balance_main.rs @@ -131,6 +131,4 @@ impl LoadBalance { pub struct LoadBalanceContext { #[cfg(feature = "sticky-cookie")] pub sticky_cookie: StickyCookie, - #[cfg(not(feature = "sticky-cookie"))] - pub sticky_cookie: (), } diff --git a/rpxy-lib/src/backend/load_balance/mod.rs b/rpxy-lib/src/backend/load_balance/mod.rs index 38d312b..c32ca65 100644 --- a/rpxy-lib/src/backend/load_balance/mod.rs +++ b/rpxy-lib/src/backend/load_balance/mod.rs @@ -4,6 +4,7 @@ mod load_balance_sticky; #[cfg(feature = "sticky-cookie")] mod sticky_cookie; +#[cfg(feature = "sticky-cookie")] use super::upstream::Upstream; use thiserror::Error; @@ -16,6 +17,7 @@ pub use load_balance_sticky::LoadBalanceStickyBuilder; pub use sticky_cookie::{StickyCookie, StickyCookieValue}; /// Result type for load balancing +#[cfg(feature = "sticky-cookie")] type LoadBalanceResult = std::result::Result; /// Describes things that can go wrong in the Load Balance #[derive(Debug, Error)] diff --git a/rpxy-lib/src/message_handler/handler_main.rs b/rpxy-lib/src/message_handler/handler_main.rs index 4b324df..2b803a8 100644 --- a/rpxy-lib/src/message_handler/handler_main.rs +++ b/rpxy-lib/src/message_handler/handler_main.rs @@ -6,7 +6,7 @@ use super::{ utils_request::InspectParseHost, }; use crate::{ - backend::{BackendAppManager, LoadBalanceContext}, + backend::BackendAppManager, error::*, forwarder::{ForwardRequest, Forwarder}, globals::Globals, @@ -25,7 +25,7 @@ use tokio::io::copy_bidirectional; /// Context object to handle sticky cookies at HTTP message handler pub(super) struct HandlerContext { #[cfg(feature = "sticky-cookie")] - pub(super) context_lb: Option, + pub(super) context_lb: Option, #[cfg(not(feature = "sticky-cookie"))] pub(super) context_lb: Option<()>, } diff --git a/rpxy-lib/src/message_handler/utils_headers.rs b/rpxy-lib/src/message_handler/utils_headers.rs index d058f88..afe6463 100644 --- a/rpxy-lib/src/message_handler/utils_headers.rs +++ b/rpxy-lib/src/message_handler/utils_headers.rs @@ -3,7 +3,7 @@ use crate::{ backend::{UpstreamCandidates, UpstreamOption}, log::*, }; -use anyhow::{anyhow, ensure, Result}; +use anyhow::{anyhow, Result}; use bytes::BufMut; use http::{header, HeaderMap, HeaderName, HeaderValue, Uri}; use std::{borrow::Cow, net::SocketAddr}; @@ -22,6 +22,8 @@ pub(super) fn takeout_sticky_cookie_lb_context( headers: &mut HeaderMap, expected_cookie_name: &str, ) -> Result> { + use anyhow::ensure; + let mut headers_clone = headers.clone(); match headers_clone.entry(header::COOKIE) { From a52f74c434e829885996ef10f9111c45d1fa72aa Mon Sep 17 00:00:00 2001 From: Jun Kurihara Date: Tue, 3 Jun 2025 14:59:37 +0900 Subject: [PATCH 2/2] refactor --- rpxy-bin/Cargo.toml | 18 ++++++++++++++++-- rpxy-lib/src/message_handler/handler_main.rs | 7 ++----- rpxy-lib/src/message_handler/utils_headers.rs | 6 ++---- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/rpxy-bin/Cargo.toml b/rpxy-bin/Cargo.toml index dfff39f..fe48b48 100644 --- a/rpxy-bin/Cargo.toml +++ b/rpxy-bin/Cargo.toml @@ -13,8 +13,22 @@ publish.workspace = true # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [features] -default = ["http3-quinn", "cache", "rustls-backend", "sticky-cookie", "acme", "post-quantum"] -# default = ["http3-s2n", "cache", "rustls-backend", "sticky-cookie", "acme", "post-quantum"] +default = [ + "http3-quinn", + "cache", + "rustls-backend", + "sticky-cookie", + "acme", + "post-quantum", +] +# default = [ +# "http3-s2n", +# "cache", +# "rustls-backend", +# "sticky-cookie", +# "acme", +# "post-quantum", +# ] http3-quinn = ["rpxy-lib/http3-quinn"] http3-s2n = ["rpxy-lib/http3-s2n"] native-tls-backend = ["rpxy-lib/native-tls-backend"] diff --git a/rpxy-lib/src/message_handler/handler_main.rs b/rpxy-lib/src/message_handler/handler_main.rs index 1b3663e..d4fd53c 100644 --- a/rpxy-lib/src/message_handler/handler_main.rs +++ b/rpxy-lib/src/message_handler/handler_main.rs @@ -6,7 +6,7 @@ use super::{ utils_request::InspectParseHost, }; use crate::{ - backend::BackendAppManager, + backend::{BackendAppManager, LoadBalanceContext}, error::*, forwarder::{ForwardRequest, Forwarder}, globals::Globals, @@ -24,10 +24,7 @@ use tokio::io::copy_bidirectional; #[derive(Debug)] /// Context object to handle sticky cookies at HTTP message handler pub(super) struct HandlerContext { - #[cfg(feature = "sticky-cookie")] - pub(super) context_lb: Option, - #[cfg(not(feature = "sticky-cookie"))] - pub(super) context_lb: Option<()>, + pub(super) context_lb: Option, } #[derive(Clone, Builder)] diff --git a/rpxy-lib/src/message_handler/utils_headers.rs b/rpxy-lib/src/message_handler/utils_headers.rs index 354fa6e..fe351d9 100644 --- a/rpxy-lib/src/message_handler/utils_headers.rs +++ b/rpxy-lib/src/message_handler/utils_headers.rs @@ -3,7 +3,7 @@ use crate::{ backend::{UpstreamCandidates, UpstreamOption}, log::*, }; -use anyhow::{Result, anyhow, ensure}; +use anyhow::{Result, anyhow}; use bytes::BufMut; use http::{HeaderMap, HeaderName, HeaderValue, Uri, header}; use std::{borrow::Cow, net::SocketAddr}; @@ -22,8 +22,6 @@ pub(super) fn takeout_sticky_cookie_lb_context( headers: &mut HeaderMap, expected_cookie_name: &str, ) -> Result> { - use anyhow::ensure; - let mut headers_clone = headers.clone(); match headers_clone.entry(header::COOKIE) { @@ -37,7 +35,7 @@ pub(super) fn takeout_sticky_cookie_lb_context( if sticky_cookies.is_empty() { return Ok(None); } - ensure!(sticky_cookies.len() == 1, "Invalid cookie: Multiple sticky cookie values"); + anyhow::ensure!(sticky_cookies.len() == 1, "Invalid cookie: Multiple sticky cookie values"); let cookies_passed_to_upstream = without_sticky_cookies.join("; "); let cookie_passed_to_lb = sticky_cookies.first().unwrap();