TsgcWSCircuitBreaker › Methods › RecordFailure
Records a failure against a key; applies Classification, updates counters and may open the circuit.
procedure RecordFailure(const aKey: string; const aException: string = '');
| Name | Type | Description |
|---|---|---|
aKey | const string | Circuit key the failure belongs to — usually the hostname of the upstream service. |
aException | const string | Optional exception text in the form ClassName: Message, passed to Classification to decide whether to record or ignore. When empty the failure is always recorded. |
Runs aException through the Classification rules first: matches in IgnoreExceptions are dropped (neither success nor failure), everything else increments the rolling-window failure counter and Metrics.TotalFailures / Metrics.TotalCalls. The OnFailureRecorded event fires for every recorded failure. EvaluateTransition is then called, which may move Closed -> Open when Thresholds.FailureCount / FailureRatePercent is reached, or HalfOpen -> Open when a trial call fails. Call RecordFailure explicitly only when you manage the call yourself (not through Execute); the HTTP API client integration already records failures on any raised exception.
try
vResponse := MyHTTPClient.Get(vURL);
sgcWSCircuitBreaker1.RecordSuccess(vHost);
except
on E: Exception do
begin
sgcWSCircuitBreaker1.RecordFailure(vHost,
Format('%s: %s', [E.ClassName, E.Message]));
raise;
end;
end;
procedure RecordFailure(const aException: string = '');
| Name | Type | Description |
|---|---|---|
aException | const string | Optional exception text in the form ClassName: Message, passed to Classification to decide whether to record or ignore. |
Shortcut that records a failure against DefaultKey. Use when a single breaker protects exactly one logical dependency. Set DefaultKey to a meaningful label before calling so Metrics and OnFailureRecorded report a readable key.
sgcWSCircuitBreaker1.DefaultKey := 'payment-gateway';
try
ChargeCustomer;
sgcWSCircuitBreaker1.RecordSuccess;
except
on E: Exception do
begin
sgcWSCircuitBreaker1.RecordFailure(
Format('%s: %s', [E.ClassName, E.Message]));
raise;
end;
end;