From 75e68437293f33e253a3f61c8e6c21d90d0c82bf Mon Sep 17 00:00:00 2001 From: jouzo Date: Thu, 14 Sep 2023 14:32:12 +0100 Subject: [PATCH] Add flag to set HttpServerBuilder max_connections --- lib/ain-cpp-imports/src/bridge.rs | 1 + lib/ain-cpp-imports/src/lib.rs | 7 +++++++ lib/ain-grpc/src/lib.rs | 3 +++ src/ffi/ffiexports.cpp | 4 ++++ src/ffi/ffiexports.h | 2 ++ src/init.cpp | 2 ++ 6 files changed, 19 insertions(+) diff --git a/lib/ain-cpp-imports/src/bridge.rs b/lib/ain-cpp-imports/src/bridge.rs index b0d0c92a8f..a74a88a847 100644 --- a/lib/ain-cpp-imports/src/bridge.rs +++ b/lib/ain-cpp-imports/src/bridge.rs @@ -25,6 +25,7 @@ pub mod ffi { fn getAccounts() -> Vec; fn getDatadir() -> String; fn getNetwork() -> String; + fn getEthMaxConnections() -> u32; fn getDifficulty(block_hash: [u8; 32]) -> u32; fn getChainWork(block_hash: [u8; 32]) -> [u8; 32]; fn getPoolTransactions() -> Vec; diff --git a/lib/ain-cpp-imports/src/lib.rs b/lib/ain-cpp-imports/src/lib.rs index ee5001904e..8c1b624096 100644 --- a/lib/ain-cpp-imports/src/lib.rs +++ b/lib/ain-cpp-imports/src/lib.rs @@ -37,6 +37,9 @@ mod ffi { pub fn getDatadir() -> String { unimplemented!("{}", UNIMPL_MSG) } + pub fn getEthMaxConnections() -> u32 { + unimplemented!("{}", UNIMPL_MSG) + } pub fn getNetwork() -> String { unimplemented!("{}", UNIMPL_MSG) } @@ -107,6 +110,10 @@ pub fn get_datadir() -> String { ffi::getDatadir() } +pub fn get_max_connections() -> u32 { + ffi::getEthMaxConnections() +} + pub fn get_network() -> String { ffi::getNetwork() } diff --git a/lib/ain-grpc/src/lib.rs b/lib/ain-grpc/src/lib.rs index 5d3b290b6b..7007dbf36e 100644 --- a/lib/ain-grpc/src/lib.rs +++ b/lib/ain-grpc/src/lib.rs @@ -69,9 +69,12 @@ pub fn init_network_services(json_addr: &str, grpc_addr: &str) -> Result<()> { pub fn init_network_json_rpc_service(runtime: &Services, addr: &str) -> Result<()> { info!("Starting JSON RPC server at {}", addr); let addr = addr.parse::()?; + let max_connections = ain_cpp_imports::get_max_connections(); + let handle = runtime.tokio_runtime.clone(); let server = runtime.tokio_runtime.block_on( HttpServerBuilder::default() + .max_connections(max_connections) .custom_tokio_runtime(handle) .build(addr), )?; diff --git a/src/ffi/ffiexports.cpp b/src/ffi/ffiexports.cpp index 9c1af972bd..2294106956 100644 --- a/src/ffi/ffiexports.cpp +++ b/src/ffi/ffiexports.cpp @@ -231,6 +231,10 @@ Attributes getAttributeDefaults() { return Attributes::Default(); } +uint32_t getEthMaxConnections() { + return gArgs.GetArg("-ethmaxconnections", DEFAULT_ETH_MAX_CONNECTIONS); +} + rust::vec getDST20Tokens(std::size_t mnview_ptr) { LOCK(cs_main); diff --git a/src/ffi/ffiexports.h b/src/ffi/ffiexports.h index 348f2b55f9..c6f45fb6a3 100644 --- a/src/ffi/ffiexports.h +++ b/src/ffi/ffiexports.h @@ -8,6 +8,7 @@ static constexpr uint64_t DEFAULT_EVM_BLOCK_GAS_TARGET = 15000000; static constexpr uint64_t DEFAULT_EVM_BLOCK_GAS_LIMIT = 30000000; static constexpr uint64_t DEFAULT_EVM_FINALITY_COUNT = 100; +static constexpr uint32_t DEFAULT_ETH_MAX_CONNECTIONS = 100; struct Attributes { uint64_t blockGasTarget; @@ -36,6 +37,7 @@ rust::vec getAccounts(); rust::string getDatadir(); rust::string getNetwork(); uint32_t getDifficulty(std::array blockHash); +uint32_t getEthMaxConnections(); std::array getChainWork(std::array blockHash); rust::vec getPoolTransactions(); uint64_t getNativeTxSize(rust::Vec rawTransaction); diff --git a/src/init.cpp b/src/init.cpp index 8f31afdacd..8211976ca1 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -65,6 +65,7 @@ #include #include #include +#include #include #include @@ -649,6 +650,7 @@ void SetupServerArgs() gArgs.AddArg("-grpcport=", strprintf("Start GRPC connections on and (default: %u, testnet: %u, changi: %u, devnet: %u, regtest: %u)", defaultBaseParams->GRPCPort(), testnetBaseParams->GRPCPort(), changiBaseParams->GRPCPort(), devnetBaseParams->GRPCPort(), regtestBaseParams->GRPCPort()), ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::RPC); gArgs.AddArg("-ethrpcbind=[:port]", "Bind to given address to listen for ETH-JSON-RPC connections. Do not expose the ETH-RPC server to untrusted networks such as the public internet! This option is ignored unless -rpcallowip is also passed. Port is optional and overrides -ethrpcport. This option can be specified multiple times (default: 127.0.0.1 i.e., localhost)", ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::RPC); gArgs.AddArg("-ethrpcport=", strprintf("Listen for ETH-JSON-RPC connections on > (default: %u, testnet: %u, changi: %u, devnet: %u, regtest: %u)", defaultBaseParams->ETHRPCPort(), testnetBaseParams->ETHRPCPort(), changiBaseParams->ETHRPCPort(), devnetBaseParams->ETHRPCPort(), regtestBaseParams->ETHRPCPort()), ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::RPC); + gArgs.AddArg("-ethmaxconnections=", strprintf("Set the maximum number of connections allowed (default: %u, testnet: %u, changi: %u, devnet: %u, regtest: %u)", DEFAULT_ETH_MAX_CONNECTIONS, DEFAULT_ETH_MAX_CONNECTIONS, DEFAULT_ETH_MAX_CONNECTIONS, DEFAULT_ETH_MAX_CONNECTIONS, DEFAULT_ETH_MAX_CONNECTIONS), ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::RPC); #if HAVE_DECL_DAEMON gArgs.AddArg("-daemon", "Run in the background as a daemon and accept commands", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);