[undertow-dev] ResponseRateLimitingHandler questions

Stuart Douglas sdouglas at redhat.com
Sun Nov 2 18:09:50 EST 2014

----- Original Message -----

> From: "ralf boogie blues" <ralf_boogie_blues at bluewin.ch>
> To: undertow-dev at lists.jboss.org
> Sent: Monday, 3 November, 2014 2:30:40 AM
> Subject: [undertow-dev] ResponseRateLimitingHandler questions

> Hi Stuart

> Thanks a lot for the ResponseRateLimitingHandler. I played a little bit with
> this handler and do have some questions.

> I added some trace logs in order to understand the logic. Of course, I didn't
> understand everything;-) May you will correct me.

> First, I tested the download response with a 1 MBytes file. The response
> limit was set to 100000 Bytes/s. This should download the whole file in
> about 10s but the result was 18s.
> Then, I started to dig into the handler.

> How I cReated the handler.
> final ResponseRateLimitingHandler responseLimiter = new
> ResponseRateLimitingHandler(proxy, 100000, 1, TimeUnit.SECONDS);

> The trace logs showed me this (with the settings above):

> * the handler writes chunks as big as the buffer is defined, I think on my
> machine 16KBytes.
> * it repeats this until the bytes written are greater than the rate limit.
> * then, the handler calculates the time to sleep until the next chunks can be
> written.

> Problem 1: The handler writes always more than the specified limit. Because
> of this, the handler calculates n * units for the time to sleep. So, in my
> test the handler slept always 2s instead of 1s.
I think this is a bug, for larger unit times I should pro-rata the wait time. If you use a smaller step time (e.g. 10k bytes per 100ms ) this effect will be less pronounced. 

> Problem 2: The size of the chunks is probably too static. I think the chunks
> should be size relatively to the rate limit.
This can result in lots more write() calls, which are expensive, so if you are using this because you have a server under heavy load you could potentially increase the load significantly. 

One option would be to cut down the size of the buffer if it is going to go over the limit, so it will never write more than the specified maximum in the time period. 


> I changed the handler slightly in this way:

> * I introduced along to the existing parameters also a chunkSize.
> * canSend(): if (byteCount < (bytes - chunks)) { return true; } // it doesn't
> write anymore than allowed
> * handleWritten(long written): nextSendTime = startTime + time; // instead
> of: nextSendTime = startTime + (units * time);
> * writeFinal and transferFrom writes not more than bytes <= chunkSize.

> This changes gave me the expected result of about 10s download time.

> This is quite a long mail :-) It could be still a mistake I did. Let me know
> what you think.

> Regards, Ralf

> _______________________________________________
> undertow-dev mailing list
> undertow-dev at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/undertow-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/undertow-dev/attachments/20141102/5de6b11f/attachment.html 

More information about the undertow-dev mailing list