<html><body><div style="font-family: times new roman, new york, times, serif; font-size: 12pt; color: #000000"><div><br></div><div><br></div><hr id="zwchr"><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><b>From: </b>"ralf boogie blues" &lt;ralf_boogie_blues@bluewin.ch&gt;<br><b>To: </b>undertow-dev@lists.jboss.org<br><b>Sent: </b>Monday, 3 November, 2014 2:30:40 AM<br><b>Subject: </b>[undertow-dev] ResponseRateLimitingHandler questions<br><div><br></div>Hi Stuart<br><div><br></div>Thanks a lot for the ResponseRateLimitingHandler. I played a little bit with this handler and do have some questions.<br><div><br></div>I added some trace logs in order to understand the logic. Of course, I didn't understand everything;-) May you will correct me.<br><div><br></div>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.<br>Then, I started to dig into the handler.<br><div><br></div>How I cReated the handler.<br>final ResponseRateLimitingHandler responseLimiter = new ResponseRateLimitingHandler(proxy, 100000, 1, TimeUnit.SECONDS);<br><div><br></div>The trace logs showed me this (with the settings above):<br><ul><li>the handler writes chunks as big as the buffer is defined, I think on my machine 16KBytes.</li><li>it repeats this until the bytes written are greater than the rate limit.</li><li>then, the handler calculates the time to sleep until the next chunks can be written.</li></ul><p>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.</p></blockquote><div><br></div><div>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.</div><div><br></div><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><p>Problem 2: The size of the chunks is probably too static. I think the chunks should be size relatively to the rate limit.</p></blockquote><div><br></div><div>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.&nbsp;</div><div><br></div><div>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.&nbsp;</div><div><br></div><div>Stuart</div><div><br></div><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><p><br></p><p>I changed the handler slightly in this way:</p><ul><li>I introduced along to the existing parameters also a chunkSize.</li><li>canSend(): if (byteCount &lt; (bytes - chunks)) {&nbsp;&nbsp;&nbsp;&nbsp; return true;&nbsp;&nbsp;&nbsp; }&nbsp; // it doesn't write anymore than allowed</li><li>handleWritten(long written): nextSendTime = startTime + time;&nbsp; // instead of: nextSendTime = startTime + (units * time);</li><li>writeFinal and transferFrom writes not more than bytes &lt;= chunkSize.</li></ul><p>This changes gave me the expected result of about 10s download time.</p><p>This is quite a long mail :-) It could be still a mistake I did. Let me know what you think.</p><p>Regards, Ralf<br></p><p><br></p><p><br></p><br>_______________________________________________<br>undertow-dev mailing list<br>undertow-dev@lists.jboss.org<br>https://lists.jboss.org/mailman/listinfo/undertow-dev</blockquote><div><br></div></div></body></html>