Skip to content

Commit

Permalink
Bugfix/connection limit (#27)
Browse files Browse the repository at this point in the history
* Limited number of simultaneous connections to prevent timeout errors
* Handle device token in response class
  • Loading branch information
ehuebner authored and edamov committed Nov 2, 2017
1 parent d6aa452 commit c559f85
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
38 changes: 18 additions & 20 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
21 changes: 20 additions & 1 deletion src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ class Response implements ApnsResponseInterface
*/
private $apnsId;

/**
* Device token.
*
* @var string|null
*/
private $deviceToken;

/**
* Response status code.
*
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit c559f85

Please sign in to comment.