Skip to content

Commit 176de6b

Browse files
authored
Add BEAMMP_MAX_CONCURRENT_CONNECTIONS env var (#496)
By creating this pull request, I understand that code that is AI generated or otherwise automatically generated may be rejected without further discussion. I declare that I fully understand all code I pushed into this PR, and wrote all this code myself and own the rights to this code.
2 parents 54f49d8 + e33f47e commit 176de6b

3 files changed

Lines changed: 22 additions & 1 deletion

File tree

include/Env.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
namespace Env {
2424

2525
enum class Key {
26+
MAX_CONCURRENT_CONNECTIONS,
2627
// provider settings
2728
PROVIDER_UPDATE_MESSAGE,
2829
PROVIDER_DISABLE_CONFIG,

src/Env.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ std::string_view Env::ToString(Env::Key key) {
4545
case Key::PROVIDER_IP_ENV:
4646
return "BEAMMP_PROVIDER_IP_ENV";
4747
break;
48+
case Key::MAX_CONCURRENT_CONNECTIONS:
49+
return "BEAMMP_MAX_CONCURRENT_CONNECTIONS";
50+
break;
4851
}
4952
return "";
5053
}

src/TNetwork.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "TNetwork.h"
2020
#include "Client.h"
2121
#include "Common.h"
22+
#include "Env.h"
2223
#include "LuaAPI.h"
2324
#include "TConnectionLimiter.h"
2425
#include "THeartbeatThread.h"
@@ -45,8 +46,24 @@
4546

4647
typedef boost::asio::detail::socket_option::integer<SOL_SOCKET, SO_RCVTIMEO> rcv_timeout_option;
4748

48-
static constexpr uint8_t MAX_CONCURRENT_CONNECTIONS = 10;
4949
static constexpr uint8_t MAX_GLOBAL_CONNECTIONS = 128;
50+
static const uint8_t MAX_CONCURRENT_CONNECTIONS = []() -> uint8_t {
51+
if (auto EnvVar = Env::Get(Env::Key::MAX_CONCURRENT_CONNECTIONS)) {
52+
try {
53+
beammp_debugf("BEAMMP_MAX_CONCURRENT_CONNECTIONS: {}", EnvVar.value());
54+
if (const int value = std::stoi(std::string(EnvVar.value())); value > 0 && value <= MAX_GLOBAL_CONNECTIONS) {
55+
beammp_debugf("BEAMMP_MAX_CONCURRENT_CONNECTIONS Parsed: {}", value);
56+
return static_cast<uint8_t>(value);
57+
}
58+
59+
beammp_warn("Env variable BEAMMP_MAX_CONCURRENT_CONNECTIONS is out of range, using default value.");
60+
} catch (const std::exception&) {
61+
beammp_warn("Error parsing Env variable BEAMMP_MAX_CONCURRENT_CONNECTIONS, using default value.");
62+
}
63+
}
64+
65+
return 10;
66+
}();
5067
static constexpr uint8_t READ_TIMEOUT_S = 10; // seconds
5168

5269
std::vector<uint8_t> StringToVector(const std::string& Str) {

0 commit comments

Comments
 (0)