mirror of
https://github.com/denoland/deno.git
synced 2024-12-26 00:59:24 -05:00
refactor: move fields from CliState to OpState (#7558)
- move rng to OpState - move GlobalTimer to OpState - move Metrics to OpState
This commit is contained in:
parent
4fcfff0393
commit
f44522eac9
7 changed files with 31 additions and 41 deletions
|
@ -20,10 +20,6 @@ pub struct GlobalTimer {
|
|||
}
|
||||
|
||||
impl GlobalTimer {
|
||||
pub fn new() -> Self {
|
||||
Self { tx: None }
|
||||
}
|
||||
|
||||
pub fn cancel(&mut self) {
|
||||
if let Some(tx) = self.tx.take() {
|
||||
tx.send(()).ok();
|
||||
|
|
|
@ -12,7 +12,6 @@ pub struct Metrics {
|
|||
pub bytes_sent_control: u64,
|
||||
pub bytes_sent_data: u64,
|
||||
pub bytes_received: u64,
|
||||
pub resolve_count: u64,
|
||||
}
|
||||
|
||||
impl Metrics {
|
||||
|
@ -93,9 +92,9 @@ pub fn metrics_op(op_fn: Box<OpFn>) -> Box<OpFn> {
|
|||
|
||||
let op = (op_fn)(op_state.clone(), bufs);
|
||||
|
||||
let cli_state = crate::ops::cli_state2(&op_state);
|
||||
let cli_state_ = cli_state.clone();
|
||||
let mut metrics = cli_state.metrics.borrow_mut();
|
||||
let op_state_ = op_state.clone();
|
||||
let mut s = op_state.borrow_mut();
|
||||
let metrics = s.borrow_mut::<Metrics>();
|
||||
|
||||
use futures::future::FutureExt;
|
||||
|
||||
|
@ -108,7 +107,8 @@ pub fn metrics_op(op_fn: Box<OpFn>) -> Box<OpFn> {
|
|||
metrics.op_dispatched_async(bytes_sent_control, bytes_sent_data);
|
||||
let fut = fut
|
||||
.inspect(move |buf| {
|
||||
let mut metrics = cli_state_.metrics.borrow_mut();
|
||||
let mut s = op_state_.borrow_mut();
|
||||
let metrics = s.borrow_mut::<Metrics>();
|
||||
metrics.op_completed_async(buf.len());
|
||||
})
|
||||
.boxed_local();
|
||||
|
@ -118,7 +118,8 @@ pub fn metrics_op(op_fn: Box<OpFn>) -> Box<OpFn> {
|
|||
metrics.op_dispatched_async_unref(bytes_sent_control, bytes_sent_data);
|
||||
let fut = fut
|
||||
.inspect(move |buf| {
|
||||
let mut metrics = cli_state_.metrics.borrow_mut();
|
||||
let mut s = op_state_.borrow_mut();
|
||||
let metrics = s.borrow_mut::<Metrics>();
|
||||
metrics.op_completed_async_unref(buf.len());
|
||||
})
|
||||
.boxed_local();
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
use deno_core::error::AnyError;
|
||||
use deno_core::OpState;
|
||||
use deno_core::ZeroCopyBuf;
|
||||
use rand::rngs::StdRng;
|
||||
use rand::thread_rng;
|
||||
use rand::Rng;
|
||||
use serde_json::Value;
|
||||
|
@ -17,9 +18,9 @@ fn op_get_random_values(
|
|||
zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, AnyError> {
|
||||
assert_eq!(zero_copy.len(), 1);
|
||||
let cli_state = super::cli_state(state);
|
||||
if let Some(seeded_rng) = &cli_state.seeded_rng {
|
||||
seeded_rng.borrow_mut().fill(&mut *zero_copy[0]);
|
||||
let maybe_seeded_rng = state.try_borrow_mut::<StdRng>();
|
||||
if let Some(seeded_rng) = maybe_seeded_rng {
|
||||
seeded_rng.fill(&mut *zero_copy[0]);
|
||||
} else {
|
||||
let mut rng = thread_rng();
|
||||
rng.fill(&mut *zero_copy[0]);
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
use crate::colors;
|
||||
use crate::metrics::Metrics;
|
||||
use crate::version;
|
||||
use crate::DenoSubcommand;
|
||||
use deno_core::error::AnyError;
|
||||
|
@ -61,8 +62,7 @@ fn op_metrics(
|
|||
_args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, AnyError> {
|
||||
let cli_state = super::cli_state(state);
|
||||
let m = &cli_state.metrics.borrow();
|
||||
let m = state.borrow::<Metrics>();
|
||||
|
||||
Ok(json!({
|
||||
"opsDispatched": m.ops_dispatched,
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
use crate::global_timer::GlobalTimer;
|
||||
use deno_core::error::AnyError;
|
||||
use deno_core::BufVec;
|
||||
use deno_core::OpState;
|
||||
|
@ -23,8 +24,8 @@ fn op_global_timer_stop(
|
|||
_args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, AnyError> {
|
||||
let cli_state = super::cli_state(state);
|
||||
cli_state.global_timer.borrow_mut().cancel();
|
||||
let global_timer = state.borrow_mut::<GlobalTimer>();
|
||||
global_timer.cancel();
|
||||
Ok(json!({}))
|
||||
}
|
||||
|
||||
|
@ -43,11 +44,9 @@ async fn op_global_timer(
|
|||
|
||||
let deadline = Instant::now() + Duration::from_millis(val);
|
||||
let timer_fut = {
|
||||
super::cli_state2(&state)
|
||||
.global_timer
|
||||
.borrow_mut()
|
||||
.new_timeout(deadline)
|
||||
.boxed_local()
|
||||
let mut s = state.borrow_mut();
|
||||
let global_timer = s.borrow_mut::<GlobalTimer>();
|
||||
global_timer.new_timeout(deadline).boxed_local()
|
||||
};
|
||||
let _ = timer_fut.await;
|
||||
Ok(json!({}))
|
||||
|
|
17
cli/state.rs
17
cli/state.rs
|
@ -2,9 +2,7 @@
|
|||
|
||||
use crate::file_fetcher::SourceFileFetcher;
|
||||
use crate::global_state::GlobalState;
|
||||
use crate::global_timer::GlobalTimer;
|
||||
use crate::import_map::ImportMap;
|
||||
use crate::metrics::Metrics;
|
||||
use crate::permissions::Permissions;
|
||||
use crate::tsc::TargetLib;
|
||||
use crate::web_worker::WebWorkerHandle;
|
||||
|
@ -15,8 +13,6 @@ use deno_core::ModuleLoader;
|
|||
use deno_core::ModuleSpecifier;
|
||||
use futures::future::FutureExt;
|
||||
use futures::Future;
|
||||
use rand::rngs::StdRng;
|
||||
use rand::SeedableRng;
|
||||
use std::cell::Cell;
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
|
@ -40,12 +36,9 @@ pub struct CliState {
|
|||
/// When flags contains a `.import_map_path` option, the content of the
|
||||
/// import map file will be resolved and set.
|
||||
pub import_map: Option<ImportMap>,
|
||||
pub metrics: RefCell<Metrics>,
|
||||
pub global_timer: RefCell<GlobalTimer>,
|
||||
pub workers: RefCell<HashMap<u32, (JoinHandle<()>, WebWorkerHandle)>>,
|
||||
pub next_worker_id: Cell<u32>,
|
||||
pub start_time: Instant,
|
||||
pub seeded_rng: Option<RefCell<StdRng>>,
|
||||
pub target_lib: TargetLib,
|
||||
pub is_main: bool,
|
||||
pub is_internal: bool,
|
||||
|
@ -87,8 +80,6 @@ impl ModuleLoader for CliState {
|
|||
_is_dyn_import: bool,
|
||||
) -> Pin<Box<deno_core::ModuleSourceFuture>> {
|
||||
let module_specifier = module_specifier.to_owned();
|
||||
// TODO(bartlomieju): incrementing resolve_count here has no sense...
|
||||
self.metrics.borrow_mut().resolve_count += 1;
|
||||
let module_url_specified = module_specifier.to_string();
|
||||
let global_state = self.global_state.clone();
|
||||
|
||||
|
@ -163,7 +154,6 @@ impl CliState {
|
|||
maybe_import_map: Option<ImportMap>,
|
||||
is_internal: bool,
|
||||
) -> Result<Rc<Self>, AnyError> {
|
||||
let fl = &global_state.flags;
|
||||
let state = CliState {
|
||||
global_state: global_state.clone(),
|
||||
main_module,
|
||||
|
@ -171,12 +161,9 @@ impl CliState {
|
|||
.unwrap_or_else(|| global_state.permissions.clone())
|
||||
.into(),
|
||||
import_map: maybe_import_map,
|
||||
metrics: Default::default(),
|
||||
global_timer: Default::default(),
|
||||
workers: Default::default(),
|
||||
next_worker_id: Default::default(),
|
||||
start_time: Instant::now(),
|
||||
seeded_rng: fl.seed.map(|v| StdRng::seed_from_u64(v).into()),
|
||||
target_lib: TargetLib::Main,
|
||||
is_main: true,
|
||||
is_internal,
|
||||
|
@ -190,7 +177,6 @@ impl CliState {
|
|||
shared_permissions: Option<Permissions>,
|
||||
main_module: ModuleSpecifier,
|
||||
) -> Result<Rc<Self>, AnyError> {
|
||||
let fl = &global_state.flags;
|
||||
let state = CliState {
|
||||
global_state: global_state.clone(),
|
||||
main_module,
|
||||
|
@ -198,12 +184,9 @@ impl CliState {
|
|||
.unwrap_or_else(|| global_state.permissions.clone())
|
||||
.into(),
|
||||
import_map: None,
|
||||
metrics: Default::default(),
|
||||
global_timer: Default::default(),
|
||||
workers: Default::default(),
|
||||
next_worker_id: Default::default(),
|
||||
start_time: Instant::now(),
|
||||
seeded_rng: fl.seed.map(|v| StdRng::seed_from_u64(v).into()),
|
||||
target_lib: TargetLib::Worker,
|
||||
is_main: false,
|
||||
is_internal: false,
|
||||
|
|
|
@ -2,8 +2,10 @@
|
|||
|
||||
use crate::fmt_errors::JsError;
|
||||
use crate::global_state::GlobalState;
|
||||
use crate::global_timer::GlobalTimer;
|
||||
use crate::inspector::DenoInspector;
|
||||
use crate::js;
|
||||
use crate::metrics::Metrics;
|
||||
use crate::ops;
|
||||
use crate::ops::io::get_stdio;
|
||||
use crate::state::CliState;
|
||||
|
@ -18,6 +20,8 @@ use futures::channel::mpsc;
|
|||
use futures::future::FutureExt;
|
||||
use futures::stream::StreamExt;
|
||||
use futures::task::AtomicWaker;
|
||||
use rand::rngs::StdRng;
|
||||
use rand::SeedableRng;
|
||||
use std::env;
|
||||
use std::future::Future;
|
||||
use std::ops::Deref;
|
||||
|
@ -126,6 +130,14 @@ impl Worker {
|
|||
let ca_file = global_state.flags.ca_file.as_deref();
|
||||
let client = crate::http_util::create_http_client(ca_file).unwrap();
|
||||
op_state.put(client);
|
||||
|
||||
op_state.put(GlobalTimer::default());
|
||||
|
||||
if let Some(seed) = global_state.flags.seed {
|
||||
op_state.put(StdRng::seed_from_u64(seed));
|
||||
}
|
||||
|
||||
op_state.put(Metrics::default());
|
||||
}
|
||||
let inspector = {
|
||||
let global_state = &state.global_state;
|
||||
|
@ -381,7 +393,6 @@ mod tests {
|
|||
panic!("Future got unexpected error: {:?}", e);
|
||||
}
|
||||
});
|
||||
assert_eq!(state.metrics.borrow().resolve_count, 2);
|
||||
// Check that we didn't start the compiler.
|
||||
assert_eq!(state.global_state.compiler_starts.load(Ordering::SeqCst), 0);
|
||||
}
|
||||
|
@ -446,7 +457,6 @@ mod tests {
|
|||
if let Err(e) = (&mut *worker).await {
|
||||
panic!("Future got unexpected error: {:?}", e);
|
||||
}
|
||||
assert_eq!(state.metrics.borrow().resolve_count, 3);
|
||||
// Check that we've only invoked the compiler once.
|
||||
assert_eq!(state.global_state.compiler_starts.load(Ordering::SeqCst), 1);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue