TsgcWSRateLimiter › Events › OnStateChange
Fired when the Token Bucket size changes for a key.
property OnStateChange: TsgcRateLimitOnStateChange;
// TsgcRateLimitOnStateChange = procedure(Sender: TObject; const aKey: string; aOldBucketSize, aNewBucketSize: Integer) of object
—
Fired after a Token Bucket consume or refill operation updates the bucket size for a key. The aKey parameter identifies the bucket owner and aOldBucketSize / aNewBucketSize report the token counts before and after the change. Useful for telemetry, debugging and real-time visualizations (for example, a chart that shows each client's bucket fill level). The event is not fired for Sliding Window or Fixed Window strategies — only for Token Bucket. Runs synchronously on the thread that invoked the allowance check; keep the handler fast and non-blocking.
procedure TForm1.RateLimiterStateChange(Sender: TObject;
const aKey: string; aOldBucketSize, aNewBucketSize: Integer);
begin
// Stream bucket-level telemetry to the dashboard
Log(Format('Bucket %s: %d -> %d', [aKey, aOldBucketSize, aNewBucketSize]));
end;