Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: duplicate read proto #1741

Merged
merged 24 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ convert_case = "0.6.0"
rand = "0.8.5"
tailcall-macros = { path = "tailcall-macros" }
tonic-types = "0.11.0"
async-lock = "3.3.0"


[dev-dependencies]
Expand Down
8 changes: 6 additions & 2 deletions src/config/reader.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::cell::RefCell;
use std::collections::HashMap;
use std::path::Path;
use std::sync::Arc;

use async_lock::Mutex;
use rustls_pemfile;
use rustls_pki_types::{
CertificateDer, PrivateKeyDer, PrivatePkcs1KeyDer, PrivatePkcs8KeyDer, PrivateSec1KeyDer,
Expand All @@ -24,10 +27,11 @@ pub struct ConfigReader {

impl ConfigReader {
pub fn init(runtime: TargetRuntime) -> Self {
let cache: Arc<Mutex<RefCell<HashMap<String, String>>>> = Default::default();
Self {
runtime: runtime.clone(),
resource_reader: ResourceReader::init(runtime.clone()),
proto_reader: ProtoReader::init(runtime),
resource_reader: ResourceReader::init(runtime.clone(), cache.clone()),
proto_reader: ProtoReader::init(runtime, cache.clone()),
Shylock-Hg marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/generator/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct Generator {
}
impl Generator {
pub fn init(runtime: TargetRuntime) -> Self {
Self { proto_reader: ProtoReader::init(runtime) }
Self { proto_reader: ProtoReader::init(runtime, Default::default()) }
Shylock-Hg marked this conversation as resolved.
Show resolved Hide resolved
}

pub async fn read_all<T: AsRef<str>>(
Expand Down
16 changes: 11 additions & 5 deletions src/proto_reader.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::cell::RefCell;
use std::collections::{HashMap, VecDeque};
use std::sync::Arc;

use anyhow::Context;
use async_lock::Mutex;
use futures_util::future::join_all;
use prost_reflect::prost_types::{FileDescriptorProto, FileDescriptorSet};
use protox::file::{FileResolver, GoogleFileResolver};
Expand All @@ -18,8 +21,11 @@ pub struct ProtoMetadata {
}

impl ProtoReader {
pub fn init(runtime: TargetRuntime) -> Self {
Self { resource_reader: ResourceReader::init(runtime) }
pub fn init(
runtime: TargetRuntime,
cache: Arc<Mutex<RefCell<HashMap<String, String>>>>,
) -> Self {
Self { resource_reader: ResourceReader::init(runtime, cache) }
}

pub async fn read_all<T: AsRef<str>>(&self, paths: &[T]) -> anyhow::Result<Vec<ProtoMetadata>> {
Expand Down Expand Up @@ -104,7 +110,7 @@ mod test_proto_config {
#[tokio::test]
async fn test_resolve() {
// Skipping IO tests as they are covered in reader.rs
let reader = ProtoReader::init(crate::runtime::test::init(None));
let reader = ProtoReader::init(crate::runtime::test::init(None), Default::default());
reader
.read_proto("google/protobuf/empty.proto")
.await
Expand Down Expand Up @@ -133,7 +139,7 @@ mod test_proto_config {
let runtime = crate::runtime::test::init(None);
let file_rt = runtime.file.clone();

let reader = ProtoReader::init(runtime);
let reader = ProtoReader::init(runtime, Default::default());
let helper_map = reader
.resolve_descriptors(reader.read_proto(&test_file).await?)
.await?;
Expand Down Expand Up @@ -177,7 +183,7 @@ mod test_proto_config {
#[tokio::test]
async fn test_proto_no_pkg() -> Result<()> {
let runtime = crate::runtime::test::init(None);
let reader = ProtoReader::init(runtime);
let reader = ProtoReader::init(runtime, Default::default());
let mut proto_no_pkg = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
proto_no_pkg.push("src/grpc/tests/proto_no_pkg.graphql");
let config_module = reader.read(proto_no_pkg.to_str().unwrap()).await;
Expand Down
42 changes: 38 additions & 4 deletions src/resource_reader.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
use std::cell::RefCell;
use std::collections::HashMap;
use std::sync::Arc;

use async_lock::Mutex;
use futures_util::future::join_all;
use futures_util::TryFutureExt;
use url::Url;
Expand All @@ -13,14 +18,44 @@

pub struct ResourceReader {
runtime: TargetRuntime,
// Cache file content, path->content
cache: Arc<Mutex<RefCell<HashMap<String, String>>>>,
}

impl ResourceReader {
pub fn init(runtime: TargetRuntime) -> Self {
Self { runtime }
pub fn init(
runtime: TargetRuntime,
cache: Arc<Mutex<RefCell<HashMap<String, String>>>>,
) -> Self {
Self { runtime, cache }
Shylock-Hg marked this conversation as resolved.
Show resolved Hide resolved
}
/// Reads a file from the filesystem or from an HTTP URL
pub async fn read_file<T: ToString>(&self, file: T) -> anyhow::Result<FileRead> {
// check cache
let file_path = file.to_string();
let content = self
.cache
.lock()
.await

Check warning on line 39 in src/resource_reader.rs

View check run for this annotation

Codecov / codecov/patch

src/resource_reader.rs#L39

Added line #L39 was not covered by tests
.get_mut()
.get(&file_path)
.map(|v| v.to_owned());
let content = if let Some(content) = content {
content
} else {
let content = self.do_read_file(file.to_string()).await?;
self.cache
.lock()
.await

Check warning on line 49 in src/resource_reader.rs

View check run for this annotation

Codecov / codecov/patch

src/resource_reader.rs#L49

Added line #L49 was not covered by tests
.get_mut()
.insert(file_path.to_owned(), content.clone());
content
};

Ok(FileRead { content, path: file_path })
}

async fn do_read_file<T: ToString>(&self, file: T) -> anyhow::Result<String> {
// Is an HTTP URL
let content = if let Ok(url) = Url::parse(&file.to_string()) {
if url.scheme().starts_with("http") {
Expand All @@ -41,8 +76,7 @@

self.runtime.file.read(&file.to_string()).await?
};

Ok(FileRead { content, path: file.to_string() })
Ok(content)
}

/// Reads all the files in parallel
Expand Down
Loading