Skip to content

Commit 310a486

Browse files
committed
feat: allow configurign top-level supervisor restart limits
This lets you start a runtime that will terminate if the top-level application terminates, which is useful for CLIs that may crash and need to stay crashed. For long-running applications this helps configure how many times you want the application to retry before fully shutting down. Also add a small pretty printer to print out the config as debug information when the runtime starts.
1 parent 58458b6 commit 310a486

File tree

3 files changed

+48
-10
lines changed

3 files changed

+48
-10
lines changed

packages/riot-runtime/Config.ml

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,33 @@
1-
type t = { rnd : Random.State.t; max_workers : int; workers : int }
1+
type t = {
2+
rnd : Random.State.t;
3+
max_workers : int;
4+
workers : int;
5+
supervisor_restart_limit : int;
6+
supervisor_restart_period : int;
7+
}
28

3-
let make ?workers () =
9+
let pp fmt t =
10+
Format.fprintf fmt "== RIOT CONFIG ==\n";
11+
Format.fprintf fmt "* max_wokers=%d\n" t.max_workers;
12+
Format.fprintf fmt "* workers=%d\n" t.workers;
13+
Format.fprintf fmt "* supervisor_restart_limit=%d\n" t.supervisor_restart_limit;
14+
Format.fprintf fmt "* supervisor_restart_period=%d\n" t.supervisor_restart_period;
15+
Format.fprintf fmt "\n%!"
16+
;;
17+
18+
let make ?(supervisor_restart_limit = 1) ?(supervisor_restart_period = 0)
19+
?workers () =
420
let max_workers = Int.max 0 (Stdlib.Domain.recommended_domain_count () - 2) in
521
let workers =
622
match workers with Some w -> Int.min w max_workers | None -> max_workers
723
in
824
let rnd = Random.State.make_self_init () in
9-
{ rnd; max_workers; workers }
25+
{
26+
rnd;
27+
max_workers;
28+
workers;
29+
supervisor_restart_limit;
30+
supervisor_restart_period;
31+
}
1032

1133
let default () = make ()

packages/riot/riot.ml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ let run ?(config = Config.default ()) main =
3131

3232
let Config.{ workers; rnd; _ } = config in
3333

34-
Log.debug (fun f -> f "Initializing Riot runtime...");
34+
Log.debug (fun f -> f "Initializing Riot runtime...\n%a" Config.pp config);
35+
3536
Printexc.record_backtrace true;
3637
Core.Pid.reset ();
3738
Scheduler.Uid.reset ();
@@ -63,20 +64,22 @@ let run_with_status ?config ~on_error main =
6364
in
6465
shutdown ~status ()
6566

66-
let start ?config ~apps () =
67-
run ?config @@ fun () ->
67+
let start ?(config = Config.default ()) ~apps () =
68+
run ~config @@ fun () ->
6869
let child_specs =
6970
List.map
7071
(fun (module App : Application.Intf) ->
7172
Supervisor.child_spec App.start ())
7273
apps
7374
in
75+
let restart_limit = config.supervisor_restart_limit in
76+
let restart_period = config.supervisor_restart_period in
7477
Supervisor.(
7578
start_supervisor
7679
{
7780
strategy = One_for_one;
78-
restart_limit = 1;
79-
restart_period = 0;
81+
restart_limit;
82+
restart_period;
8083
child_specs;
8184
children = [];
8285
restarts = [];

packages/riot/riot.mli

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,10 +305,23 @@ val shutdown : ?status:int -> unit -> unit
305305
(** Gracefully shuts down the runtime. Any non-yielding process will block this. *)
306306

307307
module Config : sig
308-
type t = { rnd : Random.State.t; max_workers : int; workers : int }
308+
type t = {
309+
rnd : Random.State.t;
310+
max_workers : int;
311+
workers : int;
312+
supervisor_restart_limit : int;
313+
supervisor_restart_period : int;
314+
}
309315

316+
val pp : Format.formatter -> t -> unit
310317
val default : unit -> t
311-
val make : ?workers:int -> unit -> t
318+
319+
val make :
320+
?supervisor_restart_limit:int ->
321+
?supervisor_restart_period:int ->
322+
?workers:int ->
323+
unit ->
324+
t
312325
end
313326

314327
val run : ?config:Config.t -> (unit -> unit) -> unit

0 commit comments

Comments
 (0)