refactor
This commit is contained in:
parent
2705db6b91
commit
e259e0b588
5 changed files with 52 additions and 60 deletions
|
|
@ -112,13 +112,16 @@ impl LoadBalanceWithPointer for LoadBalanceSticky {
|
||||||
}
|
}
|
||||||
Some(context) => {
|
Some(context) => {
|
||||||
let server_id = &context.sticky_cookie.value.value;
|
let server_id = &context.sticky_cookie.value.value;
|
||||||
if let Some(server_index) = self.get_server_index_from_id(server_id) {
|
self.get_server_index_from_id(server_id).map_or_else(
|
||||||
debug!("Valid sticky cookie: id={}, index={}", server_id, server_index);
|
|| {
|
||||||
server_index
|
debug!("Invalid sticky cookie: id={}", server_id);
|
||||||
} else {
|
self.simple_increment_ptr()
|
||||||
debug!("Invalid sticky cookie: id={}", server_id);
|
},
|
||||||
self.simple_increment_ptr()
|
|server_index| {
|
||||||
}
|
debug!("Valid sticky cookie: id={}, index={}", server_id, server_index);
|
||||||
|
server_index
|
||||||
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -72,27 +72,22 @@ impl PathManager {
|
||||||
.inner
|
.inner
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|(route_bytes, _)| {
|
.filter(|(route_bytes, _)| {
|
||||||
match path_name.starts_with(route_bytes) {
|
path_name.starts_with(route_bytes) && {
|
||||||
true => {
|
route_bytes.len() == 1 // route = '/', i.e., default
|
||||||
route_bytes.len() == 1 // route = '/', i.e., default
|
|| path_name.get(route_bytes.len()).map_or(
|
||||||
|| match path_name.get(route_bytes.len()) {
|
true, // exact case
|
||||||
None => true, // exact case
|
|p| p == &b'/'
|
||||||
Some(p) => p == &b'/', // sub-path case
|
) // sub-path case
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => false,
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.max_by_key(|(route_bytes, _)| route_bytes.len());
|
.max_by_key(|(route_bytes, _)| route_bytes.len());
|
||||||
if let Some((path, u)) = matched_upstream {
|
matched_upstream.map(|(path, u)| {
|
||||||
debug!(
|
debug!(
|
||||||
"Found upstream: {:?}",
|
"Found upstream: {:?}",
|
||||||
path.try_into().unwrap_or_else(|_| "<none>".to_string())
|
path.try_into().unwrap_or_else(|_| "<none>".to_string())
|
||||||
);
|
);
|
||||||
Some(u)
|
u
|
||||||
} else {
|
})
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -211,14 +206,15 @@ impl UpstreamCandidatesBuilder {
|
||||||
}
|
}
|
||||||
/// Set the activated upstream options defined in [[UpstreamOption]]
|
/// Set the activated upstream options defined in [[UpstreamOption]]
|
||||||
pub fn options(&mut self, v: &Option<Vec<String>>) -> &mut Self {
|
pub fn options(&mut self, v: &Option<Vec<String>>) -> &mut Self {
|
||||||
let opts = if let Some(opts) = v {
|
let opts = v.as_ref().map_or_else(
|
||||||
opts
|
|| Default::default(),
|
||||||
.iter()
|
|opts| {
|
||||||
.filter_map(|str| UpstreamOption::try_from(str.as_str()).ok())
|
opts
|
||||||
.collect::<HashSet<UpstreamOption>>()
|
.iter()
|
||||||
} else {
|
.filter_map(|str| UpstreamOption::try_from(str.as_str()).ok())
|
||||||
Default::default()
|
.collect::<HashSet<UpstreamOption>>()
|
||||||
};
|
},
|
||||||
|
);
|
||||||
self.options = Some(opts);
|
self.options = Some(opts);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
|
||||||
16
rpxy-lib/src/forwarder/cache/cache_main.rs
vendored
16
rpxy-lib/src/forwarder/cache/cache_main.rs
vendored
|
|
@ -185,9 +185,7 @@ impl RpxyCache {
|
||||||
let cache_key = derive_cache_key_from_uri(req.uri());
|
let cache_key = derive_cache_key_from_uri(req.uri());
|
||||||
|
|
||||||
// First check cache chance
|
// First check cache chance
|
||||||
let Ok(Some(cached_object)) = self.inner.get(&cache_key) else {
|
let cached_object = self.inner.get(&cache_key).ok()??;
|
||||||
return None;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Secondly check the cache freshness as an HTTP message
|
// Secondly check the cache freshness as an HTTP message
|
||||||
let now = SystemTime::now();
|
let now = SystemTime::now();
|
||||||
|
|
@ -451,10 +449,10 @@ impl LruCacheManager {
|
||||||
|
|
||||||
/// Push an entry
|
/// Push an entry
|
||||||
fn push(&self, cache_key: &str, cache_object: &CacheObject) -> CacheResult<Option<(String, CacheObject)>> {
|
fn push(&self, cache_key: &str, cache_object: &CacheObject) -> CacheResult<Option<(String, CacheObject)>> {
|
||||||
let Ok(mut lock) = self.inner.lock() else {
|
let mut lock = self.inner.lock().map_err(|_| {
|
||||||
error!("Failed to acquire mutex lock for writing cache entry");
|
error!("Failed to acquire mutex lock for writing cache entry");
|
||||||
return Err(CacheError::FailedToAcquiredMutexLockForCache);
|
CacheError::FailedToAcquiredMutexLockForCache
|
||||||
};
|
})?;
|
||||||
let res = Ok(lock.push(cache_key.to_string(), cache_object.clone()));
|
let res = Ok(lock.push(cache_key.to_string(), cache_object.clone()));
|
||||||
// This may be inconsistent with the actual number of entries
|
// This may be inconsistent with the actual number of entries
|
||||||
self.cnt.store(lock.len(), Ordering::Relaxed);
|
self.cnt.store(lock.len(), Ordering::Relaxed);
|
||||||
|
|
@ -463,10 +461,10 @@ impl LruCacheManager {
|
||||||
|
|
||||||
/// Get an entry
|
/// Get an entry
|
||||||
fn get(&self, cache_key: &str) -> CacheResult<Option<CacheObject>> {
|
fn get(&self, cache_key: &str) -> CacheResult<Option<CacheObject>> {
|
||||||
let Ok(mut lock) = self.inner.lock() else {
|
let mut lock = self.inner.lock().map_err(|_| {
|
||||||
error!("Mutex can't be locked for checking cache entry");
|
error!("Mutex can't be locked for checking cache entry");
|
||||||
return Err(CacheError::FailedToAcquiredMutexLockForCheck);
|
CacheError::FailedToAcquiredMutexLockForCheck
|
||||||
};
|
})?;
|
||||||
let Some(cached_object) = lock.get(cache_key) else {
|
let Some(cached_object) = lock.get(cache_key) else {
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -70,13 +70,15 @@ where
|
||||||
|
|
||||||
// Add te: trailer if contained in original request
|
// Add te: trailer if contained in original request
|
||||||
let contains_te_trailers = {
|
let contains_te_trailers = {
|
||||||
if let Some(te) = req.headers().get(header::TE) {
|
req
|
||||||
te.as_bytes()
|
.headers()
|
||||||
.split(|v| v == &b',' || v == &b' ')
|
.get(header::TE)
|
||||||
.any(|x| x == "trailers".as_bytes())
|
.map(|te| {
|
||||||
} else {
|
te.as_bytes()
|
||||||
false
|
.split(|v| v == &b',' || v == &b' ')
|
||||||
}
|
.any(|x| x == "trailers".as_bytes())
|
||||||
|
})
|
||||||
|
.unwrap_or(false)
|
||||||
};
|
};
|
||||||
|
|
||||||
let original_uri = req.uri().to_string();
|
let original_uri = req.uri().to_string();
|
||||||
|
|
@ -136,11 +138,7 @@ where
|
||||||
let new_uri = Uri::builder()
|
let new_uri = Uri::builder()
|
||||||
.scheme(upstream_chosen.uri.scheme().unwrap().as_str())
|
.scheme(upstream_chosen.uri.scheme().unwrap().as_str())
|
||||||
.authority(upstream_chosen.uri.authority().unwrap().as_str());
|
.authority(upstream_chosen.uri.authority().unwrap().as_str());
|
||||||
let org_pq = match req.uri().path_and_query() {
|
let org_pq = req.uri().path_and_query().map(|pq| pq.as_str()).unwrap_or("/").as_bytes();
|
||||||
Some(pq) => pq.to_string(),
|
|
||||||
None => "/".to_string(),
|
|
||||||
}
|
|
||||||
.into_bytes();
|
|
||||||
|
|
||||||
// replace some parts of path if opt_replace_path is enabled for chosen upstream
|
// replace some parts of path if opt_replace_path is enabled for chosen upstream
|
||||||
let new_pq = match &upstream_candidates.replace_path {
|
let new_pq = match &upstream_candidates.replace_path {
|
||||||
|
|
@ -155,7 +153,7 @@ where
|
||||||
new_pq.extend_from_slice(&org_pq[matched_path.len()..]);
|
new_pq.extend_from_slice(&org_pq[matched_path.len()..]);
|
||||||
new_pq
|
new_pq
|
||||||
}
|
}
|
||||||
None => org_pq,
|
None => org_pq.to_vec(),
|
||||||
};
|
};
|
||||||
*req.uri_mut() = new_uri.path_and_query(new_pq).build()?;
|
*req.uri_mut() = new_uri.path_and_query(new_pq).build()?;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -236,10 +236,9 @@ pub(super) fn add_forwarding_header(
|
||||||
pub(super) fn remove_connection_header(headers: &mut HeaderMap) {
|
pub(super) fn remove_connection_header(headers: &mut HeaderMap) {
|
||||||
if let Some(values) = headers.get(header::CONNECTION) {
|
if let Some(values) = headers.get(header::CONNECTION) {
|
||||||
if let Ok(v) = values.clone().to_str() {
|
if let Ok(v) = values.clone().to_str() {
|
||||||
for m in v.split(',') {
|
let keys = v.split(',').map(|m| m.trim()).filter(|m| !m.is_empty());
|
||||||
if !m.is_empty() {
|
for m in keys {
|
||||||
headers.remove(m.trim());
|
headers.remove(m);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -274,11 +273,9 @@ pub(super) fn extract_upgrade(headers: &HeaderMap) -> Option<String> {
|
||||||
.split(',')
|
.split(',')
|
||||||
.any(|w| w.trim().eq_ignore_ascii_case(header::UPGRADE.as_str()))
|
.any(|w| w.trim().eq_ignore_ascii_case(header::UPGRADE.as_str()))
|
||||||
{
|
{
|
||||||
if let Some(u) = headers.get(header::UPGRADE) {
|
if let Some(Ok(m)) = headers.get(header::UPGRADE).map(|u| u.to_str()) {
|
||||||
if let Ok(m) = u.to_str() {
|
debug!("Upgrade in request header: {}", m);
|
||||||
debug!("Upgrade in request header: {}", m);
|
return Some(m.to_owned());
|
||||||
return Some(m.to_owned());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue