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

feat: cache hyper 1.0 #127

Merged
merged 12 commits into from
Dec 12, 2023
13 changes: 7 additions & 6 deletions rpxy-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ http3-s2n = [
sticky-cookie = ["base64", "sha2", "chrono"]
native-tls-backend = ["hyper-tls"]
rustls-backend = []
cache = [] #"http-cache-semantics", "lru"]
cache = ["http-cache-semantics", "lru", "sha2", "base64"]
native-roots = [] #"hyper-rustls/native-tokio"]

[dependencies]
Expand All @@ -41,6 +41,7 @@ tokio = { version = "1.34.0", default-features = false, features = [
"macros",
"fs",
] }
pin-project-lite = "0.2.13"
async-trait = "0.1.74"

# Error handling
Expand All @@ -66,7 +67,7 @@ hyper-tls = { version = "0.6.0", features = ["alpn"], optional = true }

# tls and cert management for server
hot_reload = "0.1.4"
rustls = { version = "0.21.9", default-features = false }
rustls = { version = "0.21.10", default-features = false }
tokio-rustls = { version = "0.24.1", features = ["early-data"] }
webpki = "0.22.4"
x509-parser = "0.15.1"
Expand All @@ -87,9 +88,10 @@ s2n-quic-rustls = { version = "0.32.0", optional = true }
# for UDP socket wit SO_REUSEADDR when h3 with quinn
socket2 = { version = "0.5.5", features = ["all"], optional = true }

# # cache
# http-cache-semantics = { path = "../submodules/rusty-http-cache-semantics/", optional = true }
# lru = { version = "0.12.1", optional = true }
# cache
http-cache-semantics = { path = "../submodules/rusty-http-cache-semantics/", optional = true }
lru = { version = "0.12.1", optional = true }
sha2 = { version = "0.10.8", default-features = false, optional = true }

# cookie handling for sticky cookie
chrono = { version = "0.4.31", default-features = false, features = [
Expand All @@ -98,7 +100,6 @@ chrono = { version = "0.4.31", default-features = false, features = [
"clock",
], optional = true }
base64 = { version = "0.21.5", optional = true }
sha2 = { version = "0.10.8", default-features = false, optional = true }


[dev-dependencies]
Expand Down
7 changes: 4 additions & 3 deletions rpxy-lib/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ mod load_balance;
mod upstream;
mod upstream_opts;

// #[cfg(feature = "sticky-cookie")]
// pub use self::load_balance::{StickyCookie, StickyCookieValue};
#[cfg(feature = "sticky-cookie")]
pub(crate) use self::load_balance::{StickyCookie, StickyCookieValue};
#[allow(unused)]
pub(crate) use self::{
load_balance::{LoadBalance, LoadBalanceContext, StickyCookie, StickyCookieValue},
load_balance::{LoadBalance, LoadBalanceContext},
upstream::{PathManager, Upstream, UpstreamCandidates},
upstream_opts::UpstreamOption,
};
Expand Down
4 changes: 2 additions & 2 deletions rpxy-lib/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ pub const RESPONSE_HEADER_SERVER: &str = "rpxy";
pub const TCP_LISTEN_BACKLOG: u32 = 1024;
// pub const HTTP_LISTEN_PORT: u16 = 8080;
// pub const HTTPS_LISTEN_PORT: u16 = 8443;
pub const PROXY_TIMEOUT_SEC: u64 = 60;
pub const UPSTREAM_TIMEOUT_SEC: u64 = 60;
pub const PROXY_IDLE_TIMEOUT_SEC: u64 = 20;
pub const UPSTREAM_IDLE_TIMEOUT_SEC: u64 = 20;
pub const TLS_HANDSHAKE_TIMEOUT_SEC: u64 = 15; // default as with firefox browser
pub const MAX_CLIENTS: usize = 512;
pub const MAX_CONCURRENT_STREAMS: u32 = 64;
Expand Down
7 changes: 7 additions & 0 deletions rpxy-lib/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ pub enum RpxyError {
HyperIncomingLikeNewClosed,
#[error("New body write aborted")]
HyperNewBodyWriteAborted,
#[error("Hyper error in serving request or response body type: {0}")]
HyperBodyError(#[from] hyper::Error),

// http/3 errors
#[cfg(any(feature = "http3-quinn", feature = "http3-s2n"))]
Expand Down Expand Up @@ -88,6 +90,11 @@ pub enum RpxyError {
#[error("Unsupported upstream option")]
UnsupportedUpstreamOption,

// Cache error map
#[cfg(feature = "cache")]
#[error("Cache error: {0}")]
CacheError(#[from] crate::forwarder::CacheError),

// Others
#[error("Infallible")]
Infallible(#[from] std::convert::Infallible),
Expand Down
50 changes: 50 additions & 0 deletions rpxy-lib/src/forwarder/cache/cache_error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use thiserror::Error;

pub type CacheResult<T> = std::result::Result<T, CacheError>;

/// Describes things that can go wrong in the Rpxy
#[derive(Debug, Error)]
pub enum CacheError {
// Cache errors,
#[error("Invalid null request and/or response")]
NullRequestOrResponse,

#[error("Failed to write byte buffer")]
FailedToWriteByteBufferForCache,

#[error("Failed to acquire mutex lock for cache")]
FailedToAcquiredMutexLockForCache,

#[error("Failed to acquire mutex lock for check")]
FailedToAcquiredMutexLockForCheck,

#[error("Failed to create file cache")]
FailedToCreateFileCache,

#[error("Failed to write file cache")]
FailedToWriteFileCache,

#[error("Failed to open cache file")]
FailedToOpenCacheFile,

#[error("Too large to cache")]
TooLargeToCache,

#[error("Failed to cache bytes: {0}")]
FailedToCacheBytes(String),

#[error("Failed to send frame to cache {0}")]
FailedToSendFrameToCache(String),

#[error("Failed to send frame from file cache {0}")]
FailedToSendFrameFromCache(String),

#[error("Failed to remove cache file: {0}")]
FailedToRemoveCacheFile(String),

#[error("Invalid cache target")]
InvalidCacheTarget,

#[error("Hash mismatched in cache file")]
HashMismatchedInCacheFile,
}
Loading