mirror of https://github.com/Desuuuu/klipper.git
webhooks: Implement a send buffer for socket writes
This prevents ClientConnection.send() from blocking, removing the possibility that callers become reentrant. Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
parent
4dcf494b97
commit
bf221d5e26
|
@ -158,8 +158,8 @@ class ClientConnection:
|
|||
self.sock = sock
|
||||
self.fd_handle = self.reactor.register_fd(
|
||||
self.sock.fileno(), self.process_received)
|
||||
self.partial_data = ""
|
||||
self.mutex = self.reactor.mutex()
|
||||
self.partial_data = self.send_buffer = ""
|
||||
self.is_sending_data = False
|
||||
logging.info(
|
||||
"webhooks: New connection established")
|
||||
|
||||
|
@ -222,12 +222,16 @@ class ClientConnection:
|
|||
self.send({'method': "response", 'params': result})
|
||||
|
||||
def send(self, data):
|
||||
with self.mutex:
|
||||
self.send_buffer += json.dumps(data) + "\x03"
|
||||
if not self.is_sending_data:
|
||||
self.is_sending_data = True
|
||||
self.reactor.register_callback(self._do_send)
|
||||
|
||||
def _do_send(self, eventtime):
|
||||
retries = 10
|
||||
data = json.dumps(data) + "\x03"
|
||||
while data:
|
||||
while self.send_buffer:
|
||||
try:
|
||||
sent = self.sock.send(data)
|
||||
sent = self.sock.send(self.send_buffer)
|
||||
except socket.error as e:
|
||||
if e.errno == errno.EBADF or e.errno == errno.EPIPE \
|
||||
or not retries:
|
||||
|
@ -239,13 +243,13 @@ class ClientConnection:
|
|||
continue
|
||||
retries = 10
|
||||
if sent > 0:
|
||||
data = data[sent:]
|
||||
self.send_buffer = self.send_buffer[sent:]
|
||||
else:
|
||||
logging.info(
|
||||
"webhooks: Error sending server data,"
|
||||
" closing socket")
|
||||
"webhooks: Error sending server data, closing socket")
|
||||
self.close()
|
||||
break
|
||||
self.is_sending_data = False
|
||||
|
||||
class WebHooks:
|
||||
def __init__(self, printer):
|
||||
|
|
Loading…
Reference in New Issue