Skip to content

Commit 28e3cd4

Browse files
authored
feat: make the FVM concurrency configurable and set the default to 4 (#388)
Previously, we were limited to 1 message at a time.
1 parent 8baa0fa commit 28e3cd4

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

rust/src/fvm/engine.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::collections::hash_map::Entry;
22
use std::collections::HashMap;
3+
use std::ops::RangeInclusive;
34
use std::sync::{Arc, Mutex};
45

56
use anyhow::anyhow;
@@ -57,13 +58,51 @@ pub struct MultiEngineContainer {
5758
engines: Mutex<HashMap<u32, Arc<dyn AbstractMultiEngine + 'static>>>,
5859
}
5960

61+
const LOTUS_FVM_CONCURRENCY_ENV_NAME: &str = "LOTUS_FVM_CONCURRENCY";
62+
const VALID_CONCURRENCY_RANGE: RangeInclusive<u32> = 1..=128;
63+
6064
impl MultiEngineContainer {
65+
/// Constructs a new multi-engine container with the default concurrency (4).
6166
pub fn new() -> MultiEngineContainer {
67+
Self::with_concurrency(4)
68+
}
69+
70+
/// Constructs a new multi-engine container with the concurrency specified in the
71+
/// `LOTUS_FVM_CONCURRENCY` environment variable.
72+
pub fn new_env() -> MultiEngineContainer {
73+
let valosstr = match std::env::var_os(LOTUS_FVM_CONCURRENCY_ENV_NAME) {
74+
Some(v) => v,
75+
None => return Self::new(),
76+
};
77+
let valstr = match valosstr.to_str() {
78+
Some(s) => s,
79+
None => {
80+
log::error!("{LOTUS_FVM_CONCURRENCY_ENV_NAME} has invalid value");
81+
return Self::new();
82+
}
83+
};
84+
let concurrency: u32 = match valstr.parse() {
85+
Ok(v) => v,
86+
Err(e) => {
87+
log::error!("{LOTUS_FVM_CONCURRENCY_ENV_NAME} has invalid value: {e}");
88+
return Self::new();
89+
}
90+
};
91+
if !VALID_CONCURRENCY_RANGE.contains(&concurrency) {
92+
log::error!(
93+
"{LOTUS_FVM_CONCURRENCY_ENV_NAME} must be in the range {VALID_CONCURRENCY_RANGE:?}, not {concurrency}"
94+
);
95+
return Self::new();
96+
}
97+
Self::with_concurrency(concurrency)
98+
}
99+
100+
pub fn with_concurrency(concurrency: u32) -> MultiEngineContainer {
62101
MultiEngineContainer {
63102
engines: Mutex::new(HashMap::new()),
64103
// The number of messages that can be executed simultaniously on any given engine (i.e.,
65104
// on any given network version/config).
66-
concurrency: 1,
105+
concurrency,
67106
}
68107
}
69108

rust/src/fvm/machine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use crate::destructor;
2727
use crate::util::types::{catch_panic_response, catch_panic_response_no_default, Result};
2828

2929
lazy_static! {
30-
static ref ENGINES: MultiEngineContainer = MultiEngineContainer::new();
30+
static ref ENGINES: MultiEngineContainer = MultiEngineContainer::new_env();
3131
}
3232

3333
#[allow(clippy::too_many_arguments)]

0 commit comments

Comments
 (0)