Files
pricebuddy/app/Services/Ai/AiProviderHealth.php
Jeremy c0c8d0a0c8 feat: circuit breaker for repeatedly failing AI providers
Bounding the meta-extraction request stopped a wedged provider costing 120s, but
it still cost the full 25s budget on every single request. A provider that has
failed the last N calls will almost certainly fail the next one, and finding out
is the expensive part.

AiProviderHealth tracks consecutive failures per provider id in the cache. After
failure_threshold consecutive failures (default 3) the provider is treated as
unavailable for cooldown_seconds (default 300), which doubles as the counting
window so thinly spread failures never accumulate. Any success closes it
immediately.

AiService feeds it, since that is the only layer that knows whether a provider
call succeeded. The Ollama reachability check deliberately does not close the
breaker: listing models proves the HTTP front end is up, not that generation
works, which is exactly the state this instance is in.

Only callers on a deadline consult it. The meta-extraction endpoint skips healing
with the new reason `provider_unavailable`; the admin UI heal button and queued
healing keep exercising the provider, so they act as the probe that notices
recovery rather than waiting out the cooldown.

Verified live against the wedged provider: three requests at 24.1s each, then the
fourth returned in 0.105s with reason `provider_unavailable`.

Note for API clients: `provider_unavailable` is a new value in the `healing.reason`
set. It means the instance is degraded rather than the page being unextractable,
so the same URL is worth retrying later.
2026-08-08 13:23:04 +10:00

90 lines
2.9 KiB
PHP

<?php
namespace App\Services\Ai;
use App\Dto\AiProviderConfigDto;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
/**
* A circuit breaker for AI providers.
*
* A wedged provider fails the same way for every caller, and each caller pays the full
* timeout to find out. After enough consecutive failures the breaker opens and callers
* that cannot afford to wait — the synchronous meta-extraction endpoint — skip the
* provider outright instead of paying again.
*
* Callers who can afford to wait (the admin UI, queued jobs) deliberately do NOT consult
* the breaker. They keep exercising the provider, so a recovery is noticed and the
* breaker closes on the first success rather than only when the cooldown lapses.
*/
class AiProviderHealth
{
public static function new(): self
{
return resolve(static::class);
}
/**
* Whether the provider has failed often enough recently that a caller on a deadline
* should not spend its budget finding out again.
*/
public function isUnavailable(AiProviderConfigDto $provider): bool
{
return Cache::get($this->openKey($provider)) !== null;
}
/**
* Consecutive failures are what matter: a provider that fails, succeeds, then fails
* is slow or flaky, not wedged, so the counter resets on every success.
*/
public function recordFailure(AiProviderConfigDto $provider): void
{
$cooldown = $this->cooldownSeconds();
$threshold = $this->failureThreshold();
// The window is the cooldown: failures spread more thinly than that are not a
// wedged provider and should never accumulate into an open breaker.
$failures = (int) Cache::get($this->failureKey($provider), 0) + 1;
Cache::put($this->failureKey($provider), $failures, $cooldown);
if ($failures < $threshold) {
return;
}
Cache::put($this->openKey($provider), true, $cooldown);
Log::warning('AI provider circuit breaker opened.', [
'provider' => $provider->name,
'failures' => $failures,
'cooldown_seconds' => $cooldown,
]);
}
public function recordSuccess(AiProviderConfigDto $provider): void
{
Cache::forget($this->failureKey($provider));
Cache::forget($this->openKey($provider));
}
public function failureThreshold(): int
{
return max(1, (int) config('price_buddy.ai_provider_breaker.failure_threshold', 3));
}
public function cooldownSeconds(): int
{
return max(1, (int) config('price_buddy.ai_provider_breaker.cooldown_seconds', 300));
}
protected function failureKey(AiProviderConfigDto $provider): string
{
return 'ai-provider-health:failures:'.$provider->id;
}
protected function openKey(AiProviderConfigDto $provider): string
{
return 'ai-provider-health:open:'.$provider->id;
}
}