|
1 | 1 | use std::collections::hash_map::Entry;
|
2 | 2 | use std::collections::HashMap;
|
| 3 | +use std::ops::RangeInclusive; |
3 | 4 | use std::sync::{Arc, Mutex};
|
4 | 5 |
|
5 | 6 | use anyhow::anyhow;
|
@@ -57,13 +58,51 @@ pub struct MultiEngineContainer {
|
57 | 58 | engines: Mutex<HashMap<u32, Arc<dyn AbstractMultiEngine + 'static>>>,
|
58 | 59 | }
|
59 | 60 |
|
| 61 | +const LOTUS_FVM_CONCURRENCY_ENV_NAME: &str = "LOTUS_FVM_CONCURRENCY"; |
| 62 | +const VALID_CONCURRENCY_RANGE: RangeInclusive<u32> = 1..=128; |
| 63 | + |
60 | 64 | impl MultiEngineContainer {
|
| 65 | + /// Constructs a new multi-engine container with the default concurrency (4). |
61 | 66 | 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 { |
62 | 101 | MultiEngineContainer {
|
63 | 102 | engines: Mutex::new(HashMap::new()),
|
64 | 103 | // The number of messages that can be executed simultaniously on any given engine (i.e.,
|
65 | 104 | // on any given network version/config).
|
66 |
| - concurrency: 1, |
| 105 | + concurrency, |
67 | 106 | }
|
68 | 107 | }
|
69 | 108 |
|
|
0 commit comments