Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Throttle server bandwidth by percentage using Netty

Tags:

java

netty

Is it possible to limit the server's global or channel bandwidth using Netty?

I can set explicit read or write rate limits via GlobalChannelTrafficShapingHandler, but I'm trying to limit the server's global bandwidth to roughly half the available bandwidth. So if the server can upload at 8MB/s, I want to limit the uploads to roughly 4MB/s. And I want the server to adjust as available bandwidth changes; so if the server's upload rate decreases to 5MB/s (perhaps due to network utilization by other apps), then I want to limit the rate to apprx 2.5MB/s.

I've been playing with overriding AbstractTrafficShapingHandler.submitWrite() and adding a delay based on the duration since the previous call, but the logic seems fragile and unreliable.

Any ideas on how to intelligently approach this problem?

like image 855
dejuknow Avatar asked Oct 05 '16 17:10

dejuknow


Video Answer


1 Answers

Have you considered using GlobalTrafficShapingHandler and periodically letting it go full throttle to figure out the server max bandwidth and then limiting it to half of that?

I would periodically do the following (on a schedule, maybe 1 second out of every minute)

  • Increase the setReadLimit setWriteLimit to the max you would ever allow
  • using the GlobalTrafficShapingHandlers TrafficCounter (via public trafficCounter()) to reset the count by calling resetCumulativeTime
  • Make sure the interval of TrafficCounter is shorter or equal to the time you want to sample for. Change this using TrafficCounter.configure(long)
  • Sample the TrafficCounter to get the server maximum bandwidth
  • Modify the GlobalTrafficShapingHandler to use half of the bandwidth you just calculated

I think the above should give you what you want without being too fragile.

like image 174
Chris O'Toole Avatar answered Nov 15 '22 18:11

Chris O'Toole