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(