Skip to content

Instantly share code, notes, and snippets.

@nowelium
Created August 13, 2010 03:59
Show Gist options
  • Save nowelium/522243 to your computer and use it in GitHub Desktop.
Save nowelium/522243 to your computer and use it in GitHub Desktop.
<?php
// concurrent http request safe check: (PubSub tests)
//
// depends {HttpRequest, HttpRequestPool} => pecl_http
//
class MyRequest extends HttpRequest {
protected $id;
protected $time;
public function __construct($id){
$this->id = $id;
$this->time = microtime(true);
parent::__construct('http://localhost:8080/api/' . $id, HTTP_METH_GET);
}
public function getId(){
return $this->id;
}
public function getTime(){
return $this->time;
}
public function getDiff(){
return microtime(true) - $this->time;
}
}
$pool = new HttpRequestPool;
$pool->attach(new MyRequest('a'));
$pool->attach(new MyRequest('b'));
$pool->attach(new MyRequest('c'));
$pool->attach(new MyRequest('d'));
$pool->attach(new MyRequest('e'));
$pool->attach(new MyRequest('f'));
$pool->send();
foreach ($pool->getFinishedRequests() as $req){
echo 'finish: ', $req->getId(), ':', $req->getResponseBody(), ':', $req->getDiff(), PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment