TsgcWSCircuitBreaker › Methods › ExecuteWithResult
Runs a protected action that returns a TObject and reports the result via an out parameter.
function ExecuteWithResult(const aKey: string; aAction: TFunc<TObject>; out aResult: TObject): Boolean;
| Name | Type | Description |
|---|---|---|
aKey | const string | Circuit key the call belongs to — usually a hostname or logical service name. |
aAction | TFunc<TObject> | Anonymous function that performs the work and returns a TObject (the caller owns the lifetime of the returned object). |
aResult | out TObject | Receives the TObject returned by aAction on success; set to nil when the call was rejected by the breaker. |
True if the action executed and aResult carries its return value, False if the call was rejected because the circuit is Open (in which case aResult is nil). (Boolean)
Variant of Execute for actions that produce a result. Requires Delphi 2009+ because of TFunc<TObject>. Accounting is identical to Execute: IsCallAllowed is checked first, duration and outcome drive RecordSuccess / RecordFailure / RecordSlow, and exceptions are classified and re-raised. The returned object is owned by the caller — free it after use unless it is owned by another object. When rejected, OnCallRejected fires and OnFallback runs if Fallback.Enabled is True; note that OnFallback carries a var string payload, so returning a TObject fallback is the caller's responsibility.
var
oReply: TObject;
begin
if sgcWSCircuitBreaker1.ExecuteWithResult('api.openai.com',
function: TObject
begin
Result := CallOpenAIReturningJSON;
end, oReply) then
try
HandleReply(oReply as TsgcJSONObject);
finally
oReply.Free;
end
else
ServeCachedReply;
end;