你如何评价限制的IO操作? [英] How do you rate-limit an IO operation?

查看:155
本文介绍了你如何评价限制的IO操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你有一个程序,它读取一个插座。你如何保持低于某一给定阈值的下载速率?

Suppose you have a program which reads from a socket. How do you keep the download rate below a certain given threshold?

推荐答案

在应用层(使用Berkeley套接字风格的API),你只是看时钟,并在速度要限制在。读取或写入数据

At the application layer (using a Berkeley socket style API) you just watch the clock, and read or write data at the rate you want to limit at.

如果你只读平均10kbps的,但源发送更重要的是,最终则之间的所有的缓冲区,你会填满。 TCP / IP允许这一点,该协议将安排发送放缓(在应用层,可能是所有你需要知道的是,在另一端,阻断写调用将被阻塞,非阻塞写操作将失败,和异步写将无法完成,直到你读足够的数据,以允许的话)。

If you only read 10kbps on average, but the source is sending more than that, then eventually all the buffers between it and you will fill up. TCP/IP allows for this, and the protocol will arrange for the sender to slow down (at the application layer, probably all you need to know is that at the other end, blocking write calls will block, nonblocking writes will fail, and asynchronous writes won't complete, until you've read enough data to allow it).

目前应用层可以仅是近似的 - 你不能保证硬性限制,例如不超过10 kb的将通过在网络中的给定点中的任何一个第二次。但是,如果你跟踪你已经收到了什么,你可以从长远来看,平均正确的。

At the application layer you can only be approximate - you can't guarantee hard limits such as "no more than 10 kb will pass a given point in the network in any one second". But if you keep track of what you've received, you can get the average right in the long run.

这篇关于你如何评价限制的IO操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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