Allow customization of max WS connections. (#8257)

* Allow customization of max WS connections.

* remove superflous line.

* Add test for CLI argument parsing.
This commit is contained in:
Tomasz Drwięga 2018-04-02 12:33:09 +02:00 committed by André Silva
parent 5ea4c22868
commit c1cced3662
5 changed files with 27 additions and 1 deletions

View File

@ -499,6 +499,10 @@ usage! {
"--ws-hosts=[HOSTS]", "--ws-hosts=[HOSTS]",
"List of allowed Host header values. This option will validate the Host header sent by the browser, it is additional security against some attack vectors. Special options: \"all\", \"none\".", "List of allowed Host header values. This option will validate the Host header sent by the browser, it is additional security against some attack vectors. Special options: \"all\", \"none\".",
ARG arg_ws_max_connections: (usize) = 100usize, or |c: &Config| c.websockets.as_ref()?.max_connections,
"--ws-max-connections=[CONN]",
"Maximal number of allowed concurrent WS connections.",
["API and console options IPC"] ["API and console options IPC"]
FLAG flag_no_ipc: (bool) = false, or |c: &Config| c.ipc.as_ref()?.disable.clone(), FLAG flag_no_ipc: (bool) = false, or |c: &Config| c.ipc.as_ref()?.disable.clone(),
"--no-ipc", "--no-ipc",
@ -1087,6 +1091,7 @@ struct Ws {
apis: Option<Vec<String>>, apis: Option<Vec<String>>,
origins: Option<Vec<String>>, origins: Option<Vec<String>>,
hosts: Option<Vec<String>>, hosts: Option<Vec<String>>,
max_connections: Option<usize>,
} }
#[derive(Default, Debug, PartialEq, Deserialize)] #[derive(Default, Debug, PartialEq, Deserialize)]
@ -1520,6 +1525,7 @@ mod tests {
arg_ws_apis: "web3,eth,net,parity,traces,rpc,secretstore".into(), arg_ws_apis: "web3,eth,net,parity,traces,rpc,secretstore".into(),
arg_ws_origins: "none".into(), arg_ws_origins: "none".into(),
arg_ws_hosts: "none".into(), arg_ws_hosts: "none".into(),
arg_ws_max_connections: 100,
// IPC // IPC
flag_no_ipc: false, flag_no_ipc: false,
@ -1759,6 +1765,7 @@ mod tests {
apis: None, apis: None,
origins: Some(vec!["none".into()]), origins: Some(vec!["none".into()]),
hosts: None, hosts: None,
max_connections: None,
}), }),
rpc: Some(Rpc { rpc: Some(Rpc {
disable: Some(true), disable: Some(true),

View File

@ -922,6 +922,7 @@ impl Configuration {
support_token_api, support_token_api,
ui_address: ui.address(), ui_address: ui.address(),
dapps_address: http.address(), dapps_address: http.address(),
max_connections: self.args.arg_ws_max_connections,
}; };
Ok(conf) Ok(conf)
@ -1361,7 +1362,8 @@ mod tests {
signer_path: expected.into(), signer_path: expected.into(),
ui_address: None, ui_address: None,
dapps_address: Some("127.0.0.1:8545".into()), dapps_address: Some("127.0.0.1:8545".into()),
support_token_api: true support_token_api: true,
max_connections: 100,
}, UiConfiguration { }, UiConfiguration {
enabled: false, enabled: false,
interface: "127.0.0.1".into(), interface: "127.0.0.1".into(),
@ -1374,6 +1376,17 @@ mod tests {
} )); } ));
} }
#[test]
fn test_ws_max_connections() {
let args = vec!["parity", "--ws-max-connections", "1"];
let conf = parse(&args);
assert_eq!(conf.ws_config().unwrap(), WsConfiguration {
max_connections: 1,
..Default::default()
});
}
#[test] #[test]
fn test_run_cmd() { fn test_run_cmd() {
let args = vec!["parity"]; let args = vec!["parity"];

View File

@ -146,6 +146,7 @@ pub struct WsConfiguration {
pub interface: String, pub interface: String,
pub port: u16, pub port: u16,
pub apis: ApiSet, pub apis: ApiSet,
pub max_connections: usize,
pub origins: Option<Vec<String>>, pub origins: Option<Vec<String>>,
pub hosts: Option<Vec<String>>, pub hosts: Option<Vec<String>>,
pub signer_path: PathBuf, pub signer_path: PathBuf,
@ -162,6 +163,7 @@ impl Default for WsConfiguration {
interface: "127.0.0.1".into(), interface: "127.0.0.1".into(),
port: 8546, port: 8546,
apis: ApiSet::UnsafeContext, apis: ApiSet::UnsafeContext,
max_connections: 100,
origins: Some(vec!["parity://*".into(),"chrome-extension://*".into(), "moz-extension://*".into()]), origins: Some(vec!["parity://*".into(),"chrome-extension://*".into(), "moz-extension://*".into()]),
hosts: Some(Vec::new()), hosts: Some(Vec::new()),
signer_path: replace_home(&data_dir, "$BASE/signer").into(), signer_path: replace_home(&data_dir, "$BASE/signer").into(),
@ -240,6 +242,7 @@ pub fn new_ws<D: rpc_apis::Dependencies>(
remote.clone(), remote.clone(),
allowed_origins, allowed_origins,
allowed_hosts, allowed_hosts,
conf.max_connections,
rpc::WsExtractor::new(path.clone()), rpc::WsExtractor::new(path.clone()),
rpc::WsExtractor::new(path.clone()), rpc::WsExtractor::new(path.clone()),
rpc::WsStats::new(deps.stats.clone()), rpc::WsStats::new(deps.stats.clone()),

View File

@ -175,6 +175,7 @@ pub fn start_ws<M, S, H, T, U, V>(
remote: tokio_core::reactor::Remote, remote: tokio_core::reactor::Remote,
allowed_origins: ws::DomainsValidation<ws::Origin>, allowed_origins: ws::DomainsValidation<ws::Origin>,
allowed_hosts: ws::DomainsValidation<ws::Host>, allowed_hosts: ws::DomainsValidation<ws::Host>,
max_connections: usize,
extractor: T, extractor: T,
middleware: V, middleware: V,
stats: U, stats: U,
@ -191,6 +192,7 @@ pub fn start_ws<M, S, H, T, U, V>(
.request_middleware(middleware) .request_middleware(middleware)
.allowed_origins(allowed_origins) .allowed_origins(allowed_origins)
.allowed_hosts(allowed_hosts) .allowed_hosts(allowed_hosts)
.max_connections(max_connections)
.session_stats(stats) .session_stats(stats)
.start(addr) .start(addr)
} }

View File

@ -40,6 +40,7 @@ pub fn serve() -> (Server<ws::Server>, usize, GuardedAuthCodes) {
remote, remote,
ws::DomainsValidation::Disabled, ws::DomainsValidation::Disabled,
ws::DomainsValidation::Disabled, ws::DomainsValidation::Disabled,
5,
extractors::WsExtractor::new(Some(&authcodes.path)), extractors::WsExtractor::new(Some(&authcodes.path)),
extractors::WsExtractor::new(Some(&authcodes.path)), extractors::WsExtractor::new(Some(&authcodes.path)),
extractors::WsStats::new(stats), extractors::WsStats::new(stats),