1 | <?php |
||
18 | class LeakyBucketStrategy implements ThrottlingStrategyInterface |
||
19 | { |
||
20 | /** |
||
21 | * @var int |
||
22 | */ |
||
23 | protected $requestsPerTimeSpan; |
||
24 | |||
25 | /** |
||
26 | * Sets the maximum number of requests allowed per timespan for a single entity. |
||
27 | * @param int $requests |
||
28 | * @throws \InvalidArgumentException |
||
29 | */ |
||
30 | 4 | public function setRequestsPerTimeSpan($requests) |
|
41 | |||
42 | /** |
||
43 | * Returns the previously set number of requests per timespan. |
||
44 | * @return int |
||
45 | */ |
||
46 | 1 | public function getRequestsPerTimeSpan() |
|
50 | |||
51 | /** |
||
52 | * @var float |
||
53 | */ |
||
54 | protected $timeSpan; |
||
55 | |||
56 | /** |
||
57 | * Sets the timespan in which the defined number of requests is allowed per single entity. |
||
58 | * @param float|string $timeSpan |
||
59 | * @throws InvalidArgumentException |
||
60 | */ |
||
61 | 4 | public function setTimeSpan($timeSpan) |
|
62 | { |
||
63 | 4 | if (is_string($timeSpan)) { |
|
64 | 1 | $timeSpan = self::parseTime($timeSpan); |
|
65 | } |
||
66 | 4 | if (!is_numeric($timeSpan)) { |
|
67 | 1 | throw new InvalidArgumentException('timespan is not numeric'); |
|
68 | } |
||
69 | 4 | $timeSpan = floatval($timeSpan); |
|
70 | 4 | if ($timeSpan <= 0) { |
|
71 | 2 | throw new InvalidArgumentException('timespan cannot be 0 or less'); |
|
72 | } |
||
73 | 4 | $this->timeSpan = $timeSpan; |
|
74 | 4 | } |
|
75 | |||
76 | /** |
||
77 | * Returns the previously set timespan. |
||
78 | * @return float |
||
79 | */ |
||
80 | 1 | public function getTimeSpan() |
|
84 | |||
85 | /** |
||
86 | * Sets the strategy up with $requests allowed per $timeSpan |
||
87 | * @param int $requests the requests allowed per time span |
||
88 | * @param int|string $timeSpan tither the amount of seconds or a string such as "10s", "5m" or "1h" |
||
89 | * @throws InvalidArgumentException |
||
90 | * @see LeakyBucketStrategy::setRequestsPerTimeSpan |
||
91 | * @see LeakyBucketStrategy::setTimeSpan |
||
92 | */ |
||
93 | public function __construct($requests, $timeSpan) |
||
98 | |||
99 | /** |
||
100 | * @var StorageInterface |
||
101 | */ |
||
102 | protected $storage; |
||
103 | |||
104 | public function setStorage(StorageInterface $storage) |
||
108 | |||
109 | /** |
||
110 | * Parses a timespan string such as "10s", "5m" or "1h" and returns the amount of seconds. |
||
111 | * @param string $timeSpan the time span to parse to seconds |
||
112 | * @return float|null the number of seconds or null, if $timeSpan couldn't be parsed |
||
113 | */ |
||
114 | 1 | public static function parseTime($timeSpan) |
|
127 | |||
128 | /** |
||
129 | * Returns whether entity exceeds it's allowed request capacity with this request. |
||
130 | * @param string $identifier the identifer of the entity to check |
||
131 | * @return bool true if this requests exceeds the number of requests allowed |
||
132 | * @throws LogicException if no storage has been set |
||
133 | */ |
||
134 | 2 | public function isViolator($identifier) |
|
170 | } |
||
171 |