示例#1
0
文件: ifconfig.php 项目: offers/rho
 * Use Rho to infinitely retry getting our IP from ifconfig.me,
 * until the script is killed with Ctrl+C
 */
require "../vendor/autoload.php";
use Rho\Exception;
use Rho\Transport\HttpJsonTransport;
use Rho\Retrier;
use Rho\RateLimiter\SimpleRateLimiter;
use Rho\Exception\TooManyRetriesException;
use Monolog\Logger;
function showException($e)
{
    echo $e->getMessage() . "\n";
}
$logger = new Logger('ifconfig');
$client = new HttpJsonTransport("http://ifconfig.me", ['logger' => $logger]);
$client = SimpleRateLimiter::wrap($client, ['logger' => $logger, 'limits' => [10 => 1]]);
// limit to 1 request every 10s
$client = Retrier::wrap($client, ['logger' => $logger, 'delay' => 0.5]);
// retry forever with 0.5s delay
try {
    $resp = $client->rpc(['GET', '/all.json'], [], ['timeout' => 5, 'connect_timeout' => 5]);
    if (!$resp->isError()) {
        $data = $resp->getResult();
        echo "Your IP is {$data['ip_addr']}\n";
    } else {
        echo "API Server error\n";
    }
} catch (TooManyRetriesException $e) {
    showException($e);
}
示例#2
0
文件: bitcoin.php 项目: offers/rho
use Rho\CircuitBreaker\SimpleCircuitBreaker;
use Rho\Retrier;
use Rho\RateLimiter\SimpleRateLimiter;
use Rho\Metrics;
use Rho\BlackHole;
function showException($e)
{
    echo $e->getMessage() . "\n";
}
$client = new HttpJsonTransport("http://api.coindesk.com");
$client = Metrics::wrap($client, ['collector' => new BlackHole(), 'name' => 'bitcoin']);
// in production, use a DataDoggyDog or statsd client instead of BlackHole
$client = SimpleRateLimiter::wrap($client, ['limits' => [5 => 2]]);
// limit to 2 requests every 5s
$client = SimpleCircuitBreaker::wrap($client, ['failThreshold' => 3, 'resetTime' => 10]);
// allow only 3 fails every 10s
$client = Retrier::wrap($client, ['retries' => 10, 'delay' => 1.0]);
// retry up to 10 times, delaying 1s each
try {
    $resp = $client->rpc(['GET', '/v1/bpi/currentprice/USD.json'], [], ['timeout' => 3, 'connect_timeout' => 3]);
    if (!$resp->isError()) {
        $data = $resp->getResult();
        echo money_format("Current Bitcoin price in USD is \$%i\n", $data['bpi']['USD']['rate']);
    } else {
        echo "API Server error\n";
    }
} catch (Rho\Exception\CircuitBreakerOpenException $e) {
    showException($e);
} catch (Rho\Exception\TooManyRetriesException $e) {
    showException($e);
}