Skip to content

Commit

Permalink
tailcall-cloudflare build fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mehulmathur16 committed Jun 13, 2024
1 parent fbaac3d commit 3f45da3
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 19 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/core/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ pub mod file {

#[error("Cannot write to a file in an execution spec")]
ExecutionSpecFileWriteFailed,

#[error("Cloudflare Worker Execution Error : {0}")]
#[from(ignore)]
Cloudflare(String),
}
}

Expand Down
3 changes: 2 additions & 1 deletion tailcall-cloudflare/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ serde_qs = "0.13.0"
console_error_panic_hook = "0.1.7"
protox = "0.6.0"
async-graphql-value = "7.0.3"

derive_more = "0.99.17"
thiserror = "1.0.59"
12 changes: 12 additions & 0 deletions tailcall-cloudflare/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use derive_more::From;

#[derive(From, thiserror::Error, Debug)]
pub enum Error {
#[error("Worker Error")]
Worker(worker::Error),

#[error("File {0} was not found in bucket")]
MissingFileInBucket(String),
}

pub type Result<A> = std::result::Result<A, Error>;
43 changes: 25 additions & 18 deletions tailcall-cloudflare/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use std::rc::Rc;

use anyhow::anyhow;
use async_std::task::spawn_local;
use tailcall::core::error::file::FileError;
use tailcall::core::FileIO;
use worker::Env;

use crate::to_anyhow;
use super::{Error, Result};

#[derive(Clone)]
pub struct CloudflareFileIO {
Expand All @@ -26,42 +27,48 @@ impl CloudflareFileIO {
unsafe impl Sync for CloudflareFileIO {}
unsafe impl Send for CloudflareFileIO {}

async fn get(bucket: Rc<worker::Bucket>, path: String) -> anyhow::Result<String> {
let maybe_object = bucket
.get(path.clone())
.execute()
.await
.map_err(to_anyhow)?;
let object = maybe_object.ok_or(anyhow!("File '{}' was not found in bucket", path))?;
async fn get(bucket: Rc<worker::Bucket>, path: String) -> Result<String> {
let maybe_object = bucket.get(path.clone()).execute().await?;
let object = maybe_object.ok_or(Error::MissingFileInBucket(path.to_string()))?;

let body = match object.body() {
Some(body) => body.text().await.map_err(to_anyhow),
None => Ok("".to_string()),
Some(body) => body.text().await?,
None => "".to_string(),
};
body
Ok(body)
}

async fn put(bucket: Rc<worker::Bucket>, path: String, value: Vec<u8>) -> anyhow::Result<()> {
bucket.put(path, value).execute().await.map_err(to_anyhow)?;
async fn put(bucket: Rc<worker::Bucket>, path: String, value: Vec<u8>) -> Result<()> {
bucket.put(path, value).execute().await?;
Ok(())
}

#[async_trait::async_trait]
impl FileIO for CloudflareFileIO {
async fn write<'a>(&'a self, path: &'a str, content: &'a [u8]) -> anyhow::Result<()> {
type Error = FileError;

async fn write<'a>(
&'a self,
path: &'a str,
content: &'a [u8],
) -> std::result::Result<(), Self::Error> {
let content = content.to_vec();
let bucket = self.bucket.clone();
let path_cloned = path.to_string();
spawn_local(put(bucket, path_cloned, content)).await?;
let _ = spawn_local(put(bucket, path_cloned, content))
.await
.map_err(|e| FileError::Cloudflare(e.to_string()));
tracing::info!("File write: {} ... ok", path);
Ok(())
}

async fn read<'a>(&'a self, path: &'a str) -> anyhow::Result<String> {
async fn read<'a>(&'a self, path: &'a str) -> std::result::Result<String, Self::Error> {
let bucket = self.bucket.clone();
let path_cloned = path.to_string();
let content = spawn_local(get(bucket, path_cloned)).await?;
let content = spawn_local(get(bucket, path_cloned))
.await
.map_err(|e| FileError::Cloudflare(e.to_string()));
tracing::info!("File read: {} ... ok", path);
Ok(content)
content
}
}
3 changes: 3 additions & 0 deletions tailcall-cloudflare/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ use anyhow::anyhow;

mod cache;
mod env;
mod error;
mod file;
pub mod handle;
mod http;
mod runtime;

pub use error::{Error, Result};

#[worker::event(fetch)]
async fn fetch(
req: worker::Request,
Expand Down

1 comment on commit 3f45da3

@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.67ms 3.00ms 72.70ms 72.17%
Req/Sec 3.79k 205.96 4.42k 91.08%

452657 requests in 30.01s, 2.27GB read

Requests/sec: 15084.83

Transfer/sec: 77.43MB

Please sign in to comment.