fix: Add queue size limit for websocket (#1701)

For slow clients, we will disconnect with it if the message queue is too
long.

---------

Co-authored-by: Sergey Kuznetsov <skuznetsov@ripple.com>
This commit is contained in:
cyan317
2024-10-25 13:30:52 +01:00
committed by Alex Kremer
parent 717a29ecdf
commit ffc9deb0f8
8 changed files with 132 additions and 96 deletions

View File

@@ -24,7 +24,6 @@
#include "util/TestWebSocketClient.hpp"
#include "util/TmpFile.hpp"
#include "util/config/Config.hpp"
#include "util/prometheus/Gauge.hpp"
#include "util/prometheus/Label.hpp"
#include "util/prometheus/Prometheus.hpp"
#include "web/Server.hpp"
@@ -45,7 +44,6 @@
#include <boost/json/value.hpp>
#include <boost/system/system_error.hpp>
#include <fmt/core.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <test_data/SslCert.hpp>
@@ -152,8 +150,6 @@ private:
std::optional<std::thread> runner;
};
struct WebServerTestsWithMockPrometheus : WebServerTest, prometheus::WithMockPrometheus {};
class EchoExecutor {
public:
void
@@ -214,7 +210,7 @@ makeServerSync(
} // namespace
TEST_F(WebServerTestsWithMockPrometheus, Http)
TEST_F(WebServerTest, Http)
{
auto e = std::make_shared<EchoExecutor>();
auto const server = makeServerSync(cfg, ctx, dosGuard, e);
@@ -222,13 +218,8 @@ TEST_F(WebServerTestsWithMockPrometheus, Http)
EXPECT_EQ(res, R"({"Hello":1})");
}
TEST_F(WebServerTestsWithMockPrometheus, Ws)
TEST_F(WebServerTest, Ws)
{
::testing::StrictMock<util::prometheus::MockCounterImplInt>& wsMessagesCounterMock =
makeMock<util::prometheus::GaugeInt>("ws_messages_length", "");
EXPECT_CALL(wsMessagesCounterMock, add(1));
EXPECT_CALL(wsMessagesCounterMock, add(-1));
auto e = std::make_shared<EchoExecutor>();
auto const server = makeServerSync(cfg, ctx, dosGuard, e);
WebSocketSyncClient wsClient;
@@ -238,7 +229,7 @@ TEST_F(WebServerTestsWithMockPrometheus, Ws)
wsClient.disconnect();
}
TEST_F(WebServerTestsWithMockPrometheus, HttpInternalError)
TEST_F(WebServerTest, HttpInternalError)
{
auto e = std::make_shared<ExceptionExecutor>();
auto const server = makeServerSync(cfg, ctx, dosGuard, e);
@@ -249,13 +240,8 @@ TEST_F(WebServerTestsWithMockPrometheus, HttpInternalError)
);
}
TEST_F(WebServerTestsWithMockPrometheus, WsInternalError)
TEST_F(WebServerTest, WsInternalError)
{
::testing::StrictMock<util::prometheus::MockCounterImplInt>& wsMessagesCounterMock =
makeMock<util::prometheus::GaugeInt>("ws_messages_length", "");
EXPECT_CALL(wsMessagesCounterMock, add(1));
EXPECT_CALL(wsMessagesCounterMock, add(-1));
auto e = std::make_shared<ExceptionExecutor>();
auto const server = makeServerSync(cfg, ctx, dosGuard, e);
WebSocketSyncClient wsClient;
@@ -268,13 +254,8 @@ TEST_F(WebServerTestsWithMockPrometheus, WsInternalError)
);
}
TEST_F(WebServerTestsWithMockPrometheus, WsInternalErrorNotJson)
TEST_F(WebServerTest, WsInternalErrorNotJson)
{
::testing::StrictMock<util::prometheus::MockCounterImplInt>& wsMessagesCounterMock =
makeMock<util::prometheus::GaugeInt>("ws_messages_length", "");
EXPECT_CALL(wsMessagesCounterMock, add(1));
EXPECT_CALL(wsMessagesCounterMock, add(-1));
auto e = std::make_shared<ExceptionExecutor>();
auto const server = makeServerSync(cfg, ctx, dosGuard, e);
WebSocketSyncClient wsClient;
@@ -287,7 +268,7 @@ TEST_F(WebServerTestsWithMockPrometheus, WsInternalErrorNotJson)
);
}
TEST_F(WebServerTestsWithMockPrometheus, IncompleteSslConfig)
TEST_F(WebServerTest, IncompleteSslConfig)
{
auto e = std::make_shared<EchoExecutor>();
@@ -298,7 +279,7 @@ TEST_F(WebServerTestsWithMockPrometheus, IncompleteSslConfig)
EXPECT_EQ(server, nullptr);
}
TEST_F(WebServerTestsWithMockPrometheus, WrongSslConfig)
TEST_F(WebServerTest, WrongSslConfig)
{
auto e = std::make_shared<EchoExecutor>();
@@ -310,7 +291,7 @@ TEST_F(WebServerTestsWithMockPrometheus, WrongSslConfig)
EXPECT_EQ(server, nullptr);
}
TEST_F(WebServerTestsWithMockPrometheus, Https)
TEST_F(WebServerTest, Https)
{
auto e = std::make_shared<EchoExecutor>();
cfg = Config{addSslConfig(generateJSONWithDynamicPort(port))};
@@ -319,13 +300,8 @@ TEST_F(WebServerTestsWithMockPrometheus, Https)
EXPECT_EQ(res, R"({"Hello":1})");
}
TEST_F(WebServerTestsWithMockPrometheus, Wss)
TEST_F(WebServerTest, Wss)
{
::testing::StrictMock<util::prometheus::MockCounterImplInt>& wsMessagesCounterMock =
makeMock<util::prometheus::GaugeInt>("ws_messages_length", "");
EXPECT_CALL(wsMessagesCounterMock, add(1));
EXPECT_CALL(wsMessagesCounterMock, add(-1));
auto e = std::make_shared<EchoExecutor>();
cfg = Config{addSslConfig(generateJSONWithDynamicPort(port))};
auto server = makeServerSync(cfg, ctx, dosGuard, e);
@@ -336,7 +312,7 @@ TEST_F(WebServerTestsWithMockPrometheus, Wss)
wsClient.disconnect();
}
TEST_F(WebServerTestsWithMockPrometheus, HttpRequestOverload)
TEST_F(WebServerTest, HttpRequestOverload)
{
auto e = std::make_shared<EchoExecutor>();
auto const server = makeServerSync(cfg, ctx, dosGuardOverload, e);
@@ -349,13 +325,8 @@ TEST_F(WebServerTestsWithMockPrometheus, HttpRequestOverload)
);
}
TEST_F(WebServerTestsWithMockPrometheus, WsRequestOverload)
TEST_F(WebServerTest, WsRequestOverload)
{
::testing::StrictMock<util::prometheus::MockCounterImplInt>& wsMessagesCounterMock =
makeMock<util::prometheus::GaugeInt>("ws_messages_length", "");
EXPECT_CALL(wsMessagesCounterMock, add(1)).Times(2);
EXPECT_CALL(wsMessagesCounterMock, add(-1)).Times(2);
auto e = std::make_shared<EchoExecutor>();
auto const server = makeServerSync(cfg, ctx, dosGuardOverload, e);
WebSocketSyncClient wsClient;
@@ -373,7 +344,7 @@ TEST_F(WebServerTestsWithMockPrometheus, WsRequestOverload)
);
}
TEST_F(WebServerTestsWithMockPrometheus, HttpPayloadOverload)
TEST_F(WebServerTest, HttpPayloadOverload)
{
std::string const s100(100, 'a');
auto e = std::make_shared<EchoExecutor>();
@@ -385,13 +356,8 @@ TEST_F(WebServerTestsWithMockPrometheus, HttpPayloadOverload)
);
}
TEST_F(WebServerTestsWithMockPrometheus, WsPayloadOverload)
TEST_F(WebServerTest, WsPayloadOverload)
{
::testing::StrictMock<util::prometheus::MockCounterImplInt>& wsMessagesCounterMock =
makeMock<util::prometheus::GaugeInt>("ws_messages_length", "");
EXPECT_CALL(wsMessagesCounterMock, add(1));
EXPECT_CALL(wsMessagesCounterMock, add(-1));
std::string const s100(100, 'a');
auto e = std::make_shared<EchoExecutor>();
auto server = makeServerSync(cfg, ctx, dosGuardOverload, e);
@@ -405,7 +371,7 @@ TEST_F(WebServerTestsWithMockPrometheus, WsPayloadOverload)
);
}
TEST_F(WebServerTestsWithMockPrometheus, WsTooManyConnection)
TEST_F(WebServerTest, WsTooManyConnection)
{
auto e = std::make_shared<EchoExecutor>();
auto server = makeServerSync(cfg, ctx, dosGuardOverload, e);
@@ -511,17 +477,10 @@ struct WebServerAdminTestParams {
std::string expectedResponse;
};
class WebServerAdminTest : public WebServerTest,
public ::testing::WithParamInterface<WebServerAdminTestParams>,
public prometheus::WithMockPrometheus {};
class WebServerAdminTest : public WebServerTest, public ::testing::WithParamInterface<WebServerAdminTestParams> {};
TEST_P(WebServerAdminTest, WsAdminCheck)
{
::testing::StrictMock<util::prometheus::MockCounterImplInt>& wsMessagesCounterMock =
makeMock<util::prometheus::GaugeInt>("ws_messages_length", "");
EXPECT_CALL(wsMessagesCounterMock, add(1));
EXPECT_CALL(wsMessagesCounterMock, add(-1));
auto e = std::make_shared<AdminCheckExecutor>();
Config const serverConfig{boost::json::parse(GetParam().config)};
auto server = makeServerSync(serverConfig, ctx, dosGuardOverload, e);