TsgcWSRateLimiterMethods › IsAllowed

IsAllowed Method

Returns True if a request of the given cost is allowed for the specified key.

Syntax

function IsAllowed(const aKey: string; aCost: Integer = 1): Boolean;

Parameters

NameTypeDescription
aKeyconst stringRate-limit key to evaluate (for example an IP address, apikey:XXX or user:alice@example.com). Matching against PerEndpoint/PerAPIKey/PerUser/PerIP is based on this value.
aCostIntegerNumber of tokens (or window slots) the request should consume. Defaults to 1. Use a higher cost for expensive operations so a single call counts as several against the budget.

Return Value

Returns True if the request would be allowed, False if it would be throttled. (Boolean)

Remarks

Equivalent to calling Consume(aKey, aCost) and inspecting only the Allowed field of the result — but IsAllowed also updates the counters and fires OnThrottled when the request is rejected. Returns True immediately when Enabled is False. Call this from custom request handlers where you do not need the RetryAfterSec / Remaining / Reason fields that Consume returns.

Example

if not sgcWSRateLimiter1.IsAllowed('apikey:' + ApiKey) then
  raise Exception.Create('Too many requests');
// proceed with expensive operation

Back to Methods