Cheaper timeout system

This commit is contained in:
Alex Hultman
2021-02-01 23:51:47 +01:00
parent 20fbdbb7ae
commit 49929a6c74
4 changed files with 16 additions and 8 deletions
+12 -3
View File
@@ -140,7 +140,7 @@ public:
/* Maximum message size we can receive */ /* Maximum message size we can receive */
unsigned int maxPayloadLength = 16 * 1024; unsigned int maxPayloadLength = 16 * 1024;
/* 2 minutes timeout is good */ /* 2 minutes timeout is good */
unsigned int idleTimeout = 120; unsigned short idleTimeout = 120;
/* 64kb backpressure is probably good */ /* 64kb backpressure is probably good */
unsigned int maxBackpressure = 64 * 1024; unsigned int maxBackpressure = 64 * 1024;
bool closeOnBackpressureLimit = false; bool closeOnBackpressureLimit = false;
@@ -167,6 +167,16 @@ public:
return std::move(*this); 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 */ /* Every route has its own websocket context with its own behavior and user data type */
auto *webSocketContext = WebSocketContext<SSL, true>::create(Loop::get(), (us_socket_context_t *) httpContext); auto *webSocketContext = WebSocketContext<SSL, true>::create(Loop::get(), (us_socket_context_t *) httpContext);
@@ -207,7 +217,6 @@ public:
/* Copy settings */ /* Copy settings */
webSocketContext->getExt()->maxPayloadLength = behavior.maxPayloadLength; webSocketContext->getExt()->maxPayloadLength = behavior.maxPayloadLength;
webSocketContext->getExt()->idleTimeout = behavior.idleTimeout;
webSocketContext->getExt()->maxBackpressure = behavior.maxBackpressure; webSocketContext->getExt()->maxBackpressure = behavior.maxBackpressure;
webSocketContext->getExt()->closeOnBackpressureLimit = behavior.closeOnBackpressureLimit; webSocketContext->getExt()->closeOnBackpressureLimit = behavior.closeOnBackpressureLimit;
webSocketContext->getExt()->resetIdleTimeoutOnSend = behavior.resetIdleTimeoutOnSend; webSocketContext->getExt()->resetIdleTimeoutOnSend = behavior.resetIdleTimeoutOnSend;
@@ -215,7 +224,7 @@ public:
webSocketContext->getExt()->compression = behavior.compression; webSocketContext->getExt()->compression = behavior.compression;
/* Calculate idleTimeoutCompnents */ /* Calculate idleTimeoutCompnents */
webSocketContext->getExt()->calculateIdleTimeoutCompnents(); webSocketContext->getExt()->calculateIdleTimeoutCompnents(behavior.idleTimeout);
httpContext->onHttp("get", pattern, [webSocketContext, behavior = std::move(behavior)](auto *res, auto *req) mutable { httpContext->onHttp("get", pattern, [webSocketContext, behavior = std::move(behavior)](auto *res, auto *req) mutable {
+1 -1
View File
@@ -300,7 +300,7 @@ public:
} }
/* Arm idleTimeout */ /* 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 */ /* Move construct the UserData right before calling open handler */
new (webSocket->getUserData()) UserData(std::move(userData)); new (webSocket->getUserData()) UserData(std::move(userData));
+2 -3
View File
@@ -57,7 +57,6 @@ public:
/* Settings for this context */ /* Settings for this context */
size_t maxPayloadLength = 0; size_t maxPayloadLength = 0;
unsigned int idleTimeout = 0;
/* We do need these for async upgrade */ /* We do need these for async upgrade */
CompressOptions compression; CompressOptions compression;
@@ -75,11 +74,11 @@ public:
TopicTree topicTree; TopicTree topicTree;
/* This is run once on start-up */ /* This is run once on start-up */
void calculateIdleTimeoutCompnents() { void calculateIdleTimeoutCompnents(unsigned short idleTimeout) {
unsigned short margin = 4; unsigned short margin = 4;
/* 4, 8 or 16 seconds margin based on idleTimeout */ /* 4, 8 or 16 seconds margin based on idleTimeout */
while ((int) idleTimeout - margin * 2 >= margin * 2 && margin < 16) { 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 */ /* We should have no margin if not using sendPingsAutomatically */
if (!sendPingsAutomatically) { if (!sendPingsAutomatically) {