Fixed missing reuse of existing http connections which are kept-alive by remote servers (e.g. dedimania).

This commit is contained in:
Sebastian Büttner 2017-05-21 17:52:56 +02:00
parent 8241abdbf2
commit 2ec6fdbed9
2 changed files with 40 additions and 22 deletions

View File

@ -3,6 +3,7 @@
namespace ManiaControl\Files;
use cURL\Request;
use cURL\RequestsQueue;
use ManiaControl\General\UsageInformationAble;
use ManiaControl\General\UsageInformationTrait;
use ManiaControl\ManiaControl;
@ -27,8 +28,9 @@ class AsynchronousFileReader implements UsageInformationAble {
*/
/** @var ManiaControl $maniaControl */
private $maniaControl = null;
/** @var Request[] $requests */
private $requests = array();
/** @var \cURL\RequestsQueue|null $requestQueue */
private $requestQueue = null;
/**
* Construct a new Asynchronous File Reader Instance
@ -37,19 +39,22 @@ class AsynchronousFileReader implements UsageInformationAble {
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
$this->requestQueue = new RequestsQueue();
}
/**
* Append available Data of active Requests
*/
public function appendData() {
foreach ($this->requests as $key => $request) {
if ($request->socketPerform()) {
$request->socketSelect();
} else {
unset($this->requests[$key]);
do {
if (($count = $this->requestQueue->count()) == 0) {
break;
}
if ($this->requestQueue->socketPerform()) {
$this->requestQueue->socketSelect();
}
} while ($count != $this->requestQueue->count());
}
/**
@ -91,6 +96,6 @@ class AsynchronousFileReader implements UsageInformationAble {
* @param Request $request
*/
public function addRequest(Request $request) {
array_push($this->requests, $request);
$request->attachTo($this->requestQueue);
}
}

View File

@ -33,18 +33,6 @@ class Request extends EventDispatcher implements RequestInterface
$this->ch = curl_init();
}
/**
* Closes cURL resource and frees the memory.
* It is neccessary when you make a lot of requests
* and you want to avoid fill up the memory.
*/
public function __destruct()
{
if (isset($this->ch)) {
curl_close($this->ch);
}
}
/**
* Get the cURL\Options instance
* Creates empty one if does not exist
@ -70,6 +58,17 @@ class Request extends EventDispatcher implements RequestInterface
$this->options = $options;
}
/**
* Closes cURL resource and frees the memory.
* It is neccessary when you make a lot of requests
* and you want to avoid fill up the memory.
*/
public function __destruct() {
if (isset($this->ch)) {
curl_close($this->ch);
}
}
/**
* Returns cURL raw resource
*
@ -116,6 +115,20 @@ class Request extends EventDispatcher implements RequestInterface
return $response;
}
/**
* Binds the request to a given RequestQueue.
*
* @param \cURL\RequestsQueue $requestsQueue
* @throws \cURL\Exception
*/
public function attachTo(RequestsQueue $requestsQueue) {
if (isset($this->queue)) {
throw new Exception('Already bound to a RequestQueue.');
}
$this->queue = $requestsQueue;
$this->queue->attach($this);
}
/**
* Creates new RequestsQueue with single Request attached to it
* and calls RequestsQueue::socketPerform() method.