# HG changeset patch # User Kim Alvefur # Date 1482082756 0 # Node ID dda1c7ccb209b0664e28a65737093c44f1cbfcd5 # Parent 1f45d316b22271c831731191c938599a6ae8b1ff server_epoll: Add native support for per socket bandwith limits diff -r 1f45d316b222 -r dda1c7ccb209 net/server_epoll.lua --- a/net/server_epoll.lua Sun Jul 28 16:43:47 2019 +0200 +++ b/net/server_epoll.lua Sun Dec 18 17:39:16 2016 +0000 @@ -377,6 +377,14 @@ end end if not self.conn then return; end + if self._limit and (data or partial) then + local cost = self._limit * #(data or partial); + if cost > cfg.min_wait then + self:setreadtimeout(false); + self:pausefor(cost); + return; + end + end if self._wantread and self.conn:dirty() then self:setreadtimeout(false); self:pausefor(cfg.read_retry_delay); @@ -609,6 +617,7 @@ -- Pause connection for some time function interface:pausefor(t) + self:debug("Pause for %fs", t); if self._pausefor then self._pausefor:close(); end @@ -623,6 +632,14 @@ end); end +function interface:setlimit(Bps) + if Bps > 0 then + self._limit = 1/Bps; + else + self._limit = nil; + end +end + function interface:pause_writes() self._write_lock = true; self:setwritetimeout(false);