Skip to content

Commit

Permalink
drop DefaultJsRuntime
Browse files Browse the repository at this point in the history
  • Loading branch information
ssddOnTop committed May 24, 2024
1 parent 40a2bf5 commit 8f69141
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 65 deletions.
5 changes: 2 additions & 3 deletions benches/data_loader_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use reqwest::Request;
use tailcall::core::config::Batch;
use tailcall::core::http::{DataLoaderRequest, HttpDataLoader, Response};
use tailcall::core::runtime::TargetRuntime;
use tailcall::core::worker::DefaultJsRuntime;
use tailcall::core::{EnvIO, FileIO, HttpIO};

#[derive(Clone)]
Expand Down Expand Up @@ -80,8 +79,8 @@ pub fn benchmark_data_loader(c: &mut Criterion) {
file: Arc::new(File {}),
cache: Arc::new(Cache {}),
extensions: Arc::new(vec![]),
http_worker: Arc::new(DefaultJsRuntime {}),
worker: Arc::new(DefaultJsRuntime {}),
http_worker: None,
worker: None,
};
let loader = HttpDataLoader::new(rt, None, false);
let loader = loader.to_data_loader(Batch::default().delay(1));
Expand Down
5 changes: 2 additions & 3 deletions benches/impl_path_string_for_evaluation_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use tailcall::core::http::{RequestContext, Response};
use tailcall::core::lambda::{EvaluationContext, ResolverContextLike};
use tailcall::core::path::PathString;
use tailcall::core::runtime::TargetRuntime;
use tailcall::core::worker::DefaultJsRuntime;
use tailcall::core::{EnvIO, FileIO, HttpIO};
use tailcall_http_cache::HttpCacheManager;

Expand Down Expand Up @@ -247,8 +246,8 @@ fn request_context() -> RequestContext {
file: Arc::new(File {}),
cache: Arc::new(InMemoryCache::new()),
extensions: Arc::new(vec![]),
http_worker: Arc::new(DefaultJsRuntime {}),
worker: Arc::new(DefaultJsRuntime {}),
http_worker: None,
worker: None,
};
RequestContext::new(runtime)
.server(server)
Expand Down
20 changes: 6 additions & 14 deletions src/cli/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,16 @@ fn init_hook_http(http: Arc<impl HttpIO>, script: Option<blueprint::Script>) ->
http
}

fn init_http_worker_io(script: Option<blueprint::Script>) -> Arc<dyn WorkerIO<Event, Command>> {
#[cfg(feature = "js")]
if let Some(script) = script {
return super::javascript::init_worker_io(script);
};

Arc::new(crate::core::worker::DefaultJsRuntime {})
fn init_http_worker_io(
script: Option<blueprint::Script>,
) -> Option<Arc<dyn WorkerIO<Event, Command>>> {
Some(super::javascript::init_worker_io(script?))
}

fn init_resolver_worker_io(
script: Option<blueprint::Script>,
) -> Arc<dyn WorkerIO<async_graphql::Value, async_graphql::Value>> {
#[cfg(feature = "js")]
if let Some(script) = script {
return super::javascript::init_worker_io(script);
};

Arc::new(crate::core::worker::DefaultJsRuntime {})
) -> Option<Arc<dyn WorkerIO<async_graphql::Value, async_graphql::Value>>> {
Some(super::javascript::init_worker_io(script?))
}

// Provides access to http in native rust environment
Expand Down
18 changes: 10 additions & 8 deletions src/core/lambda/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,19 @@ impl IO {

Ok(res.body)
}
IO::Js { name: method } => {
let value = ctx.value().cloned().unwrap_or(ConstValue::Null);
let value = ctx
IO::Js { name } => {
if let Some((worker, value)) = ctx
.request_ctx
.runtime
.worker
.call(method, value)
.await
.map_err(EvaluationError::from)?;

Ok(value.unwrap_or_default())
.as_ref()
.zip(ctx.value().cloned())
{
let val = worker.call(name, value).await?;
Ok(val.unwrap_or_default())
} else {
Ok(ConstValue::Null)

Check warning on line 154 in src/core/lambda/io.rs

View check run for this annotation

Codecov / codecov/patch

src/core/lambda/io.rs#L154

Added line #L154 was not covered by tests
}
}
}
})
Expand Down
9 changes: 4 additions & 5 deletions src/core/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ pub struct TargetRuntime {
/// functionality or integrate additional features.
pub extensions: Arc<Vec<SchemaExtension>>,
/// Worker middleware for handling HTTP requests.
pub http_worker: Arc<dyn WorkerIO<Event, Command>>,
pub http_worker: Option<Arc<dyn WorkerIO<Event, Command>>>,
/// Worker middleware for resolving data.
pub worker: Arc<dyn WorkerIO<ConstValue, ConstValue>>,
pub worker: Option<Arc<dyn WorkerIO<ConstValue, ConstValue>>>,
}

