TsgcWSCircuitBreakerMethods › ExecuteWithResult

ExecuteWithResult Method

Runs a protected action that returns a TObject and reports the result via an out parameter.

Syntax

function ExecuteWithResult(const aKey: string; aAction: TFunc<TObject>; out aResult: TObject): Boolean;

Parameters

NameTypeDescription
aKeyconst stringCircuit key the call belongs to — usually a hostname or logical service name.
aActionTFunc<TObject>Anonymous function that performs the work and returns a TObject (the caller owns the lifetime of the returned object).
aResultout TObjectReceives the TObject returned by aAction on success; set to nil when the call was rejected by the breaker.

Return Value

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)

Remarks

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.

Example

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;

Back to Methods