Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reversing Dependency: Implementing Untyped-RPC on Top of Typed-RPC (draft) #51

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 60 additions & 17 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
(synopsis "A modular gRPC library")
(description
"This library builds some of the signatures and implementations of gRPC functionality. This is used in the more specialised package `grpc-lwt` which has more machinery, however this library can also be used to do some bits yourself.")
(tags (network rpc serialisation))
(tags
(network rpc serialisation))
(depends
(ocaml
(>= 4.08))
Expand All @@ -44,7 +45,8 @@
(synopsis "An Lwt implementation of gRPC")
(description
"Functionality for building gRPC services and rpcs with `lwt`.")
(tags (network rpc serialisation))
(tags
(network rpc serialisation))
(depends
(grpc
(= :version))
Expand All @@ -57,57 +59,98 @@
(synopsis "An Async implementation of gRPC")
(description
"Functionality for building gRPC services and rpcs with `async`.")
(tags (network rpc serialisation))
(tags
(network rpc serialisation))
(depends
(ocaml
(>= 4.11))
(grpc
(= :version))
(async (>= v0.16))
(async
(>= v0.16))
stringext))

(package
(name grpc-eio)
(synopsis "An Eio implementation of gRPC")
(description
"Functionality for building gRPC services and rpcs with `eio`.")
"Functionality for building gRPC services and rpcs with `eio`.")
(depends
(grpc
(= :version))
(eio (>= 0.12))
stringext))
(eio
(>= 0.12))
stringext))

(package
(name grpc-protoc-plugin)
(synopsis "An implementation of gRPC using ocaml-protoc-plugin")
(description
"Functionality for building gRPC services and rpcs with `ocaml-protoc-plugin`")
(depends
(grpc
(= :version))
(ocaml-protoc-plugin
(>= 4.5))))

(package
(name grpc-protoc)
(synopsis "An implementation of gRPC using ocaml-protoc")
(description
"Functionality for building gRPC services and rpcs with `ocaml-protoc`")
(depends
(grpc
(= :version))
(ocaml-protoc
(>= 3.0))
(pbrt
(>= 3.0))
(pbrt_services
(>= 3.0))))

(package
(name grpc-examples)
(synopsis "Various gRPC examples")
(description "Various gRPC examples.")
(tags (network rpc serialisation))
(tags
(network rpc serialisation))
(depends
grpc-lwt
h2-lwt-unix
grpc-async
h2-async
grpc-eio
h2-eio
(ocaml-protoc-plugin (>= 4.5))
(ocaml-protoc-plugin
(>= 4.5))
ppx_deriving_yojson
conduit-lwt-unix
cohttp-lwt-unix
tls-async
(lwt_ssl (>= 1.2.0))
(mdx (and (>= 2.2.1) :with-test))
(eio_main (>= 0.12))
(lwt_ssl
(>= 1.2.0))
(mdx
(and
(>= 2.2.1)
:with-test))
(eio_main
(>= 0.12))
stringext))

(package
(name grpc-bench)
(synopsis "Benchmarking package for gRPC")
(description "Benchmarking package for gRPC.")
(tags (network rpc serialisation benchmark))
(tags
(network rpc serialisation benchmark))
(depends
grpc
(bechamel(>= 0.4.0))
(bechamel
(>= 0.4.0))
notty
(bechamel-notty (>= 0.4.0))
(bigstringaf (>= 0.9.1))
(notty (>= 0.2.3))))
(bechamel-notty
(>= 0.4.0))
(bigstringaf
(>= 0.9.1))
(notty
(>= 0.2.3))))
2 changes: 1 addition & 1 deletion examples/greeter-client-eio/dune
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
(executable
(name greeter_client_eio)
(libraries grpc grpc-eio ocaml-protoc-plugin eio_main greeter h2 h2-eio))
(libraries grpc grpc-eio grpc-protoc-plugin eio_main greeter h2 h2-eio))
24 changes: 8 additions & 16 deletions examples/greeter-client-eio/greeter_client_eio.ml
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,23 @@ let main env =
H2_eio.Client.create_connection ~sw ~error_handler:ignore socket
in

let open Ocaml_protoc_plugin in
let open Greeter.Mypackage in
let encode, decode = Service.make_client_functions Greeter.sayHello in
let encoded_request =
HelloRequest.make ~name () |> encode |> Writer.contents
in
let request = HelloRequest.make ~name () in

