diff --git a/src/App.h b/src/App.h index 50852c4..309865a 100644 --- a/src/App.h +++ b/src/App.h @@ -139,6 +139,7 @@ public: unsigned int maxPayloadLength = 16 * 1024; unsigned int idleTimeout = 120; unsigned int maxBackpressure = 1 * 1024 * 1024; + bool closeOnBackpressureLimit = false; fu2::unique_function *, HttpRequest *, struct us_socket_context_t *)> upgrade = nullptr; fu2::unique_function *)> open = nullptr; fu2::unique_function *, std::string_view, uWS::OpCode)> message = nullptr; @@ -200,6 +201,7 @@ public: webSocketContext->getExt()->maxPayloadLength = behavior.maxPayloadLength; webSocketContext->getExt()->idleTimeout = behavior.idleTimeout; webSocketContext->getExt()->maxBackpressure = behavior.maxBackpressure; + webSocketContext->getExt()->closeOnBackpressureLimit = behavior.closeOnBackpressureLimit; webSocketContext->getExt()->compression = behavior.compression; httpContext->onHttp("get", pattern, [webSocketContext, behavior = std::move(behavior)](auto *res, auto *req) mutable { diff --git a/src/WebSocket.h b/src/WebSocket.h index d5bab12..f36e03d 100644 --- a/src/WebSocket.h +++ b/src/WebSocket.h @@ -64,6 +64,10 @@ public: /* Skip sending and report success if we are over the limit of maxBackpressure */ if (webSocketContextData->maxBackpressure && webSocketContextData->maxBackpressure < getBufferedAmount()) { + /* Also defer a close if we should */ + if (webSocketContextData->closeOnBackpressureLimit) { + us_socket_shutdown_read(SSL, (us_socket_t *) this); + } return true; } diff --git a/src/WebSocketContextData.h b/src/WebSocketContextData.h index 7ebad53..e2278dd 100644 --- a/src/WebSocketContextData.h +++ b/src/WebSocketContextData.h @@ -64,6 +64,7 @@ public: /* There needs to be a maxBackpressure which will force close everything over that limit */ size_t maxBackpressure = 0; + bool closeOnBackpressureLimit; /* Each websocket context has a topic tree for pub/sub */ TopicTree topicTree; @@ -143,6 +144,11 @@ public: /* If we have too much backpressure, simply skip sending from here */ + /* Also (defer) a close if we have too much backpressure if that is what we want */ + if (maxBackpressure && closeOnBackpressureLimit && asyncSocket->getBufferedAmount() > maxBackpressure) { + us_socket_shutdown_read(SSL, (us_socket_t *) asyncSocket); + } + /* Reserved, unused */ return 0; }) { diff --git a/uSockets b/uSockets index 7683672..45a7014 160000 --- a/uSockets +++ b/uSockets @@ -1 +1 @@ -Subproject commit 7683672d87067cd75b854f4e36b9820f4809a4be +Subproject commit 45a70140b191e74c66301e5fefdacbd298b8c518