2 HttpRequestPool chain with libevent
6 checkcls('HttpRequest');
7 checkcls('HttpRequestPool');
8 $pool = new RequestPool;
9 skipif(!@$pool->enableEvents(), "need libevent support");
17 ini_set('error_reporting', E_ALL);
18 ini_set('html_errors', 0);
20 class Request extends HttpRequest
26 $this->pool->detach($this);
29 $c = $this->getResponseCode();
30 $b = $this->getResponseBody();
32 printf("%d %s %d\n", $c, $u, strlen($b));
34 if ($c == 200 && $this->pool->dir) {
35 file_put_contents($this->pool->all[$u], $b);
38 if ($a = each($this->pool->rem)) {
39 list($url, $file) = $a;
42 HttpRequest::METH_GET,
47 'connecttimeout' => TOUT,
48 'lastmodified' => is_file($file)?filemtime($file):0
51 $r->pool = $this->pool;
52 $this->pool->attach($r);
58 class Pool extends HttpRequestPool
64 public final function __construct($urls_file = 'urls.txt', $cache_dir = 'HttpRequestPool_cache')
66 $this->dir = (is_dir($cache_dir) or @mkdir($cache_dir)) ? $cache_dir : null;
68 $urls = file($urls_file);
70 foreach (array_map('trim', $urls) as $url) {
71 $this->all[$url] = $this->dir ? $this->dir .'/'. md5($url) : null;
75 $now = array_slice($this->all, 0, RMAX);
76 $this->rem = array_slice($this->all, RMAX);
82 $this->enableEvents();
84 foreach ($now as $url => $file) {
87 HttpRequest::METH_GET,
92 'connecttimeout' => TOUT,
93 'lastmodified' => is_file($file)?filemtime($file):0
104 define('GZIP', true);
107 chdir(dirname(__FILE__));
109 $time = microtime(true);
111 printf("Elapsed: %0.3fs\n", microtime(true)-$time);