Skip to content

Commit

Permalink
chore: upgrade rustc version (#1874)
Browse files Browse the repository at this point in the history
Co-authored-by: Tushar Mathur <[email protected]>
  • Loading branch information
meskill and tusharmath authored May 7, 2024
1 parent 7dc6191 commit 5e3ca1d
Show file tree
Hide file tree
Showing 18 changed files with 163 additions and 62 deletions.
2 changes: 1 addition & 1 deletion .nightly/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "nightly-2024-02-01"
channel = "nightly-2024-05-06"
profile = "default"
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "1.75"
channel = "1.78"
profile = "default"
2 changes: 1 addition & 1 deletion src/cli/javascript/request_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub struct RequestFilter {

impl RequestFilter {
pub fn new(
client: Arc<impl HttpIO + Send + Sync + 'static>,
client: Arc<impl HttpIO + 'static>,
worker: Arc<impl WorkerIO<Event, Command>>,
) -> Self {
Self { worker, client }
Expand Down
72 changes: 42 additions & 30 deletions src/cli/javascript/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,10 @@ use crate::{blueprint, WorkerIO};
struct LocalRuntime(Context);

thread_local! {
// Practically only one JS runtime is created because CHANNEL_RUNTIME is single threaded.
// TODO: that is causing issues in `execution_spec` tests because the runtime
// is initialized only once and that implementation will be reused by all the tests
// Practically only one JS runtime is created for every Runtime because tokio_runtime is single threaded.
static LOCAL_RUNTIME: RefCell<OnceCell<LocalRuntime>> = const { RefCell::new(OnceCell::new()) };
}

// Single threaded JS runtime, that's shared across all tokio workers.
lazy_static::lazy_static! {
static ref CHANNEL_RUNTIME: tokio::runtime::Runtime = tokio::runtime::Builder::new_multi_thread()
.worker_threads(1)
.build()
.expect("JS runtime not initialized");
}

#[rquickjs::function]
fn qjs_print(msg: String, is_err: bool) {
if is_err {
Expand Down Expand Up @@ -57,39 +47,61 @@ impl LocalRuntime {

pub struct Runtime {
script: blueprint::Script,
// Single threaded JS runtime, that's shared across all tokio workers.
tokio_runtime: Option<tokio::runtime::Runtime>,
}

impl Runtime {
pub fn new(script: blueprint::Script) -> Self {
Self { script }
let tokio_runtime = tokio::runtime::Builder::new_multi_thread()
.worker_threads(1)
.build()
.expect("JS runtime not initialized");

Self { script, tokio_runtime: Some(tokio_runtime) }
}
}

impl Drop for Runtime {
fn drop(&mut self) {
// implicit call implementation to shutdown the tokio runtime
// without blocking. Otherwise it will panic on an attempt to
// drop AppContext in async runtime (e.g. in tests at least)
if let Some(runtime) = self.tokio_runtime.take() {
runtime.shutdown_background();
}
}
}

#[async_trait::async_trait]
impl WorkerIO<Event, Command> for Runtime {
async fn call(&self, name: String, event: Event) -> anyhow::Result<Option<Command>> {
let script = self.script.clone();
CHANNEL_RUNTIME
.spawn(async move {
// initialize runtime if this is the first call
// exit if failed to initialize
LOCAL_RUNTIME.with(move |cell| {
if cell.borrow().get().is_none() {
LocalRuntime::try_new(script).and_then(|runtime| {
cell.borrow().set(runtime).map_err(|_| {
anyhow::anyhow!(
if let Some(runtime) = &self.tokio_runtime {
runtime
.spawn(async move {
// initialize runtime if this is the first call
// exit if failed to initialize
LOCAL_RUNTIME.with(move |cell| {
if cell.borrow().get().is_none() {
LocalRuntime::try_new(script).and_then(|runtime| {
cell.borrow().set(runtime).map_err(|_| {
anyhow::anyhow!(
"trying to reinitialize an already initialized QuickJS runtime"
)
})
})
})
} else {
Ok(())
}
})?;

call(name, event)
})
.await?
} else {
Ok(())
}
})?;

call(name, event)
})
.await?
} else {
anyhow::bail!("JS Runtime is stopped")
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/config/config_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,14 @@ impl ConfigModule {
for field in ty.fields.values_mut() {
if let Some(resolution) = resolution_map.get(&field.type_of) {
if self.output_types.contains(&k) {
field.type_of = resolution.output.clone();
field.type_of.clone_from(&resolution.output);
} else if self.input_types.contains(&k) {
field.type_of = resolution.input.clone();
field.type_of.clone_from(&resolution.input);
}
}
for arg in field.args.values_mut() {
if let Some(resolution) = resolution_map.get(&arg.type_of) {
arg.type_of = resolution.input.clone();
arg.type_of.clone_from(&resolution.input);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/config/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ impl ConfigReader {

/// Reads the links in a Config and fill the content
#[async_recursion::async_recursion]
async fn ext_links<'a: 'async_recursion>(
async fn ext_links(
&self,
mut config_module: ConfigModule,
parent_dir: Option<&'a Path>,
parent_dir: Option<&'async_recursion Path>,
) -> anyhow::Result<ConfigModule> {
let links: Vec<Link> = config_module
.config
Expand Down
16 changes: 0 additions & 16 deletions src/data_loader/data_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ use std::time::Duration;

use futures_channel::oneshot;
use futures_timer::Delay;
#[cfg(feature = "tracing")]
use tracing::{info_span, instrument, Instrument};
#[cfg(feature = "tracing")]
use tracinglib as tracing;

pub use super::cache::NoCache;
pub use super::factory::CacheFactory;
Expand Down Expand Up @@ -108,7 +104,6 @@ where
}

/// Use this `DataLoader` load a data.
#[cfg_attr(feature = "tracing", instrument(skip_all))]
pub async fn load_one(&self, key: K) -> Result<Option<T::Value>, T::Error>
where
K: Send + Sync + Hash + Eq + Clone + 'static,
Expand All @@ -119,7 +114,6 @@ where
}

/// Use this `DataLoader` to load some data.
#[cfg_attr(feature = "tracing", instrument(skip_all))]
pub async fn load_many<I>(&self, keys: I) -> Result<HashMap<K, T::Value>, T::Error>
where
K: Send + Sync + Hash + Eq + Clone + 'static,
Expand Down Expand Up @@ -182,10 +176,6 @@ where
let inner = self.inner.clone();
let disable_cache = self.disable_cache.load(Ordering::SeqCst);
let task = async move { inner.do_load(disable_cache, keys).await };
#[cfg(feature = "tracing")]
let task = task
.instrument(info_span!("immediate_load"))
.in_current_span();

#[cfg(not(target_arch = "wasm32"))]
tokio::spawn(Box::pin(task));
Expand All @@ -209,8 +199,6 @@ where
inner.do_load(disable_cache, keys).await
}
};
#[cfg(feature = "tracing")]
let task = task.instrument(info_span!("start_fetch")).in_current_span();
#[cfg(not(target_arch = "wasm32"))]
tokio::spawn(Box::pin(task));
#[cfg(target_arch = "wasm32")]
Expand All @@ -226,7 +214,6 @@ where
///
/// **NOTE: If the cache type is [NoCache], this function will not take
/// effect. **
#[cfg_attr(feature = "tracing", instrument(skip_all))]
pub async fn feed_many<I>(&self, values: I)
where
K: Send + Sync + Hash + Eq + Clone + 'static,
Expand All @@ -245,7 +232,6 @@ where
///
/// **NOTE: If the cache type is [NoCache], this function will not take
/// effect. **
#[cfg_attr(feature = "tracing", instrument(skip_all))]
pub async fn feed_one(&self, key: K, value: T::Value)
where
K: Send + Sync + Hash + Eq + Clone + 'static,
Expand All @@ -258,7 +244,6 @@ where
///
/// **NOTE: If the cache type is [NoCache], this function will not take
/// effect. **
#[cfg_attr(feature = "tracing", instrument(skip_all))]
pub fn clear(&self)
where
K: Send + Sync + Hash + Eq + Clone + 'static,
Expand Down Expand Up @@ -339,7 +324,6 @@ where
T: Loader<K>,
C: CacheFactory<K, T::Value>,
{
#[cfg_attr(feature = "tracing", instrument(skip_all))]
async fn do_load(&self, disable_cache: bool, (keys, senders): KeysAndSender<K, T>)
where
K: Send + Sync + Hash + Eq + Clone + 'static,
Expand Down
2 changes: 1 addition & 1 deletion src/generator/from_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ impl Context {
cfg_field.type_of = output_ty;
cfg_field.required = true;

grpc_method.service = service_name.clone();
grpc_method.service.clone_from(&service_name);
grpc_method.name = field_name.to_string();

cfg_field.grpc = Some(Grpc {
Expand Down
2 changes: 1 addition & 1 deletion src/proto_reader/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl ProtoReader {

for result in results {
let proto = result?;
if descriptors.get(proto.name()).is_none() {
if !descriptors.contains_key(proto.name()) {
queue.push_back(proto.clone());
descriptors.insert(proto.name().to_string(), proto);
}
Expand Down
6 changes: 3 additions & 3 deletions tailcall-upstream-grpc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ impl NewsService for MyNewsService {
let new_news = request.into_inner();
let mut lock = self.news.lock().unwrap();
if let Some(news) = lock.iter_mut().find(|n| n.id == new_news.id) {
news.title = new_news.title.clone();
news.body = new_news.body.clone();
news.post_image = new_news.post_image.clone();
news.title.clone_from(&new_news.title);
news.body.clone_from(&new_news.body);
news.post_image.clone_from(&new_news.post_image);
return Ok(Response::new(new_news));
}
Err(Status::not_found("News not found"))
Expand Down
16 changes: 16 additions & 0 deletions tests/core/snapshots/test-js-request-response-2.md_0.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
source: tests/core/spec.rs
expression: response
---
{
"status": 200,
"headers": {
"content-type": "application/json"
},
"body": {
"data": {
"hello": "darkness",
"hi": "I've come to talk with you again"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ expression: response
},
"body": {
"data": {
"hi": "hello world"
"hello": "hello world",
"hi": "bye world"
}
}
}
24 changes: 24 additions & 0 deletions tests/core/snapshots/test-js-request-response.md_client.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
source: tests/core/spec.rs
expression: client
---
scalar Date

scalar Email

scalar Empty

scalar JSON

scalar PhoneNumber

type Query {
hello: String
hi: String
}

scalar Url

schema {
query: Query
}
12 changes: 12 additions & 0 deletions tests/core/snapshots/test-js-request-response.md_merged.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: tests/core/spec.rs
expression: merged
---
schema @server @upstream @link(src: "test.js", type: Script) {
query: Query
}

type Query {
hello: String @http(baseURL: "http://localhost:3000", path: "/hello")
hi: String @http(baseURL: "http://localhost:3000", path: "/hi")
}
52 changes: 52 additions & 0 deletions tests/execution/test-js-request-response-2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Js Request Response Hello World

This test is just a mirror of existing `test-js-request-response.md` but with changed values. It exists to test that js runtime is created separately for every app_ctx and they not interfere with each other.

```js @file:test.js
function onRequest({request}) {
if (request.uri.path.endsWith("/hello")) {
return {
response: {
status: 200,
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify("darkness"),
},
}
} else if (request.uri.path.endsWith("/hi")) {
request.uri.path = "/old-friend"
console.log({request})
return {request}
} else {
return {request}
}
}
```

```graphql @server
schema @server @link(type: Script, src: "test.js") {
query: Query
}

type Query {
hello: String @http(baseURL: "http://localhost:3000", path: "/hello")
hi: String @http(baseURL: "http://localhost:3000", path: "/hi")
}
```

```yml @mock
- request:
method: GET
url: http://localhost:3000/old-friend
response:
status: 200
body: I've come to talk with you again
```

```yml @test
- method: POST
url: http://localhost:8080/graphql
body:
query: query { hello hi }
```
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ type Query {
url: http://localhost:3000/bye
response:
status: 200
body: hello world
body: bye world
```

```yml @test
- method: POST
url: http://localhost:8080/graphql
body:
query: query { hi }
query: query { hello hi }
```

1 comment on commit 5e3ca1d

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running 30s test @ http://localhost:8000/graphql

4 threads and 100 connections

Thread Stats Avg Stdev Max +/- Stdev
Latency 7.13ms 3.21ms 89.43ms 73.26%
Req/Sec 3.55k 182.22 3.95k 93.17%

424015 requests in 30.00s, 2.13GB read

Requests/sec: 14131.54

Transfer/sec: 72.53MB

Please sign in to comment.