Skip to content

Commit 74d3071

Browse files
committed
fix(relay): classify script quarantine by failure type
A single cooldown duration is too coarse for Apps Script deployment failures. Quota exhaustion and account-level authorization failures recover on a much longer cadence than transient Google edge or Apps Script backend failures. Treating both classes the same either probes exhausted deployments too aggressively or removes transiently unhealthy deployments for longer than necessary. Relay failure handling now classifies script failures into two explicit quarantine classes. HTTP 429, HTTP 403, and response bodies that match quota or service-invocation limit text are treated as hard quota/account failures and quarantined for 24 hours. Google or Apps Script transient 5xx responses are treated as temporary relay failures and use the existing short cooldown window. The transient class is deliberately narrow. Generic upstream 5xx bodies such as a destination-origin bad gateway do not quarantine a script ID by themselves; the body must look like a Google, Apps Script, GFE, backend, service-unavailable, temporary, or timeout failure. This avoids punishing healthy deployments for ordinary origin-side errors that Apps Script relayed correctly. The same classifier is used across the direct relay path, h1 fallback path, tunnel single-operation path, and tunnel batch path. Quota-like errors returned inside the Apps Script JSON envelope still force the hard quarantine path even when the outer HTTP status is 200. The English and Persian guides now describe auto-quarantine as two failure classes instead of a single ten-minute blacklist. Unit coverage verifies hard quota/account classification, transient Google-edge classification, ordinary upstream 5xx pass-through, and the quarantine durations for both classes.
1 parent 40b5386 commit 74d3071

3 files changed

Lines changed: 101 additions & 24 deletions

File tree

‎docs/guide.fa.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ sni_hosts = ["www.google.com", "drive.google.com", "docs.google.com"]
350350
| Connection pool | TTL ۴۵ ثانیه، حداکثر ۲۰ idle |
351351
| رمزگشایی gzip | اتوماتیک |
352352
| چند اسکریپت | چرخش round-robin |
353-
| Blacklist خودکار | روی خطای 429 / quota، با cooldown ۱۰ دقیقه |
353+
| قرنطینهٔ خودکار اسکریپت | خطاهای quota/account برای ۲۴ ساعت؛ خطاهای گذرای relay با cooldown کوتاه |
354354
| کش پاسخ | ۵۰ مگابایت، FIFO + TTL، آگاه از `Cache-Control: max-age`، heuristic برای static asset |
355355
| Coalescing | GETهای یکسان همزمان یک fetch upstream را به اشتراک می‌گذارند |
356356
| تونل بازنویسی SNI | مستقیم به لبهٔ گوگل (بدون رله) برای `google.com`، `youtube.com`، `youtu.be`، `youtube-nocookie.com`، `fonts.googleapis.com` — دامنه‌های اضافی از فیلد `hosts` |

‎docs/guide.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ This port focuses on the **`apps_script` mode** — the only one that reliably w
346346
- [x] Connection pooling (45 s TTL, max 20 idle)
347347
- [x] Gzip response decoding
348348
- [x] Multi-script round-robin
349-
- [x] Auto-blacklist failing scripts on 429 / quota errors (10 min cooldown)
349+
- [x] Auto-quarantine failing scripts: quota/account failures for 24 h, transient relay failures for a short cooldown
350350
- [x] Response cache (50 MB, FIFO + TTL, `Cache-Control: max-age` aware, heuristics for static assets)
351351
- [x] Request coalescing: concurrent identical GETs share one upstream fetch
352352
- [x] SNI-rewrite tunnels for `google.com`, `youtube.com`, `youtu.be`, `youtube-nocookie.com`, `fonts.googleapis.com`, configurable via `hosts` map

‎src/domain_fronter.rs‎

Lines changed: 99 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,30 @@ impl HostStat {
460460
}
461461
}
462462

463-
const BLACKLIST_COOLDOWN_SECS: u64 = 600;
463+
const TRANSIENT_SCRIPT_COOLDOWN_SECS: u64 = 600;
464+
const HARD_SCRIPT_QUARANTINE_SECS: u64 = 24 * 60 * 60;
465+
466+
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
467+
enum ScriptQuarantine {
468+
Hard,
469+
Transient,
470+
}
471+
472+
impl ScriptQuarantine {
473+
fn cooldown(self) -> Duration {
474+
match self {
475+
ScriptQuarantine::Hard => Duration::from_secs(HARD_SCRIPT_QUARANTINE_SECS),
476+
ScriptQuarantine::Transient => Duration::from_secs(TRANSIENT_SCRIPT_COOLDOWN_SECS),
477+
}
478+
}
479+
480+
fn label(self) -> &'static str {
481+
match self {
482+
ScriptQuarantine::Hard => "hard quota/account quarantine",
483+
ScriptQuarantine::Transient => "transient relay cooldown",
484+
}
485+
}
486+
}
464487

