aKode fase 1 (parziale): niente Statsig, analytics, upload Sentry, update check e announcement remoti

This commit is contained in:
Andrea
2026-11-09 11:15:06 +01:00
parent 7874033c81
commit 5b5b83964d
10 changed files with 45 additions and 165 deletions

View File

@@ -4,7 +4,6 @@ use std::time::Duration;
use std::time::Instant;
use std::time::SystemTime;
use anyhow::Context;
use anyhow::Result;
use bytes::Bytes;
use codex_http_client::RequestBuilder;
@@ -51,79 +50,18 @@ pub(super) fn envelope_request(
}
/// Send an already-serialized envelope within the report's shared deadline.
///
/// aKode: feedback is never uploaded to a remote service (upstream sent it to
/// Sentry). The signature is kept so callers compile; it always fails.
pub(super) async fn send_envelope(
client_pool: &RouteAwareClientPool,
dsn: &Dsn,
body: Vec<u8>,
kind: EnvelopeKind,
deadline: Instant,
rate_limited: &mut bool,
_client_pool: &RouteAwareClientPool,
_dsn: &Dsn,
_body: Vec<u8>,
_kind: EnvelopeKind,
_deadline: Instant,
_rate_limited: &mut bool,
) -> Result<StatusCode> {
let body = Bytes::from(body);
// Retain the exact request bytes for at most two diagnostic retries. Never replay the core.
let mut retry_delays = [250, 500].map(Duration::from_millis).into_iter();
let response = loop {
let timeout = deadline.saturating_duration_since(Instant::now());
anyhow::ensure!(!timeout.is_zero(), "feedback upload deadline exceeded");
let response = envelope_request(client_pool, dsn, body.clone(), timeout)
.send()
.await;
if response.as_ref().is_ok_and(|response| {
sentry_rate_limit_delay(response.headers()).is_some_and(|delay| !delay.is_zero())
|| (response.status().is_success()
&& response.headers().get("Retry-After").is_some_and(|value| {
parse_retry_after(value.to_str().unwrap_or_default())
.is_none_or(|delay| !delay.is_zero())
}))
}) {
// Even accepted requests can impose a quota on events or attachments.
// https://develop.sentry.dev/sdk/foundations/transport/rate-limiting/
*rate_limited = true;
break response;
}
let retryable = match &response {
Ok(response) => matches!(response.status().as_u16(), 408 | 429 | 500..=599),
Err(error) => {
error.is_timeout()
|| (error.failure_class().is_none()
&& (error.is_connect() || error.is_request() || error.is_body()))
}
};
if matches!(kind, EnvelopeKind::Event) || !retryable {
break response;
}
let retry_delay = retry_delays.next();
if response.as_ref().is_ok_and(|response| {
response.status() == StatusCode::TOO_MANY_REQUESTS
&& (retry_delay.is_none() || !response.headers().contains_key("Retry-After"))
}) {
// Never let later files bypass an exhausted rate limit.
*rate_limited = true;
break response;
}
let Some(retry_delay) = retry_delay else {
break response;
};
let retry_after = response
.as_ref()
.ok()
.and_then(|response| response.headers().get("Retry-After"))
.map(|value| {
// An invalid cooldown is not permission to send immediately.
parse_retry_after(value.to_str().unwrap_or_default())
.unwrap_or_else(|| deadline.saturating_duration_since(Instant::now()))
});
let delay = retry_delay.max(retry_after.unwrap_or_default());
if delay >= deadline.saturating_duration_since(Instant::now()) {
// Leave long cooldowns for a later submission, including its remaining files.
*rate_limited = true;
break response;
}
tokio::time::sleep(delay).await;
};
response
.map(|response| response.status())
.context("failed to upload feedback to Sentry")
anyhow::bail!("feedback upload is disabled in aKode; use the local save option instead")
}
pub(super) fn sentry_rate_limit_delay(headers: &HeaderMap) -> Option<Duration> {