Debian Codegraph @

vendor/react/dns/src/Query/CoopExecutor.php

raw
1 <?php
2
3 namespace React\Dns\Query;
4
5 use React\Promise\Promise;
6
7 /**
8 * Cooperatively resolves hosts via the given base executor to ensure same query is not run concurrently
9 *
10 * Wraps an existing `ExecutorInterface` to keep tracking of pending queries
11 * and only starts a new query when the same query is not already pending. Once
12 * the underlying query is fulfilled/rejected, it will forward its value to all
13 * promises awaiting the same query.
14 *
15 * This means it will not limit concurrency for queries that differ, for example
16 * when sending many queries for different host names or types.
17 *
18 * This is useful because all executors are entirely async and as such allow you
19 * to execute any number of queries concurrently. You should probably limit the
20 * number of concurrent queries in your application or you're very likely going
21 * to face rate limitations and bans on the resolver end. For many common
22 * applications, you may want to avoid sending the same query multiple times
23 * when the first one is still pending, so you will likely want to use this in
24 * combination with some other executor like this:
25 *
26 * ```php
27 * $executor = new CoopExecutor(
28 * new RetryExecutor(
29 * new TimeoutExecutor(
30 * new UdpTransportExecutor($nameserver),
31 * 3.0
32 * )
33 * )
34 * );
35 * ```
36 */
37 final class CoopExecutor implements ExecutorInterface
38 {
39 private $executor;
40 private $pending = array();
41 private $counts = array();
42
43 public function __construct(ExecutorInterface $base)
44 {
45 $this->executor = $base;
46 }
47
48 public function query(Query $query)
49 {
50 $key = $this->serializeQueryToIdentity($query);
51 if (isset($this->pending[$key])) {
52 // same query is already pending, so use shared reference to pending query
53 $promise = $this->pending[$key];
54 ++$this->counts[$key];
55 } else {
56 // no such query pending, so start new query and keep reference until it's fulfilled or rejected
57 $promise = $this->executor->query($query);
58 $this->pending[$key] = $promise;
59 $this->counts[$key] = 1;
60
61 $pending =& $this->pending;
62 $counts =& $this->counts;
63 $promise->then(function () use ($key, &$pending, &$counts) {
64 unset($pending[$key], $counts[$key]);
65 }, function () use ($key, &$pending, &$counts) {
66 unset($pending[$key], $counts[$key]);
67 });
68 }
69
70 // Return a child promise awaiting the pending query.
71 // Cancelling this child promise should only cancel the pending query
72 // when no other child promise is awaiting the same query.
73 $pending =& $this->pending;
74 $counts =& $this->counts;
75 return new Promise(function ($resolve, $reject) use ($promise) {
76 $promise->then($resolve, $reject);
77 }, function () use (&$promise, $key, $query, &$pending, &$counts) {
78 if (--$counts[$key] < 1) {
79 unset($pending[$key], $counts[$key]);
80 $promise->cancel();
81 $promise = null;
82 }
83 throw new \RuntimeException('DNS query for ' . $query->describe() . ' has been cancelled');
84 });
85 }
86
87 private function serializeQueryToIdentity(Query $query)
88 {
89 return sprintf('%s:%s:%s', $query->name, $query->type, $query->class);
90 }
91 }