let f decoder =
match decoder with
| Some decoder -> (
Reader.create decoder |> decode |> function
| Ok v -> v
| Error e ->
failwith
(Printf.sprintf "Could not decode request: %s"
(Result.show_error e)))
let f response =
match response with
| Some response -> response
| None -> Greeter.SayHello.Response.make ()
in

let result =
Grpc_eio.Client.call ~service:"mypackage.Greeter" ~rpc:"SayHello"
Grpc_eio.Client.Typed_rpc.call
(Grpc_protoc_plugin.Client_rpc.unary (module Greeter.SayHello))
~do_request:(H2_eio.Client.request connection ~error_handler:ignore)
~handler:(Grpc_eio.Client.Rpc.unary encoded_request ~f)
~handler:(Grpc_eio.Client.Typed_rpc.unary request ~f)
()
in

Eio.Promise.await (H2_eio.Client.shutdown connection);
result
in
Expand Down
3 changes: 3 additions & 0 deletions examples/greeter-protoc-client-eio/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(executable
(name greeter_client_eio)
(libraries grpc grpc-eio grpc-protoc eio_main greeter_protoc h2 h2-eio))
48 changes: 48 additions & 0 deletions examples/greeter-protoc-client-eio/greeter_client_eio.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
let main env =
let name = if Array.length Sys.argv > 1 then Sys.argv.(1) else "anonymous" in
let host = "localhost" in
let port = "8080" in
let network = Eio.Stdenv.net env in
let run sw =
let inet, port =
Eio_unix.run_in_systhread (fun () ->
Unix.getaddrinfo host port [ Unix.(AI_FAMILY PF_INET) ])
|> List.filter_map (fun (addr : Unix.addr_info) ->
match addr.ai_addr with
| Unix.ADDR_UNIX _ -> None
| ADDR_INET (addr, port) -> Some (addr, port))
|> List.hd
in
let addr = `Tcp (Eio_unix.Net.Ipaddr.of_unix inet, port) in
let socket = Eio.Net.connect ~sw network addr in
let connection =
H2_eio.Client.create_connection ~sw ~error_handler:ignore socket
in

let request = Greeter_protoc.Greeter.default_hello_request ~name () in

let f response =
match response with
| Some response -> response
| None -> Greeter_protoc.Greeter.default_hello_reply ()
in

let result =
Grpc_eio.Client.Typed_rpc.call
(Grpc_protoc.Client_rpc.unary
Greeter_protoc.Greeter.Greeter.Client.sayHello)
~do_request:(H2_eio.Client.request connection ~error_handler:ignore)
~handler:(Grpc_eio.Client.Typed_rpc.unary request ~f)
()
in

Eio.Promise.await (H2_eio.Client.shutdown connection);
result
in
Eio.Switch.run run

let () =
match Eio_main.run main with
| Ok (reply, status) ->
Eio.traceln "%s: %s" (Grpc.Status.show status) reply.message
| Error err -> Eio.traceln "Error: %a" H2.Status.pp_hum err
3 changes: 3 additions & 0 deletions examples/greeter-protoc-server-eio/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(executable
(name greeter_server_eio)
(libraries grpc grpc-eio grpc-protoc eio_main greeter_protoc h2 h2-eio))
59 changes: 59 additions & 0 deletions examples/greeter-protoc-server-eio/greeter_server_eio.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
open Grpc_eio

let sayHello rpc =
Grpc_eio.Server.Typed_rpc.unary (Grpc_protoc.Server_rpc.unary rpc)
~f:(fun (request : Greeter_protoc.Greeter.hello_request) ->
let message =
if request.name = "" then "You forgot your name!"
else Format.sprintf "Hello, %s!" request.name
in
let reply = Greeter_protoc.Greeter.default_hello_reply ~message () in
(Grpc.Status.(v OK), Some reply))

let connection_handler server sw =
let error_handler client_address ?request:_ _error start_response =
Eio.traceln "Error in request from:%a" Eio.Net.Sockaddr.pp client_address;
let response_body = start_response H2.Headers.empty in
H2.Body.Writer.write_string response_body
"There was an error handling your request.\n";
H2.Body.Writer.close response_body
in
let request_handler client_address request_descriptor =
Eio.traceln "Handling a request from:%a" Eio.Net.Sockaddr.pp client_address;
Eio.Fiber.fork ~sw (fun () ->
Grpc_eio.Server.handle_request server request_descriptor)
in
fun socket addr ->
H2_eio.Server.create_connection_handler ?config:None ~request_handler
~error_handler addr ~sw socket

let serve server env =
let port = 8080 in
let net = Eio.Stdenv.net env in
let addr = `Tcp (Eio.Net.Ipaddr.V4.loopback, port) in
Eio.Switch.run @@ fun sw ->
let handler = connection_handler server sw in
let server_socket =
Eio.Net.listen net ~sw ~reuse_addr:true ~backlog:10 addr
in
let rec listen () =
Eio.Net.accept_fork ~sw server_socket
~on_error:(fun exn -> Eio.traceln "%s" (Printexc.to_string exn))
handler;
listen ()
in
Printf.printf "Listening on port %i for grpc requests\n" port;
print_endline "";
print_endline "Try running:";
print_endline "";
print_endline
{| dune exec -- examples/greeter-protoc-client-eio/greeter_client_eio.exe <your_name> |};
listen ()

