Skip to content

Commit 8fb6989

Browse files
Config for web3signer keep-alive (#5007)
* Allow tweaking connection pool settings * Build docker image * Fix imports * Merge tag 'v4.6.0' into web3signer-keep-alive v4.6.0 * Delete temp docker build stuff * Fix tests * Merge remote-tracking branch 'origin/unstable' into web3signer-keep-alive * Update CLI text
1 parent 0b6416c commit 8fb6989

File tree

8 files changed

+68
-2
lines changed

8 files changed

+68
-2
lines changed

book/src/help_vc.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,9 @@ OPTIONS:
209209
--validators-dir <VALIDATORS_DIR>
210210
The directory which contains the validator keystores, deposit data for each validator along with the common
211211
slashing protection database and the validator_definitions.yml
212+
--web3-signer-keep-alive-timeout <MILLIS>
213+
Keep-alive timeout for each web3signer connection. Set to 'null' to never timeout [default: 90000]
214+
215+
--web3-signer-max-idle-connections <COUNT>
216+
Maximum number of idle connections to maintain per web3signer host. Default is unlimited.
212217
```

testing/web3signer_tests/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,12 @@ mod tests {
301301
let log = environment::null_logger().unwrap();
302302
let validator_dir = TempDir::new().unwrap();
303303

304+
let config = validator_client::Config::default();
304305
let validator_definitions = ValidatorDefinitions::from(validator_definitions);
305306
let initialized_validators = InitializedValidators::from_definitions(
306307
validator_definitions,
307308
validator_dir.path().into(),
309+
config.clone(),
308310
log.clone(),
309311
)
310312
.await
@@ -331,7 +333,6 @@ mod tests {
331333

332334
let slot_clock =
333335
TestingSlotClock::new(Slot::new(0), Duration::from_secs(0), Duration::from_secs(1));
334-
let config = validator_client::Config::default();
335336

336337
let validator_store = ValidatorStore::<_, E>::new(
337338
initialized_validators,

validator_client/src/cli.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,4 +367,24 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
367367
constructed by builders, regardless of payload value.")
368368
.takes_value(false),
369369
)
370+
/*
371+
* Experimental/development options.
372+
*/
373+
.arg(
374+
Arg::with_name("web3-signer-keep-alive-timeout")
375+
.long("web3-signer-keep-alive-timeout")
376+
.value_name("MILLIS")
377+
.default_value("90000")
378+
.help("Keep-alive timeout for each web3signer connection. Set to 'null' to never \
379+
timeout")
380+
.takes_value(true),
381+
)
382+
.arg(
383+
Arg::with_name("web3-signer-max-idle-connections")
384+
.long("web3-signer-max-idle-connections")
385+
.value_name("COUNT")
386+
.help("Maximum number of idle connections to maintain per web3signer host. Default \
387+
is unlimited.")
388+
.takes_value(true),
389+
)
370390
}

validator_client/src/config.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use slog::{info, warn, Logger};
1414
use std::fs;
1515
use std::net::IpAddr;
1616
use std::path::PathBuf;
17+
use std::time::Duration;
1718
use types::{Address, GRAFFITI_BYTES_LEN};
1819

1920
pub const DEFAULT_BEACON_NODE: &str = "http://localhost:5052/";
@@ -81,6 +82,8 @@ pub struct Config {
8182
pub builder_boost_factor: Option<u64>,
8283
/// If true, Lighthouse will prefer builder proposals, if available.
8384
pub prefer_builder_proposals: bool,
85+
pub web3_signer_keep_alive_timeout: Option<Duration>,
86+
pub web3_signer_max_idle_connections: Option<usize>,
8487
}
8588

8689
impl Default for Config {
@@ -124,6 +127,8 @@ impl Default for Config {
124127
produce_block_v3: false,
125128
builder_boost_factor: None,
126129
prefer_builder_proposals: false,
130+
web3_signer_keep_alive_timeout: Some(Duration::from_secs(90)),
131+
web3_signer_max_idle_connections: None,
127132
}
128133
}
129134
}
@@ -245,6 +250,22 @@ impl Config {
245250
.collect::<Result<_, _>>()?;
246251
}
247252

253+
/*
254+
* Web3 signer
255+
*/
256+
if let Some(s) = parse_optional::<String>(cli_args, "web3-signer-keep-alive-timeout")? {
257+
config.web3_signer_keep_alive_timeout = if s == "null" {
258+
None
259+
} else {
260+
Some(Duration::from_millis(
261+
s.parse().map_err(|_| "invalid timeout value".to_string())?,
262+
))
263+
}
264+
}
265+
if let Some(n) = parse_optional::<usize>(cli_args, "web3-signer-max-idle-connections")? {
266+
config.web3_signer_max_idle_connections = Some(n);
267+
}
268+
248269
/*
249270
* Http API server
250271
*/

validator_client/src/http_api/test_utils.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ impl ApiTester {
8080
let initialized_validators = InitializedValidators::from_definitions(
8181
validator_defs,
8282
validator_dir.path().into(),
83+
Default::default(),
8384
log.clone(),
8485
)
8586
.await

validator_client/src/http_api/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ impl ApiTester {
6868
let initialized_validators = InitializedValidators::from_definitions(
6969
validator_defs,
7070
validator_dir.path().into(),
71+
Config::default(),
7172
log.clone(),
7273
)
7374
.await

validator_client/src/initialized_validators.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use validator_dir::Builder as ValidatorDirBuilder;
3434

3535
use crate::key_cache;
3636
use crate::key_cache::KeyCache;
37+
use crate::Config;
3738

3839
/// Default timeout for a request to a remote signer for a signature.
3940
///
@@ -208,6 +209,7 @@ impl InitializedValidator {
208209
key_cache: &mut KeyCache,
209210
key_stores: &mut HashMap<PathBuf, Keystore>,
210211
web3_signer_client_map: &mut Option<HashMap<Web3SignerDefinition, Client>>,
212+
config: &Config,
211213
) -> Result<Self, Error> {
212214
if !def.enabled {
213215
return Err(Error::UnableToInitializeDisabledValidator);
@@ -311,6 +313,8 @@ impl InitializedValidator {
311313
web3_signer.client_identity_path.clone(),
312314
web3_signer.client_identity_password.clone(),
313315
request_timeout,
316+
config.web3_signer_keep_alive_timeout,
317+
config.web3_signer_max_idle_connections,
314318
)?;
315319
client_map.insert(web3_signer, client.clone());
316320
client
@@ -325,6 +329,8 @@ impl InitializedValidator {
325329
web3_signer.client_identity_path.clone(),
326330
web3_signer.client_identity_password.clone(),
327331
request_timeout,
332+
config.web3_signer_keep_alive_timeout,
333+
config.web3_signer_max_idle_connections,
328334
)?;
329335
new_web3_signer_client_map.insert(web3_signer, client.clone());
330336
*web3_signer_client_map = Some(new_web3_signer_client_map);
@@ -393,8 +399,13 @@ fn build_web3_signer_client(
393399
client_identity_path: Option<PathBuf>,
394400
client_identity_password: Option<String>,
395401
request_timeout: Duration,
402+
keep_alive_timeout: Option<Duration>,
403+
max_idle_connections: Option<usize>,
396404
) -> Result<Client, Error> {
397-
let builder = Client::builder().timeout(request_timeout);
405+
let builder = Client::builder()
406+
.timeout(request_timeout)
407+
.pool_idle_timeout(keep_alive_timeout)
408+
.pool_max_idle_per_host(max_idle_connections.unwrap_or(usize::MAX));
398409

399410
let builder = if let Some(path) = root_certificate_path {
400411
let certificate = load_pem_certificate(path)?;
@@ -475,20 +486,23 @@ pub struct InitializedValidators {
475486
web3_signer_client_map: Option<HashMap<Web3SignerDefinition, Client>>,
476487
/// For logging via `slog`.
477488
log: Logger,
489+
config: Config,
478490
}
479491

480492
impl InitializedValidators {
481493
/// Instantiates `Self`, initializing all validators in `definitions`.
482494
pub async fn from_definitions(
483495
definitions: ValidatorDefinitions,
484496
validators_dir: PathBuf,
497+
config: Config,
485498
log: Logger,
486499
) -> Result<Self, Error> {
487500
let mut this = Self {
488501
validators_dir,
489502
definitions,
490503
validators: HashMap::default(),
491504
web3_signer_client_map: None,
505+
config,
492506
log,
493507
};
494508
this.update_validators().await?;
@@ -1234,6 +1248,7 @@ impl InitializedValidators {
12341248
&mut key_cache,
12351249
&mut key_stores,
12361250
&mut None,
1251+
&self.config,
12371252
)
12381253
.await
12391254
{
@@ -1284,6 +1299,7 @@ impl InitializedValidators {
12841299
&mut key_cache,
12851300
&mut key_stores,
12861301
&mut self.web3_signer_client_map,
1302+
&self.config,
12871303
)
12881304
.await
12891305
{

validator_client/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ impl<T: EthSpec> ProductionValidatorClient<T> {
192192
let validators = InitializedValidators::from_definitions(
193193
validator_defs,
194194
config.validator_dir.clone(),
195+
config.clone(),
195196
log.clone(),
196197
)
197198
.await

0 commit comments

Comments
 (0)