1212use GuzzleHttp \Command \Guzzle \Description ;
1313use GuzzleHttp \Command \Guzzle \GuzzleClient ;
1414use GuzzleHttp \Command \Guzzle \Deserializer ;
15+ use GuzzleHttp \Exception \RequestException ;
1516use GuzzleHttp \Command \CommandInterface ;
1617use GuzzleHttp \Command \Exception \CommandException ;
18+ use GuzzleHttp \Exception \ConnectException ;
1719use GuzzleHttp \Middleware ;
1820use GuzzleHttp \Psr7 ;
1921
7577 * @method object SelectObjectContent (array $arg)
7678 */
7779class Client extends GuzzleClient {
78- const VERSION = '2.1.2 ' ;
80+ const VERSION = '2.1.3 ' ;
7981
8082 public $ httpClient ;
8183
@@ -110,6 +112,7 @@ public function __construct($cosConfig) {
110112
111113 $ service = Service::getService ();
112114 $ handler = HandlerStack::create ();
115+ $ handler ->push (Middleware::retry ($ this ->retryDecide (), $ this ->retryDelay ()));
113116 $ handler ->push (Middleware::mapRequest (function (RequestInterface $ request ) {
114117 return $ request ->withHeader ('User-Agent ' , $ this ->cosConfig ['userAgent ' ]);
115118 }));
@@ -135,6 +138,38 @@ public function __construct($cosConfig) {
135138 'commandToRequestTransformer ' ], [$ this , 'responseToResultTransformer ' ],
136139 null );
137140 }
141+ public function retryDecide () {
142+ return function (
143+ $ retries ,
144+ RequestInterface $ request ,
145+ ResponseInterface $ response = null ,
146+ \Exception $ exception = null
147+ ) {
148+ if ($ retries >= $ this ->cosConfig ['retry ' ]) {
149+ return false ;
150+ }
151+ if ($ response != null && $ response ->getStatusCode () >= 400 ) {
152+ return true ;
153+ }
154+ if ($ exception instanceof \Qcloud \Cos \Exception \ServiceResponseException) {
155+ if ($ exception ->getStatusCode () >= 400 ) {
156+ return true ;
157+ }
158+ }
159+
160+ if ($ exception instanceof ConnectException) {
161+ return true ;
162+ }
163+
164+ return false ;
165+ };
166+ }
167+
168+ public function retryDelay () {
169+ return function ($ numberOfRetries ) {
170+ return 1000 * $ numberOfRetries ;
171+ };
172+ }
138173 public function commandToRequestTransformer (CommandInterface $ command )
139174 {
140175 $ this ->action = $ command ->GetName ();
@@ -168,21 +203,15 @@ public function __destruct() {
168203 }
169204
170205 public function __call ($ method , array $ args ) {
171- for ($ i = 0 ; $ i <= $ this ->cosConfig ['retry ' ]; $ i ++) {
172- try {
173- $ rt = parent ::__call (ucfirst ($ method ), $ args );
174- return $ rt ;
175- } catch (\Exception $ e ) {
176- if ($ i != $ this ->cosConfig ['retry ' ]) {
177- sleep (1 << ($ i ));
178- continue ;
179- }
180- $ previous = $ e ->getPrevious ();
181- if ($ previous !== null ) {
182- throw $ previous ;
183- } else {
184- throw $ e ;
185- }
206+ try {
207+ $ rt = parent ::__call (ucfirst ($ method ), $ args );
208+ return $ rt ;
209+ } catch (\Exception $ e ) {
210+ $ previous = $ e ->getPrevious ();
211+ if ($ previous !== null ) {
212+ throw $ previous ;
213+ } else {
214+ throw $ e ;
186215 }
187216 }
188217 }
0 commit comments