TsgcWSCircuitBreakerMethods › IsCallAllowed

IsCallAllowed Method

Returns True when a new call is currently allowed for the given key; advances HalfOpen trial accounting.

Syntax

function IsCallAllowed(const aKey: string): Boolean;

Parameters

NameTypeDescription
aKeyconst stringCircuit key to check — usually the hostname of the upstream service. Each key owns its own state and counters.

Return Value

True when the circuit is Closed, or HalfOpen with remaining trial calls; False when Open or when the HalfOpen trial budget is exhausted. Returns True unconditionally while Enabled is False. (Boolean)

Remarks

Core gatekeeper consulted by Execute / ExecuteWithResult and by the HTTP API client integration before every outbound request. If the circuit is Open and CooldownSec has elapsed, IsCallAllowed also performs the Open -> HalfOpen transition and decrements the trial-call budget. When it returns False the internal TotalRejected counter is incremented and OnCallRejected fires with a short reason string (for example 'Circuit Open'). Call this from custom code when you want to check state without actually running a protected action — for example to decide whether to skip a step and fall straight to a fallback.

Example

if not sgcWSCircuitBreaker1.IsCallAllowed('api.openai.com') then
begin
  ServeCachedReply;
  Exit;
end;

try
  Response := sgcAI_OpenAI1.Get('https://api.openai.com/v1/models');
  sgcWSCircuitBreaker1.RecordSuccess('api.openai.com');
except
  on E: Exception do
  begin
    sgcWSCircuitBreaker1.RecordFailure('api.openai.com', E.Message);
    raise;
  end;
end;

Back to Methods