-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.php
More file actions
66 lines (57 loc) · 2.12 KB
/
Copy pathexample.php
File metadata and controls
66 lines (57 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
/**
* Orbit — runnable demo. `php example.php`
*
* Uses a scripted prober so the demo needs no live proxies or network.
* In real use you'd just call `$pool->check()` and let the curl_multi prober
* test them for you.
*
* @author Vxsilisk — Orbit
* @license MIT
*/
require __DIR__ . '/autoload.php';
$pool = Pool::fromList([
'203.0.113.1:8080',
'user:secret@203.0.113.2:3128',
'socks5://203.0.113.3:1080',
'203.0.113.4:8080',
])->strategy(Strategy::LeastLatency);
echo "Loaded {$pool->count()} proxies.\n\n";
// Pretend a health check came back (in production: $pool->check('https://httpbin.org/ip')).
$scripted = new class(['203.0.113.4:8080']) implements ProberInterface {
public function __construct(private array $deadHosts) {}
public function probe(array $proxies, string $url, int $timeout, int $concurrency): array
{
$latencies = [0.42, 0.19, 0.88, 0.0];
$out = [];
foreach (array_values($proxies) as $i => $p) {
$dead = in_array("{$p->host}:{$p->port}", $this->deadHosts, true);
$out[$p->id()] = ['ok' => !$dead, 'latency' => $dead ? null : $latencies[$i]];
}
return $out;
}
};
$summary = $pool->check(prober: $scripted);
echo "Health check: {$summary['alive']} alive, {$summary['dead']} dead of {$summary['checked']}.\n\n";
echo "Rotating (least-latency first):\n";
for ($i = 0; $i < 5; $i++) {
$proxy = $pool->tryNext();
if ($proxy === null) {
echo " pool exhausted\n";
break;
}
$latency = $proxy->latency !== null ? sprintf('%.0fms', $proxy->latency * 1000) : '—';
echo " → {$proxy->id()} ({$latency})\n";
// Simulate the 3rd pick failing, so it drops into cooldown.
$pool->report($proxy, $i !== 2, $proxy->latency);
}
echo "\nPool stats:\n";
foreach ($pool->stats() as $key => $value) {
printf(" %-13s %s\n", $key, is_float($value) ? sprintf('%.3fs', $value) : $value);
}
// The intended pairing with PulsarX:
//
// $s = new Pulsar();
// $proxy = $pool->next();
// $r = $s->get($url, server: $proxy->forPulsar());
// $pool->report($proxy, $r->ok(), $r->getElapsed());