Skip to content

Commit 0129cee

Browse files
committed
Simplify using two distinct functions
This produces a simpler API w/r/t language surface complexity, and to the complexity users need to consider when sending RPC messages. The cost is a few lines of code duplication. Signed-off-by: Shon Feder <[email protected]>
1 parent 46d39fa commit 0129cee

File tree

13 files changed

+70
-55
lines changed

13 files changed

+70
-55
lines changed

bin/build.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,12 @@ let build =
199199
Scheduler.go_without_rpc_server ~common ~config (fun () ->
200200
let open Fiber.O in
201201
let+ build_outcome =
202-
Rpc.Common.fire_message
202+
Rpc.Common.fire_request
203203
~name:"build"
204204
~wait:true
205205
~lock_held_by
206206
builder
207-
(Rpc.Common.Request Dune_rpc_impl.Decl.build)
207+
Dune_rpc_impl.Decl.build
208208
targets
209209
in
210210
Rpc.Common.wrap_build_outcome_exn ~print_on_success:true build_outcome)

bin/diagnostics.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ let term =
1010
Rpc.Common.client_term builder (fun () ->
1111
let open Fiber.O in
1212
let+ errors =
13-
Rpc.Common.fire_message
13+
Rpc.Common.fire_request
1414
~name:"diagnostics_cmd"
1515
~wait:false
1616
builder
17-
(Rpc.Common.Request Dune_rpc_private.Procedures.Public.diagnostics)
17+
Dune_rpc_private.Procedures.Public.diagnostics
1818
()
1919
in
2020
List.iter errors ~f:(fun err ->

bin/exec.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,12 @@ let build_prog_via_rpc_if_necessary ~dir ~no_rebuild builder lock_held_by prog =
228228
let targets = Rpc.Group.Build.prepare_targets [ target ] in
229229
let open Fiber.O in
230230
let+ build_outcome =
231-
Rpc.Common.fire_message
231+
Rpc.Common.fire_request
232232
~name:"build"
233233
~wait:true
234234
~lock_held_by
235235
builder
236-
(Rpc.Common.Request Dune_rpc_impl.Decl.build)
236+
Dune_rpc_impl.Decl.build
237237
targets
238238
in
239239
Rpc.Common.wrap_build_outcome_exn ~print_on_success:false build_outcome)

bin/fmt.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ let run_fmt_command ~common ~config ~preview builder =
4848
Scheduler.go_without_rpc_server
4949
~common
5050
~config
51-
(Rpc.Common.fire_message
51+
(Rpc.Common.fire_request
5252
~name:"format"
5353
~wait:true
5454
~warn_forwarding:false
5555
~lock_held_by
5656
builder
57-
(Rpc.Common.Request Dune_rpc.Procedures.Public.format))
57+
Dune_rpc.Procedures.Public.format)
5858
;;
5959

6060
let command =

bin/promotion.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ module Apply = struct
6565
Scheduler.go_without_rpc_server ~common ~config (fun () ->
6666
let open Fiber.O in
6767
let+ build_outcome =
68-
Rpc.Common.fire_message
68+
Rpc.Common.fire_request
6969
~name:"promote_many"
7070
~wait:true
7171
~lock_held_by
7272
builder
73-
(Rpc.Common.Request Dune_rpc_private.Procedures.Public.promote_many)
73+
Dune_rpc_private.Procedures.Public.promote_many
7474
files_to_promote
7575
in
7676
Rpc.Common.wrap_build_outcome_exn ~print_on_success:true build_outcome)

bin/rpc/rpc_build.ml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,7 @@ let term =
1515
@@ fun () ->
1616
let open Fiber.O in
1717
let+ response =
18-
Rpc_common.fire_message
19-
~name:"build"
20-
~wait
21-
builder
22-
(Rpc_common.Request Dune_rpc_impl.Decl.build)
23-
targets
18+
Rpc_common.fire_request ~name:"build" ~wait builder Dune_rpc_impl.Decl.build targets
2419
in
2520
match response with
2621
| Success -> print_endline "Success"

bin/rpc/rpc_common.ml

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ let raise_rpc_error (e : Rpc_error.t) =
2525
]
2626
;;
2727

28-
type ('a, 'b) message_kind =
29-
| Request : ('a, 'b) Dune_rpc.Decl.request -> ('a, 'b) message_kind
30-
| Notification : 'a Dune_rpc.Decl.notification -> ('a, unit) message_kind
31-
3228
let request_exn client request arg =
3329
let* decl =
3430
Client.Versioned.prepare_request client (Dune_rpc.Decl.Request.witness request)
@@ -49,18 +45,6 @@ let notify_exn client notification arg =
4945
| Error e -> raise (Dune_rpc.Version_error.E e)
5046
;;
5147

52-
let prepare_message_and_send : type b. Client.t -> ('a, b) message_kind -> 'a -> b Fiber.t
53-
=
54-
fun client message arg ->
55-
match message with
56-
| Notification witness -> notify_exn client witness arg
57-
| Request witness ->
58-
let+ res = request_exn client witness arg in
59-
(match res with
60-
| Ok (result : b) -> result
61-
| Error e -> raise_rpc_error e)
62-
;;
63-
6448
let client_term builder f =
6549
let builder = Common.Builder.forbid_builds builder in
6650
let builder = Common.Builder.disable_log_file builder in
@@ -109,22 +93,47 @@ let warn_ignore_arguments lock_held_by =
10993
]
11094
;;
11195

