From 166f3af918711dce5073023e93d0efc14703da46 Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Thu, 30 Nov 2023 17:38:12 +0100 Subject: [PATCH] Add .dropped events for pub/sub backpressure notification --- src/App.h | 2 ++ src/WebSocket.h | 6 ++++++ src/WebSocketContextData.h | 1 + 3 files changed, 9 insertions(+) diff --git a/src/App.h b/src/App.h index 87f5c145d..a9ffd4c58 100644 --- a/src/App.h +++ b/src/App.h @@ -242,6 +242,7 @@ struct TemplatedApp { MoveOnlyFunction *, HttpRequest *, struct us_socket_context_t *)> upgrade = nullptr; MoveOnlyFunction *)> open = nullptr; MoveOnlyFunction *, std::string_view, OpCode)> message = nullptr; + MoveOnlyFunction *, std::string_view, OpCode)> dropped = nullptr; MoveOnlyFunction *)> drain = nullptr; MoveOnlyFunction *, std::string_view)> ping = nullptr; MoveOnlyFunction *, std::string_view)> pong = nullptr; @@ -372,6 +373,7 @@ struct TemplatedApp { /* Copy all handlers */ webSocketContext->getExt()->openHandler = std::move(behavior.open); webSocketContext->getExt()->messageHandler = std::move(behavior.message); + webSocketContext->getExt()->droppedHandler = std::move(behavior.dropped); webSocketContext->getExt()->drainHandler = std::move(behavior.drain); webSocketContext->getExt()->subscriptionHandler = std::move(behavior.subscription); webSocketContext->getExt()->closeHandler = std::move([closeHandler = std::move(behavior.close)](WebSocket *ws, int code, std::string_view message) mutable { diff --git a/src/WebSocket.h b/src/WebSocket.h index 1eedc2257..f2663e58f 100644 --- a/src/WebSocket.h +++ b/src/WebSocket.h @@ -100,6 +100,12 @@ struct WebSocket : AsyncSocket { if (webSocketContextData->closeOnBackpressureLimit) { us_socket_shutdown_read(SSL, (us_socket_t *) this); } + + /* It is okay to call send again from within this callback since we immediately return with DROPPED afterwards */ + if (webSocketContextData->droppedHandler) { + webSocketContextData->droppedHandler(this, message, opCode); + } + return DROPPED; } diff --git a/src/WebSocketContextData.h b/src/WebSocketContextData.h index e5ae49aea..7f04aa6f3 100644 --- a/src/WebSocketContextData.h +++ b/src/WebSocketContextData.h @@ -59,6 +59,7 @@ struct WebSocketContextData { /* The callbacks for this context */ MoveOnlyFunction *)> openHandler = nullptr; MoveOnlyFunction *, std::string_view, OpCode)> messageHandler = nullptr; + MoveOnlyFunction *, std::string_view, OpCode)> droppedHandler = nullptr; MoveOnlyFunction *)> drainHandler = nullptr; MoveOnlyFunction *, std::string_view, int, int)> subscriptionHandler = nullptr; MoveOnlyFunction *, int, std::string_view)> closeHandler = nullptr;