限速传出PHP +卷曲请求 [英] Rate limit outgoing PHP+curl requests

查看:132
本文介绍了限速传出PHP +卷曲请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有率限制(有延迟)即将离任的PHP +卷曲请求到外部服务器的方式,以便有n只有每秒请求数? PHP在FastCGI的模式,所以不可能用用睡眠。

Is there a way to rate limit (with delay) outgoing PHP+curl requests to an external server, so that there are only n requests per second? PHP is used in Fastcgi mode so not possible to use sleep.

推荐答案

您可以控制​​与令牌率桶算法。当你要控制一个资源所有PHP进程的速度,您将需要共享桶的状态。 带宽节流/令牌桶:您可以用我的执行线程的方式做到这一点

You can control the rate with the token bucket algorithm. As you want to control the rate of all your PHP processes for one resource you will need to share the state of the bucket. You can do this in a threadsafe way with my implementation: bandwidth-throttle/token-bucket

use bandwidthThrottle\tokenBucket\Rate;
use bandwidthThrottle\tokenBucket\TokenBucket;
use bandwidthThrottle\tokenBucket\BlockingConsumer;
use bandwidthThrottle\tokenBucket\storage\FileStorage;

$storage  = new FileStorage(__DIR__ . "/api.bucket");
$rate     = new Rate(10, Rate::SECOND);
$bucket   = new TokenBucket(10, $rate, $storage);
$consumer = new BlockingConsumer($bucket);
$bucket->bootstrap(10);

// This blocks all processes to the rate of 10 requests/second
$consumer->consume(1);

$api->doSomething();

这篇关于限速传出PHP +卷曲请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