mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-06 02:00:46 +00:00
better behavior
This commit is contained in:
1 parent
6d2dafb244
commit
e89846000b
2 files changed
+22
No files matched your search
+11
@@ -47,6 +47,10 @@ pub async fn list_image_models_command(
|
||||
}
|
||||
Err(error_message) => {
|
||||
warn!("list_image_models_command failed after {} attempts: {}", MAX_ATTEMPTS, error_message);
|
||||
if let Some(stale) = any_cached_response() {
|
||||
warn!("list_image_models_command: refresh failed; serving stale cached response");
|
||||
return Ok(stale.into());
|
||||
}
|
||||
Err(error_message.into())
|
||||
}
|
||||
}
|
||||
@@ -62,6 +66,13 @@ fn cached_response() -> Option<ListImageModelsResponse> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The cached response regardless of age. Used as a fallback so that a failed
|
||||
/// refresh still returns the last-known-good data instead of an error.
|
||||
fn any_cached_response() -> Option<ListImageModelsResponse> {
|
||||
let guard = CACHE.read().ok()?;
|
||||
guard.as_ref().map(|entry| entry.response.clone())
|
||||
}
|
||||
|
||||
fn store_response(response: ListImageModelsResponse) {
|
||||
if let Ok(mut guard) = CACHE.write() {
|
||||
*guard = Some(CacheEntry { response, loaded_at: Instant::now() });
|
||||
|
||||
+11
@@ -47,6 +47,10 @@ pub async fn list_video_models_command(
|
||||
}
|
||||
Err(error_message) => {
|
||||
warn!("list_video_models_command failed after {} attempts: {}", MAX_ATTEMPTS, error_message);
|
||||
if let Some(stale) = any_cached_response() {
|
||||
warn!("list_video_models_command: refresh failed; serving stale cached response");
|
||||
return Ok(stale.into());
|
||||
}
|
||||
Err(error_message.into())
|
||||
}
|
||||
}
|
||||
@@ -62,6 +66,13 @@ fn cached_response() -> Option<ListVideoModelsResponse> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The cached response regardless of age. Used as a fallback so that a failed
|
||||
/// refresh still returns the last-known-good data instead of an error.
|
||||
fn any_cached_response() -> Option<ListVideoModelsResponse> {
|
||||
let guard = CACHE.read().ok()?;
|
||||
guard.as_ref().map(|entry| entry.response.clone())
|
||||
}
|
||||
|
||||
fn store_response(response: ListVideoModelsResponse) {
|
||||
if let Ok(mut guard) = CACHE.write() {
|
||||
*guard = Some(CacheEntry { response, loaded_at: Instant::now() });
|
||||
|
||||
Reference in new issue
Block a user