Skip to content

Commit

Permalink
Avoid race condition in /query
Browse files Browse the repository at this point in the history
  • Loading branch information
ushitora-anqou committed Aug 29, 2023
1 parent d7046a9 commit 9cfa1d3
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions lib/web_server.ml
Original file line number Diff line number Diff line change
Expand Up @@ -451,19 +451,27 @@ let get_models ~year ~depth con =
("cashflow100", model_cashflow100);
]

let generate in_filename thn err =
try
let m, notes = Loader.load_file in_filename in
let con = Sql_writer.dump_on_memory m in
match Verifier.verify con notes with
| Error s -> failwithf "Verification error: %s" s
| Ok () ->
Fun.protect
(fun () -> thn con)
~finally:(fun () -> Datastore.disconnect con)
with e ->
let message = match e with Failure s -> s | _ -> Printexc.to_string e in
err message
let generate =
let mtx = Lwt_mutex.create () in
fun in_filename thn err ->
try%lwt
let%lwt m, notes =
Lwt_mutex.with_lock mtx (fun () ->
Loader.load_file in_filename |> Lwt.return)
in
Lwt_preemptive.detach
(fun () ->
let con = Sql_writer.dump_on_memory m in
match Verifier.verify con notes with
| Error s -> failwithf "Verification error: %s" s
| Ok () ->
Fun.protect
(fun () -> thn con)
~finally:(fun () -> Datastore.disconnect con))
()
with e ->
let message = match e with Failure s -> s | _ -> Printexc.to_string e in
err message |> Lwt.return

let generate_json in_filename =
let aux_ok con = get_models ~year:2023 ~depth:1 con |> fun xs -> `Assoc xs in
Expand Down Expand Up @@ -531,15 +539,11 @@ let serve ?(interface = "127.0.0.1") ?(port = 8080) in_filename =
Lwt.async (finalize_websocket_stream ws);
Lwt.return_unit) );
( Dream.get "/data.json" @@ fun _ ->
Lwt_preemptive.detach (fun () -> generate_json in_filename) ()
>|= Yojson.Safe.to_string
generate_json in_filename >|= Yojson.Safe.to_string
>>= Dream.json ~headers:[ ("Access-Control-Allow-Origin", "*") ] );
( Dream.post "/query" @@ fun req ->
let%lwt body = Dream.body req in
Lwt_preemptive.detach
(fun () ->
handle_query ~in_filename ~query:(Yojson.Safe.from_string body))
()
handle_query ~in_filename ~query:(Yojson.Safe.from_string body)
>|= Yojson.Safe.to_string
>>= Dream.json ~headers:[ ("Access-Control-Allow-Origin", "*") ] );
]
Expand Down

0 comments on commit 9cfa1d3

Please sign in to comment.