-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFastClient.php
271 lines (228 loc) · 8.86 KB
/
FastClient.php
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php
/**
* FastClient.php
*
* Implements a parallelized version of the Amazon SimpleDB client.
* This client utilizes cURL and the curl_multi* library to send
* the requests.
*
* This class requires changes to the original Amazon_SimpleDB_Client
* class. All of the private classes will need to be made protected.
* This is so that the FastClient can inherit them from the original
* Client.
*
* This file should be placed in the same directory as its parent
* class, Client.php.
*
* Copyright (c) 2008-2009 Joel Poloney
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
require_once ("Amazon/SimpleDB/Client.php");
class Amazon_SimpleDB_Fast_Client extends Amazon_SimpleDB_Client {
/**
* This will perform a parallelized request to get all of the objects.
*
* @param Array $actions This is an array of Amazon_SimpleDB_Model_GetAttributes objects.
*
* @return Array This is an array of Amazon_SimpleDB_Model_GetAttributesResponse objects.
*/
public function getAttributes($actions) {
if (!is_array($actions)) {
throw new Exception ("Parameter supplied to " . __FUNCTION__ . " is not an array.");
}
require_once ('Amazon/SimpleDB/Model/GetAttributesResponse.php');
$parameters = array();
foreach ($actions as $action) {
$parameters[] = $this->_convertGetAttributes($action);
}
$results = $this->_invoke($parameters);
$objects = array();
foreach ($results as $result) {
if($result != "") {
$objects[] = Amazon_SimpleDB_Model_GetAttributesResponse::fromXML($result);
}
}
return $objects;
}
/**
* This will perform a parallelized request to put all of the objects.
*
* @param Array $actions This is an array of Amazon_SimpleDB_Model_PutAttributes objects.
*
* @return Array This is an array of Amazon_SimpleDB_Model_PutAttributesResponse objects.
*/
public function putAttributes($actions) {
if (!is_array($actions)) {
throw new Exception ("Parameter supplied to " . __FUNCTION__ . " is not an array.");
}
require_once ('Amazon/SimpleDB/Model/PutAttributesResponse.php');
$parameters = array();
foreach ($actions as $action) {
$parameters[] = $this->_convertPutAttributes($action);
}
$results = $this->_invoke($parameters);
$objects = array();
foreach ($results as $result) {
if($result != "") {
$objects[] = Amazon_SimpleDB_Model_PutAttributesResponse::fromXML($result);
}
}
return $objects;
}
/**
* This will perform a parallelized request to delete all of the objects.
*
* @param Array $actions This is an array of Amazon_SimpleDB_Model_DeleteAttributes objects.
*
* @return Array This is an array of Amazon_SimpleDB_Model_DeleteAttributesResponse objects.
*/
public function deleteAttributes($actions) {
if (!is_array($actions)) {
throw new Exception ("Parameter supplied to " . __FUNCTION__ . " is not an array.");
}
require_once ('Amazon/SimpleDB/Model/DeleteAttributesResponse.php');
$parameters = array();
foreach ($actions as $action) {
$parameters[] = $this->_convertDeleteAttributes($action);
}
$results = $this->_invoke($parameters);
$objects = array();
foreach ($results as $result) {
if($result != "") {
$objects[] = Amazon_SimpleDB_Model_DeleteAttributesResponse::fromXML($result);
}
}
return $objects;
}
/** Invoke request and return response. */
protected function _invoke(array $parameters) {
$actionName = $parameters["Action"];
$responses = array();
$responseBodies = array();
$shouldRetry = FALSE;
// submit the request and read response body
try {
// add required request parameters
$parameters = $this->_addRequiredParameters($parameters);
$retries = 0;
do {
// exponential sleep on failed requests
if($shouldRetry) {
$this->_pauseOnRetry(++$retries, 503);
$shouldRetry = FALSE;
}
// make sure we use the normal client for only 1 request and the FastClient for multiple requests
if(is_array($parameters) && count($parameters) == 1) {
$parameter = array_pop($parameters);
$response = parent::_httpPost($parameter);
$responses = array($response);
} else {
$responses = $this->_httpPost($parameters);
}
// loop to make sure we received successful response codes (retry the FastClient if necessary)
foreach ($responses as $key => &$response) {
try {
if ($response['Status'] === 200) {
$responseBodies[] = $response['ResponseBody'];
unset($parameters[$key]);
} else {
if ($response['Status'] === 500 || $response['Status'] === 503) {
$shouldRetry = TRUE;
} else {
throw $this->_reportAnyErrors($response['ResponseBody'], $response['Status']);
}
}
} catch (Exception $e) {
require_once ('Amazon/SimpleDB/Exception.php');
if ($e instanceof Amazon_SimpleDB_Exception) {
throw $e;
} else {
require_once ('Amazon/SimpleDB/Exception.php');
throw new Amazon_SimpleDB_Exception(array('Exception' => $e, 'Message' => $e->getMessage()));
}
}
}
} while ($shouldRetry);
} catch (Amazon_SimpleDB_Exception $se) {
throw $se;
} catch (Exception $t) {
throw new Amazon_SimpleDB_Exception(array('Exception' => $t, 'Message' => $t->getMessage()));
}
return $responseBodies;
}
/** Perform HTTP post with exponential retries on error 500 and 503. */
protected function _httpPost(array $parameters) {
$curly = array(); // array of curl handles
$result = array(); // data to be returned
$multiHandle = curl_multi_init(); // multi handle
$url = parse_url ($this->_config['ServiceURL']);
$port = array_key_exists('port',$url) ? $url['port'] : null;
switch ($url['scheme']) {
case 'https':
$port = $port === null ? 443 : $port;
break;
default:
$port = $port == null ? 80 : $port;
break;
}
foreach ($parameters as $key => $data) {
$curly[$key] = curl_init($url['scheme'] . "://" . $url['host']);
$data = $this->_getParametersAsString($data);
curl_setopt($curly[$key], CURLOPT_POST, 1);
curl_setopt($curly[$key], CURLOPT_POSTFIELDS, $data);
curl_setopt($curly[$key], CURLOPT_PORT, $port);
curl_setopt($curly[$key], CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curly[$key], CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded; charset=utf-8"));
curl_setopt($curly[$key], CURLOPT_HEADER, 1);
curl_setopt($curly[$key], CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
// these are commented out do to Internal Errors from SimpleDB
// curl_setopt($curly[$key], CURLOPT_CONNECTTIMEOUT, 5);
// curl_setopt($curly[$key], CURLOPT_TIMEOUT, 10);
curl_multi_add_handle($multiHandle, $curly[$key]);
}
$running = null;
do {
curl_multi_exec($multiHandle, $running);
} while($running > 0);
foreach($curly as $key => $content) {
$result[$key] = curl_multi_getcontent($content);
curl_multi_remove_handle($multiHandle, $content);
}
curl_multi_close($multiHandle);
foreach($result as &$response) {
list($other, $responseBody) = explode("\r\n\r\n", $response, 2);
$other = preg_split("/\r\n|\n|\r/", $other);
list($protocol, $code, $text) = explode(' ', trim(array_shift($other)), 3);
$response = array ('Status' => (int)$code, 'ResponseBody' => $responseBody);
}
return $result;
}
/** Add authentication related and version parameters */
protected function _addRequiredParameters(array $parameters) {
foreach ($parameters as &$parameter) {
$parameter = parent::_addRequiredParameters($parameter);
}
return $parameters;
}
}
?>