Skip to content

Commit

Permalink
avoid unwanted unwraps in cache too
Browse files Browse the repository at this point in the history
  • Loading branch information
akshay111meher committed Nov 1, 2024
1 parent 0028280 commit 9e64219
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
15 changes: 10 additions & 5 deletions matching_engine/src/routes/ui_routes/dashboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,16 @@ pub async fn get_dashboard(
_local_ask_store: Data<Arc<RwLock<LocalAskStore>>>,
_local_generator_store: Data<Arc<RwLock<GeneratorStore>>>,
) -> actix_web::Result<HttpResponse> {
if let Some(response) = DASHBOARD_RESPONSE
.try_read()
.unwrap()
.get_if_valid(Duration::from_secs(10))
{
let dashboard_cache = match DASHBOARD_RESPONSE.try_read() {
Ok(data) => data,
_ => {
return Ok(HttpResponse::Locked().json(WelcomeResponse {
status: "Resource Busy".into(),
}))
}
};

if let Some(response) = dashboard_cache.get_if_valid(Duration::from_secs(10)) {
// Return the cached response if valid
return Ok(HttpResponse::Ok().json(response));
}
Expand Down
15 changes: 10 additions & 5 deletions matching_engine/src/routes/ui_routes/generators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,16 @@ pub async fn get_generators_all(
) -> actix_web::Result<HttpResponse> {
// Step 1: Check if there's a cached response (lock for reading)

if let Some(response) = GENERATOR_RESPONSE
.try_read()
.unwrap()
.get_if_valid(Duration::from_secs(10))
{
let generator_cache = match GENERATOR_RESPONSE.try_read() {
Ok(data) => data,
_ => {
return Ok(HttpResponse::Locked().json(WelcomeResponse {
status: "Resource Busy".into(),
}))
}
};

if let Some(response) = generator_cache.get_if_valid(Duration::from_secs(10)) {
// Return the cached response if valid
return Ok(HttpResponse::Ok().json(response));
}
Expand Down
15 changes: 10 additions & 5 deletions matching_engine/src/routes/ui_routes/markets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,16 @@ pub async fn total_market_info(
) -> actix_web::Result<HttpResponse> {
// Step 1: Check if there's a cached response (lock for reading)

if let Some(response) = MARKET_RESPONSE
.try_read()
.unwrap()
.get_if_valid(Duration::from_secs(10))
{
let market_cache = match MARKET_RESPONSE.try_read() {
Ok(data) => data,
_ => {
return Ok(HttpResponse::Locked().json(WelcomeResponse {
status: "Resource Busy".into(),
}))
}
};

if let Some(response) = market_cache.get_if_valid(Duration::from_secs(10)) {
// Return the cached response if valid
return Ok(HttpResponse::Ok().json(response));
}
Expand Down

0 comments on commit 9e64219

Please sign in to comment.