简体   繁体   English

控制每秒吞吐量

[英]Control throughput per second

I have a class witch is responsible for sending data to a client and all others classes use this when they need to send data.我有一个 class 女巫负责向客户端发送数据,所有其他类在需要发送数据时都使用它。 Let's call it 'DataSender.class'.我们称它为“DataSender.class”。

Now the client is asking us to control the throughput to a maximum of 50 calls per second.现在客户要求我们将吞吐量控制在每秒最多 50 次调用。

I need to create an algoritm on this class (if possible) to keep the number of calls in the current second and if it reaches the maximum of 50, hold the process either with a sleep or something and continue without losing data.我需要在这个 class 上创建一个算法(如果可能的话)以保持当前秒内的调用次数,如果它达到最大值 50,则通过睡眠或其他方式保持进程并继续而不会丢失数据。 Maybe I have to implement a queue or something better than a simple sleep.也许我必须实现一个队列或比简单睡眠更好的东西。 I need sugestions or a direction to follow.我需要建议或指导。

For the sake of simplicity, just imagine that everyone is using something like this and I cannot change how they call me now.为了简单起见,假设每个人都在使用这样的东西,我无法改变他们现在如何称呼我。 post() return is syncronous but that maybe I can possibly change (not sure yet): post() 返回是同步的,但也许我可以改变(还不确定):

DataSender ds = new DataSender();
ds.setdata(mydata);
if (ds.post()) {
   //data send succesfull
}

If I am not mistaken, what you are looking for is throttling or rate limiting.如果我没记错的话,您正在寻找的是节流或速率限制。

As Andrew S pointed out, you will need a Queue, to hold the extra requests and a sender algorithm.正如 Andrew S 指出的那样,您将需要一个队列来保存额外的请求和一个发送方算法。

The main point is that because you are not sending the data right away, the callers need to be aware that the data is not necessarily sent when their call returns.要点是因为您没有立即发送数据,调用者需要知道当他们的调用返回时数据不一定发送。 Usually senders will not be happy, if their call returns, they assume data is sent, and then the data is lost.通常发件人不会高兴,如果他们的电话返回,他们假设数据已发送,然后数据丢失。 There are many reasons why data can be lost in this scenario.在这种情况下数据丢失的原因有很多。 As Andrew S pointed out, making senders aware that it will be an asynchronous send queue, maybe with confirmations upon successful send, will be a safer or proper approach.正如 Andrew S 指出的那样,让发送者意识到这将是一个异步发送队列,可能在成功发送后进行确认,这将是一种更安全或正确的方法。

You will need to decide on the size of the queue, you have to limit the size or you can run out of memory. And you need to decide what happens with the request when queue is full.您将需要决定队列的大小,您必须限制大小,否则您可能会用完 memory。并且您需要决定当队列已满时请求会发生什么。 Also what happens when the end point is not accessible (server down,.network issues, solar flares), keep accepting data to queue or reject / throw exception.此外,当端点不可访问时会发生什么(服务器关闭、网络问题、太阳耀斑),继续接受数据排队或拒绝/抛出异常。

Hint: if you have 50 requests limit, don't blast 50 requests and then sleep for 1 second.提示:如果您有 50 个请求限制,请不要爆破 50 个请求然后休眠 1 秒。 Figure out the interval between sends, send one request, make a short interval sleep.弄清楚发送之间的间隔,发送一个请求,进行短暂的间隔休眠。

Pro hint: if new data sent invalidates the data that was previously requested to be sent, but not sent yet, you can optimize the data sent by removing the invalidated data from the queue.专业提示:如果新发送的数据使之前请求发送但尚未发送的数据失效,您可以通过从队列中删除失效数据来优化发送的数据。 This is called Conflation.这称为合并。 Usual example is stock market prices.通常的例子是股票市场价格。 Say you got price of ACME 100 ten seconds ago and for whatever reason that data was not sent.假设您在十秒前获得了 ACME 100 的价格,但由于某种原因数据未发送。 If you get a new price for ACME 101 now, it is usually not useful to send the 100 price record, just send the 101 price.如果你现在拿到ACME 101的新价格,一般发送100的价格记录是没有用的,直接发送101的价格就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM