-
Notifications
You must be signed in to change notification settings - Fork 259
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Refactor execution_spec (#1727)
Co-authored-by: Tushar Mathur <[email protected]>
- Loading branch information
1 parent
28b670f
commit 260907d
Showing
874 changed files
with
11,775 additions
and
11,699 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
extern crate core; | ||
|
||
use std::borrow::Cow; | ||
use std::collections::HashMap; | ||
|
||
use tailcall::EnvIO; | ||
|
||
#[derive(Clone)] | ||
pub struct Env { | ||
vars: HashMap<String, String>, | ||
} | ||
|
||
impl EnvIO for Env { | ||
fn get(&self, key: &str) -> Option<Cow<'_, str>> { | ||
self.vars.get(key).map(Cow::from) | ||
} | ||
} | ||
|
||
impl Env { | ||
pub fn init(vars: Option<HashMap<String, String>>) -> Self { | ||
Self { vars: vars.unwrap_or_default() } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
extern crate core; | ||
|
||
use std::path::PathBuf; | ||
|
||
use anyhow::{anyhow, Context}; | ||
use tailcall::FileIO; | ||
use tokio::io::{AsyncReadExt, AsyncWriteExt}; | ||
|
||
use super::runtime::ExecutionSpec; | ||
pub struct File { | ||
spec: ExecutionSpec, | ||
} | ||
|
||
impl File { | ||
pub fn new(spec: ExecutionSpec) -> File { | ||
File { spec } | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl FileIO for File { | ||
async fn write<'a>(&'a self, _path: &'a str, _content: &'a [u8]) -> anyhow::Result<()> { | ||
Err(anyhow!("Cannot write to a file in an execution spec")) | ||
} | ||
|
||
async fn read<'a>(&'a self, path: &'a str) -> anyhow::Result<String> { | ||
let base = PathBuf::from(path); | ||
let path = base | ||
.file_name() | ||
.context("Invalid file path")? | ||
.to_str() | ||
.context("Invalid OsString")?; | ||
match self.spec.files.get(path) { | ||
Some(x) => Ok(x.to_owned()), | ||
None => Err(anyhow!("No such file or directory (os error 2)")), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Clone)] | ||
pub struct TestFileIO {} | ||
|
||
impl TestFileIO { | ||
pub fn init() -> Self { | ||
TestFileIO {} | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl FileIO for TestFileIO { | ||
async fn write<'a>(&'a self, path: &'a str, content: &'a [u8]) -> anyhow::Result<()> { | ||
let mut file = tokio::fs::File::create(path).await?; | ||
file.write_all(content) | ||
.await | ||
.map_err(|e| anyhow!("{}", e))?; | ||
Ok(()) | ||
} | ||
|
||
async fn read<'a>(&'a self, path: &'a str) -> anyhow::Result<String> { | ||
let mut file = tokio::fs::File::open(path).await?; | ||
let mut buffer = Vec::new(); | ||
file.read_to_end(&mut buffer) | ||
.await | ||
.map_err(|e| anyhow!("{}", e))?; | ||
Ok(String::from_utf8(buffer)?) | ||
} | ||
} |
Oops, something went wrong.
260907d
There was a problem hiding this comment.
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
401656 requests in 30.02s, 2.01GB read
Requests/sec: 13379.35
Transfer/sec: 68.67MB