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 2, 2024
1 parent f1cfcc3 commit cdb9ffa
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/routing/middleware/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,19 +163,27 @@ 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
let response = execute_request(request, next, cache.clone(), cache_key.clone()).await;
notify.notify_waiters();
return response;
}
_ = notified => {}
_ = timeout(PROXY_LOCK_TIMEOUT, async {
sleep(2 * PROXY_LOCK_TIMEOUT).await;
}) => {
// Execute the request and cache the response.
}
}) => {}
}
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 cdb9ffa

Please sign in to comment.