Skip to content

Commit

Permalink
Exposed expensive queries file as an option
Browse files Browse the repository at this point in the history
  • Loading branch information
Kai Wetlesen authored and lutter committed Jul 28, 2022
1 parent b75642c commit 81f9bcd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
23 changes: 18 additions & 5 deletions node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,20 @@ use tokio::sync::mpsc;

git_testament!(TESTAMENT);

fn read_expensive_queries() -> Result<Vec<Arc<q::Document>>, std::io::Error> {
fn read_expensive_queries(
logger: &Logger,
expensive_queries_filename: String,
) -> Result<Vec<Arc<q::Document>>, std::io::Error> {
// A file with a list of expensive queries, one query per line
// Attempts to run these queries will return a
// QueryExecutionError::TooExpensive to clients
const EXPENSIVE_QUERIES: &str = "/etc/graph-node/expensive-queries.txt";
let path = Path::new(EXPENSIVE_QUERIES);
let path = Path::new(&expensive_queries_filename);
let mut queries = Vec::new();
if path.exists() {
info!(
logger,
"Reading expensive queries file: {}", expensive_queries_filename
);
let file = std::fs::File::open(path)?;
let reader = BufReader::new(file);
for line in reader.lines() {
Expand All @@ -63,7 +69,7 @@ fn read_expensive_queries() -> Result<Vec<Arc<q::Document>>, std::io::Error> {
.map_err(|e| {
let msg = format!(
"invalid GraphQL query in {}: {}\n{}",
EXPENSIVE_QUERIES,
expensive_queries_filename,
e.to_string(),
line
);
Expand All @@ -72,6 +78,11 @@ fn read_expensive_queries() -> Result<Vec<Arc<q::Document>>, std::io::Error> {
.into_static();
queries.push(Arc::new(query));
}
} else {
warn!(
logger,
"Expensive queries file not set to a valid file: {}", expensive_queries_filename
);
}
Ok(queries)
}
Expand Down Expand Up @@ -220,7 +231,9 @@ async fn main() {

let contention_logger = logger.clone();

let expensive_queries = read_expensive_queries().unwrap();
// TODO: make option loadable from configuration TOML and environment:
let expensive_queries =
read_expensive_queries(&logger, opt.expensive_queries_filename).unwrap();

let store_builder = StoreBuilder::new(
&logger,
Expand Down
8 changes: 8 additions & 0 deletions node/src/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,14 @@ pub struct Opt {
help = "a unique identifier for this node. Should have the same value between consecutive node restarts"
)]
pub node_id: String,
#[structopt(
long,
value_name = "FILE",
env = "GRAPH_NODE_EXPENSIVE_QUERIES_FILE",
default_value = "/etc/graph-node/expensive-queries.txt",
help = "a file with a list of expensive queries, one query per line. Attempts to run these queries will return a QueryExecutionError::TooExpensive to clients"
)]
pub expensive_queries_filename: String,
#[structopt(long, help = "Enable debug logging")]
pub debug: bool,

Expand Down

0 comments on commit 81f9bcd

Please # to comment.