Anonymous avatar Anonymous committed 9a31cdf

Parvel.server_respawn

Comments (0)

Files changed (2)

     value send_ctx_cmd ctx cmd = ctx.c_send_cmd cmd
     ;
 
-    value send proc msg = proc.p_send_msg msg;
+    value send
+     : process 'i -> process_message_req 'i -> IO.m unit
+     = fun proc msg ->
+         proc.p_send_msg msg
+    ;
 
     value addrt_of_proc proc =
       { asend = proc.p_send_msg
     ;
 
 
+(*
+    value server_retry
+     : ?retries:int -> ?init_timeout:float -> server_factory 'i 'o -> sf
+     = fun ?(retries=3) ?(init_timeout=0.2) serv_fact ->
+         .
+    ;
+*)
+
+
+    (* respawns server on error, up to [max_count] times, then
+       returns last error.
+     *)
+
+    value server_respawn
+     : ! 'i 'o . ?max_count:int -> server_factory 'i 'o -> server_factory 'i 'o
+     = fun ?(max_count=10) in_fact ->
+         let () = assert (max_count > 0) in
+         fun _ctx ->
+
+           let opt_serv = ref None in
+
+           let out_disp i =
+             loop None max_count
+             where rec loop last_error left =
+               match (left, opt_serv.val, last_error) with
+               [ (0, _, None) -> assert False
+               | (0, None, Some e) -> server_error e
+               | (left, None, last_error) ->
+                   create_server in_fact >>= fun serv ->
+                   ( opt_serv.val := Some serv
+                   ; loop last_error left
+                   )
+               | (left, Some serv, _l) ->
+                   call serv i >>= fun
+                   [ CR_Ok r -> server_return r
+                   | CR_Error e ->
+                       send serv (Cmd `Shutdown) >>= fun () ->
+                       ( opt_serv.val := None
+                       ; loop (Some e) (left - 1)
+                       )
+                   ]
+               ]
+           in
+             IO.return out_disp
+    ;
+
+
     (*************************************************************)
 
     module type IT_TYPE
     ;
   end
 ;
+
+
+value server_respawn
+ : ?max_count:int -> server_factory 'i 'o -> server_factory 'i 'o
+;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.