Search code examples
nginxstreamhttp-live-streamingrtmpbandwidth-throttling

How to restrict output bandwidth of each stream in RTMP


I have a digital ocean server for serving RTMP and hls live streaming.Is there any way I can restrict output bandwith for each stream to 1mbps Or 1.5mpbs.

I already tried wondershaper but it is limiting the entire server bandwidth but I need some way to restrict bandwidth only for rtmp and hls streams.


Solution

  • Personaly,I would use either cgroups + tc or iptables for that.

    On my servers I have used a tool named wondersharper https://github.com/magnific0/wondershaper/blob/master/wondershaper

    This can limit the bandwith / throughput on a given network-interface. My NGINX server is listing on a virtual network interface that is limited in its bandwith.

    ip link add br0 type bridge
    ip link set br0 up
    

    Another way of doing it (but not tested) are iptables and the hashlimit extension.

    https://ipset.netfilter.org/iptables-extensions.man.html

    hashlimit uses hash buckets to express a rate limiting match (like the limit match) for a group of connections using a single iptables rule. Grouping can be done per-hostgroup (source and/or destination address) and/or per-port. It gives you the ability to express "N packets per time quantum per group" or "N bytes per seconds" (see below for some examples).

    #matching bytes per second flows exceeding 512kbyte/s
    
    --hashlimit-mode srcip,dstip,srcport,dstport --hashlimit-above 512kb/s
    
    #matching bytes per second hosts that exceed 512kbyte/s, but permit up to 1Megabytes without matching
    
    --hashlimit-mode dstip --hashlimit-above 512kb/s --hashlimit-burst 1mb
    

    While researching the ipbtables part I found this

    http://info.iet.unipi.it/~luigi/dummynet/

    Documentation can be found here:\

    https://www.freebsd.org/cgi/man.cgi?query=ipfw&manpath=FreeBSD+9-current&format=html

    That one looks pretty interessting though.