限制 ruby 文件流的速率
我正在开发一个项目,该项目涉及将 Flash 视频文件从多个地理分布的节点上传到 S3 存储桶。
每个视频文件约为 2-3mb,我们每十分钟仅发送一个文件(每个节点),但是我们消耗的带宽需要将速率限制在 ~20k/s,因为这些节点将流媒体传输到CDN,由于位置的原因,我们只能获得 512k 的最大上传量。
我一直在研究 ASW-S3 gem,虽然它不提供任何类型的速率限制,但我知道您可以传入 IO 流。鉴于此,我想知道是否可以创建一个速率限制的流来覆盖 read
方法,添加速率限制逻辑(例如,以最简单的形式调用 sleep< /code> 之间的读取),然后调用被重写方法的 super。
我考虑的另一个选择是破解 Net::HTTP 的代码,并将速率限制放入使用 while
循环的 send_request_with_body_stream
方法中,但我不完全确定这将是最好的选择。
我尝试扩展 IO 类,但这根本不起作用,只是从类继承 class ThrottledIO
class ThrottledIO
class ThrottledIO
class ThrottledIO
class ThrottledIO
class ThrottledIO < IO
没有做任何事情。
任何建议将不胜感激。
I am working on a project which involves uploading flash video files to a S3 bucket from a number of geographically distributed nodes.
The video files are about 2-3mb each, and we are only sending one file (per node) every ten minutes, however the bandwidth we consume needs to be rate limited to ~20k/s, as these nodes are delivering streaming media to a CDN, and due to the locations we are only able to get 512k max upload.
I have been looking into the ASW-S3 gem and while it doesn't offer any kind of rate limiting I am aware that you can pass in a IO Stream. Given this I am wondering if it might be possible to create a rate-limited stream which overrides the read
method, adds in the rate limiting logic (e.g. in its simplest form a call to sleep
between reads) and then call out to the super of the overridden method.
Another option I considered is hacking the code for Net::HTTP and putting the rate limiting into the send_request_with_body_stream
method which is using a while
loop, but I'm not entirely sure which would be the best option.
I have attempted at extending the IO class, however that didn't work at all, simply inheriting from the class with class ThrottledIO < IO
didn't do anything.
Any suggestions will be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你想“增强”IO,你需要使用Delegate。这会在 IO 对象周围放置一个“外观”,该外观将由对象的所有“外部”读取器使用,但不会影响对象本身的操作。
我将其提取到一个 gem 中,因为它被证明是普遍有用的
读取的 IO 示例
这是一个从http ://rubygems.org/gems/progressive_io
这里为所有阅读方法添加了一个方面。我认为你也许可以扩展它来进行基本的限制。完成后,您将能够将您的文件打包到其中:
You need to use Delegate if you want to "augment" an IO. This puts a "facade" around your IO object that will be used by all "external" readers of the object but will have no effect on the operation of the object itself.
I've extracted that into a gem since it proved to be generally useful
Here's an example for an IO that gets read from
http://rubygems.org/gems/progressive_io
Here there is an aspect added to all reading methods. I think you might be able to extend that to do basic throttling. After you are done you will be able to wrap your, say, File, into it:
我们使用 aiaio 的 active_resource_throttle 来限制从工作项目中的 Harvest API 拉取的请求。我没有设置它,但它有效。
We've used the aiaio's active_resource_throttle to limit requests from pulling from the Harvest API on a project at work. I didn't set it up, but it works.