aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKevin O'Connor <kevin@koconnor.net>2022-05-23 22:04:18 -0400
committerKevin O'Connor <kevin@koconnor.net>2022-07-06 09:54:44 -0400
commita283e0f7d4156baff4ab7eccfee6d50738b162aa (patch)
treeb3e95c2ecef93225fc5cd9ea8ac394b452cde331
parent7b9583391ed135190c463552f1754c8faae51250 (diff)
downloadkutter-a283e0f7d4156baff4ab7eccfee6d50738b162aa.tar.gz
kutter-a283e0f7d4156baff4ab7eccfee6d50738b162aa.tar.xz
kutter-a283e0f7d4156baff4ab7eccfee6d50738b162aa.zip
webhooks: Close clients that become unresponsive
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
-rw-r--r--klippy/webhooks.py15
1 files changed, 15 insertions, 0 deletions
diff --git a/klippy/webhooks.py b/klippy/webhooks.py
index 63342dd7..43ff9a91 100644
--- a/klippy/webhooks.py
+++ b/klippy/webhooks.py
@@ -162,6 +162,16 @@ class ServerSocket:
def pop_client(self, client_id):
self.clients.pop(client_id, None)
+ def stats(self, eventtime):
+ # Called once per second - check for idle clients
+ for client in list(self.clients.values()):
+ if client.is_blocking:
+ client.blocking_count -= 1
+ if client.blocking_count < 0:
+ logging.info("Closing unresponsive client %s", client.uid)
+ client.close()
+ return False, ""
+
class ClientConnection:
def __init__(self, server, sock):
self.printer = server.printer
@@ -174,6 +184,7 @@ class ClientConnection:
self.sock.fileno(), self.process_received, self._do_send)
self.partial_data = self.send_buffer = b""
self.is_blocking = False
+ self.blocking_count = 0
self.set_client_info("?", "New connection")
self.request_log = collections.deque([], REQUEST_LOG_SIZE)
@@ -277,6 +288,7 @@ class ClientConnection:
if not self.is_blocking:
self.reactor.set_fd_wake(self.fd_handle, False, True)
self.is_blocking = True
+ self.blocking_count = 5
elif self.is_blocking:
self.reactor.set_fd_wake(self.fd_handle, True, False)
self.is_blocking = False
@@ -370,6 +382,9 @@ class WebHooks:
state_message, state = self.printer.get_state_message()
return {'state': state, 'state_message': state_message}
+ def stats(self, eventtime):
+ return self.sconn.stats(eventtime)
+
def call_remote_method(self, method, **kwargs):
if method not in self._remote_methods:
raise self.printer.command_error(