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

Speeding up build/run requests with a pool of idle docker containers #463

Closed
wants to merge 2 commits into from
Closed
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 compiler/base/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ RUN cd / && \
WORKDIR /playground

ADD Cargo.toml /playground/Cargo.toml
ADD cargo.sh /playground/cargo.sh
ADD crate-information.json /playground/crate-information.json
RUN cargo fetch

Expand Down
28 changes: 28 additions & 0 deletions compiler/base/cargo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash

set -eu

POSITIONAL=()

while [[ $# -gt 0 ]]; do
case $1 in
--env)
export $2
shift 2
;;
*)
POSITIONAL+=("$1")
shift
;;
esac
done

eval set -- "${POSITIONAL[@]}"

timeout=${PLAYGROUND_TIMEOUT:-10}

modify-cargo-toml

timeout --signal=KILL ${timeout} cargo "$@" || pkill sleep

pkill sleep
7 changes: 1 addition & 6 deletions compiler/base/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
#!/bin/bash

set -eu

timeout=${PLAYGROUND_TIMEOUT:-10}

modify-cargo-toml
timeout --signal=KILL ${timeout} "$@"
sleep 365d
30 changes: 30 additions & 0 deletions ui/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 ui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ router = "0.6.0"
openssl-probe = "0.1.2"
dotenv = "0.13.0"
snafu = "0.2.0"
ctrlc = { version = "3.1.1", features = ["termination"] }

[dependencies.playground-middleware]
git = "https://github.com/integer32llc/playground-middleware"
44 changes: 32 additions & 12 deletions ui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@ use std::{
convert::{TryFrom, TryInto},
env,
path::PathBuf,
process,
sync::{Arc, Mutex},
time::{Duration, Instant},
};

use crate::sandbox::Sandbox;
use crate::sandbox::{Channel, DockerContainers, Sandbox};

const DEFAULT_ADDRESS: &str = "127.0.0.1";
const DEFAULT_PORT: u16 = 5000;
const DEFAULT_LOG_FILE: &str = "access-log.csv";
const DEFAULT_DOCKER_CONTAINER_POOL_SIZE: usize = 10;

mod asm_cleanup;
mod gist;
Expand All @@ -42,6 +44,13 @@ const ONE_YEAR_IN_SECONDS: u64 = 60 * 60 * 24 * 365;

const SANDBOX_CACHE_TIME_TO_LIVE_IN_SECONDS: u64 = ONE_HOUR_IN_SECONDS as u64;

fn set_graceful_shutdown_hook(containers: Arc<Mutex<DockerContainers>>) {
ctrlc::set_handler(move || {
containers.lock().unwrap().terminate();
process::exit(0);
}).expect("Error setting Ctrl-C handler");
}

fn main() {
// Dotenv may be unable to load environment variables, but that's ok in production
let _ = dotenv::dotenv();
Expand All @@ -55,6 +64,11 @@ fn main() {
let port = env::var("PLAYGROUND_UI_PORT").ok().and_then(|p| p.parse().ok()).unwrap_or(DEFAULT_PORT);
let logfile = env::var("PLAYGROUND_LOG_FILE").unwrap_or_else(|_| DEFAULT_LOG_FILE.to_string());
let cors_enabled = env::var_os("PLAYGROUND_CORS_ENABLED").is_some();
let docker_containers_pool_size = env::var("DOCKER_CONTAINER_POOL_SIZE").ok().and_then(|v| v.parse().ok()).unwrap_or(DEFAULT_DOCKER_CONTAINER_POOL_SIZE);

let containers = Arc::new(Mutex::new(DockerContainers::new(docker_containers_pool_size)));

set_graceful_shutdown_hook(containers.clone());

let files = Staticfile::new(&root).expect("Unable to open root directory");
let mut files = Chain::new(files);
Expand All @@ -71,8 +85,10 @@ fn main() {

let mut mount = Mount::new();
mount.mount("/", files);
mount.mount("/compile", compile);
mount.mount("/execute", execute);
let containers_clone = containers.clone();
mount.mount("/compile", move |req: &mut Request<'_, '_>| compile(req, &containers_clone));
let containers_clone = containers.clone();
mount.mount("/execute", move |req: &mut Request<'_, '_>| handle(req, &containers_clone));
mount.mount("/format", format);
mount.mount("/clippy", clippy);
mount.mount("/miri", miri);
Expand All @@ -84,7 +100,8 @@ fn main() {
mount.mount("/meta/version/clippy", meta_version_clippy);
mount.mount("/meta/version/miri", meta_version_miri);
mount.mount("/meta/gist", gist_router);
mount.mount("/evaluate.json", evaluate);
let containers_clone = containers.clone();
mount.mount("/evaluate.json", move |req: &mut Request<'_, '_>| evaluate(req, &containers_clone));

let mut chain = Chain::new(mount);
let file_logger = FileLogger::new(logfile).expect("Unable to create file logger");
Expand Down Expand Up @@ -135,21 +152,23 @@ impl iron::typemap::Key for GhToken {
type Value = Self;
}

fn compile(req: &mut Request<'_, '_>) -> IronResult<Response> {
fn compile(req: &mut Request<'_, '_>, containers: &Mutex<DockerContainers>) -> IronResult<Response> {
with_sandbox(req, |sandbox, req: CompileRequest| {
let req = req.try_into()?;
let req: sandbox::CompileRequest = req.try_into()?;
let container = containers.lock().unwrap().pop(req.channel).unwrap();
sandbox
.compile(&req)
.compile(&req, &container)
.map(CompileResponse::from)
.eager_context(Compilation)
})
}

fn execute(req: &mut Request<'_, '_>) -> IronResult<Response> {
fn handle(req: &mut Request<'_, '_>, containers: &Mutex<DockerContainers>) -> IronResult<Response> {
with_sandbox(req, |sandbox, req: ExecuteRequest| {
let req = req.try_into()?;
let req: sandbox::ExecuteRequest = req.try_into()?;
let container = containers.lock().unwrap().pop(req.channel).unwrap();
sandbox
.execute(&req)
.execute(&req, &container)
.map(ExecuteResponse::from)
.eager_context(Execution)
})
Expand Down Expand Up @@ -262,11 +281,12 @@ fn meta_gist_get(req: &mut Request<'_, '_>) -> IronResult<Response> {

// This is a backwards compatibilty shim. The Rust homepage and the
// documentation use this to run code in place.
fn evaluate(req: &mut Request<'_, '_>) -> IronResult<Response> {
fn evaluate(req: &mut Request<'_, '_>, containers: &Mutex<DockerContainers>) -> IronResult<Response> {
with_sandbox(req, |sandbox, req: EvaluateRequest| {
let req = req.try_into()?;
let container = containers.lock().unwrap().pop(Channel::Stable).unwrap();
sandbox
.execute(&req)
.execute(&req, &container)
.map(EvaluateResponse::from)
.eager_context(Evaluation)
})
Expand Down
Loading