Skip to content

fix(api): stop blind 5s retries on bare 429, prevent concurrent streak inflation, and expose retry_after - #213

Open
to3k wants to merge 1 commit into
fustom:mainfrom
to3k:fix/rate-limit-cooldown-and-menu-cache
Open

to3k wants to merge 1 commit into
fustom:mainfrom
to3k:fix/rate-limit-cooldown-and-menu-cache

Conversation

@to3k

@to3k to3k commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Follow-up to #200 based on live 0.19.21 testing on a GALEVO heat pump (hpSys: true) and designed to pair cleanly with fustom/ariston-remotethermo-home-assistant-v3#558.

Problem

Observed in live Home Assistant logs on ariston==0.19.21 / integration v0.20.13:

  1. When Ariston Cloud returns a bare 429 Requests are blocked without a duration or Retry-After header, the first hit slept 5 s and fired a blind retry that consistently hit 429 again before entering cooldown.
  2. Concurrent requests from galevo_device.async_update_state() (asyncio.gather) hitting bare 429 within milliseconds both escalated _rate_limit_streak in a single poll cycle (e.g. streak 2 → streak 3 175 ms apart), and a 5xx/405 retry (is_retry=True) could bypass an active 429 block window.
  3. ConnectionException did not expose retry_after, and connect() / async_connect() wrapped ConnectionException into an empty exception, hiding the 429 status and remaining delay from callers like HA PR #558.

Changes

  • Immediate cooldown on unparsable 429: bare 429 responses (no "blocked for N seconds" and no Retry-After) enter the exponential cooldown (60 s → 600 s) immediately instead of sleeping 5 s and retrying blindly. Parsed 429 durations keep the existing sleep-and-retry-once behaviour.
  • Concurrency-safe escalation & retry separation:
    • Concurrent in-flight bare 429 responses arriving while a block window is already active (now < _blocked_until) or a retry is in flight do not escalate _rate_limit_streak again.
    • Separated rate_limit_retry from is_retry so 5xx/405 retries respect an active 429 block window instead of bypassing it.
    • Successful responses only reset _rate_limit_streak once time.time() >= _blocked_until, so a concurrent 200 OK finishing during an active block cannot wipe out the streak.
  • Public ConnectionException.retry_after & login propagation:
    • Added optional keyword-only retry_after: Optional[float] = None to ConnectionException (preserving existing args untouched) and populated it on all 429 exceptions.
    • Let connect() and async_connect() re-raise ConnectionException directly so callers receive the 429 status and retry_after.

Tests

  • python -m coverage run --source=ariston -m unittest discover --start-directory tests → 126 passed (99% total coverage).
  • Added regression tests covering immediate cooldown on bare 429, overlapping unparsable 429 single-step escalation, 5xx retry respecting active 429 blocks, retry_after exposure (short-circuit, repeated 429, and connect()/async_connect()), and streak preservation when an in-flight 200 OK lands during an active block.

Enter exponential cooldown immediately on unparsable 429 responses instead of firing a 5s blind retry, prevent concurrent in-flight 429s or 5xx/405 retries from bypassing blocks or double-escalating streak, preserve streak when an in-flight 200 OK completes during an active block, expose retry_after on ConnectionException, and propagate ConnectionException through connect()/async_connect().
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant