Debian Codegraph @

vendor/react/http/src/Middleware/LimitConcurrentRequestsMiddleware.php

raw
1 <?php
2
3 namespace React\Http\Middleware;
4
5 use Psr\Http\Message\ResponseInterface;
6 use Psr\Http\Message\ServerRequestInterface;
7 use React\Http\Io\HttpBodyStream;
8 use React\Http\Io\PauseBufferStream;
9 use React\Promise;
10 use React\Promise\PromiseInterface;
11 use React\Promise\Deferred;
12 use React\Stream\ReadableStreamInterface;
13
14 /**
15 * Limits how many next handlers can be executed concurrently.
16 *
17 * If this middleware is invoked, it will check if the number of pending
18 * handlers is below the allowed limit and then simply invoke the next handler
19 * and it will return whatever the next handler returns (or throws).
20 *
21 * If the number of pending handlers exceeds the allowed limit, the request will
22 * be queued (and its streaming body will be paused) and it will return a pending
23 * promise.
24 * Once a pending handler returns (or throws), it will pick the oldest request
25 * from this queue and invokes the next handler (and its streaming body will be
26 * resumed).
27 *
28 * The following example shows how this middleware can be used to ensure no more
29 * than 10 handlers will be invoked at once:
30 *
31 * ```php
32 * $server = new React\Http\Server(
33 * $loop,
34 * new React\Http\Middleware\StreamingRequestMiddleware(),
35 * new React\Http\Middleware\LimitConcurrentRequestsMiddleware(10),
36 * $handler
37 * );
38 * ```
39 *
40 * Similarly, this middleware is often used in combination with the
41 * [`RequestBodyBufferMiddleware`](#requestbodybuffermiddleware) (see below)
42 * to limit the total number of requests that can be buffered at once:
43 *
44 * ```php
45 * $server = new React\Http\Server(
46 * $loop,
47 * new React\Http\Middleware\StreamingRequestMiddleware(),
48 * new React\Http\Middleware\LimitConcurrentRequestsMiddleware(100), // 100 concurrent buffering handlers
49 * new React\Http\Middleware\RequestBodyBufferMiddleware(2 * 1024 * 1024), // 2 MiB per request
50 * new React\Http\Middleware\RequestBodyParserMiddleware(),
51 * $handler
52 * );
53 * ```
54 *
55 * More sophisticated examples include limiting the total number of requests
56 * that can be buffered at once and then ensure the actual request handler only
57 * processes one request after another without any concurrency:
58 *
59 * ```php
60 * $server = new React\Http\Server(
61 * $loop,
62 * new React\Http\Middleware\StreamingRequestMiddleware(),
63 * new React\Http\Middleware\LimitConcurrentRequestsMiddleware(100), // 100 concurrent buffering handlers
64 * new React\Http\Middleware\RequestBodyBufferMiddleware(2 * 1024 * 1024), // 2 MiB per request
65 * new React\Http\Middleware\RequestBodyParserMiddleware(),
66 * new React\Http\Middleware\LimitConcurrentRequestsMiddleware(1), // only execute 1 handler (no concurrency)
67 * $handler
68 * );
69 * ```
70 *
71 * @see RequestBodyBufferMiddleware
72 */
73 final class LimitConcurrentRequestsMiddleware
74 {
75 private $limit;
76 private $pending = 0;
77 private $queue = array();
78
79 /**
80 * @param int $limit Maximum amount of concurrent requests handled.
81 *
82 * For example when $limit is set to 10, 10 requests will flow to $next
83 * while more incoming requests have to wait until one is done.
84 */
85 public function __construct($limit)
86 {
87 $this->limit = $limit;
88 }
89
90 public function __invoke(ServerRequestInterface $request, $next)
91 {
92 // happy path: simply invoke next request handler if we're below limit
93 if ($this->pending < $this->limit) {
94 ++$this->pending;
95
96 try {
97 $response = $next($request);
98 } catch (\Exception $e) {
99 $this->processQueue();
100 throw $e;
101 } catch (\Throwable $e) { // @codeCoverageIgnoreStart
102 // handle Errors just like Exceptions (PHP 7+ only)
103 $this->processQueue();
104 throw $e; // @codeCoverageIgnoreEnd
105 }
106
107 // happy path: if next request handler returned immediately,
108 // we can simply try to invoke the next queued request
109 if ($response instanceof ResponseInterface) {
110 $this->processQueue();
111 return $response;
112 }
113
114 // if the next handler returns a pending promise, we have to
115 // await its resolution before invoking next queued request
116 return $this->await(Promise\resolve($response));
117 }
118
119 // if we reach this point, then this request will need to be queued
120 // check if the body is streaming, in which case we need to buffer everything
121 $body = $request->getBody();
122 if ($body instanceof ReadableStreamInterface) {
123 // pause actual body to stop emitting data until the handler is called
124 $size = $body->getSize();
125 $body = new PauseBufferStream($body);
126 $body->pauseImplicit();
127
128 // replace with buffering body to ensure any readable events will be buffered
129 $request = $request->withBody(new HttpBodyStream(
130 $body,
131 $size
132 ));
133 }
134
135 // get next queue position
136 $queue =& $this->queue;
137 $queue[] = null;
138 \end($queue);
139 $id = \key($queue);
140
141 $deferred = new Deferred(function ($_, $reject) use (&$queue, $id) {
142 // queued promise cancelled before its next handler is invoked
143 // remove from queue and reject explicitly
144 unset($queue[$id]);
145 $reject(new \RuntimeException('Cancelled queued next handler'));
146 });
147
148 // queue request and process queue if pending does not exceed limit
149 $queue[$id] = $deferred;
150
151 $pending = &$this->pending;
152 $that = $this;
153 return $deferred->promise()->then(function () use ($request, $next, $body, &$pending, $that) {
154 // invoke next request handler
155 ++$pending;
156
157 try {
158 $response = $next($request);
159 } catch (\Exception $e) {
160 $that->processQueue();
161 throw $e;
162 } catch (\Throwable $e) { // @codeCoverageIgnoreStart
163 // handle Errors just like Exceptions (PHP 7+ only)
164 $that->processQueue();
165 throw $e; // @codeCoverageIgnoreEnd
166 }
167
168 // resume readable stream and replay buffered events
169 if ($body instanceof PauseBufferStream) {
170 $body->resumeImplicit();
171 }
172
173 // if the next handler returns a pending promise, we have to
174 // await its resolution before invoking next queued request
175 return $that->await(Promise\resolve($response));
176 });
177 }
178
179 /**
180 * @internal
181 * @param PromiseInterface $promise
182 * @return PromiseInterface
183 */
184 public function await(PromiseInterface $promise)
185 {
186 $that = $this;
187
188 return $promise->then(function ($response) use ($that) {
189 $that->processQueue();
190
191 return $response;
192 }, function ($error) use ($that) {
193 $that->processQueue();
194
195 return Promise\reject($error);
196 });
197 }
198
199 /**
200 * @internal
201 */
202 public function processQueue()
203 {
204 // skip if we're still above concurrency limit or there's no queued request waiting
205 if (--$this->pending >= $this->limit || !$this->queue) {
206 return;
207 }
208
209 $first = \reset($this->queue);
210 unset($this->queue[key($this->queue)]);
211
212 $first->resolve();
213 }
214 }