Skip to content
Open
Show file tree
Hide file tree
Changes from all 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 bindings/nodejs/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

export interface ZenConfig {
nodesInContext?: boolean
functionTimeout?: number
}
export function overrideConfig(config: ZenConfig): void
export interface ZenEvaluateOptions {
Expand Down
9 changes: 8 additions & 1 deletion bindings/nodejs/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@ use zen_engine::ZEN_CONFIG;
#[napi(object)]
pub struct ZenConfig {
pub nodes_in_context: Option<bool>,
pub function_timeout: Option<u32>,
}

#[allow(dead_code)]
#[napi]
pub fn override_config(config: ZenConfig) {
if let Some(val) = config.nodes_in_context {
ZEN_CONFIG.nodes_in_context.store(val, Ordering::Relaxed)
ZEN_CONFIG.nodes_in_context.store(val, Ordering::Relaxed);
}

if let Some(val) = config.function_timeout {
ZEN_CONFIG
.function_timeout
.store(val as u64, Ordering::Relaxed);
}
}
6 changes: 5 additions & 1 deletion core/engine/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
use std::sync::atomic::{AtomicBool, AtomicU64};

use once_cell::sync::Lazy;
use std::sync::atomic::AtomicBool;

#[derive(Debug)]
pub struct ZenConfig {
pub nodes_in_context: AtomicBool,
/// Function timeout presented in millis
pub function_timeout: AtomicU64,
}

impl Default for ZenConfig {
fn default() -> Self {
Self {
nodes_in_context: AtomicBool::new(true),
function_timeout: AtomicU64::new(5_000),
}
}
}
Expand Down
12 changes: 9 additions & 3 deletions core/engine/src/handler/function/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::rc::Rc;
use std::sync::atomic::Ordering;
use std::time::Duration;

use ::serde::{Deserialize, Serialize};
Expand All @@ -11,6 +12,7 @@ use crate::handler::function::function::{Function, HandlerResponse};
use crate::handler::function::serde::JsValue;
use crate::handler::node::{NodeRequest, NodeResponse, NodeResult};
use crate::model::{DecisionNodeKind, FunctionNodeContent};
use crate::ZEN_CONFIG;

pub(crate) mod error;
pub(crate) mod function;
Expand All @@ -29,17 +31,19 @@ pub struct FunctionHandler {
trace: bool,
iteration: u8,
max_depth: u8,
max_duration: Duration,
}

static MAX_DURATION: Duration = Duration::from_millis(5_000);

impl FunctionHandler {
pub fn new(function: Rc<Function>, trace: bool, iteration: u8, max_depth: u8) -> Self {
let max_duration_millis = ZEN_CONFIG.function_timeout.load(Ordering::Relaxed);

Self {
function,
trace,
iteration,
max_depth,
max_duration: Duration::from_millis(max_duration_millis),
}
}

Expand All @@ -56,7 +60,9 @@ impl FunctionHandler {
let module_name = self
.function
.suggest_module_name(request.node.id.as_str(), request.node.name.as_str());
let interrupt_handler = Box::new(move || start.elapsed() > MAX_DURATION);

let max_duration = self.max_duration.clone();
let interrupt_handler = Box::new(move || start.elapsed() > max_duration);
self.function
.runtime()
.set_interrupt_handler(Some(interrupt_handler))
Expand Down
16 changes: 12 additions & 4 deletions core/engine/src/handler/function_v1/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::sync::atomic::Ordering;
use std::time::{Duration, Instant};

use crate::handler::function_v1::script::Script;
use crate::handler::node::{NodeRequest, NodeResponse, NodeResult};
use crate::model::{DecisionNodeKind, FunctionNodeContent};
use crate::ZEN_CONFIG;
use anyhow::anyhow;
use rquickjs::Runtime;
use serde_json::json;
Expand All @@ -13,13 +15,18 @@ mod script;
pub struct FunctionHandler {
trace: bool,
runtime: Runtime,
max_duration: Duration,
}

static MAX_DURATION: Duration = Duration::from_millis(500);

impl FunctionHandler {
pub fn new(trace: bool, runtime: Runtime) -> Self {
Self { trace, runtime }
let max_duration_millis = ZEN_CONFIG.function_timeout.load(Ordering::Relaxed);

Self {
trace,
runtime,
max_duration: Duration::from_millis(max_duration_millis),
}
}

pub async fn handle(&self, request: NodeRequest) -> NodeResult {
Expand All @@ -32,7 +39,8 @@ impl FunctionHandler {
}?;

let start = Instant::now();
let interrupt_handler = Box::new(move || start.elapsed() > MAX_DURATION);
let max_duration = self.max_duration.clone();
let interrupt_handler = Box::new(move || start.elapsed() > max_duration);
self.runtime.set_interrupt_handler(Some(interrupt_handler));

let mut script = Script::new(self.runtime.clone());
Expand Down