You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: lib/RateLimit/JsonRateLimitStore.php
+34-1Lines changed: 34 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -20,6 +20,13 @@ class JsonRateLimitStore implements RateLimitStoreInterface
20
20
privatestring$filename;
21
21
privatearray$data = [];
22
22
23
+
/**
24
+
* Create a JsonRateLimitStore backed by the given file and load any existing data.
25
+
*
26
+
* If the file exists, its JSON contents are decoded into the in-memory store; decoding failures result in an empty dataset.
27
+
*
28
+
* @param string $filename Path to the JSON file used to persist rate limit data.
29
+
*/
23
30
publicfunction__construct(string$filename)
24
31
{
25
32
$this->filename = $filename;
@@ -30,11 +37,26 @@ public function __construct(string $filename)
30
37
}
31
38
}
32
39
40
+
/**
41
+
* Retrieve the stored rate-limit entry for a client and window.
42
+
*
43
+
* @param string $clientId The client identifier.
44
+
* @param string $window The rate-limit window identifier.
45
+
* @return array{remaining:int,timestamp:int}|null The entry for the specified client and window: an array with keys `remaining` and `timestamp`, or `null` if not found.
@@ -45,13 +67,24 @@ public function set(string $clientId, string $window, int $remaining, int $times
45
67
$this->save();
46
68
}
47
69
70
+
/**
71
+
* Retrieve all rate-limit window entries for a client.
72
+
*
73
+
* @param string $clientId The client identifier.
74
+
* @return array An associative array of windows to entries where each entry contains keys `remaining` (int) and `timestamp` (int); returns an empty array if the client has no entries.
Copy file name to clipboardExpand all lines: lib/RateLimit/PdoRateLimitStore.php
+34-1Lines changed: 34 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -19,12 +19,25 @@ class PdoRateLimitStore implements RateLimitStoreInterface
19
19
{
20
20
private\PDO$pdo;
21
21
22
+
/**
23
+
* Store the PDO connection and ensure the rate_limits table exists.
24
+
*
25
+
* Initializes the store by saving the provided PDO instance and creating the
26
+
* `rate_limits` table if it does not already exist.
27
+
*/
22
28
publicfunction__construct(\PDO$pdo)
23
29
{
24
30
$this->pdo = $pdo;
25
31
$this->init();
26
32
}
27
33
34
+
/**
35
+
* Fetches the remaining token count and expiry timestamp for a client's rate-limit window.
36
+
*
37
+
* @param string $clientId Identifier of the client.
38
+
* @param string $window Identifier of the rate-limit window.
39
+
* @return array{remaining:int, timestamp:int}|null The row for the specified client and window with integer `remaining` and `timestamp`, or `null` if no record exists.
* Fetches all rate-limit entries for a client, indexed by window.
77
+
*
78
+
* @param string $clientId The client identifier.
79
+
* @return array<string, array{remaining:int,timestamp:int}> Associative array keyed by window name; each value contains `remaining` (int) and `timestamp` (int).
* Returns all data for one client (for debugging).
35
-
*/
43
+
* Retrieve stored rate-limit data for the given client (intended for debugging).
44
+
*
45
+
* @param string $clientId The client identifier.
46
+
* @return array An associative array keyed by window name (e.g., 'second', 'day') where each value is an array with keys `remaining` (int) and `timestamp` (int, Unix time). Returns an empty array if no data exists for the client.
Copy file name to clipboardExpand all lines: lib/RateLimit/RateLimiter.php
+25-4Lines changed: 25 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -20,19 +20,34 @@ class RateLimiter
20
20
privateRateLimitStoreInterface$store;
21
21
privatebool$waitMode;
22
22
23
+
/**
24
+
* Create a RateLimiter configured with a storage backend and a handling mode for exceeded limits.
25
+
*
26
+
* @param RateLimitStoreInterface $store Storage backend for per-client rate-limit state.
27
+
* @param bool $waitMode If true, the limiter will wait until the limit window resets; if false, it will throw a RateLimitExceededException when limits are exceeded.
0 commit comments