From c559f85fbe58b0fa0a86c2121dbbc44152092848 Mon Sep 17 00:00:00 2001 From: ehuebner Date: Thu, 2 Nov 2017 11:22:25 +0100 Subject: [PATCH] Bugfix/connection limit (#27) * Limited number of simultaneous connections to prevent timeout errors * Handle device token in response class --- src/Client.php | 38 ++++++++++++++++++-------------------- src/Response.php | 21 ++++++++++++++++++++- 2 files changed, 38 insertions(+), 21 deletions(-) diff --git a/src/Client.php b/src/Client.php index 77143a8..fd236ab 100644 --- a/src/Client.php +++ b/src/Client.php @@ -74,32 +74,30 @@ public function push(): array curl_setopt_array($ch, $request->getOptions()); curl_setopt($ch, CURLOPT_HTTPHEADER, $request->getDecoratedHeaders()); - - curl_multi_add_handle($mh, $ch); } - $active = null; - do { - $mrc = curl_multi_exec($mh, $active); - } while ($mrc == CURLM_CALL_MULTI_PERFORM); - - while ($active && $mrc == CURLM_OK) { - if (curl_multi_select($mh) == -1) { - usleep(1); + $handleChunks = array_chunk($handles, 10); + foreach ($handleChunks as $handleChunk) { + foreach ($handleChunk as $handle) { + curl_multi_add_handle($mh, $handle); } + + $running = null; do { - $mrc = curl_multi_exec($mh, $active); - } while ($mrc == CURLM_CALL_MULTI_PERFORM); - } + while(($execrun = curl_multi_exec($mh, $running)) == CURLM_CALL_MULTI_PERFORM); + + while($done = curl_multi_info_read($mh)) { + $handle = $done['handle']; - $responseCollection = []; - foreach ($handles as $handle) { - curl_multi_remove_handle($mh, $handle); - $result = curl_multi_getcontent($handle); + $result = curl_multi_getcontent($handle); + $token = curl_getinfo($handle, CURLINFO_PRIVATE); - list($headers, $body) = explode("\r\n\r\n", $result, 2); - $statusCode = curl_getinfo($handle, CURLINFO_HTTP_CODE); - $responseCollection[] = new Response($statusCode, $headers, $body); + list($headers, $body) = explode("\r\n\r\n", $result, 2); + $statusCode = curl_getinfo($handle, CURLINFO_HTTP_CODE); + $responseCollection[] = new Response($statusCode, $headers, $body, $token); + curl_multi_remove_handle($mh, $handle); + } + } while ($running); } curl_multi_close($mh); diff --git a/src/Response.php b/src/Response.php index 570ad39..4825389 100644 --- a/src/Response.php +++ b/src/Response.php @@ -112,6 +112,13 @@ class Response implements ApnsResponseInterface */ private $apnsId; + /** + * Device token. + * + * @var string|null + */ + private $deviceToken; + /** * Response status code. * @@ -132,12 +139,14 @@ class Response implements ApnsResponseInterface * @param int $statusCode * @param string $headers * @param string $body + * @param string $deviceToken */ - public function __construct(int $statusCode, string $headers, string $body) + public function __construct(int $statusCode, string $headers, string $body, string $deviceToken = null) { $this->statusCode = $statusCode; $this->apnsId = self::fetchApnsId($headers); $this->errorReason = self::fetchErrorReason($body); + $this->deviceToken = $deviceToken; } /** @@ -184,6 +193,16 @@ public function getApnsId() return $this->apnsId; } + /** + * Get device token + * + * @return string|null + */ + public function getDeviceToken() + { + return $this->deviceToken; + } + /** * Get status code. *