-
-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathPool.php
More file actions
173 lines (146 loc) · 5.28 KB
/
Copy pathPool.php
File metadata and controls
173 lines (146 loc) · 5.28 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
declare(strict_types=1);
namespace Saloon\Http;
use Closure;
use Generator;
use GuzzleHttp\Promise\EachPromise;
use GuzzleHttp\Promise\PromiseInterface;
use Saloon\Exceptions\InvalidPoolItemException;
class Pool
{
/**
* Requests inside the pool
*
* @var iterable<\GuzzleHttp\Promise\PromiseInterface|\Saloon\Http\Request<mixed>>
*/
protected iterable $requests;
/**
* Handle Response Callback
*
* @var \Closure(\Saloon\Http\Response<mixed>, array-key, \GuzzleHttp\Promise\PromiseInterface): (void)|null
*/
protected ?Closure $responseHandler = null;
/**
* Handle Exception Callback
*
* @var \Closure(mixed, array-key, \GuzzleHttp\Promise\PromiseInterface): (void)|null
*/
protected ?Closure $exceptionHandler = null;
/**
* Connector
*/
protected Connector $connector;
/**
* Concurrency
*
* How many requests will be sent at once.
*
* @var int|\Closure(int): int
*/
protected int|Closure $concurrency;
/**
* Constructor
*
* @param iterable<\GuzzleHttp\Promise\PromiseInterface|\Saloon\Http\Request<mixed>>|callable(\Saloon\Http\Connector): iterable<\GuzzleHttp\Promise\PromiseInterface|\Saloon\Http\Request<mixed>> $requests
* @param int|callable(int $pendingRequests): (int) $concurrency
* @param callable(\Saloon\Http\Response<mixed>, array-key $key, \GuzzleHttp\Promise\PromiseInterface $poolAggregate): (void)|null $responseHandler
* @param callable(mixed $reason, array-key $key, \GuzzleHttp\Promise\PromiseInterface $poolAggregate): (void)|null $exceptionHandler
*/
public function __construct(Connector $connector, iterable|callable $requests = [], int|callable $concurrency = 5, callable|null $responseHandler = null, callable|null $exceptionHandler = null)
{
$this->connector = $connector;
$this->setRequests($requests);
$this->setConcurrency($concurrency);
if (! is_null($responseHandler)) {
$this->withResponseHandler($responseHandler);
}
if (! is_null($exceptionHandler)) {
$this->withExceptionHandler($exceptionHandler);
}
}
/**
* Specify a callback to happen for each successful request
*
* @param callable(\Saloon\Http\Response<mixed>, array-key $key, \GuzzleHttp\Promise\PromiseInterface $poolAggregate): (void) $callable
* @return $this
*/
public function withResponseHandler(callable $callable): static
{
$this->responseHandler = $callable(...);
return $this;
}
/**
* Specify a callback to happen for each failed request
*
* @param callable(mixed $reason, array-key $key, \GuzzleHttp\Promise\PromiseInterface $poolAggregate): (void) $callable
* @return $this
*/
public function withExceptionHandler(callable $callable): static
{
$this->exceptionHandler = $callable(...);
return $this;
}
/**
* Set the amount of concurrent requests that should be sent
*
* @param int|callable(int $pendingRequests): (int) $concurrency
* @return $this
*/
public function setConcurrency(int|callable $concurrency): static
{
$this->concurrency = is_callable($concurrency) ? $concurrency(...) : $concurrency;
return $this;
}
/**
* Set the requests
*
* @param iterable<\GuzzleHttp\Promise\PromiseInterface|\Saloon\Http\Request<mixed>>|callable(\Saloon\Http\Connector): iterable<\GuzzleHttp\Promise\PromiseInterface|\Saloon\Http\Request<mixed>> $requests
* @return $this
*/
public function setRequests(iterable|callable $requests): static
{
if (is_callable($requests)) {
$requests = $requests($this->connector);
}
if (is_iterable($requests)) {
$requests = static fn (): Generator => yield from $requests;
}
$this->requests = $requests();
return $this;
}
/**
* Get the request generator
*
* @return iterable<\GuzzleHttp\Promise\PromiseInterface|\Saloon\Http\Request<mixed>>
*/
public function getRequests(): iterable
{
return $this->requests;
}
/**
* Send the pool and create a Promise
*/
public function send(): PromiseInterface
{
// Iterate through the existing generator and "prepare" the requests.
// If they are SaloonRequests then we should convert them into
// promises.
$preparedRequests = function (): Generator {
foreach ($this->requests as $key => $request) {
match (true) {
$request instanceof Request => yield $key => $this->connector->sendAsync($request),
$request instanceof PromiseInterface => yield $key => $request,
default => throw new InvalidPoolItemException
};
}
};
// Next we'll use an EachPromise which accepts an iterator of
// requests and will process them as the concurrency we set.
$eachPromise = new EachPromise($preparedRequests(), [
'concurrency' => $this->concurrency,
'fulfilled' => $this->responseHandler,
'rejected' => $this->exceptionHandler,
]);
return $eachPromise->promise();
}
}