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) => {
|
||||
let server_id = &context.sticky_cookie.value.value;
|
||||
if let Some(server_index) = self.get_server_index_from_id(server_id) {
|
||||
debug!("Valid sticky cookie: id={}, index={}", server_id, server_index);
|
||||
server_index
|
||||
} else {
|
||||
self.get_server_index_from_id(server_id).map_or_else(
|
||||
|| {
|
||||
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
|
||||
.iter()
|
||||
.filter(|(route_bytes, _)| {
|
||||
match path_name.starts_with(route_bytes) {
|
||||
true => {
|
||||
path_name.starts_with(route_bytes) && {
|
||||
route_bytes.len() == 1 // route = '/', i.e., default
|
||||
|| match path_name.get(route_bytes.len()) {
|
||||
None => true, // exact case
|
||||
Some(p) => p == &b'/', // sub-path case
|
||||
}
|
||||
}
|
||||
_ => false,
|
||||
|| path_name.get(route_bytes.len()).map_or(
|
||||
true, // exact case
|
||||
|p| p == &b'/'
|
||||
) // sub-path case
|
||||
}
|
||||
})
|
||||
.max_by_key(|(route_bytes, _)| route_bytes.len());
|
||||
if let Some((path, u)) = matched_upstream {
|
||||
matched_upstream.map(|(path, u)| {
|
||||
debug!(
|
||||
"Found upstream: {:?}",
|
||||
path.try_into().unwrap_or_else(|_| "<none>".to_string())
|
||||
);
|
||||
Some(u)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
u
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -211,14 +206,15 @@ impl UpstreamCandidatesBuilder {
|
|||
}
|
||||
/// Set the activated upstream options defined in [[UpstreamOption]]
|
||||
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(
|
||||
|| Default::default(),
|
||||
|opts| {
|
||||
opts
|
||||
.iter()
|
||||
.filter_map(|str| UpstreamOption::try_from(str.as_str()).ok())
|
||||
.collect::<HashSet<UpstreamOption>>()
|
||||
} else {
|
||||
Default::default()
|
||||
};
|
||||
},
|
||||
);
|
||||
self.options = Some(opts);
|
||||
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());
|
||||
|
||||
// First check cache chance
|
||||
let Ok(Some(cached_object)) = self.inner.get(&cache_key) else {
|
||||
return None;
|
||||
};
|
||||
let cached_object = self.inner.get(&cache_key).ok()??;
|
||||
|
||||
// Secondly check the cache freshness as an HTTP message
|
||||
let now = SystemTime::now();
|
||||
|
|
@ -451,10 +449,10 @@ impl LruCacheManager {
|
|||
|
||||
/// Push an entry
|
||||
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");
|
||||
return Err(CacheError::FailedToAcquiredMutexLockForCache);
|
||||
};
|
||||
CacheError::FailedToAcquiredMutexLockForCache
|
||||
})?;
|
||||
let res = Ok(lock.push(cache_key.to_string(), cache_object.clone()));
|
||||
// This may be inconsistent with the actual number of entries
|
||||
self.cnt.store(lock.len(), Ordering::Relaxed);
|
||||
|
|
@ -463,10 +461,10 @@ impl LruCacheManager {
|
|||
|
||||
/// Get an entry
|
||||
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");
|
||||
return Err(CacheError::FailedToAcquiredMutexLockForCheck);
|
||||
};
|
||||
CacheError::FailedToAcquiredMutexLockForCheck
|
||||
})?;
|
||||
let Some(cached_object) = lock.get(cache_key) else {
|
||||
return Ok(None);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -70,13 +70,15 @@ where
|
|||
|
||||
// Add te: trailer if contained in original request
|
||||
let contains_te_trailers = {
|
||||
if let Some(te) = req.headers().get(header::TE) {
|
||||
req
|
||||
.headers()
|
||||
.get(header::TE)
|
||||
.map(|te| {
|
||||
te.as_bytes()
|
||||
.split(|v| v == &b',' || v == &b' ')
|
||||
.any(|x| x == "trailers".as_bytes())
|
||||
} else {
|
||||
false
|
||||
}
|
||||
})
|
||||
.unwrap_or(false)
|
||||
};
|
||||
|
||||
let original_uri = req.uri().to_string();
|
||||
|
|
@ -136,11 +138,7 @@ where
|
|||
let new_uri = Uri::builder()
|
||||
.scheme(upstream_chosen.uri.scheme().unwrap().as_str())
|
||||
.authority(upstream_chosen.uri.authority().unwrap().as_str());
|
||||
let org_pq = match req.uri().path_and_query() {
|
||||
Some(pq) => pq.to_string(),
|
||||
None => "/".to_string(),
|
||||
}
|
||||
.into_bytes();
|
||||
let org_pq = req.uri().path_and_query().map(|pq| pq.as_str()).unwrap_or("/").as_bytes();
|
||||
|
||||
// replace some parts of path if opt_replace_path is enabled for chosen upstream
|
||||
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
|
||||
}
|
||||
None => org_pq,
|
||||
None => org_pq.to_vec(),
|
||||
};
|
||||
*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) {
|
||||
if let Some(values) = headers.get(header::CONNECTION) {
|
||||
if let Ok(v) = values.clone().to_str() {
|
||||
for m in v.split(',') {
|
||||
if !m.is_empty() {
|
||||
headers.remove(m.trim());
|
||||
}
|
||||
let keys = v.split(',').map(|m| m.trim()).filter(|m| !m.is_empty());
|
||||
for m in keys {
|
||||
headers.remove(m);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -274,13 +273,11 @@ pub(super) fn extract_upgrade(headers: &HeaderMap) -> Option<String> {
|
|||
.split(',')
|
||||
.any(|w| w.trim().eq_ignore_ascii_case(header::UPGRADE.as_str()))
|
||||
{
|
||||
if let Some(u) = headers.get(header::UPGRADE) {
|
||||
if let Ok(m) = u.to_str() {
|
||||
if let Some(Ok(m)) = headers.get(header::UPGRADE).map(|u| u.to_str()) {
|
||||
debug!("Upgrade in request header: {}", m);
|
||||
return Some(m.to_owned());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue