-
Notifications
You must be signed in to change notification settings - Fork 10
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
+1,797
−458
Closed
Changes from 5 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e04a2e2
draft
mbarbin c6c8e5a
split codec into own lib
mbarbin 65f0f7f
move rpc spec to grpc
mbarbin 7531988
rename grpc-protoc-plugin
mbarbin 4f33fac
adding support for ocaml-protoc and example
mbarbin 45d4878
improve support for ocaml-protoc (wip)
mbarbin 042efeb
add dedicated user facing libraries
mbarbin 8677baa
Revert "add dedicated user facing libraries"
mbarbin c0c4df5
enforce consistency of request and response modes
mbarbin 358e2c0
allow implementing several services on a single server
mbarbin 225db75
revert unintentional local variable rename
mbarbin 67537c1
fix the service names used by Typed_rpc
mbarbin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 .))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
If you have no objections, I favor 2, but I'm open to 1 as well. Thanks!