Limit download file width / speed in Servlet

we got a high load java application that works in cluster mode.

I need to add the ability to download and upload files for our clients. I am going to store the files in the userโ€™s gridFs, but I'm not sure if this is the best choice, but mongo can be clustered, and mongo can replicate data between diff nodes. This is exactly what I need.

Different user groups should be limited by different bandwidth. Based on some business rules, I should limit the download speed for some users. I saw several solutions for this

Most of them work the same.

  • Read a bunch of bytes
  • Sleepy thread
  • Repeat

Mongo just provided me with an InputStrem, and I can read from this stream and write to the servlet output stream. I am not sure if this is a valid approach. In addition, I am afraid that users may create many juicy threads at boot time, and this may hurt performance.

Could this be a problem for the servlet container?

If this can be a problem, how can it be avoided? perhaps using nio?

I prefer to use a purely java solution.

Any help would be greatly appreciated.

+6
java servlets download
source share
2 answers

A leaky bucket or token can be used to control network bandwidth.

EDIT: I did a little prototyping and implemented an algorithm using asynchronous servlet 3.0 processing. The results are pretty good. Full source code can be found on GitHub . Enjoy!

+9
source share

I am also afraid that users might create a lot of juicy threads at boot time, and that could hurt performance.

Could this be a problem for the servlet container?

Yes it is possible.

If this can be a problem, how can it be avoided? perhaps using nio?

NIO will not help on its own. This, of course, does not interfere with low-bandwidth responses linking streams over long periods of time.

I think that you will need to do the download in a special web container. I'm not sure, but I think Servlet 3.0 with asynchronous mode can do the trick.

+1
source share

All Articles