setCloseClientOnQueueFull(bool): Allow to choose between discard message or close client on queue full

This commit is contained in:
Mathieu Carbou
2024-06-01 21:44:20 +02:00
parent 67ca4bdd39
commit da9cfe7f01
4 changed files with 37 additions and 4 deletions

View File

@@ -29,6 +29,7 @@ This fork is based on [yubox-node-org/ESPAsyncWebServer](https://github.com/yubo
- Depends on `mathieucarbou/Async TCP @ ^3.1.4` - Depends on `mathieucarbou/Async TCP @ ^3.1.4`
- Arduino 3 / ESP-IDF 5.1 compatibility - Arduino 3 / ESP-IDF 5.1 compatibility
- Added all flavors of `binary()`, `text()`, `binaryAll()` and `textAll()` in `AsyncWebSocket` - Added all flavors of `binary()`, `text()`, `binaryAll()` and `textAll()` in `AsyncWebSocket`
- Added `setCloseClientOnQueueFull(bool)` which can be set on a client to either close the connection or discard messages but not close the connection when the queue is full
## Documentation ## Documentation

View File

@@ -29,6 +29,7 @@ This fork is based on [yubox-node-org/ESPAsyncWebServer](https://github.com/yubo
- Depends on `mathieucarbou/Async TCP @ ^3.1.4` - Depends on `mathieucarbou/Async TCP @ ^3.1.4`
- Arduino 3 / ESP-IDF 5.1 compatibility - Arduino 3 / ESP-IDF 5.1 compatibility
- Added all flavors of `binary()`, `text()`, `binaryAll()` and `textAll()` in `AsyncWebSocket` - Added all flavors of `binary()`, `text()`, `binaryAll()` and `textAll()` in `AsyncWebSocket`
- Added `setCloseClientOnQueueFull(bool)` which can be set on a client to either close the connection or discard messages but not close the connection when the queue is full
## Documentation ## Documentation

View File

@@ -461,13 +461,22 @@ void AsyncWebSocketClient::_queueMessage(std::shared_ptr<std::vector<uint8_t>> b
if (_messageQueue.size() >= WS_MAX_QUEUED_MESSAGES) if (_messageQueue.size() >= WS_MAX_QUEUED_MESSAGES)
{ {
l.unlock(); l.unlock();
if(closeWhenFull)
{
#ifdef ESP8266 #ifdef ESP8266
ets_printf("AsyncWebSocketClient::_queueMessage: Too many messages queued, closing connection\n"); ets_printf("AsyncWebSocketClient::_queueMessage: Too many messages queued: closing connection\n");
#else #else
log_e("Too many messages queued: closing connection"); log_e("Too many messages queued: closing connection");
#endif #endif
_status = WS_DISCONNECTED; _status = WS_DISCONNECTED;
if (_client) _client->close(true); if (_client) _client->close(true);
} else {
#ifdef ESP8266
ets_printf("AsyncWebSocketClient::_queueMessage: Too many messages queued: discarding new message\n");
#else
log_e("Too many messages queued: discarding new message");
#endif
}
return; return;
} }
else else

View File

@@ -137,6 +137,7 @@ class AsyncWebSocketClient {
std::deque<AsyncWebSocketControl> _controlQueue; std::deque<AsyncWebSocketControl> _controlQueue;
std::deque<AsyncWebSocketMessage> _messageQueue; std::deque<AsyncWebSocketMessage> _messageQueue;
bool closeWhenFull = true;
uint8_t _pstate; uint8_t _pstate;
AwsFrameInfo _pinfo; AwsFrameInfo _pinfo;
@@ -164,6 +165,27 @@ class AsyncWebSocketClient {
const AsyncWebSocket *server() const { return _server; } const AsyncWebSocket *server() const { return _server; }
AwsFrameInfo const &pinfo() const { return _pinfo; } AwsFrameInfo const &pinfo() const { return _pinfo; }
// - If "true" (default), the connection will be closed if the message queue is full.
// This is the default behavior in yubox-node-org, which is not silently discarding messages but instead closes the connection.
// The big issue with this behavior is that is can cause the UI to automatically re-create a new WS connection, which can be filled again,
// and so on, causing a resource exhaustion.
//
// - If "false", the incoming message will be discarded if the queue is full.
// This is the default behavior in the original ESPAsyncWebServer library from me-no-dev.
// This behavior allows the best performance at the expense of unreliable message delivery in case the queue is full (some messages may be lost).
//
// - In any case, when the queue is full, a message is logged.
// - IT is recommended to use the methods queueIsFull(), availableForWriteAll(), availableForWrite(clientId) to check if the queue is full before sending a message.
//
// Usage:
// - can be set in the onEvent listener when connecting (event type is: WS_EVT_CONNECT)
//
// Use cases:,
// - if using websocket to send logging messages, maybe some loss is acceptable.
// - But if using websocket to send UI update messages, maybe the connection should be closed and the UI redrawn.
void setCloseClientOnQueueFull(bool close) { closeWhenFull = close; }
bool willCloseClientOnQueueFull() const { return closeWhenFull; }
IPAddress remoteIP() const; IPAddress remoteIP() const;
uint16_t remotePort() const; uint16_t remotePort() const;