Skip to content

Commit

Permalink
Add .dropped events for pub/sub backpressure notification
Browse files Browse the repository at this point in the history
  • Loading branch information
uNetworkingAB committed Nov 30, 2023
1 parent bc2815d commit 166f3af
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/App.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ struct TemplatedApp {
MoveOnlyFunction<void(HttpResponse<SSL> *, HttpRequest *, struct us_socket_context_t *)> upgrade = nullptr;
MoveOnlyFunction<void(WebSocket<SSL, true, UserData> *)> open = nullptr;
MoveOnlyFunction<void(WebSocket<SSL, true, UserData> *, std::string_view, OpCode)> message = nullptr;
MoveOnlyFunction<void(WebSocket<SSL, true, UserData> *, std::string_view, OpCode)> dropped = nullptr;
MoveOnlyFunction<void(WebSocket<SSL, true, UserData> *)> drain = nullptr;
MoveOnlyFunction<void(WebSocket<SSL, true, UserData> *, std::string_view)> ping = nullptr;
MoveOnlyFunction<void(WebSocket<SSL, true, UserData> *, std::string_view)> pong = nullptr;
Expand Down Expand Up @@ -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<SSL, true, UserData> *ws, int code, std::string_view message) mutable {
Expand Down
6 changes: 6 additions & 0 deletions src/WebSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ struct WebSocket : AsyncSocket<SSL> {
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;
}

Expand Down
1 change: 1 addition & 0 deletions src/WebSocketContextData.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ struct WebSocketContextData {
/* The callbacks for this context */
MoveOnlyFunction<void(WebSocket<SSL, true, USERDATA> *)> openHandler = nullptr;
MoveOnlyFunction<void(WebSocket<SSL, true, USERDATA> *, std::string_view, OpCode)> messageHandler = nullptr;
MoveOnlyFunction<void(WebSocket<SSL, true, USERDATA> *, std::string_view, OpCode)> droppedHandler = nullptr;
MoveOnlyFunction<void(WebSocket<SSL, true, USERDATA> *)> drainHandler = nullptr;
MoveOnlyFunction<void(WebSocket<SSL, true, USERDATA> *, std::string_view, int, int)> subscriptionHandler = nullptr;
MoveOnlyFunction<void(WebSocket<SSL, true, USERDATA> *, int, std::string_view)> closeHandler = nullptr;
Expand Down

0 comments on commit 166f3af

Please sign in to comment.