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

Explore API with decode/encode in the library (draft) #48

Closed
wants to merge 12 commits into from
77 changes: 60 additions & 17 deletions dune-project
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The unrelated diffs you see on this file are due to my editor config which uses format-dune-file on save. After carefully editing the file by other means in the first few commits on the PR, I eventually gave up on trying to keep the file non formatted. Before enabling code review, I think either of the 2 following follow up will be satisfactory:

  1. I clean up the file and remove formatting changes from the PR
  2. We add a commit upstream that formats the file, and rebase the PR on top, thus voiding all the formatting changes.

If you have no objections, I favor 2, but I'm open to 1 as well. Thanks!

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.rpc (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))
49 changes: 49 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,49 @@
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.rpc ~client:Greeter_protoc.Greeter.Greeter.Client.sayHello
~server:(fun f ->
Greeter_protoc.Greeter.Greeter.Server.make ~sayHello:f ()))
~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))
58 changes: 58 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,58 @@
open Grpc_eio

let say_hello =
Grpc_eio.Server.Typed_rpc.unary
(Grpc_protoc.rpc ~client:Greeter_protoc.Greeter.Greeter.Client.sayHello
~server:(fun f ->
Greeter_protoc.Greeter.Greeter.Server.make ~sayHello:f ()))
~f:(fun 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 = Server.Typed_rpc.server [ say_hello ] in
Eio_main.run (serve server)
11 changes: 11 additions & 0 deletions examples/greeter-protoc/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(library
(name greeter_protoc)
(public_name grpc-examples.greeter-protoc)
(libraries ocaml-protoc pbrt pbrt_services))

(rule
(targets greeter.ml greeter.mli)
(deps
(:proto greeter.proto))
(action
(run ocaml-protoc %{proto} --binary --pp --services --ml_out .)))
14 changes: 14 additions & 0 deletions examples/greeter-protoc/greeter.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
syntax = "proto3";
package mypackage;

// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello(HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest { string name = 1; }

// The response message containing the greetings
message HelloReply { string message = 1; }
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))
37 changes: 12 additions & 25 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.rpc (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,5 @@ 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)
in
let server = Server.Typed_rpc.server [ say_hello ] in
Eio_main.run (serve server)
Loading