diff --git a/robot-docs/runtime.md b/robot-docs/runtime.md index 24d114a5..c6f29af6 100644 --- a/robot-docs/runtime.md +++ b/robot-docs/runtime.md @@ -69,7 +69,9 @@ first start within a wait. A start that outlasts the wait returns `TimedOut` after the keeper is killed, so it never comes up behind the caller. Later exits restart it after a doubling pause: `Always` for the daemon's own port, `Bounded(attempts, stopped)` for the webhook intake, which stops and runs -`stopped` after that many failed starts in a row. +`stopped` after that many failed starts in a row. The daemon loads mist, glisten, +gleam_http and gleam_otp before its first listener starts, so the wait times the +start rather than cold code loading, which under load can take hundreds of ms. ## boot timing diff --git a/src/albedo/daemon/albedo_code.erl b/src/albedo/daemon/albedo_code.erl index f420da6c..1024d929 100644 --- a/src/albedo/daemon/albedo_code.erl +++ b/src/albedo/daemon/albedo_code.erl @@ -1,7 +1,7 @@ %% Preserve boot-time bytecode without eagerly loading the whole code path. %% OTP installs can disappear during upgrades; checkout beams can be rebuilt. -module(albedo_code). --export([prepare/1, cleanup/0, start/0, preload/0, runtime_removed/0, priv_dir/0]). +-export([prepare/1, cleanup/0, start/0, preload/0, load_applications/1, runtime_removed/0, priv_dir/0]). prepare(Home) -> pin_priv(), @@ -240,6 +240,12 @@ preload() -> {error, iolist_to_binary([integer_to_binary(length(Failed)), <<" modules did not preload: ">>, Names])} end. +%% Loads every module of these applications now. A module that will not load +%% still fails only when it is called, as it would lazily. +load_applications(Names) -> + _ = code:ensure_modules_loaded(lists:append([app_modules(binary_to_atom(Name)) || Name <- Names])), + nil. + %% True once the Erlang install this node was started from is gone. runtime_removed() -> not filelib:is_dir(filename:join(code:root_dir(), "lib")). diff --git a/src/albedo/daemon/server.gleam b/src/albedo/daemon/server.gleam index e9c5dd44..9dee3b3f 100644 --- a/src/albedo/daemon/server.gleam +++ b/src/albedo/daemon/server.gleam @@ -106,6 +106,9 @@ pub fn start( let instance_id = http_api.instance_id() let boundary = HttpBoundary(instance_id, configured_list("ALBEDO_HTTP_ORIGINS")) + // Load the listener's code before its start is timed: under load, finding + // and committing each cold module can take hundreds of ms. + load_applications(["mist", "glisten", "gleam_http", "gleam_otp"]) // A restarted listener comes back on the port it was built with, so port 0 // would bring it back somewhere daemon.json does not say: pick a concrete port. use port <- result.try( @@ -852,6 +855,9 @@ fn preserve_code(home: String) -> Nil @external(erlang, "albedo_code", "start") fn preload_code() -> Nil +@external(erlang, "albedo_code", "load_applications") +fn load_applications(names: List(String)) -> Nil + @external(erlang, "albedo_code", "runtime_removed") fn runtime_removed() -> Bool