Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
IDX GitLab Automation committed Jul 3, 2024
1 parent f1cfcc3 commit 2b291cc
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions src/routing/middleware/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,19 +163,25 @@ pub async fn middleware(

select! {
_ = mutex.lock() => {
// check again if key is cached, yes => remove sync_entry from the map, notify.notify_waiters(), return the response.
// no => executed response and cache it
// remove sync_entry from the map, notify.notify_waiters(), return the response.
}
_ = notified => {
// Another parallel request finished earlier and cached the response.
// Get cached response
}
_ = timeout(PROXY_LOCK_TIMEOUT, async {
sleep(2 * PROXY_LOCK_TIMEOUT).await;
}) => {
// Execute the request and cache the response.
let response = execute_request(request, next, cache.clone(), cache_key.clone()).await;
notify.notify_waiters();
return response;
}
_ = notified => {}
_ = sleep(PROXY_LOCK_TIMEOUT) => {}
}
return execute_request(request, next, cache, cache_key).await;
}

async fn execute_request(
request: Request,
next: Next,
cache: Arc<Cache>,
cache_key: CacheKey,
) -> Result<Response, ErrorCause> {
// Use cached response if found.
if let Some(full_response) = cache.get(&cache_key).await {
return Ok(CacheStatus::Hit.with_response(from_full_response(full_response)));
}

// If response is not cached, we propagate request as is further.
Expand Down

0 comments on commit 2b291cc

Please sign in to comment.