Skip to content

Commit

Permalink
perf(runtime): store timeouts as Duration (#561)
Browse files Browse the repository at this point in the history
* perf(runtime): store timeouts as Duration

* feat(cli): increase isolate timeout
  • Loading branch information
QuiiBz authored Feb 4, 2023
1 parent 8e2eaa0 commit 8e67150
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/clever-pigs-sit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lagon/cli': patch
---

Increase timeout of isolate for `dev` command
2 changes: 2 additions & 0 deletions crates/cli/src/commands/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ async fn handle_request(
IsolateOptions::new(
String::from_utf8(index).expect("Code is not UTF-8"),
)
.timeout(Duration::from_secs(1))
.startup_timeout(Duration::from_secs(2))
.metadata(Some((String::from(""), String::from(""))))
.environment_variables(environment_variables),
);
Expand Down
4 changes: 2 additions & 2 deletions crates/runtime/tests/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use httptest::bytes::Bytes;
use lagon_runtime::{options::RuntimeOptions, Runtime};
use lagon_runtime_http::{Method, Request, RunResult};
use lagon_runtime_isolate::{options::IsolateOptions, Isolate};
use std::sync::Once;
use std::{sync::Once, time::Duration};

fn setup() {
static START: Once = Once::new();
Expand Down Expand Up @@ -153,7 +153,7 @@ async fn memory_reached() {
.into(),
)
// Increase timeout for CI
.startup_timeout(10000)
.startup_timeout(Duration::from_millis(10000))
.memory(1),
);
let (tx, rx) = flume::unbounded();
Expand Down
4 changes: 2 additions & 2 deletions crates/runtime_isolate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,8 @@ impl Isolate {
// Script parsing may take a long time, so we use the startup_timeout
// when the isolate has not been used yet.
let timeout = match self.handler.is_none() && self.compilation_error.is_none() {
true => Duration::from_millis(self.options.startup_timeout as u64),
false => Duration::from_millis(self.options.timeout as u64),
true => self.options.startup_timeout,
false => self.options.timeout,
};
let (termination_tx, termination_rx) = flume::bounded(1);

Expand Down
16 changes: 8 additions & 8 deletions crates/runtime_isolate/src/options.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use lagon_runtime_v8_utils::v8_string;
use std::{collections::HashMap, rc::Rc};
use std::{collections::HashMap, rc::Rc, time::Duration};

use super::IsolateStatistics;

Expand All @@ -12,9 +12,9 @@ type OnIsolateStatisticsCallback = Box<dyn Fn(Rc<Metadata>, IsolateStatistics)>;
pub struct IsolateOptions {
pub code: String,
pub environment_variables: Option<HashMap<String, String>>,
pub memory: usize, // in MB (MegaBytes)
pub timeout: usize, // in ms (MilliSeconds)
pub startup_timeout: usize, // is ms (MilliSeconds)
pub memory: usize, // in MB (MegaBytes)
pub timeout: Duration,
pub startup_timeout: Duration,
pub metadata: Rc<Metadata>,
pub on_drop: Option<OnIsolateDropCallback>,
pub on_statistics: Option<OnIsolateStatisticsCallback>,
Expand All @@ -28,8 +28,8 @@ impl IsolateOptions {
Self {
code,
environment_variables: None,
timeout: 50,
startup_timeout: 200,
timeout: Duration::from_millis(50),
startup_timeout: Duration::from_millis(200),
memory: 128,
metadata: Rc::new(None),
on_drop: None,
Expand All @@ -43,12 +43,12 @@ impl IsolateOptions {
self
}

pub fn timeout(mut self, timeout: usize) -> Self {
pub fn timeout(mut self, timeout: Duration) -> Self {
self.timeout = timeout;
self
}

pub fn startup_timeout(mut self, startup_timeout: usize) -> Self {
pub fn startup_timeout(mut self, startup_timeout: Duration) -> Self {
self.startup_timeout = startup_timeout;
self
}
Expand Down
6 changes: 3 additions & 3 deletions crates/serverless/src/cronjob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use lagon_runtime_isolate::{options::IsolateOptions, Isolate, CONSOLE_SOURCE};
use lagon_runtime_utils::Deployment;
use log::{error, info, warn};
use metrics::{decrement_gauge, histogram, increment_gauge};
use std::{collections::HashMap, sync::Arc};
use std::{collections::HashMap, sync::Arc, time::Duration};
use tokio_cron_scheduler::{Job, JobScheduler};
use uuid::Uuid;

Expand Down Expand Up @@ -58,8 +58,8 @@ impl Cronjob {
let options = IsolateOptions::new(code)
.environment_variables(deployment.environment_variables.clone())
.memory(deployment.memory)
.timeout(deployment.timeout)
.startup_timeout(deployment.startup_timeout)
.timeout(Duration::from_millis(deployment.timeout as u64))
.startup_timeout(Duration::from_millis(deployment.startup_timeout as u64))
.metadata(Some((deployment.id.clone(), deployment.function_id.clone())))
.on_drop_callback(Box::new(|metadata| {
if let Some(metadata) = metadata.as_ref().as_ref() {
Expand Down
8 changes: 3 additions & 5 deletions crates/serverless/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ use std::convert::Infallible;
use std::env;
use std::net::SocketAddr;
use std::path::Path;
#[cfg(not(debug_assertions))]
use std::path::Path;
use std::sync::Arc;
use std::time::Instant;
use std::time::{Duration, Instant};
use tokio::sync::{Mutex, RwLock};
use tokio_util::task::LocalPoolHandle;

Expand Down Expand Up @@ -220,8 +218,8 @@ async fn handle_request(
deployment.environment_variables.clone(),
)
.memory(deployment.memory)
.timeout(deployment.timeout)
.startup_timeout(deployment.startup_timeout)
.timeout(Duration::from_millis(deployment.timeout as u64))
.startup_timeout(Duration::from_millis(deployment.startup_timeout as u64))
.metadata(Some((deployment.id.clone(), deployment.function_id.clone())))
.on_drop_callback(Box::new(|metadata| {
if let Some(metadata) = metadata.as_ref().as_ref() {
Expand Down

0 comments on commit 8e67150

Please sign in to comment.