TsgcWSCircuitBreakerMethods › SaveStateToFile

SaveStateToFile Method

Persists every tracked circuit's state and counters to a file so they survive a restart.

Syntax

procedure SaveStateToFile(const aFileName: string);

Parameters

NameTypeDescription
aFileNameconst stringAbsolute or relative path of the file to write. The file is overwritten; the parent directory must already exist.

Remarks

Serializes every key's current state, rolling-window counters and last state-change timestamp to a single file so a process restart can resume with a warm breaker. The write is atomic relative to concurrent RecordSuccess / RecordFailure calls — internal critical sections serialize access. Call periodically from a TTimer (for example every 60 seconds) or during graceful shutdown. Use LoadStateFromFile at startup to restore. Configuration (Thresholds, Recovery, Fallback, Classification, PerEndpoint) is not persisted — only runtime state. Any I/O error raised by the OS propagates to the caller.

Example

// Periodically snapshot state so a crash does not lose Open circuits
procedure TForm1.PersistTimer(Sender: TObject);
begin
  sgcWSCircuitBreaker1.SaveStateToFile('circuit.dat');
end;

// Also on graceful shutdown
procedure TForm1.FormDestroy(Sender: TObject);
begin
  sgcWSCircuitBreaker1.SaveStateToFile('circuit.dat');
end;

Back to Methods