112-
let fire_message
96+
let should_warn ~warn_forwarding ~builder =
97+
warn_forwarding && not (Common.Builder.equal builder Common.Builder.default)
98+
;;
99+
100+
let send_request ~f connection name =
101+
Dune_rpc_impl.Client.client
102+
connection
103+
(Dune_rpc.Initialize.Request.create ~id:(Dune_rpc.Id.make (Sexp.Atom name)))
104+
~f
105+
;;
106+
107+
let fire_request
113108
~name
114109
~wait
115110
?(warn_forwarding = true)
116111
?(lock_held_by = Dune_util.Global_lock.Lock_held_by.Unknown)
117112
builder
118-
message
113+
request
119114
arg
120115
=
121116
let* connection = establish_client_session ~wait in
122-
if warn_forwarding && not (Common.Builder.equal builder Common.Builder.default)
123-
then warn_ignore_arguments lock_held_by;
124-
Dune_rpc_impl.Client.client
125-
connection
126-
(Dune_rpc.Initialize.Request.create ~id:(Dune_rpc.Id.make (Sexp.Atom name)))
127-
~f:(fun client -> prepare_message_and_send client message arg)
117+
if should_warn ~warn_forwarding ~builder then warn_ignore_arguments lock_held_by;
118+
send_request connection name ~f:(fun client ->
119+
let+ res = request_exn client request arg in
120+
match res with
121+
| Ok result -> result
122+
| Error e -> raise_rpc_error e)
123+
;;
124+
125+
let fire_notification
126+
~name
127+
~wait
128+
?(warn_forwarding = true)
129+
?(lock_held_by = Dune_util.Global_lock.Lock_held_by.Unknown)
130+
builder
131+
notification
132+
arg
133+
=
134+
let* connection = establish_client_session ~wait in
135+
if should_warn ~warn_forwarding ~builder then warn_ignore_arguments lock_held_by;
136+
send_request connection name ~f:(fun client -> notify_exn client notification arg)
128137
;;
129138

130139
let wrap_build_outcome_exn ~print_on_success build_outcome =

bin/rpc/rpc_common.mli

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,36 @@ val client_term : Common.Builder.t -> (unit -> 'a Fiber.t) -> 'a
1313
(** Cmdliner argument for a wait flag. *)
1414
val wait_term : bool Cmdliner.Term.t
1515

16-
type ('a, 'b) message_kind =
17-
| Request : ('a, 'b) Dune_rpc.Decl.request -> ('a, 'b) message_kind
18-
| Notification : 'a Dune_rpc.Decl.notification -> ('a, unit) message_kind
19-
2016
(** Send a request to the RPC server. If [wait], it will poll forever until a server is listening.
2117
Should be scheduled by a scheduler that does not come with a RPC server on its own.
2218
2319
[warn_forwarding] defaults to true, warns the user that since a RPC server is running, some arguments are ignored.
2420
[lock_held_by] defaults to [Unknown], is only used to allow error messages to print the PID. *)
25-
val fire_message
21+
val fire_request
2622
: name:string
2723
-> wait:bool
2824
-> ?warn_forwarding:bool
2925
-> ?lock_held_by:Dune_util.Global_lock.Lock_held_by.t
3026
-> Common.Builder.t
31-
-> ('a, 'b) message_kind
27+
-> ('a, 'b) Dune_rpc.Decl.request
3228
-> 'a
3329
-> 'b Fiber.t
3430

31+
(** Send a notification to the RPC server. If [wait], it will poll forever until a server is listening.
32+
Should be scheduled by a scheduler that does not come with a RPC server on its own.
33+
34+
[warn_forwarding] defaults to true, warns the user that since a RPC server is running, some arguments are ignored.
35+
[lock_held_by] defaults to [Unknown], is only used to allow error messages to print the PID. *)
36+
val fire_notification
37+
: name:string
38+
-> wait:bool
39+
-> ?warn_forwarding:bool
40+
-> ?lock_held_by:Dune_util.Global_lock.Lock_held_by.t
41+
-> Common.Builder.t
42+
-> 'a Dune_rpc.Decl.notification
43+
-> 'a
44+
-> unit Fiber.t
45+
3546
val wrap_build_outcome_exn
3647
: print_on_success:bool
3748
-> Dune_rpc.Build_outcome_with_diagnostics.t

bin/rpc/rpc_ping.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ let term =
1616
@@ fun () ->
1717
let open Fiber.O in
1818
let+ () =
19-
Rpc_common.fire_message
19+
Rpc_common.fire_request
2020
~name:"ping_cmd"
2121
~wait
2222
builder
23-
(Rpc_common.Request Dune_rpc_private.Procedures.Public.ping)
23+
Dune_rpc_private.Procedures.Public.ping
2424
()
2525
in
2626
Console.print [ Pp.text "Server appears to be responding normally" ]

bin/runtest.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ let runtest_term =
5151
Scheduler.go_without_rpc_server ~common ~config (fun () ->
5252
let open Fiber.O in
5353
let+ build_outcome =
54-
Rpc.Common.fire_message
54+
Rpc.Common.fire_request
5555
~name:"runtest"
5656
~wait:false
5757
~lock_held_by
5858
builder
59-
(Rpc.Common.Request Dune_rpc.Procedures.Public.runtest)
59+
Dune_rpc.Procedures.Public.runtest
6060
dir_or_cram_test_paths
6161
in
6262
Rpc.Common.wrap_build_outcome_exn ~print_on_success:true build_outcome)

0 commit comments

Comments
 (0)