限制 ruby​​ 文件流的速率

发布于 2024-08-25 17:19:46 字数 742 浏览 6 评论 0原文

我正在开发一个项目,该项目涉及将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉酒的小男人 2024-09-01 17:19:46

如果你想“增强”IO,你需要使用Delegate。这会在 IO 对象周围放置一个“外观”,该外观将由对象的所有“外部”读取器使用,但不会影响对象本身的操作。

我将其提取到一个 gem 中,因为它被证明是普遍有用的

读取的 IO 示例

这是一个从http ://rubygems.org/gems/progressive_io

这里为所有阅读方法添加了一个方面。我认为你也许可以扩展它来进行基本的限制。完成后,您将能够将您的文件打包到其中:

 throttled_file = ProgressiveIO.new(some_file) do | offset, size |
    # compute rate and if needed sleep()
 end

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:

 throttled_file = ProgressiveIO.new(some_file) do | offset, size |
    # compute rate and if needed sleep()
 end
┾廆蒐ゝ 2024-09-01 17:19:46

我们使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文