diff --git a/README.md b/README.md index f2da0b4..fdf020e 100644 --- a/README.md +++ b/README.md @@ -197,12 +197,16 @@ context. What is *not* retried: connect failures (that state is `offline`, and h own it), any `4xx` other than the ones named, and anything after the stream head has reached the client. -If the provider names a `Retry-After` in seconds, that number wins over the backoff — -it knows when its quota reopens, we do not. Retrying earlier only spends the second -attempt on the same closed door. `retry_max_wait` (default 30 s) caps it: an hour of -waiting is not a retry, it is an outage with an announcement. HTTP-date values are -ignored (a timezone question with no upside); a `Retry-After: 0` means *now*, not "wait -zero plus backoff", so it changes nothing. Both cases are visible in the log: +If the provider names a `Retry-After`, that number wins over the backoff — it knows when +its quota reopens, we do not. Retrying earlier only spends the second attempt on the same +closed door. Both RFC 7231 forms are read: seconds and the HTTP date +(`Sun, 06 Nov 1994 08:49:37 GMT`, converted with the same civil-date arithmetic the +monthly rollover uses — `timegm` is not POSIX and a timezone has no business deciding a +retry). A date in the past means *now*, a `Retry-After: 0` changes nothing, and anything +unparseable is treated as silence rather than guessed at. + +`retry_max_wait` (default 30 s) caps whatever comes back: an hour of waiting is not a +retry, it is an outage with an announcement. The decision is visible in the log: `retry ... backoff_ms=2000 retry_after_s=2`. When the **whole chain** is offline or cooling down, failing at once is the wrong @@ -645,9 +649,11 @@ sent, because another thread may replace that entry while this one is still writ the whole suite also runs green under AddressSanitizer. The last section covers `Retry-After`: a stub that answers the first request with `503` plus `Retry-After: 2` makes junction wait 2.0 s (measured) instead of its 0.2 s -backoff, the same stub saying `60` under `retry_max_wait 1` is capped at 1.0 s, and every -decision names the provider's number in the log line. -Pass = `PASS` (227 checks), including two that would have caught the two mistakes this +backoff, the same stub saying `60` under `retry_max_wait 1` is capped at 1.0 s, an HTTP +date two seconds ahead has the same effect as the number, a date two minutes in the past +means "now" (0.2 s, just the backoff), and every decision names the provider's number in +the log line. +Pass = `PASS` (229 checks), including two that would have caught the two mistakes this design invites: a serial stub making a concurrent router look serialised, and a "waiting" comment leaking into a caller that never asked for a stream. diff --git a/config.example b/config.example index d0f74f0..7c994c4 100644 --- a/config.example +++ b/config.example @@ -20,8 +20,8 @@ queue_timeout 300 # 0 disables it. retry_backoff is per attempt (attempt n waits n × this). retry_num 1 retry_backoff 0.5 -# The provider's Retry-After wins over the backoff; this is the ceiling, so a "come back -# in an hour" does not turn a retry into an outage. 0 = no ceiling. +# The provider's Retry-After wins over the backoff (seconds or an HTTP date); this is the +# ceiling, so a "come back in an hour" does not turn a retry into an outage. 0 = none. retry_max_wait 30 # If the WHOLE chain is offline or cooling down, wait this many seconds for it to come diff --git a/examples/full-reference.conf b/examples/full-reference.conf index feb33f6..8017ab2 100644 --- a/examples/full-reference.conf +++ b/examples/full-reference.conf @@ -61,9 +61,10 @@ queue_timeout 300 retry_num 1 retry_backoff 0.5 -# If the provider names a Retry-After in seconds, that number wins over the backoff — it -# knows when its quota reopens. The ceiling keeps a "come back in an hour" from turning a -# retry into an outage: 0 means no ceiling. HTTP-date values are ignored. +# If the provider names a Retry-After (seconds or the RFC 7231 HTTP date), that wins over +# the backoff — it knows when its quota reopens. The ceiling keeps a "come back in an +# hour" from turning a retry into an outage: 0 means no ceiling. A date in the past means +# now; anything unparseable counts as silence. retry_max_wait 30 # Whole chain offline? A request may wait this long instead of getting a 503 at diff --git a/src/junction.c b/src/junction.c index 73d1bb2..ac021f5 100644 --- a/src/junction.c +++ b/src/junction.c @@ -822,6 +822,40 @@ static long days_from_civil(long y, unsigned m, unsigned d) return era * 146097L + (long)doe - 719468L; } +/* Retry-After ist entweder eine Zahl (Sekunden) oder ein HTTP-Datum. Beides wird + * gelesen — das Datum über days_from_civil statt timegm, weil timegm nicht POSIX ist und + * die Zeitzone hier ohnehin keine Rolle spielt: die Angabe ist immer GMT. Alles, was + * weder Zahl noch lesbares Datum ist, ergibt 0 ("keine Auskunft"), nicht eine geratene + * Zahl. */ +static int retry_after_seconds(const char *v) +{ + if (!v || !*v) return 0; + if (v[0] >= '0' && v[0] <= '9') { + long n = strtol(v, NULL, 10); + if (n < 0) return 0; + if (n > 86400) n = 86400; + return (int)n; + } + /* IMF-fixdate, die einzige Form, die RFC 7231 dafür vorsieht: + * Sun, 06 Nov 1994 08:49:37 GMT */ + static const char *mon_names[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", + "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; + char wd[8] = "", mon[8] = ""; + int d = 0, y = 0, hh = 0, mm = 0, ss = 0; + if (sscanf(v, "%7[^,], %2d %3s %4d %2d:%2d:%2d", wd, &d, mon, &y, &hh, &mm, &ss) != 7) + return 0; + int m = 0; + for (int i = 0; i < 12; i++) + if (!strcasecmp(mon, mon_names[i])) { m = i + 1; break; } + if (!m || d < 1 || d > 31 || y < 1970 || hh > 23 || mm > 59 || ss > 60) return 0; + long epoch = days_from_civil(y, (unsigned)m, (unsigned)d) * 86400L + + (long)hh * 3600L + (long)mm * 60L + (long)ss; + long diff = epoch - (long)time(NULL); + if (diff <= 0) return 0; /* Zeitpunkt vorbei: sofort weiterversuchen */ + if (diff > 86400) diff = 86400; + return (int)diff; +} + #define SPEND_MAX_KEYS 32 #define SPEND_MAX_MODELS 64 @@ -2482,8 +2516,7 @@ static int forward_step(j_conn *client, j_endpoint *ep, const j_req *rq, char ra[J_HDR_VALUE]; t_retry_after_s = 0; if (!j_hdrs_find(&r.hdrs, "Retry-After", ra, sizeof ra)) - t_retry_after_s = atoi(ra); - if (t_retry_after_s < 0) t_retry_after_s = 0; + t_retry_after_s = retry_after_seconds(ra); } STATE_LOCK(); snprintf(ep->last_error, sizeof ep->last_error, "upstream_status_%d", r.status); diff --git a/tests/retry-after.sh b/tests/retry-after.sh index 2620c91..9d214a1 100755 --- a/tests/retry-after.sh +++ b/tests/retry-after.sh @@ -71,6 +71,64 @@ fi contains "$(grep -o 'retry_after_s=[0-9]*' "$TMP/rc.log" | head -1)" "retry_after_s=60" \ "the cap is visible: the provider said 60, junction waited 1" +# Retry-After darf auch ein HTTP-Datum sein — dieselbe Wirkung, andere Form. +RDP=$((PORT+216)) # Knoten mit Datum in der Zukunft +RDD=$((PORT+217)) # Stub dazu +RPP=$((PORT+218)) # Knoten mit Datum in der Vergangenheit +RPD=$((PORT+219)) # Stub dazu + +STUB_STATE="$TMP/rad.state" tests/stub $RDD "RAD" --fail-n 1 --retry-after-date 2 \ + > "$TMP/ra_date.log" 2>&1 & +PIDS+=($!) +STUB_STATE="$TMP/rap.state" tests/stub $RPD "RAPAST" --fail-n 1 --retry-after-date -120 \ + > "$TMP/ra_past.log" 2>&1 & +PIDS+=($!) +sleep 0.4 + +cat > "$TMP/rad.conf" < "$TMP/rap.conf" < "$TMP/rad.log" 2>&1 & +PIDS+=($!) +./junction --config "$TMP/rap.conf" > "$TMP/rap.log" 2>&1 & +PIDS+=($!) +wait_port "$RDP" 10 || bad "http-date instance did not come up" +wait_port "$RPP" 10 || bad "past-date instance did not come up" + +read -r dcode delapsed < <(curl -s -o /dev/null -m 30 -w '%{http_code} %{time_total}' \ + -X POST -H 'Content-Type: application/json' -d "$RABODY" \ + "http://127.0.0.1:$RDP/v1/chat/completions") +if [ "$dcode" = "200" ] && awk "BEGIN{exit !($delapsed >= 1.5 && $delapsed < 6.0)}"; then + ok "an HTTP-date Retry-After is honoured like a number (${delapsed}s)" +else + bad "the dated Retry-After was not honoured: $dcode after ${delapsed}s" +fi + +# Ein Datum in der Vergangenheit heißt "sofort" — nicht "eine Minute warten". +read -r pcode pelapsed < <(curl -s -o /dev/null -m 30 -w '%{http_code} %{time_total}' \ + -X POST -H 'Content-Type: application/json' -d "$RABODY" \ + "http://127.0.0.1:$RPP/v1/chat/completions") +if [ "$pcode" = "200" ] && awk "BEGIN{exit !($pelapsed < 2.0)}"; then + ok "a date in the past means now, not a minute of waiting (${pelapsed}s)" +else + bad "a past date was not treated as now: $pcode after ${pelapsed}s" +fi + # Ohne Retry-After bleibt es beim eigenen Backoff (der Stub schickt dann 0). if grep -q "retry_after_s=0" "$TMP/ra.log" 2>/dev/null; then bad "a zero Retry-After was treated as an instruction" diff --git a/tests/run.sh b/tests/run.sh index 15736a1..835a0c0 100755 --- a/tests/run.sh +++ b/tests/run.sh @@ -46,7 +46,7 @@ log_has() { # answer requests with somebody else's config), so fail loudly and early. # 8157 gehört auf dieser Maschine dem Hermes-Gateway — der Port wird von keinem # Test benutzt, also darf er die Suite nicht aufhalten. -for port in 8347 8348 8349 8350 8345 8346 8335 8336 8337 8338 8339 8340 8341 8342 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8150 8151 8152 8153 8154 8155 8156 8158 8159 8165 8166 8167 8168 8170 8171 8172 8173 8175 8180 8181 8194 8176 8177 8185 8186 8187 8188 8189 8190 9443 9990 9991 9992 9993 9994 9995 9996; do +for port in 8347 8348 8349 8350 8351 8352 8353 8354 8345 8346 8335 8336 8337 8338 8339 8340 8341 8342 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8150 8151 8152 8153 8154 8155 8156 8158 8159 8165 8166 8167 8168 8170 8171 8172 8173 8175 8180 8181 8194 8176 8177 8185 8186 8187 8188 8189 8190 9443 9990 9991 9992 9993 9994 9995 9996; do if ss -tln 2>/dev/null | grep -q ":$port "; then echo "port $port is busy — a leftover junction/stub is still running:" ss -tlnp 2>/dev/null | grep ":$port " | sed 's/^/ /' diff --git a/tests/stub.c b/tests/stub.c index fd86309..8441a0e 100644 --- a/tests/stub.c +++ b/tests/stub.c @@ -5,6 +5,8 @@ * * --retry-after N puts "Retry-After: N" into every forced 503/429, so a test can check * that junction waits as long as the provider asked instead of its own backoff. + * --retry-after-date OFF sends the same value as an HTTP date (now + OFF seconds), the + * other form RFC 7231 allows — with a negative OFF it is a date in the past. * --fail always answers 503 (an endpoint that is up but will not serve) * --delay waits that long before answering (to exercise "still_working") * --fail-n answers 503 for the first N requests, then serves — the counter lives in @@ -91,6 +93,8 @@ int main(int argc, char **argv) double delay = 0; int fail_n = 0; int retry_after = 0; + int retry_after_date = 0; + int has_retry_date = 0; const char *deny_auth = ""; const char *cert = NULL, *key = NULL; for (int i = 3; i < argc; i++) { @@ -99,6 +103,10 @@ int main(int argc, char **argv) else if (!strcmp(argv[i], "--fail-n") && i + 1 < argc) fail_n = atoi(argv[++i]); else if (!strcmp(argv[i], "--deny-auth") && i + 1 < argc) deny_auth = argv[++i]; else if (!strcmp(argv[i], "--retry-after") && i + 1 < argc) retry_after = atoi(argv[++i]); + else if (!strcmp(argv[i], "--retry-after-date") && i + 1 < argc) { + retry_after_date = atoi(argv[++i]); + has_retry_date = 1; + } else if (!strcmp(argv[i], "--tls") && i + 2 < argc) { cert = argv[++i]; key = argv[++i]; } } char tlserr[256]; @@ -229,11 +237,20 @@ int main(int argc, char **argv) * dass junction genau so lange wartet. */ const char *b = (forced == 429) ? "{\"error\":\"rate limited\"}" : "{\"error\":\"not serving yet\"}"; + char ra[64]; + if (has_retry_date) { + time_t when = time(NULL) + retry_after_date; + struct tm g; + gmtime_r(&when, &g); + strftime(ra, sizeof ra, "%a, %d %b %Y %H:%M:%S GMT", &g); + } else { + snprintf(ra, sizeof ra, "%d", retry_after); + } snprintf(body, sizeof body, - "HTTP/1.1 %d %s\r\nContent-Type: application/json\r\nRetry-After: %d\r\n" + "HTTP/1.1 %d %s\r\nContent-Type: application/json\r\nRetry-After: %s\r\n" "Content-Length: %zu\r\nConnection: close\r\n\r\n%s", forced, (forced == 429) ? "Too Many Requests" : "Service Unavailable", - retry_after, strlen(b), b); + ra, strlen(b), b); } else if (fail) { const char *b = "{\"error\":\"not serving\"}"; snprintf(body, sizeof body,