-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathWaiter.php
More file actions
228 lines (191 loc) · 5.55 KB
/
Waiter.php
File metadata and controls
228 lines (191 loc) · 5.55 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
declare(strict_types=1);
namespace AsyncAws\Core;
use AsyncAws\Core\Exception\Http\HttpException;
use AsyncAws\Core\Exception\Http\NetworkException;
use AsyncAws\Core\Exception\LogicException;
/**
* The waiter promise is always returned from every API call to a waiter.
*/
class Waiter
{
public const STATE_SUCCESS = 'success';
public const STATE_FAILURE = 'failure';
public const STATE_PENDING = 'pending';
protected const WAIT_TIMEOUT = 30.0;
protected const WAIT_DELAY = 5.0;
/**
* @var AbstractApi|null
*/
protected $awsClient;
/**
* Input used to build the API request that generate this Waiter.
*
* @var object|null
*/
protected $input;
/**
* @var Response
*/
private $response;
/**
* Whether or not a new response should be fetched.
*
* @var bool
*/
private $needRefresh = false;
/**
* @var string|null
*/
private $finalState;
/**
* @var bool
*/
private $resolved = false;
public function __construct(Response $response, AbstractApi $awsClient, ?object $request)
{
$this->response = $response;
$this->awsClient = $awsClient;
$this->input = $request;
}
public function __destruct()
{
if (!$this->resolved) {
$this->resolve();
}
}
final public function isSuccess(): bool
{
return self::STATE_SUCCESS === $this->getState();
}
final public function isFailure(): bool
{
return self::STATE_FAILURE === $this->getState();
}
final public function isPending(): bool
{
return self::STATE_PENDING === $this->getState();
}
final public function getState(): string
{
if (null !== $this->finalState) {
return $this->finalState;
}
if ($this->needRefresh) {
$this->stealResponse($this->refreshState());
}
try {
$this->response->resolve();
$exception = null;
} catch (HttpException $exception) {
// use $exception later
} finally {
$this->resolved = true;
$this->needRefresh = true;
}
$state = $this->extractState($this->response, $exception);
switch ($state) {
case self::STATE_SUCCESS:
case self::STATE_FAILURE:
$this->finalState = $state;
break;
case self::STATE_PENDING:
break;
default:
throw new LogicException(\sprintf('Unexpected state "%s" from Waiter "%s".', $state, __CLASS__));
}
return $state;
}
/**
* Make sure the actual request is executed.
*
* @param float|null $timeout Duration in seconds before aborting. When null wait until the end of execution.
*
* @return bool false on timeout. True if the response has returned with as status code.
*
* @throws NetworkException
*/
final public function resolve(?float $timeout = null): bool
{
try {
return $this->response->resolve($timeout);
} catch (HttpException $exception) {
return true;
} finally {
$this->resolved = true;
}
}
/**
* Returns info on the current request.
*
* @return array{
* resolved: bool,
* body_downloaded: bool,
* response: \Symfony\Contracts\HttpClient\ResponseInterface,
* status: int,
* }
*/
final public function info(): array
{
return $this->response->info();
}
final public function cancel(): void
{
$this->response->cancel();
$this->needRefresh = true;
$this->resolved = true;
}
/**
* Wait until the state is success.
* Stopped when the state become Failure or the defined timeout is reached.
*
* @param float $timeout Duration in seconds before aborting
* @param float $delay Duration in seconds between each check
*
* @return bool true if a final state was reached
*/
final public function wait(?float $timeout = null, ?float $delay = null): bool
{
if (null !== $this->finalState) {
return true;
}
$timeout = $timeout ?? static::WAIT_TIMEOUT;
$delay = $delay ?? static::WAIT_DELAY;
$start = microtime(true);
while (true) {
if ($this->needRefresh) {
$this->stealResponse($this->refreshState());
}
// If request times out
if (!$this->resolve($timeout - (microtime(true) - $start))) {
break;
}
$this->getState();
// If we reached a final state
if ($this->finalState) {
return true;
}
// If the timeout will expire during our sleep, then exit early.
if ($delay > $timeout - (microtime(true) - $start)) {
break;
}
usleep((int) ceil($delay * 1000000));
}
return false;
}
protected function extractState(Response $response, ?HttpException $exception): string
{
return self::STATE_PENDING;
}
protected function refreshState(): Waiter
{
return $this;
}
private function stealResponse(self $waiter): void
{
$this->response = $waiter->response;
$this->resolved = $waiter->resolved;
$waiter->resolved = true;
$this->needRefresh = false;
}
}