Skip to content

Commit

Permalink
perf(serverless): do not spawn a thread on each request (#335)
Browse files Browse the repository at this point in the history
  • Loading branch information
QuiiBz authored Dec 2, 2022
1 parent b38ec59 commit e10aabe
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/funny-peaches-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lagon/serverless': patch
---

Improve performances by not spawning a thread on each request
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.

2 changes: 2 additions & 0 deletions packages/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
[dependencies]
v8 = "0.58.0"
tokio = { version = "1", features = ["full"] }
tokio-util = { version = "0.7.4", features = ["rt"] }
futures = "0.3.25"
hyper = { version = "0.14", features = ["client", "http1", "http2", "tcp"] }
hyper-tls = { version = "0.5.0", features = ["vendored"] }
Expand All @@ -14,6 +15,7 @@ anyhow = "1.0.66"
uuid = { version = "1.2.2", features = ["v4", "fast-rng"] }
rand = "0.8.5"
log = { version = "0.4.17", features = ["std", "kv_unstable"] }
lazy_static = "1.4.0"
# Cryptography
hmac = "0.12.1"
sha2 = "0.10.6"
Expand Down
8 changes: 3 additions & 5 deletions packages/runtime/src/isolate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@ use std::{
Arc,
},
task::{Context, Poll},
thread::sleep,
time::{Duration, Instant},
};

use futures::{future::poll_fn, stream::FuturesUnordered, Future, StreamExt};
use tokio::task::spawn_blocking;
use v8::PromiseState;

use crate::{
http::{FromV8, IntoV8, Request, Response, RunResult, StreamResult},
runtime::get_runtime_code,
runtime::{get_runtime_code, POOL},
utils::{v8_boolean, v8_string, v8_uint8array},
};

Expand Down Expand Up @@ -556,7 +554,7 @@ impl Isolate {
let request_ended_handle = request_ended.clone();
let running_promises_handle = self.running_promises.clone();

spawn_blocking(move || {
POOL.spawn_pinned(move || async move {
let mut paused_timer = None;

loop {
Expand All @@ -573,7 +571,7 @@ impl Isolate {
paused_timer = Some(Instant::now());
}

sleep(TIMEOUT_LOOP_DELAY);
tokio::time::sleep(TIMEOUT_LOOP_DELAY).await;

continue;
} else if let Some(timer) = paused_timer {
Expand Down
6 changes: 6 additions & 0 deletions packages/runtime/src/runtime/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use lazy_static::lazy_static;
use tokio_util::task::LocalPoolHandle;
use v8::V8;

use crate::{isolate::IsolateOptions, utils::v8_string};
Expand All @@ -8,6 +10,10 @@ struct IcuData([u8; 10454784]);
static JS_RUNTIME: &str = include_str!("../../runtime.js");
static ICU_DATA: IcuData = IcuData(*include_bytes!("../../icudtl.dat"));

lazy_static! {
pub static ref POOL: LocalPoolHandle = LocalPoolHandle::new(1);
}

#[derive(Default)]
pub struct RuntimeOptions {
allow_eval: bool,
Expand Down

0 comments on commit e10aabe

Please sign in to comment.