465488
/// Auto-blacklist defaults are now per-instance fields on `DomainFronter`,
466489
/// driven by `Config::auto_blacklist_strikes` / `_window_secs` /
@@ -849,11 +872,11 @@ impl DomainFronter {
849872
picked
850873
}
851874

852-
fn blacklist_script(&self, script_id: &str, reason: &str) {
875+
fn quarantine_script(&self, script_id: &str, quarantine: ScriptQuarantine, reason: &str) {
853876
self.blacklist_script_for(
854877
script_id,
855-
Duration::from_secs(BLACKLIST_COOLDOWN_SECS),
856-
reason,
878+
quarantine.cooldown(),
879+
&format!("{}: {}", quarantine.label(), reason),
857880
);
858881
}
859882

@@ -2503,8 +2526,12 @@ impl DomainFronter {
25032526
.chars()
25042527
.take(200)
25052528
.collect::<String>();
2506-
if should_blacklist(status, &body_txt) {
2507-
self.blacklist_script(&script_id, &format!("HTTP {}", status));
2529+
if let Some(quarantine) = classify_script_failure(status, &body_txt) {
2530+
self.quarantine_script(
2531+
&script_id,
2532+
quarantine,
2533+
&format!("HTTP {}", status),
2534+
);
25082535
}
25092536
return Err(FronterError::Relay(format!(
25102537
"Apps Script HTTP {}: {}",
@@ -2514,7 +2541,7 @@ impl DomainFronter {
25142541
return parse_relay_json(&resp_body).map_err(|e| {
25152542
if let FronterError::Relay(ref msg) = e {
25162543
if looks_like_quota_error(msg) {
2517-
self.blacklist_script(&script_id, msg);
2544+
self.quarantine_script(&script_id, ScriptQuarantine::Hard, msg);
25182545
}
25192546
}
25202547
e
@@ -2612,8 +2639,12 @@ impl DomainFronter {
26122639
.chars()
26132640
.take(200)
26142641
.collect::<String>();
2615-
if should_blacklist(status, &body_txt) {
2616-
self.blacklist_script(&script_id, &format!("HTTP {}", status));
2642+
if let Some(quarantine) = classify_script_failure(status, &body_txt) {
2643+
self.quarantine_script(
2644+
&script_id,
2645+
quarantine,
2646+
&format!("HTTP {}", status),
2647+
);
26172648
}
26182649
return Err(FronterError::Relay(format!(
26192650
"Apps Script HTTP {}: {}",
@@ -2625,7 +2656,11 @@ impl DomainFronter {
26252656
Err(e) => {
26262657
if let FronterError::Relay(ref msg) = e {
26272658
if looks_like_quota_error(msg) {
2628-
self.blacklist_script(&script_id, msg);
2659+
self.quarantine_script(
2660+
&script_id,
2661+
ScriptQuarantine::Hard,
2662+
msg,
2663+
);
26292664
}
26302665
}
26312666
Err(e)
@@ -3022,8 +3057,8 @@ impl DomainFronter {
30223057
.chars()
30233058
.take(200)
30243059
.collect::<String>();
3025-
if should_blacklist(status, &body_txt) {
3026-
self.blacklist_script(script_id, &format!("HTTP {}", status));
3060+
if let Some(quarantine) = classify_script_failure(status, &body_txt) {
3061+
self.quarantine_script(script_id, quarantine, &format!("HTTP {}", status));
30273062
}
30283063
return Err(FronterError::Relay(format!(
30293064
"tunnel HTTP {}: {}",
@@ -3212,8 +3247,8 @@ impl DomainFronter {
32123247
.chars()
32133248
.take(200)
32143249
.collect::<String>();
3215-
if should_blacklist(status, &body_txt) {
3216-
self.blacklist_script(script_id, &format!("HTTP {}", status));
3250+
if let Some(quarantine) = classify_script_failure(status, &body_txt) {
3251+
self.quarantine_script(script_id, quarantine, &format!("HTTP {}", status));
32173252
}
32183253
return Err(FronterError::Relay(format!(
32193254
"batch tunnel HTTP {}: {}",
@@ -4909,11 +4944,17 @@ impl StatsSnapshot {
49094944
}
49104945
}
49114946

4912-
fn should_blacklist(status: u16, body: &str) -> bool {
4947+
fn classify_script_failure(status: u16, body: &str) -> Option<ScriptQuarantine> {
49134948
if status == 429 || status == 403 {
4914-
return true;
4949+
return Some(ScriptQuarantine::Hard);
4950+
}
4951+
if looks_like_quota_error(body) {
4952+
return Some(ScriptQuarantine::Hard);
49154953
}
4916-
looks_like_quota_error(body)
4954+
if matches!(status, 500 | 502 | 503 | 504) && looks_like_transient_script_error(body) {
4955+
return Some(ScriptQuarantine::Transient);
4956+
}
4957+
None
49174958
}
49184959

49194960
fn looks_like_quota_error(msg: &str) -> bool {
@@ -4930,6 +4971,20 @@ fn looks_like_quota_error(msg: &str) -> bool {
49304971
|| lower.contains("limit exceeded")
49314972
}
49324973

4974+
fn looks_like_transient_script_error(msg: &str) -> bool {
4975+
let lower = msg.to_ascii_lowercase();
4976+
lower.contains("google")
4977+
|| lower.contains("apps script")
4978+
|| lower.contains("script.google")
4979+
|| lower.contains("googleusercontent")
4980+
|| lower.contains("gfe")
4981+
|| lower.contains("backend error")
4982+
|| lower.contains("service unavailable")
4983+
|| lower.contains("temporary")
4984+
|| lower.contains("timeout")
4985+
|| lower.contains("timed out")
4986+
}
4987+
49334988
fn mask_script_id(id: &str) -> String {
49344989
let n = id.chars().count();
49354990
if n <= 8 {
@@ -6369,18 +6424,40 @@ hello";
63696424

63706425
#[test]
63716426
fn blacklist_heuristics() {
6372-
assert!(should_blacklist(429, ""));
6373-
assert!(should_blacklist(403, "quota"));
6374-
assert!(should_blacklist(500, "Service invoked too many times per day: urlfetch"));
6375-
assert!(!should_blacklist(200, ""));
6376-
assert!(!should_blacklist(502, "bad gateway"));
6427+
assert_eq!(classify_script_failure(429, ""), Some(ScriptQuarantine::Hard));
6428+
assert_eq!(
6429+
classify_script_failure(403, "quota"),
6430+
Some(ScriptQuarantine::Hard)
6431+
);
6432+
assert_eq!(
6433+
classify_script_failure(500, "Service invoked too many times per day: urlfetch"),
6434+
Some(ScriptQuarantine::Hard)
6435+
);
6436+
assert_eq!(classify_script_failure(502, "bad gateway"), None);
6437+
assert_eq!(
6438+
classify_script_failure(502, "Google backend error"),
6439+
Some(ScriptQuarantine::Transient)
6440+
);
6441+
assert_eq!(classify_script_failure(200, ""), None);
63776442
assert!(looks_like_quota_error("Exception: Service invoked too many times per day"));
63786443
assert!(looks_like_quota_error(
63796444
"Exception: Bandbreitenkontingent überschritten: https://example.com. Verringern Sie die Datenübertragungsrate."
63806445
));
63816446
assert!(!looks_like_quota_error("bad url"));
63826447
}
63836448

6449+
#[test]
6450+
fn script_quarantine_durations_match_failure_class() {
6451+
assert_eq!(
6452+
ScriptQuarantine::Hard.cooldown(),
6453+
Duration::from_secs(HARD_SCRIPT_QUARANTINE_SECS)
6454+
);
6455+
assert_eq!(
6456+
ScriptQuarantine::Transient.cooldown(),
6457+
Duration::from_secs(TRANSIENT_SCRIPT_COOLDOWN_SECS)
6458+
);
6459+
}
6460+
63846461
#[test]
63856462
fn mask_script_id_hides_middle() {
63866463
assert_eq!(mask_script_id("short"), "***");

0 commit comments

Comments
 (0)