From 49929a6c74c82e96fe43b19fe32b9702480093bb Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Mon, 1 Feb 2021 15:41:26 +0100 Subject: [PATCH] Cheaper timeout system --- src/App.h | 15 ++++++++++++--- src/HttpResponse.h | 2 +- src/WebSocketContextData.h | 5 ++--- uSockets | 2 +- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/App.h b/src/App.h index d72e699..ee174fb 100644 --- a/src/App.h +++ b/src/App.h @@ -140,7 +140,7 @@ public: /* Maximum message size we can receive */ unsigned int maxPayloadLength = 16 * 1024; /* 2 minutes timeout is good */ - unsigned int idleTimeout = 120; + unsigned short idleTimeout = 120; /* 64kb backpressure is probably good */ unsigned int maxBackpressure = 64 * 1024; bool closeOnBackpressureLimit = false; @@ -167,6 +167,16 @@ public: return std::move(*this); } + /* Terminate on misleading idleTimeout values */ + if (behavior.idleTimeout && behavior.idleTimeout < 8) { + std::cerr << "Error: idleTimeout must be either 0 or greater than 8!" << std::endl; + std::terminate(); + } + + if (behavior.idleTimeout % 4) { + std::cerr << "Warning: idleTimeout should be a multiple of 4!" << std::endl; + } + /* Every route has its own websocket context with its own behavior and user data type */ auto *webSocketContext = WebSocketContext::create(Loop::get(), (us_socket_context_t *) httpContext); @@ -207,7 +217,6 @@ public: /* Copy settings */ webSocketContext->getExt()->maxPayloadLength = behavior.maxPayloadLength; - webSocketContext->getExt()->idleTimeout = behavior.idleTimeout; webSocketContext->getExt()->maxBackpressure = behavior.maxBackpressure; webSocketContext->getExt()->closeOnBackpressureLimit = behavior.closeOnBackpressureLimit; webSocketContext->getExt()->resetIdleTimeoutOnSend = behavior.resetIdleTimeoutOnSend; @@ -215,7 +224,7 @@ public: webSocketContext->getExt()->compression = behavior.compression; /* Calculate idleTimeoutCompnents */ - webSocketContext->getExt()->calculateIdleTimeoutCompnents(); + webSocketContext->getExt()->calculateIdleTimeoutCompnents(behavior.idleTimeout); httpContext->onHttp("get", pattern, [webSocketContext, behavior = std::move(behavior)](auto *res, auto *req) mutable { diff --git a/src/HttpResponse.h b/src/HttpResponse.h index dc31f4f..779b461 100644 --- a/src/HttpResponse.h +++ b/src/HttpResponse.h @@ -300,7 +300,7 @@ public: } /* Arm idleTimeout */ - us_socket_timeout(SSL, (us_socket_t *) webSocket, webSocketContextData->idleTimeout); + us_socket_timeout(SSL, (us_socket_t *) webSocket, webSocketContextData->idleTimeoutComponents.first); /* Move construct the UserData right before calling open handler */ new (webSocket->getUserData()) UserData(std::move(userData)); diff --git a/src/WebSocketContextData.h b/src/WebSocketContextData.h index ca04a54..b3e4e1c 100644 --- a/src/WebSocketContextData.h +++ b/src/WebSocketContextData.h @@ -57,7 +57,6 @@ public: /* Settings for this context */ size_t maxPayloadLength = 0; - unsigned int idleTimeout = 0; /* We do need these for async upgrade */ CompressOptions compression; @@ -75,11 +74,11 @@ public: TopicTree topicTree; /* This is run once on start-up */ - void calculateIdleTimeoutCompnents() { + void calculateIdleTimeoutCompnents(unsigned short idleTimeout) { unsigned short margin = 4; /* 4, 8 or 16 seconds margin based on idleTimeout */ while ((int) idleTimeout - margin * 2 >= margin * 2 && margin < 16) { - margin *= 2; + margin = (unsigned short) (margin << 2); } /* We should have no margin if not using sendPingsAutomatically */ if (!sendPingsAutomatically) { diff --git a/uSockets b/uSockets index 45a7014..6ae5e01 160000 --- a/uSockets +++ b/uSockets @@ -1 +1 @@ -Subproject commit 45a70140b191e74c66301e5fefdacbd298b8c518 +Subproject commit 6ae5e01f9970afb9c5551d4ed50031de05da1be1