Skip to content

Commit 46d39fa

Browse files
committed
Use GADTs to do the same simplification to shutdown as to diagnostics
Signed-off-by: Ambre Austen Suhamy <[email protected]>
1 parent 6827d10 commit 46d39fa

File tree

12 files changed

+65
-59
lines changed

12 files changed

+65
-59
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_request
202+
Rpc.Common.fire_message
203203
~name:"build"
204204
~wait:true
205205
~lock_held_by
206206
builder
207-
Dune_rpc_impl.Decl.build
207+
(Rpc.Common.Request 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_request
13+
Rpc.Common.fire_message
1414
~name:"diagnostics_cmd"
1515
~wait:false
1616
builder
17-
Dune_rpc_private.Procedures.Public.diagnostics
17+
(Rpc.Common.Request 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_request
231+
Rpc.Common.fire_message
232232
~name:"build"
233233
~wait:true
234234
~lock_held_by
235235
builder
236-
Dune_rpc_impl.Decl.build
236+
(Rpc.Common.Request 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_request
51+
(Rpc.Common.fire_message
5252
~name:"format"
5353
~wait:true
5454
~warn_forwarding:false
5555
~lock_held_by
5656
builder
57-
Dune_rpc.Procedures.Public.format)
57+
(Rpc.Common.Request 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_request
68+
Rpc.Common.fire_message
6969
~name:"promote_many"
7070
~wait:true
7171
~lock_held_by
7272
builder
73-
Dune_rpc_private.Procedures.Public.promote_many
73+
(Rpc.Common.Request 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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ let term =
1515
@@ fun () ->
1616
let open Fiber.O in
1717
let+ response =
18-
Rpc_common.fire_request ~name:"build" ~wait builder Dune_rpc_impl.Decl.build targets
18+
Rpc_common.fire_message
19+
~name:"build"
20+
~wait
21+
builder
22+
(Rpc_common.Request Dune_rpc_impl.Decl.build)
23+
targets
1924
in
2025
match response with
2126
| Success -> print_endline "Success"

bin/rpc/rpc_common.ml

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,40 @@ 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+
2832
let request_exn client request arg =
2933
let* decl =
3034
Client.Versioned.prepare_request client (Dune_rpc.Decl.Request.witness request)
3135
in
3236
match decl with
33-
| Ok decl ->
34-
let+ res = Client.request client decl arg in
37+
| Ok decl -> Client.request client decl arg
38+
| Error e -> raise (Dune_rpc.Version_error.E e)
39+
;;
40+
41+
let notify_exn client notification arg =
42+
let* res =
43+
Client.Versioned.prepare_notification
44+
client
45+
(Dune_rpc.Decl.Notification.witness notification)
46+
in
47+
match res with
48+
| Ok decl -> Client.notification client decl arg
49+
| Error e -> raise (Dune_rpc.Version_error.E e)
50+
;;
51+
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
3559
(match res with
36-
| Ok response -> response
60+
| Ok (result : b) -> result
3761
| Error e -> raise_rpc_error e)
38-
| Error e -> raise (Dune_rpc.Version_error.E e)
3962
;;
4063

4164
let client_term builder f =
@@ -86,13 +109,13 @@ let warn_ignore_arguments lock_held_by =
86109
]
87110
;;
88111

89-
let fire_request
112+
let fire_message
90113
~name
91114
~wait
92115
?(warn_forwarding = true)
93116
?(lock_held_by = Dune_util.Global_lock.Lock_held_by.Unknown)
94117
builder
95-
request
118+
message
96119
arg
97120
=
98121
let* connection = establish_client_session ~wait in
@@ -101,7 +124,7 @@ let fire_request
101124
Dune_rpc_impl.Client.client
102125
connection
103126
(Dune_rpc.Initialize.Request.create ~id:(Dune_rpc.Id.make (Sexp.Atom name)))
104-
~f:(fun client -> request_exn client request arg)
127+
~f:(fun client -> prepare_message_and_send client message arg)
105128
;;
106129

107130
let wrap_build_outcome_exn ~print_on_success build_outcome =

bin/rpc/rpc_common.mli

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,28 @@ val active_server_exn : unit -> Dune_rpc.Where.t
77
(** Raise an RPC response error. *)
88
val raise_rpc_error : Dune_rpc.Response.Error.t -> 'a
99

10-
(** Make a request and raise an exception if the preparation for the request
11-
fails in any way. Returns an [Error] if the response errors. *)
12-
val request_exn
13-
: Dune_rpc_client.Client.t
14-
-> ('a, 'b) Dune_rpc.Decl.request
15-
-> 'a
16-
-> 'b Fiber.t
17-
1810
(** Cmdliner term for a generic RPC client. *)
1911
val client_term : Common.Builder.t -> (unit -> 'a Fiber.t) -> 'a
2012

2113
(** Cmdliner argument for a wait flag. *)
2214
val wait_term : bool Cmdliner.Term.t
2315

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+
2420
(** Send a request to the RPC server. If [wait], it will poll forever until a server is listening.
2521
Should be scheduled by a scheduler that does not come with a RPC server on its own.
2622
2723
[warn_forwarding] defaults to true, warns the user that since a RPC server is running, some arguments are ignored.
2824
[lock_held_by] defaults to [Unknown], is only used to allow error messages to print the PID. *)
29-
val fire_request
25+
val fire_message
3026
: name:string
3127
-> wait:bool
3228
-> ?warn_forwarding:bool
3329
-> ?lock_held_by:Dune_util.Global_lock.Lock_held_by.t
3430
-> Common.Builder.t
35-
-> ('a, 'b) Dune_rpc.Decl.request
31+
-> ('a, 'b) message_kind
3632
-> 'a
3733
-> 'b Fiber.t
3834

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_request
19+
Rpc_common.fire_message
2020
~name:"ping_cmd"
2121
~wait
2222
builder
23-
Dune_rpc_private.Procedures.Public.ping
23+
(Rpc_common.Request 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_request
54+
Rpc.Common.fire_message
5555
~name:"runtest"
5656
~wait:false
5757
~lock_held_by
5858
builder
59-
Dune_rpc.Procedures.Public.runtest
59+
(Rpc.Common.Request 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)