impl TargetRuntime {
Expand Down Expand Up @@ -59,7 +59,6 @@ pub mod test {
use crate::core::cache::InMemoryCache;
use crate::core::http::Response;
use crate::core::runtime::TargetRuntime;
use crate::core::worker::DefaultJsRuntime;
use crate::core::{blueprint, EnvIO, FileIO, HttpIO};

#[derive(Clone)]
Expand Down Expand Up @@ -197,8 +196,8 @@ pub mod test {
file: Arc::new(file),
cache: Arc::new(InMemoryCache::new()),
extensions: Arc::new(vec![]),
http_worker: Arc::new(DefaultJsRuntime {}),
worker: Arc::new(DefaultJsRuntime {}),
http_worker: None,
worker: None,
}
}
}
11 changes: 1 addition & 10 deletions src/core/worker.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
use crate::core::{Response, WorkerIO};

pub struct DefaultJsRuntime;

#[async_trait::async_trait]
impl<A: Send + Sync + 'static, B> WorkerIO<A, B> for DefaultJsRuntime {
async fn call(&self, _: &'async_trait str, _: A) -> anyhow::Result<Option<B>> {
anyhow::bail!("JavaScript runtime is not supported in this build")
}
}
use crate::core::Response;

#[derive(Debug)]
pub struct WorkerResponse(pub Response<String>);
Expand Down
5 changes: 2 additions & 3 deletions tailcall-aws-lambda/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::sync::Arc;
use anyhow::anyhow;
use tailcall::core::cache::InMemoryCache;
use tailcall::core::runtime::TargetRuntime;
use tailcall::core::worker::DefaultJsRuntime;
use tailcall::core::{EntityCache, EnvIO, FileIO};
use tokio::io::AsyncReadExt;

Expand Down Expand Up @@ -61,7 +60,7 @@ pub fn init_runtime() -> TargetRuntime {
env: init_env(),
cache: init_cache(),
extensions: Arc::new(vec![]),
http_worker: Arc::new(DefaultJsRuntime {}),
worker: Arc::new(DefaultJsRuntime {}),
http_worker: None,
worker: None,

Check warning on line 64 in tailcall-aws-lambda/src/runtime.rs

View check run for this annotation

Codecov / codecov/patch

tailcall-aws-lambda/src/runtime.rs#L63-L64

Added lines #L63 - L64 were not covered by tests
}
}
5 changes: 2 additions & 3 deletions tailcall-cloudflare/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::sync::Arc;
use anyhow::anyhow;
use async_graphql_value::ConstValue;
use tailcall::core::runtime::TargetRuntime;
use tailcall::core::worker::DefaultJsRuntime;
use tailcall::core::{EnvIO, FileIO, HttpIO};

