threading.Lock() around any access to _write_buffer

This commit is contained in:
jesopo 2019-07-04 06:56:05 +01:00
parent 61d354eb94
commit 3afbe5fd82

View file

@ -1,4 +1,4 @@
import datetime, socket, ssl, time, typing
import datetime, socket, ssl, time, threading, typing
from src import IRCLine, Logging, IRCObject, utils
THROTTLE_LINES = 4
@ -25,6 +25,7 @@ class Socket(IRCObject.Object):
self.connected = False
self._write_buffer = b""
self._write_buffer_lock = threading.Lock()
self._queued_lines = [] # type: typing.List[IRCLine.SentLine]
self._buffered_lines = [] # type: typing.List[IRCLine.SentLine]
self._read_buffer = b""
@ -124,12 +125,14 @@ class Socket(IRCObject.Object):
self._buffered_lines.append(line)
def send(self, line: IRCLine.SentLine, immediate: bool=False):
with self._write_buffer_lock:
if immediate:
self._immediate_buffer(line)
else:
self._queued_lines.append(line)
def _fill_throttle(self):
with self._write_buffer_lock:
if not self._write_buffer and self._throttle_when_empty:
self._throttle_when_empty = False
self._write_throttling = True
@ -143,11 +146,12 @@ class Socket(IRCObject.Object):
self._immediate_buffer(line)
def _send(self) -> typing.List[IRCLine.SentLine]:
sent_lines = [] # type: typing.List[IRCLine.SentLine]
with self._write_buffer_lock:
bytes_written_i = self._socket.send(self._write_buffer)
bytes_written = self._write_buffer[:bytes_written_i]
sent_lines_count = bytes_written.count(b"\n")
sent_lines = [] # type: typing.List[IRCLine.SentLine]
for i in range(sent_lines_count):
sent_lines.append(self._buffered_lines.pop(0))