|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Zenstruck\Governator; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Fluent interface for configuring and creating throttles. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Kevin Bond <[email protected]> |
|
9
|
|
|
*/ |
|
10
|
|
|
final class ThrottleBuilder |
|
11
|
|
|
{ |
|
12
|
|
|
private ThrottleFactory $factory; |
|
13
|
|
|
private array $resource; |
|
14
|
|
|
private ?int $limit = null; |
|
15
|
|
|
private ?int $ttl = null; |
|
16
|
|
|
|
|
17
|
40 |
|
public function __construct(ThrottleFactory $factory, string ...$resource) |
|
18
|
|
|
{ |
|
19
|
40 |
|
$this->factory = $factory; |
|
20
|
40 |
|
$this->resource = $resource; |
|
21
|
40 |
|
} |
|
22
|
|
|
|
|
23
|
1 |
|
public function with(string ...$resource): self |
|
24
|
|
|
{ |
|
25
|
1 |
|
$this->resource = \array_merge($this->resource, $resource); |
|
26
|
|
|
|
|
27
|
1 |
|
return $this; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param int $limit The maximum number of throttle "hits" in its "time window" |
|
32
|
|
|
*/ |
|
33
|
39 |
|
public function allow(int $limit): self |
|
34
|
|
|
{ |
|
35
|
39 |
|
$this->limit = $limit; |
|
36
|
|
|
|
|
37
|
39 |
|
return $this; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param int $seconds the "time window" for the throttle in seconds |
|
42
|
|
|
*/ |
|
43
|
39 |
|
public function every(int $seconds): self |
|
44
|
|
|
{ |
|
45
|
39 |
|
$this->ttl = $seconds; |
|
46
|
|
|
|
|
47
|
39 |
|
return $this; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Create the throttle for the current configuration. |
|
52
|
|
|
* |
|
53
|
|
|
* @throws \LogicException If the limit or TTL was not set |
|
54
|
|
|
*/ |
|
55
|
40 |
|
public function create(): Throttle |
|
56
|
|
|
{ |
|
57
|
40 |
|
if (null === $this->limit) { |
|
58
|
1 |
|
throw new \LogicException(\sprintf('You must set a "Limit" for the throttle via "%s::allow($limit)"', self::class)); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
39 |
|
if (null === $this->ttl) { |
|
62
|
1 |
|
throw new \LogicException(\sprintf('You must set a "TTL" for the throttle via "%s::every($ttl)"', self::class)); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
38 |
|
if (empty($this->resource)) { |
|
66
|
1 |
|
throw new \LogicException('The resource for the throttle cannot be blank.'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
37 |
|
return $this->factory->create(\implode('', $this->resource), $this->limit, $this->ttl); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Create the throttle for the current configuration and "hit" it, potentially blocking the process |
|
74
|
|
|
* for the passed time if it's quota has been exceeded. |
|
75
|
|
|
* |
|
76
|
|
|
* @see Throttle::hit() |
|
77
|
|
|
*/ |
|
78
|
28 |
|
public function hit(int $blockFor = 0): Quota |
|
79
|
|
|
{ |
|
80
|
28 |
|
return $this->create()->hit($blockFor); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Create the throttle for the current configuration and resets it. |
|
85
|
|
|
* |
|
86
|
|
|
* @see Throttle::reset() |
|
87
|
|
|
*/ |
|
88
|
9 |
|
public function reset(): void |
|
89
|
|
|
{ |
|
90
|
9 |
|
$this->create()->reset(); |
|
91
|
9 |
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|