Only enable write throttling when _write_buffer is empty

closes #59
This commit is contained in:
jesopo 2019-05-31 15:49:34 +01:00
parent f2cf86b8c0
commit dc284bab4a
2 changed files with 9 additions and 4 deletions

View file

@ -7,7 +7,7 @@ def ping(event):
event["server"].send_pong(event["args"][0]) event["server"].send_pong(event["args"][0])
def handle_001(event): def handle_001(event):
event["server"].socket.set_write_throttling(True) event["server"].socket.enable_write_throttle()
event["server"].name = event["prefix"].hostmask event["server"].name = event["prefix"].hostmask
event["server"].set_own_nickname(event["args"][0]) event["server"].set_own_nickname(event["args"][0])
event["server"].send_whois(event["server"].nickname) event["server"].send_whois(event["server"].nickname)

View file

@ -27,13 +27,15 @@ class Socket(IRCObject.Object):
self._write_buffer = b"" self._write_buffer = b""
self._queued_lines = [] # type: typing.List[IRCLine.SentLine] self._queued_lines = [] # type: typing.List[IRCLine.SentLine]
self._buffered_lines = [] # type: typing.List[IRCLine.SentLine] self._buffered_lines = [] # type: typing.List[IRCLine.SentLine]
self._write_throttling = False
self._read_buffer = b"" self._read_buffer = b""
self._recent_sends = [] # type: typing.List[float] self._recent_sends = [] # type: typing.List[float]
self.cached_fileno = None # type: typing.Optional[int] self.cached_fileno = None # type: typing.Optional[int]
self.bytes_written = 0 self.bytes_written = 0
self.bytes_read = 0 self.bytes_read = 0
self._write_throttling = False
self._throttle_when_empty = False
self.last_read = time.monotonic() self.last_read = time.monotonic()
self.last_send = None # type: typing.Optional[float] self.last_send = None # type: typing.Optional[float]
@ -140,6 +142,9 @@ class Socket(IRCObject.Object):
self._write_buffer = self._write_buffer[bytes_written_i:] self._write_buffer = self._write_buffer[bytes_written_i:]
if not self._write_buffer and self._throttle_when_empty:
self._write_throttling = True
self.bytes_written += bytes_written_i self.bytes_written += bytes_written_i
now = time.monotonic() now = time.monotonic()
@ -185,5 +190,5 @@ class Socket(IRCObject.Object):
time_left = time_left-time.monotonic() time_left = time_left-time.monotonic()
return time_left return time_left
def set_write_throttling(self, is_on: bool): def enable_write_throttle(self):
self._write_throttling = is_on self._throttle_when_empty = True