TsgcWSCircuitBreaker › Methods › RecordFailure
针对某个密钥记录一次失败;应用分类规则,更新计数器,并可能触发断路器。
procedure RecordFailure(const aKey: string; const aException: string = '');
| 名称 | 类型 | 描述 |
|---|---|---|
aKey | const string | 故障所属的电路键,通常是上游服务的主机名。 |
aException | const string | 可选的异常文本,格式为 ClassName: Message,传递给 Classification 以决定是记录还是忽略。为空时,故障始终被记录。 |
首先通过分类规则运行 aException:匹配 IgnoreExceptions 的异常将被丢弃(既不计入成功也不计入失败),其他所有异常均使滚动窗口失败计数器递增,并更新 Metrics.TotalFailures / Metrics.TotalCalls。OnFailureRecorded 事件对每次记录的失败触发一次。随后调用 EvaluateTransition,当达到 Thresholds.FailureCount / FailureRatePercent 时可能将状态从 Closed 转为 Open,或当试验调用失败时从 HalfOpen 转为 Open。仅当您自行管理调用(而非通过 Execute)时才需显式调用 RecordFailure;HTTP API 客户端集成已在任何引发的异常上自动记录失败。
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 = '');
| 名称 | 类型 | 描述 |
|---|---|---|
aException | const string | 可选的异常文本,格式为 ClassName: Message,传递给 Classification 以决定是否记录或忽略。 |
针对 DefaultKey 记录一次失败的快捷方式。当单个断路器恰好保护一个逻辑依赖项时使用。调用前请将 DefaultKey 设为有意义的标签,以便 Metrics 和 OnFailureRecorded 报告可读的键名。
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;