perf: queue request candidate persistence

This commit is contained in:
elky
2026-06-22 02:49:17 +08:00
parent f75894acbb
commit 5b7805181b
11 changed files with 1143 additions and 51 deletions
@@ -113,9 +113,67 @@ impl AppState {
&self,
candidate: candidates::UpsertRequestCandidateRecord,
) -> Result<Option<candidates::StoredRequestCandidate>, GatewayError> {
if let Some(queue) = self.request_candidate_queue.as_ref() {
let stored = stored_request_candidate_from_upsert(&candidate)?;
queue
.enqueue_or_fallback(candidate)
.await
.map_err(|err| GatewayError::Internal(err.to_string()))?;
return Ok(Some(stored));
}
self.data
.upsert_request_candidate(candidate)
.await
.map_err(|err| GatewayError::Internal(err.to_string()))
}
}
fn stored_request_candidate_from_upsert(
candidate: &candidates::UpsertRequestCandidateRecord,
) -> Result<candidates::StoredRequestCandidate, GatewayError> {
candidate
.validate()
.map_err(|err| GatewayError::Internal(err.to_string()))?;
candidates::StoredRequestCandidate::new(
candidate.id.clone(),
candidate.request_id.clone(),
candidate.user_id.clone(),
candidate.api_key_id.clone(),
candidate.username.clone(),
candidate.api_key_name.clone(),
candidate.candidate_index.try_into().unwrap_or(i32::MAX),
candidate.retry_index.try_into().unwrap_or(i32::MAX),
candidate.provider_id.clone(),
candidate.endpoint_id.clone(),
candidate.key_id.clone(),
candidate.status,
candidate.skip_reason.clone(),
candidate.is_cached.unwrap_or(false),
candidate.status_code.map(i32::from),
candidate.error_type.clone(),
candidate.error_message.clone(),
candidate
.latency_ms
.map(|value| i32::try_from(value).unwrap_or(i32::MAX)),
candidate
.concurrent_requests
.map(|value| i32::try_from(value).unwrap_or(i32::MAX)),
candidate.extra_data.clone(),
candidate.required_capabilities.clone(),
candidate
.created_at_unix_ms
.or(candidate.started_at_unix_ms)
.or(candidate.finished_at_unix_ms)
.unwrap_or_else(crate::clock::current_unix_ms)
.try_into()
.unwrap_or(i64::MAX),
candidate
.started_at_unix_ms
.map(|value| value.try_into().unwrap_or(i64::MAX)),
candidate
.finished_at_unix_ms
.map(|value| value.try_into().unwrap_or(i64::MAX)),
)
.map_err(|err| GatewayError::Internal(err.to_string()))
}