use*_*260 4 algorithm service throttling rate-limiting
我需要为节流请求设计一个速率限制器服务。对于每个传入的请求,方法将检查每秒的请求是否超过其限制。如果已超过,它将返回等待处理的时间。
寻找一种简单的解决方案,仅使用系统刻度计数和rps(每秒请求数)。不应使用队列或复杂的速率限制算法和数据结构。
编辑:我将在C ++中实现。另外,请注意,我不想使用任何数据结构来存储当前正在执行的请求。API将如下所示:
如果(!RateLimiter.Limit()){做RateLimiter.Done();
} else拒绝请求
因为您没有给出语言或平台的提示,所以我只会给出一些伪代码。
你需要的东西
代码可以很简单
var ListOfCurrentRequests; //A list of the start time of current requests
var MaxAmoutOfRequests;// just a limit
var AverageExecutionTime;//if the execution time is non deterministic the best we can do is have a average
//for each request ether execute or return the PROBABLE amount to wait
function OnNewRequest(Identifier)
{
if(count(ListOfCurrentRequests) < MaxAmoutOfRequests)//if we have room
{
Struct Tracker
Tracker.Request = Identifier;
Tracker.StartTime = Now; // save the start time
AddToList(Tracker) //add to list
}
else
{
return CalculateWaitTime()//return the PROBABLE time it will take for a 'slot' to be available
}
}
//when request as ended release a 'slot' and update the average execution time
function OnRequestEnd(Identifier)
{
Tracker = RemoveFromList(Identifier);
UpdateAverageExecutionTime(Now - Tracker.StartTime);
}
function CalculateWaitTime()
{
//the one that started first is PROBABLY the first to finish
Tracker = GetTheOneThatIsRunnigTheLongest(ListOfCurrentRequests);
//assume the it will finish in avg time
ProbableTimeToFinish = AverageExecutionTime - Tracker.StartTime;
return ProbableTimeToFinish
}
Run Code Online (Sandbox Code Playgroud)
但请记住,这有几个问题
所以理想的解决方案应该是一个实际的执行队列,但因为你不想要一个..我想这是下一个最好的事情。