@@ -49,6 +49,11 @@ class Waiter
4949 */
5050 private $ finalState ;
5151
52+ /**
53+ * @var bool
54+ */
55+ private $ resolved = false ;
56+
5257 public function __construct (Response $ response , AbstractApi $ awsClient , $ request )
5358 {
5459 $ this ->response = $ response ;
@@ -58,7 +63,9 @@ public function __construct(Response $response, AbstractApi $awsClient, $request
5863
5964 public function __destruct ()
6065 {
61- $ this ->resolve ();
66+ if (!$ this ->resolved ) {
67+ $ this ->resolve ();
68+ }
6269 }
6370
6471 final public function isSuccess (): bool
@@ -91,10 +98,12 @@ final public function getState(): string
9198 $ exception = null ;
9299 } catch (HttpException $ exception ) {
93100 // use $exception later
101+ } finally {
102+ $ this ->resolved = true ;
103+ $ this ->needRefresh = true ;
94104 }
95105
96106 $ state = $ this ->extractState ($ this ->response , $ exception );
97- $ this ->needRefresh = true ;
98107
99108 switch ($ state ) {
100109 case self ::STATE_SUCCESS :
@@ -126,6 +135,8 @@ final public function resolve(?float $timeout = null): bool
126135 return $ this ->response ->resolve ($ timeout );
127136 } catch (HttpException $ exception ) {
128137 return true ;
138+ } finally {
139+ $ this ->resolved = true ;
129140 }
130141 }
131142
@@ -148,6 +159,7 @@ final public function cancel(): void
148159 {
149160 $ this ->response ->cancel ();
150161 $ this ->needRefresh = true ;
162+ $ this ->resolved = true ;
151163 }
152164
153165 /**
@@ -161,18 +173,27 @@ final public function cancel(): void
161173 */
162174 final public function wait (float $ timeout = null , float $ delay = null ): bool
163175 {
176+ if (null !== $ this ->finalState ) {
177+ return true ;
178+ }
179+
164180 $ timeout = $ timeout ?? static ::WAIT_TIMEOUT ;
165181 $ delay = $ delay ?? static ::WAIT_DELAY ;
166182
167183 $ start = \microtime (true );
168184 while (true ) {
185+ if ($ this ->needRefresh ) {
186+ $ this ->stealResponse ($ this ->refreshState ());
187+ }
188+
169189 // If request times out
170190 if (!$ this ->resolve ($ timeout - (\microtime (true ) - $ start ))) {
171191 break ;
172192 }
173193
194+ $ this ->getState ();
174195 // If we reached a final state
175- if (\in_array ( $ this ->getState (), [ self :: STATE_SUCCESS , self :: STATE_FAILURE ]) ) {
196+ if ($ this ->finalState ) {
176197 return true ;
177198 }
178199
@@ -182,7 +203,6 @@ final public function wait(float $timeout = null, float $delay = null): bool
182203 }
183204
184205 \usleep ((int ) ceil ($ delay * 1000000 ));
185- $ this ->stealResponse ($ this ->refreshState ());
186206 }
187207
188208 return false ;
@@ -201,6 +221,8 @@ protected function refreshState(): Waiter
201221 private function stealResponse (self $ waiter ): void
202222 {
203223 $ this ->response = $ waiter ->response ;
224+ $ this ->resolved = $ waiter ->resolved ;
225+ $ waiter ->resolved = true ;
204226 $ this ->needRefresh = false ;
205227 }
206228}
0 commit comments