1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-25 15:29:32 -05:00

refactor: move mod tokio_util to runtime (#12332)

This avoids a bunch of duplicated code.
This commit is contained in:
Bert Belder 2021-10-21 13:05:43 +02:00
parent b042697902
commit f1b1a3f389
No known key found for this signature in database
GPG key ID: 7A77887B2E2ED461
9 changed files with 21 additions and 40 deletions

View file

@ -5,12 +5,12 @@ use crate::cache::FetchCacher;
use crate::flags::Flags;
use crate::proc_state::ProcState;
use crate::resolver::ImportMapResolver;
use crate::tokio_util::create_basic_runtime;
use deno_core::error::anyhow;
use deno_core::error::AnyError;
use deno_core::ModuleSpecifier;
use deno_runtime::permissions::Permissions;
use deno_runtime::tokio_util::create_basic_runtime;
use import_map::ImportMap;
use std::path::PathBuf;
use std::thread;

View file

@ -1,7 +1,5 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
use crate::tokio_util::create_basic_runtime;
use deno_core::error::anyhow;
use deno_core::error::AnyError;
use deno_core::parking_lot::RwLock;
@ -11,6 +9,7 @@ use deno_core::serde_json;
use deno_core::serde_json::Value;
use deno_core::url::Url;
use deno_core::ModuleSpecifier;
use deno_runtime::tokio_util::create_basic_runtime;
use log::error;
use lsp::WorkspaceFolder;
use lspower::lsp;

View file

@ -7,13 +7,13 @@ use super::language_server;
use super::tsc;
use crate::diagnostics;
use crate::tokio_util::create_basic_runtime;
use deno_core::error::anyhow;
use deno_core::error::AnyError;
use deno_core::resolve_url;
use deno_core::serde_json::json;
use deno_core::ModuleSpecifier;
use deno_runtime::tokio_util::create_basic_runtime;
use log::error;
use lspower::lsp;
use std::collections::HashMap;

View file

@ -16,7 +16,6 @@ use super::text::LineIndex;
use super::urls::INVALID_SPECIFIER;
use crate::config_file::TsConfig;
use crate::tokio_util::create_basic_runtime;
use crate::tsc;
use crate::tsc::ResolveArgs;
@ -37,6 +36,7 @@ use deno_core::JsRuntime;
use deno_core::ModuleSpecifier;
use deno_core::OpFn;
use deno_core::RuntimeOptions;
use deno_runtime::tokio_util::create_basic_runtime;
use log::warn;
use lspower::lsp;
use regex::Captures;

View file

@ -30,7 +30,6 @@ mod resolver;
mod source_maps;
mod standalone;
mod text_encoding;
mod tokio_util;
mod tools;
mod tsc;
mod unix_util;
@ -78,6 +77,7 @@ use deno_core::ModuleSpecifier;
use deno_runtime::colors;
use deno_runtime::ops::worker_host::CreateWebWorkerCb;
use deno_runtime::permissions::Permissions;
use deno_runtime::tokio_util::run_basic;
use deno_runtime::web_worker::WebWorker;
use deno_runtime::web_worker::WebWorkerOptions;
use deno_runtime::worker::MainWorker;
@ -1389,7 +1389,7 @@ pub fn main() {
let args: Vec<String> = env::args().collect();
let standalone_res = match standalone::extract_standalone(args.clone()) {
Ok(Some((metadata, bundle))) => {
tokio_util::run_basic(standalone::run(bundle, metadata))
run_basic(standalone::run(bundle, metadata))
}
Ok(None) => Ok(()),
Err(err) => Err(err),
@ -1417,5 +1417,5 @@ pub fn main() {
logger::init(flags.log_level);
unwrap_or_exit(tokio_util::run_basic(get_subcommand(flags)));
unwrap_or_exit(run_basic(get_subcommand(flags)));
}

View file

@ -1,25 +0,0 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
pub fn create_basic_runtime() -> tokio::runtime::Runtime {
tokio::runtime::Builder::new_current_thread()
.enable_io()
.enable_time()
// This limits the number of threads for blocking operations (like for
// synchronous fs ops) or CPU bound tasks like when we run dprint in
// parallel for deno fmt.
// The default value is 512, which is an unhelpfully large thread pool. We
// don't ever want to have more than a couple dozen threads.
.max_blocking_threads(32)
.build()
.unwrap()
}
// TODO(ry) rename to run_local ?
pub fn run_basic<F, R>(future: F) -> R
where
F: std::future::Future<Output = R>,
{
let rt = create_basic_runtime();
let local = tokio::task::LocalSet::new();
local.block_on(&rt, future)
}

View file

@ -18,7 +18,6 @@ use crate::lockfile;
use crate::ops;
use crate::proc_state::ProcState;
use crate::resolver::ImportMapResolver;
use crate::tokio_util;
use crate::tools::coverage::CoverageCollector;
use deno_ast::swc::common::comments::CommentKind;
@ -33,6 +32,7 @@ use deno_core::serde_json::json;
use deno_core::JsRuntime;
use deno_core::ModuleSpecifier;
use deno_runtime::permissions::Permissions;
use deno_runtime::tokio_util::run_basic;
use log::Level;
use rand::rngs::SmallRng;
use rand::seq::SliceRandom;
@ -782,7 +782,7 @@ async fn test_specifiers(
sender,
);
tokio_util::run_basic(future)
run_basic(future)
});
join_handle.join().unwrap()

View file

@ -13,3 +13,13 @@ pub fn create_basic_runtime() -> tokio::runtime::Runtime {
.build()
.unwrap()
}
// TODO(ry) rename to run_local ?
pub fn run_basic<F, R>(future: F) -> R
where
F: std::future::Future<Output = R>,
{
let rt = create_basic_runtime();
let local = tokio::task::LocalSet::new();
local.block_on(&rt, future)
}

View file

@ -4,7 +4,7 @@ use crate::inspector_server::InspectorServer;
use crate::js;
use crate::ops;
use crate::permissions::Permissions;
use crate::tokio_util::create_basic_runtime;
use crate::tokio_util::run_basic;
use crate::BootstrapOptions;
use deno_broadcast_channel::InMemoryBroadcastChannel;
use deno_core::error::AnyError;
@ -545,8 +545,6 @@ pub fn run_web_worker(
) -> Result<(), AnyError> {
let name = worker.name.to_string();
let rt = create_basic_runtime();
// TODO(bartlomieju): run following block using "select!"
// with terminate
@ -587,6 +585,5 @@ pub fn run_web_worker(
debug!("Worker thread shuts down {}", &name);
result
};
rt.block_on(fut)
run_basic(fut)
}