-
Notifications
You must be signed in to change notification settings - Fork 4
/
Psr16RateLimiter.php
139 lines (109 loc) · 3.82 KB
/
Psr16RateLimiter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/**
* Implementation of nikolaposa/rate-limit to allow using a PSR-16 cache for storage.
*
* @see https://github.com/nikolaposa/rate-limit
* @see https://www.php-fig.org/psr/psr-16/
*
* I think this should maybe be part of the RateLimit library, hence the PHPCS rules are disabled and the licence
* is MIT.
*
* @license MIT
* @author BrianHenryIE <BrianHenryIE@gmail.com>
* @link https://BrianHenryIE.com
*/
declare(strict_types=1);
namespace BrianHenryIE\Checkout_Rate_Limiter\API\RateLimiter;
use BrianHenryIE\Checkout_Rate_Limiter\Psr\SimpleCache\CacheInterface;
use BrianHenryIE\Checkout_Rate_Limiter\Psr\SimpleCache\InvalidArgumentException;
use BrianHenryIE\Checkout_Rate_Limiter\RateLimit\Exception\LimitExceeded;
use BrianHenryIE\Checkout_Rate_Limiter\RateLimit\RateLimiter;
use BrianHenryIE\Checkout_Rate_Limiter\RateLimit\SilentRateLimiter;
use BrianHenryIE\Checkout_Rate_Limiter\RateLimit\Rate;
use BrianHenryIE\Checkout_Rate_Limiter\RateLimit\Status;
use function time;
/**
* Class Psr16RateLimiter
*
* @package BrianHenryIE\Checkout_Rate_Limiter\API\RateLimiter
*/
class Psr16RateLimiter implements RateLimiter, SilentRateLimiter {
protected CacheInterface $psrCache;
/** @var string */
protected $keyPrefix;
public function __construct( CacheInterface $psrCache, string $keyPrefix = '' ) {
$this->keyPrefix = $keyPrefix;
$this->psrCache = $psrCache;
}
public function limit( string $identifier, Rate $rate ): void {
$key = $this->key( $identifier, $rate->getInterval() );
$current = $this->getCurrentCount( $key );
if ( $current >= $rate->getOperations() ) {
throw LimitExceeded::for( $identifier, $rate );
}
$this->updateCounter( $key, $rate->getInterval() );
}
public function limitSilently( string $identifier, Rate $rate ): Status {
$key = $this->key( $identifier, $rate->getInterval() );
$current = $this->updateCounter( $key, $rate->getInterval() );
return Status::from(
$identifier,
$current,
$rate->getOperations(),
time() + $rate->getInterval()
);
}
/**
* The key includes the interval so multiple intervals:incidents can be counted against the one identifier.
* e.g. it can happen five times in one minute but no more then ten times in one hour.
*
* @see https://github.com/wp-oop/transient-cache/blob/94b21321867dfb82eda7fe2ab962895c939f446d/src/CachePool.php#L38
*
* @param string $identifier
* @param int $interval
* @return string
*/
private function key( string $identifier, int $interval ): string {
return "{$this->keyPrefix}{$identifier}--$interval";
}
/**
* Return a count of unexpired records for the key.
*
* @param string $key
* @return int
*/
protected function getCurrentCount( string $key ): int {
$stored_values = $this->getCurrentStoredCounter( $key );
return count( $stored_values );
}
/**
* @param string $key
* @return array<int, array{key: string, created_at: int, expires_at: int, interval :int}>
*/
protected function getCurrentStoredCounter( string $key ): array {
try {
$stored_values = $this->psrCache->get( $key, array() );
} catch ( InvalidArgumentException $e ) {
$stored_values = array();
}
foreach ( $stored_values as $created_time => $value ) {
if ( isset( $value['expires_at'] ) && $value['expires_at'] < time() ) {
unset( $stored_values[ $created_time ] );
}
}
return $stored_values;
}
protected function updateCounter( string $key, int $interval ): int {
$stored_values = $this->getCurrentStoredCounter( $key );
$created_time = time();
$expires_at = $created_time + $interval;
$stored_values[ $created_time ] = array(
'key' => $key,
'created_time' => $created_time,
'expires_at' => $expires_at,
'interval' => $interval,
);
$this->psrCache->set( $key, $stored_values, $interval );
return count( $stored_values );
}
}