Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
353 views
in Technique[技术] by (71.8m points)

java - Restrict download file bandwidth/speed in Servlet

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...