use crate::{cache, env, file, http};
Expand Down Expand Up @@ -41,7 +40,7 @@ pub fn init(env: Rc<worker::Env>) -> anyhow::Result<TargetRuntime> {
file: init_file(env.clone(), &bucket_id)?,
cache: init_cache(env),
extensions: Arc::new(vec![]),
http_worker: Arc::new(DefaultJsRuntime {}),
worker: Arc::new(DefaultJsRuntime {}),
http_worker: None,
worker: None,

Check warning on line 44 in tailcall-cloudflare/src/runtime.rs

View check run for this annotation

Codecov / codecov/patch

tailcall-cloudflare/src/runtime.rs#L43-L44

Added lines #L43 - L44 were not covered by tests
})
}
23 changes: 13 additions & 10 deletions tests/core/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::str::FromStr;
use std::sync::Arc;

use anyhow::anyhow;
use async_graphql_value::ConstValue;
use markdown::mdast::Node;
use markdown::ParseOptions;
use tailcall::cli::javascript;
Expand All @@ -16,8 +17,8 @@ use tailcall::core::cache::InMemoryCache;
use tailcall::core::config::{ConfigModule, Source};
use tailcall::core::http::AppContext;
use tailcall::core::runtime::TargetRuntime;
use tailcall::core::worker::DefaultJsRuntime;
use tailcall::core::EnvIO;
use tailcall::core::worker::{Command, Event};
use tailcall::core::{EnvIO, WorkerIO};

use super::file::File;
use super::http::Http;
Expand Down Expand Up @@ -281,16 +282,18 @@ impl ExecutionSpec {

let http2_only = http.clone();

let http_worker = if let Some(script) = script.clone() {
javascript::init_worker_io(script)
} else {
Arc::new(DefaultJsRuntime {})
};
let http_worker: Option<Arc<dyn WorkerIO<Event, Command>>> =
if let Some(script) = script.clone() {
Some(javascript::init_worker_io(script))
} else {
None
};

let worker = if let Some(script) = script {
javascript::init_worker_io(script)
let worker: Option<Arc<dyn WorkerIO<ConstValue, ConstValue>>> = if let Some(script) = script
{
Some(javascript::init_worker_io(script))
} else {
Arc::new(DefaultJsRuntime {})
None
};

let runtime = TargetRuntime {
Expand Down
5 changes: 2 additions & 3 deletions tests/core/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use tailcall::core::blueprint::Script;
use tailcall::core::cache::InMemoryCache;
use tailcall::core::config::Source;
use tailcall::core::runtime::TargetRuntime;
use tailcall::core::worker::DefaultJsRuntime;

use super::env::Env;
use super::file::TestFileIO;
Expand Down Expand Up @@ -88,7 +87,7 @@ pub fn create_runtime(
file: Arc::new(file),
cache: Arc::new(InMemoryCache::new()),
extensions: Arc::new(vec![]),
http_worker: Arc::new(DefaultJsRuntime {}),
worker: Arc::new(DefaultJsRuntime {}),
http_worker: None,
worker: None,
}
}
5 changes: 2 additions & 3 deletions tests/server_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ pub mod test {
use tailcall::core::cache::InMemoryCache;
use tailcall::core::http::Response;
use tailcall::core::runtime::TargetRuntime;
use tailcall::core::worker::DefaultJsRuntime;
use tailcall::core::{EnvIO, FileIO, HttpIO};
use tailcall_http_cache::HttpCacheManager;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
Expand Down Expand Up @@ -155,8 +154,8 @@ pub mod test {
file: Arc::new(file),
cache: Arc::new(InMemoryCache::new()),
extensions: Arc::new(vec![]),
http_worker: Arc::new(DefaultJsRuntime {}),
worker: Arc::new(DefaultJsRuntime {}),
http_worker: None,
worker: None,
}
}
}
Expand Down

1 comment on commit 8f69141

@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 6.71ms 2.97ms 76.33ms 72.63%
Req/Sec 3.77k 203.07 4.51k 93.08%

449756 requests in 30.00s, 2.25GB read

Requests/sec: 14989.46

Transfer/sec: 76.94MB

Please sign in to comment.