macro_rules! get_cache_store_client {
($pools:expr, $error:expr, $client:ident $code:block) => {{
let mut last_error = $error;
for pool in $pools {
let now = Instant::now();
let pool_delinquent_until = {
let pool_delinquent_until_read = pool.delinquent_until.read().unwrap();
pool_delinquent_until_read.unwrap_or(now)
};
if pool_delinquent_until <= now {
match pool.connection.get().await {
Ok(mut $client) => {
debug!("acquired cache store client at: {}", pool.target);
return $code;
}
Err(err) => {
error!(
"could not acquire cache store client from sub-pool: {}",
err
);
*pool.delinquent_until.write().unwrap() = Instant::now()
.checked_add(Duration::from_secs(APP_CONF.redis.delinquency_seconds));
last_error = $error
}
}
} else {
warn!(
"skipped acquiring delinquent cache store client from sub-pool: {}",
pool.target
);
}
}
error!("failed getting a cache store client from all pools");
Err(last_error)
}};
}