限制Servlet下载文件带宽/速度 [英] Restrict download file bandwidth/speed in Servlet

查看:259
本文介绍了限制Servlet下载文件带宽/速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们得到了高效的java应用程序,可以在群集模式下工作。



我需要添加为我们的客户下载和上传文件的功能。
对于存储文件,我要去用户网格,不确定,它是最好的选择,但是mongo可以被聚类,并且mongo可以在diff节点之间复制数据。
这就是我需要的。



不同的用户组应该受到不同带宽的限制。基于一些业务规则,我应该限制一些用户的下载速度。
我看到几个解决方案这个



大部分的工作方式相同。




  • 读一堆字节

  • 睡眠线程

  • 重复



Mongo只需提供InputStrem我可以从该流读取并写入到servlet输出流。我不知道这是有效的方法。另外恐怕用户可以在下载过程中创建很多简洁的线程,并且会影响性​​能。



这可能是servlet容器的一个问题?



如果可能是一个问题,怎么可能避免了可能使用nio?



我更喜欢使用纯java解决方案。



任何帮助将非常感谢。 / p>

解决方案

漏桶令牌桶算法可用于控制网络带宽。



<编辑:我做了一些快速原型,并实现了利用Servlet 3.0异步处理的算法。 结果相当不错。完整的源代码可以在 GitHub 上找到。玩得开心!


we got high-load java application which works in clustered mode.

I need to add ability to download and upload files for our customers. For storing files i'm going to user gridFs, not sure, it's best choice, but mongo can be clustered and mongo can replicate data between diff nodes. That's exactly what i need.

Different group of users should be limited with different bandwidth. Based of some business rules i should restrict download speed for some users. I saw few solutions for this

Most of them works same way.

  • Read bunch of bytes
  • Sleep thread
  • Repeat

Mongo simply provide me InputStrem and i can read from that stream and write to servlet output stream. I'm not sure it is valid approach. Also I'm afraid, that users can create a lot of concurent threads during download and it can hurt performance.

Could it be an issue for servlet container ?

If it could be an issue, how can it be avoided ? probably using nio ?

I prefer to use pure java solution.

Any help will be highly appreciated.

解决方案

Leaky bucket or token bucket algorithms can be used to control the network bandwidth.

EDIT: I did some quick prototyping and implemented the algorithm leveraging Servlet 3.0 asynchronous processing. Results are pretty good. Full source code can be found on GitHub. Have fun!

这篇关于限制Servlet下载文件带宽/速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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