Skip to content

Commit 8e27c47

Browse files
committed
Limit the number of open files at a given time
1 parent 35beab0 commit 8e27c47

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/Cosmetrics.ml

+14-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ open Cosmetrics_utils
66
module Store = Git_unix.FS
77
module G = Git_unix.Sync.Make(Store)
88

9+
let openfile_pool = Lwt_pool.create 100 (fun () -> return_unit)
10+
911
let read_commit_exn t sha =
1012
Store.read_exn t (Git.SHA.of_commit sha) >>= fun v ->
1113
match v with
@@ -70,20 +72,23 @@ module Cache = struct
7072
if version <> t.version || newer_dep then (
7173
Lwt_io.close fh >>= fun () ->
7274
log t "Update cache %s\n" t.fname >>= fun () ->
73-
update t
75+
update t (* previous handle closed, thus only 1 in use *)
7476
)
7577
else (
7678
Lwt_io.read_value fh >>= fun v ->
7779
Lwt_io.close fh >|= fun () ->
7880
v
7981
)
8082

83+
let read_exn_pool t () =
84+
Lwt_pool.use openfile_pool (read_exn t)
85+
8186
let read t =
82-
catch (read_exn t)
87+
catch (read_exn_pool t)
8388
(function Unix.Unix_error(Unix.ENOENT, _, _) ->
8489
(* Cache does not exist, create one *)
8590
log t "Create cache %s\n" t.fname >>= fun () ->
86-
update t
91+
Lwt_pool.use openfile_pool (fun () -> update t)
8792
| e -> fail e)
8893

8994
let default_log s =
@@ -344,7 +349,7 @@ module Tag = struct
344349
name = String.sub s 10 (String.length s - 10);
345350
date = Calendar.from_unixfloat (Int64.to_float t) }
346351

347-
let get_ref t r =
352+
let get_ref t r () =
348353
if String.starting ~w:"refs/tags/" (Git.Reference.to_raw r) then
349354
Store.read_reference_exn t r >>= fun sha ->
350355
Store.read_exn t (Git.SHA.of_commit sha) >|= fun v ->
@@ -358,13 +363,17 @@ module Tag = struct
358363
| Git.Value.Blob _ | Git.Value.Tree _ -> None
359364
else return_none
360365

366+
(* Like [get_ref] but wait if too many files are already opened. *)
367+
let get_ref_pool t r =
368+
Lwt_pool.use openfile_pool (get_ref t r)
369+
361370
let get t =
362371
Store.references t >>= fun r ->
363372
(* Because of the bug https://github.com/mirage/ocaml-git/issues/124
364373
remove possible duplicates in the list. *)
365374
let r = List.sort Git.Reference.compare r in
366375
let r = List.remove_consecutive_duplicates Git.Reference.equal r in
367-
Lwt_list.filter_map_p (get_ref t) r
376+
Lwt_list.filter_map_p (get_ref_pool t) r
368377
end
369378

370379

src/Cosmetrics_html.ml

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ open Lwt
55
open CalendarLib
66
module T = Cosmetrics.Timeseries
77

8+
let openfile_pool = Lwt_pool.create 100 (fun () -> return_unit)
9+
810
let single_quote s =
911
let b = Buffer.create (2 + String.length s) in
1012
Buffer.add_char b '\'';
@@ -356,7 +358,7 @@ let chord html ?(padding=0.05) ?(width=600) ?(height=600)
356358
print html "</script>\n"
357359

358360

359-
let write html fname =
361+
let write_no_pool html fname () =
360362
let open Lwt_io in
361363
open_file fname ~mode:output >>= fun fh ->
362364
let common_head =
@@ -379,3 +381,6 @@ let write html fname =
379381
write fh "</style>\n</head>\n<body>\n" >>= fun () ->
380382
write fh (Buffer.contents html.body) >>= fun () ->
381383
write fh "</body>\n</html>"
384+
385+
let write html fname =
386+
Lwt_pool.use openfile_pool (write_no_pool html fname)

0 commit comments

Comments
 (0)