1: | <?php |
2: | |
3: | declare(strict_types=1); |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | |
18: | |
19: | |
20: | |
21: | |
22: | namespace OpenSearch; |
23: | |
24: | use OpenSearch\Common\Exceptions; |
25: | use OpenSearch\ConnectionPool\AbstractConnectionPool; |
26: | use OpenSearch\Connections\Connection; |
27: | use OpenSearch\Connections\ConnectionInterface; |
28: | use GuzzleHttp\Ring\Future\FutureArrayInterface; |
29: | use Psr\Log\LoggerInterface; |
30: | |
31: | class Transport |
32: | { |
33: | |
34: | |
35: | |
36: | public $connectionPool; |
37: | |
38: | |
39: | |
40: | |
41: | private $log; |
42: | |
43: | |
44: | |
45: | |
46: | public $retryAttempts = 0; |
47: | |
48: | |
49: | |
50: | |
51: | public $lastConnection; |
52: | |
53: | |
54: | |
55: | |
56: | public $retries; |
57: | |
58: | |
59: | |
60: | |
61: | |
62: | |
63: | |
64: | |
65: | |
66: | |
67: | public function __construct(int $retries, AbstractConnectionPool $connectionPool, LoggerInterface $log, bool $sniffOnStart = false) |
68: | { |
69: | $this->log = $log; |
70: | $this->connectionPool = $connectionPool; |
71: | $this->retries = $retries; |
72: | |
73: | if ($sniffOnStart === true) { |
74: | $this->log->notice('Sniff on Start.'); |
75: | $this->connectionPool->scheduleCheck(); |
76: | } |
77: | } |
78: | |
79: | |
80: | |
81: | |
82: | |
83: | public function getConnection(): ConnectionInterface |
84: | { |
85: | return $this->connectionPool->nextConnection(); |
86: | } |
87: | |
88: | |
89: | |
90: | |
91: | |
92: | |
93: | |
94: | |
95: | |
96: | |
97: | |
98: | |
99: | public function performRequest(string $method, string $uri, array $params = [], $body = null, array $options = []): FutureArrayInterface |
100: | { |
101: | try { |
102: | $connection = $this->getConnection(); |
103: | } catch (Exceptions\NoNodesAvailableException $exception) { |
104: | $this->log->critical('No alive nodes found in cluster'); |
105: | throw $exception; |
106: | } |
107: | |
108: | $response = []; |
109: | $caughtException = null; |
110: | $this->lastConnection = $connection; |
111: | |
112: | $future = $connection->performRequest( |
113: | $method, |
114: | $uri, |
115: | $params, |
116: | $body, |
117: | $options, |
118: | $this |
119: | ); |
120: | |
121: | $future->promise()->then( |
122: | |
123: | function ($response) { |
124: | $this->retryAttempts = 0; |
125: | |
126: | }, |
127: | |
128: | function ($response) { |
129: | $code = $response->getCode(); |
130: | |
131: | if ($code < 400 || $code >= 500) { |
132: | |
133: | $this->connectionPool->scheduleCheck(); |
134: | } |
135: | } |
136: | ); |
137: | |
138: | return $future; |
139: | } |
140: | |
141: | |
142: | |
143: | |
144: | |
145: | |
146: | |
147: | public function resultOrFuture(FutureArrayInterface $result, array $options = []) |
148: | { |
149: | $response = null; |
150: | $async = isset($options['client']['future']) ? $options['client']['future'] : null; |
151: | if (is_null($async) || $async === false) { |
152: | do { |
153: | $result = $result->wait(); |
154: | } while ($result instanceof FutureArrayInterface); |
155: | } |
156: | return $result; |
157: | } |
158: | |
159: | public function shouldRetry(array $request): bool |
160: | { |
161: | if ($this->retryAttempts < $this->retries) { |
162: | $this->retryAttempts += 1; |
163: | |
164: | return true; |
165: | } |
166: | |
167: | return false; |
168: | } |
169: | |
170: | |
171: | |
172: | |
173: | |
174: | public function getLastConnection(): ConnectionInterface |
175: | { |
176: | return $this->lastConnection; |
177: | } |
178: | } |
179: | |