let () =
let server =
Greeter_protoc.Greeter.Greeter.Server.make ~sayHello ()
|> Grpc_protoc.handlers |> Server.Typed_rpc.server
in

Eio_main.run (serve server)
14 changes: 14 additions & 0 deletions examples/greeter-protoc/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
(library
(name greeter_protoc)
(package grpc-examples)
(libraries ocaml-protoc pbrt pbrt_services))

(rule
(copy ../greeter/greeter.proto greeter.proto))

(rule
(targets greeter.ml greeter.mli)
(deps
(:proto greeter.proto))
(action
(run ocaml-protoc %{proto} --binary --pp --services --ml_out .)))
2 changes: 1 addition & 1 deletion examples/greeter-server-eio/dune
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
(executable
(name greeter_server_eio)
(libraries grpc grpc-eio ocaml-protoc-plugin eio_main greeter h2 h2-eio))
(libraries grpc grpc-eio grpc-protoc-plugin eio_main greeter h2 h2-eio))
36 changes: 13 additions & 23 deletions examples/greeter-server-eio/greeter_server_eio.ml
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
open Grpc_eio

let say_hello buffer =
let open Ocaml_protoc_plugin in
let open Greeter.Mypackage in
let decode, encode = Service.make_service_functions Greeter.sayHello in
let request =
Reader.create buffer |> decode |> function
| Ok v -> v
| Error e ->
failwith
(Printf.sprintf "Could not decode request: %s" (Result.show_error e))
in
let message =
if request = "" then "You forgot your name!"
else Format.sprintf "Hello, %s!" request
in
let reply = Greeter.SayHello.Response.make ~message () in
(Grpc.Status.(v OK), Some (encode reply |> Writer.contents))
let say_hello =
let module SayHello = Greeter.Mypackage.Greeter.SayHello in
Grpc_eio.Server.Typed_rpc.unary
(Grpc_protoc_plugin.Server_rpc.unary (module SayHello))
~f:(fun request ->
let message =
if request = "" then "You forgot your name!"
else Format.sprintf "Hello, %s!" request
in
let reply = SayHello.Response.make ~message () in
(Grpc.Status.(v OK), Some reply))

let connection_handler server sw =
let error_handler client_address ?request:_ _error start_response =
Expand Down Expand Up @@ -59,12 +53,8 @@ let serve server env =
listen ()

let () =
let greeter_service =
Server.Service.(
v () |> add_rpc ~name:"SayHello" ~rpc:(Unary say_hello) |> handle_request)
in
let server =
Server.(
v () |> add_service ~name:"mypackage.Greeter" ~service:greeter_service)
Server.Typed_rpc.server (Grpc_protoc_plugin.handlers [ say_hello ])
in

Eio_main.run (serve server)
24 changes: 24 additions & 0 deletions examples/routeguide-protoc/proto/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
(library
(name routeguide_protoc)
(package grpc-examples)
(preprocess
(pps ppx_deriving.show ppx_deriving.eq))
(libraries ocaml-protoc pbrt pbrt_services))

(rule
(copy ../../routeguide/proto/route_guide.proto route_guide.proto))

(rule
(targets route_guide.ml route_guide.mli)
(deps
(:proto route_guide.proto))
(action
(run
ocaml-protoc
%{proto}
--binary
--ocaml_all_types_ppx
"deriving show { with_path = false }, eq"
--services
--ml_out
.)))
Loading