art-w / sherlodoc

Fuzzy type search for OCaml documentation
MIT License
70 stars 6 forks source link

Make requests cancelable #36

Closed EmileTrotignon closed 6 months ago

EmileTrotignon commented 7 months ago

When you use sherlodoc as a website, requests are made for each letter you type. But if a request is slow enough that it is not answered by the time another request arrives, it would be ideal to cancel it.

Lwt is not suitable for this kind of thing, because we do not want to link the performance critical code with Lwt, because that would make the js file size too big.

This means that we need to do that with system threads. System thread have interruption points injected by the OCaml runtime, so this should work nicely.

Fabrice kindly provided me with a short demo of interruption with system threads :

exception Interrupt_Me

let work () =
  let i = ref 0 in
  while true; do
    Printf.printf "I'm still here (%d)...\n%!" !i;
    Unix.sleep 1;
    i := !i + 1;
  done

let install_sighandler () =
  let signum = Sys.sigint in
  Printf.printf "Installing signal handler for %d\n%!" signum;
  let _ = Sys.set_signal signum (Sys.Signal_handle (fun _ ->
    Printf.printf "Signal received! Raising excpetion...\n%!";
    raise Interrupt_Me))
  in
  ()

let interruptable f =
  install_sighandler ();
  Printf.printf "Hello, my pid is %d, interrupt me!\n%!" (Unix.getpid ());
  try f () with
  | Interrupt_Me -> Printf.printf "Exception caught... aborting life\n%!"
  | _ -> assert false

let () =
  Printf.printf "Hello world\n%!";
  interruptable work;
  Printf.printf "Goodbye world\n%!"
art-w commented 6 months ago

As discussed with Emile, I'm not sure about this one:

I kinda like this caching that we get for free right now, so I'm not sure this issue is worth the effort (but a timeout on query execution #37 would be nice).