Commits

Yaron Minsky committed 2d5e673

removed robust connection in favor of retry

Comments (0)

Files changed (3)

   Command.async_basic
     ~summary:"Monitor your time spent"
     (shared_flags ())
-    (fun user () -> setup_robust_conn ~reconnect_after:(sec 10.) user (fun user rconn ->
+    (fun user () -> setup_conn user (fun user conn ->
+       let stop = force on_term_signal in
        force clear_string
        >>= fun cstring ->
-       let stop = force on_term_signal in
-       Clock.every' (sec 1.) ~stop (fun () ->
-         let spinner = force spinner in
-         match Robust_connection.get rconn with
-         | None -> printf "%sWaiting for reconnection\n" cstring; return ()
-         | Some conn ->
-           Rpc.Rpc.dispatch Protocol.status conn user
-           >>| function
-           | Error _ ->
-             printf "%s%s Unable to reach server\n" cstring (spinner ())
-           | Ok { Protocol.Status. state; elapsed } ->
-             let total_elapsed =
-               Map.data elapsed
-               |> List.fold ~init:Time.Span.zero ~f:Time.Span.(+)
-             in
-             printf "%s%s %s %s\n"
-               cstring
-               (spinner ())
-               (span_string total_elapsed)
-               (match state with
-                | `Active cat -> sprintf "{%s}" (Category.to_string cat)
-                | `Not_running -> "(paused)"
-               )
-       );
-       stop
+       retry
+         ~on_retry:(fun () -> printf "%sRetrying...\n" cstring)
+         ~on_error:(fun err ->
+           printf "%sFailed.  Will retry\n\n%s\n"
+             cstring (Exn.sexp_of_t err |> Sexp.to_string_hum))
+         (fun () ->
+            Clock.every' (sec 1.) ~stop (fun () ->
+              let spinner = force spinner in
+              Rpc.Rpc.dispatch Protocol.status conn user
+              >>| function
+              | Error _ ->
+                printf "%s%s Unable to reach server\n" cstring (spinner ())
+              | Ok { Protocol.Status. state; elapsed } ->
+                let total_elapsed =
+                  Map.data elapsed
+                  |> List.fold ~init:Time.Span.zero ~f:Time.Span.(+)
+                in
+                printf "%s%s %s %s\n"
+                  cstring
+                  (spinner ())
+                  (span_string total_elapsed)
+                  (match state with
+                   | `Active cat -> sprintf "{%s}" (Category.to_string cat)
+                   | `Not_running -> "(paused)"
+                  ));
+            (stop >>| fun x -> Ok x)
+         )
+       |> Deferred.ignore
      ))
 
 module Ascii_table = Textutils.Ascii_table
     ~port:Common.port
     (fun conn -> k username conn)
 
-module Robust_connection : sig
-  type t
-  val get : t -> Rpc.Connection.t option
-
-  val with_connect
-    :  reconnect_after:Time.Span.t
-    -> 'addr Tcp.where_to_connect
-    -> (t -> 'a Deferred.t)
-    -> 'a Deferred.t
-
-end = struct
-  type t = Rpc.Connection.t option ref
-
-  let get t = !t
-
-  let create ~reconnect_after ~stop where_to_connect =
-    let t = ref None in
-    let rec connect_loop () =
-      if Deferred.is_determined stop then Deferred.unit
-      else (
-        try_with (fun () -> 
-          Tcp.connect ~interrupt:stop where_to_connect
-          >>= fun (_,r,w) ->
-          Rpc.Connection.create r w ~connection_state:()
-          >>= function
-          | Error exn -> raise exn
-          | Ok conn -> return conn            
-        )
-        >>= function
-        | Error _ ->
-          after reconnect_after
-          >>= fun () ->
-          connect_loop ()
-        | Ok conn ->
-          t := Some conn;
-          Rpc.Connection.close_finished conn
-          >>= fun () ->
-          t := None;
-          connect_loop ()
-      )
-    in
-    don't_wait_for (connect_loop ());
-    t
-
-  let with_connect ~reconnect_after where_to_connect k =
-    let stop = Ivar.create () in
-    let t = create where_to_connect
-              ~reconnect_after ~stop:(Ivar.read stop)
-    in
-    Monitor.protect (fun () -> k t)
-      ~finally:(fun () -> Ivar.fill stop (); Ivar.read stop)
-end
-
-let setup_robust_conn ~reconnect_after username k =
-  load_config ()
-  >>= fun config ->
-  resolve_username username config
-  >>= fun username ->
-  let host = config.Config.server in
-  let port = Common.port in
-  Robust_connection.with_connect
-    ~reconnect_after
-    (Tcp.to_host_and_port host port)
-    (fun conn -> k username conn)
-
-
 let retry
       ?(retry_after=sec 10.)
       ?(num_tries=`Unbounded)

client_common.mli

   -> (Username.t -> Rpc.Connection.t -> 'a Deferred.t)
   -> 'a Deferred.t
 
-module Robust_connection : sig
-  type t
-  val get : t -> Rpc.Connection.t option
-
-  val with_connect
-    :  reconnect_after:Time.Span.t
-    -> 'addr Tcp.where_to_connect
-    -> (t -> 'a Deferred.t)
-    -> 'a Deferred.t
-
-end
-
-val setup_robust_conn
-  :  reconnect_after:Core.Span.t
-  -> Username.t option
-  -> (Username.t -> Robust_connection.t -> 'a Deferred.t)
-  -> 'a Deferred.t
-
-
 (** Retries the function in question until it gets an [Ok] response.
     If the provided function throws an exception, the retry loop will
     end *)
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.