Comparison

net/server_epoll.lua @ 10098:dda1c7ccb209

server_epoll: Add native support for per socket bandwith limits
author Kim Alvefur <zash@zash.se>
date Sun, 18 Dec 2016 17:39:16 +0000
parent 10091:bd547587f48c
child 10228:e77bf4222fae
comparison
equal deleted inserted replaced
10097:1f45d316b222 10098:dda1c7ccb209
375 self:destroy() 375 self:destroy()
376 return; 376 return;
377 end 377 end
378 end 378 end
379 if not self.conn then return; end 379 if not self.conn then return; end
380 if self._limit and (data or partial) then
381 local cost = self._limit * #(data or partial);
382 if cost > cfg.min_wait then
383 self:setreadtimeout(false);
384 self:pausefor(cost);
385 return;
386 end
387 end
380 if self._wantread and self.conn:dirty() then 388 if self._wantread and self.conn:dirty() then
381 self:setreadtimeout(false); 389 self:setreadtimeout(false);
382 self:pausefor(cfg.read_retry_delay); 390 self:pausefor(cfg.read_retry_delay);
383 else 391 else
384 self:setreadtimeout(); 392 self:setreadtimeout();
607 return self:set(true); 615 return self:set(true);
608 end 616 end
609 617
610 -- Pause connection for some time 618 -- Pause connection for some time
611 function interface:pausefor(t) 619 function interface:pausefor(t)
620 self:debug("Pause for %fs", t);
612 if self._pausefor then 621 if self._pausefor then
613 self._pausefor:close(); 622 self._pausefor:close();
614 end 623 end
615 if t == false then return; end 624 if t == false then return; end
616 self:set(false); 625 self:set(false);
619 self:set(true); 628 self:set(true);
620 if self.conn and self.conn:dirty() then 629 if self.conn and self.conn:dirty() then
621 self:onreadable(); 630 self:onreadable();
622 end 631 end
623 end); 632 end);
633 end
634
635 function interface:setlimit(Bps)
636 if Bps > 0 then
637 self._limit = 1/Bps;
638 else
639 self._limit = nil;
640 end
624 end 641 end
625 642
626 function interface:pause_writes() 643 function interface:pause_writes()
627 self._write_lock = true; 644 self._write_lock = true;
628 self:setwritetimeout(false); 645 self:setwritetimeout(false);