async-grpc 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/code.md +0 -0
- data/context/getting-started.md +174 -0
- data/context/index.yaml +12 -0
- data/design.md +1121 -0
- data/lib/async/grpc/client.rb +351 -0
- data/lib/async/grpc/dispatcher_middleware.rb +113 -0
- data/lib/async/grpc/service.rb +93 -0
- data/lib/async/grpc/stub.rb +89 -0
- data/lib/async/grpc/version.rb +12 -0
- data/lib/async/grpc.rb +15 -0
- data/license.md +21 -0
- data/readme.md +52 -0
- data/releases.md +3 -0
- data/spanner_integration.md +309 -0
- metadata +81 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: a4796002440be1be16d777561582938e9ba8651636a7ebdc14e5f2ec5500e317
|
|
4
|
+
data.tar.gz: 47658a764095cc220dc087f9c12199cbfd78490c32f25949f4c93d94790525bc
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: e2ec6aff5358f4385d8d99db9ddcdfb2bf0987a50fbf2eefa12baebbbbebeeb6c206244fc57f7d73676be899e2d373975111f325cc147603b9b73ba3d0767a74
|
|
7
|
+
data.tar.gz: d1b3823eb2ce13f7c2ed01e8cccefb36bb7a17500f25b260ffa7227e0bb0d3c77c06cf05f5ae7b6e0e963ab408d9ba398200da3f6bec0d3e23fb3647dd4977d1
|
data/code.md
ADDED
|
File without changes
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
# Getting Started
|
|
2
|
+
|
|
3
|
+
This guide explains how to get started with `Async::GRPC` for building gRPC clients and servers.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add the gem to your project:
|
|
8
|
+
|
|
9
|
+
~~~ bash
|
|
10
|
+
$ bundle add async-grpc
|
|
11
|
+
~~~
|
|
12
|
+
|
|
13
|
+
You'll also need `protocol-grpc` and `async-http`:
|
|
14
|
+
|
|
15
|
+
~~~ bash
|
|
16
|
+
$ bundle add protocol-grpc async-http
|
|
17
|
+
~~~
|
|
18
|
+
|
|
19
|
+
## Core Concepts
|
|
20
|
+
|
|
21
|
+
`async-grpc` provides:
|
|
22
|
+
|
|
23
|
+
- {ruby Async::GRPC::Client} - An asynchronous gRPC client that wraps `Async::HTTP::Client`.
|
|
24
|
+
- {ruby Async::GRPC::Stub} - A method-based stub for making RPC calls.
|
|
25
|
+
- {ruby Async::GRPC::Service} - A concrete service implementation that uses a `Protocol::GRPC::Interface`.
|
|
26
|
+
- {ruby Async::GRPC::DispatcherMiddleware} - Middleware that routes requests to registered services.
|
|
27
|
+
|
|
28
|
+
## Client Usage
|
|
29
|
+
|
|
30
|
+
### Creating a Client
|
|
31
|
+
|
|
32
|
+
``` ruby
|
|
33
|
+
require "async/grpc"
|
|
34
|
+
require "async/http/endpoint"
|
|
35
|
+
|
|
36
|
+
endpoint = Async::HTTP::Endpoint.parse("https://grpc.example.com")
|
|
37
|
+
http_client = Async::HTTP::Client.new(endpoint)
|
|
38
|
+
client = Async::GRPC::Client.new(http_client)
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Using a Stub
|
|
42
|
+
|
|
43
|
+
Create a stub from an interface definition:
|
|
44
|
+
|
|
45
|
+
``` ruby
|
|
46
|
+
# Define the interface
|
|
47
|
+
class GreeterInterface < Protocol::GRPC::Interface
|
|
48
|
+
rpc :SayHello, request_class: Hello::HelloRequest, response_class: Hello::HelloReply
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Create a stub
|
|
52
|
+
stub = client.stub(GreeterInterface, "hello.Greeter")
|
|
53
|
+
|
|
54
|
+
# Make RPC calls (accepts both snake_case and PascalCase)
|
|
55
|
+
response = stub.say_hello(Hello::HelloRequest.new(name: "World"))
|
|
56
|
+
# or
|
|
57
|
+
response = stub.SayHello(Hello::HelloRequest.new(name: "World"))
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Streaming RPCs
|
|
61
|
+
|
|
62
|
+
``` ruby
|
|
63
|
+
# Server streaming
|
|
64
|
+
stub.server_streaming_call(request) do |response|
|
|
65
|
+
puts response.value
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Client streaming
|
|
69
|
+
stub.client_streaming_call do |output|
|
|
70
|
+
output.write(request1)
|
|
71
|
+
output.write(request2)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Bidirectional streaming
|
|
75
|
+
stub.bidirectional_streaming do |input, output|
|
|
76
|
+
input.each do |request|
|
|
77
|
+
output.write(process(request))
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Server Usage
|
|
83
|
+
|
|
84
|
+
### Defining a Service
|
|
85
|
+
|
|
86
|
+
``` ruby
|
|
87
|
+
require "async/grpc/service"
|
|
88
|
+
|
|
89
|
+
# Define the interface
|
|
90
|
+
class GreeterInterface < Protocol::GRPC::Interface
|
|
91
|
+
rpc :SayHello, request_class: Hello::HelloRequest, response_class: Hello::HelloReply
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# Implement the service
|
|
95
|
+
class GreeterService < Async::GRPC::Service
|
|
96
|
+
def say_hello(input, output, call)
|
|
97
|
+
request = input.read
|
|
98
|
+
reply = Hello::HelloReply.new(message: "Hello, #{request.name}!")
|
|
99
|
+
output.write(reply)
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Registering Services
|
|
105
|
+
|
|
106
|
+
``` ruby
|
|
107
|
+
require "async/grpc/dispatcher_middleware"
|
|
108
|
+
|
|
109
|
+
dispatcher = Async::GRPC::DispatcherMiddleware.new
|
|
110
|
+
|
|
111
|
+
service = GreeterService.new(GreeterInterface, "hello.Greeter")
|
|
112
|
+
dispatcher.register("hello.Greeter", service)
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Running a Server
|
|
116
|
+
|
|
117
|
+
``` ruby
|
|
118
|
+
require "async/http/server"
|
|
119
|
+
require "async/http/endpoint"
|
|
120
|
+
|
|
121
|
+
endpoint = Async::HTTP::Endpoint.parse("http://localhost:50051")
|
|
122
|
+
server = Async::HTTP::Server.for(endpoint, dispatcher)
|
|
123
|
+
|
|
124
|
+
Async do
|
|
125
|
+
server.run
|
|
126
|
+
end
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## Complete Example
|
|
130
|
+
|
|
131
|
+
``` ruby
|
|
132
|
+
require "async"
|
|
133
|
+
require "async/grpc"
|
|
134
|
+
require "async/http/server"
|
|
135
|
+
require "async/http/endpoint"
|
|
136
|
+
|
|
137
|
+
# Define interface
|
|
138
|
+
class GreeterInterface < Protocol::GRPC::Interface
|
|
139
|
+
rpc :SayHello, request_class: Hello::HelloRequest, response_class: Hello::HelloReply
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# Implement service
|
|
143
|
+
class GreeterService < Async::GRPC::Service
|
|
144
|
+
def say_hello(input, output, call)
|
|
145
|
+
request = input.read
|
|
146
|
+
reply = Hello::HelloReply.new(message: "Hello, #{request.name}!")
|
|
147
|
+
output.write(reply)
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
Async do
|
|
152
|
+
# Setup server
|
|
153
|
+
endpoint = Async::HTTP::Endpoint.parse("http://localhost:50051")
|
|
154
|
+
dispatcher = Async::GRPC::DispatcherMiddleware.new
|
|
155
|
+
dispatcher.register("hello.Greeter", GreeterService.new(GreeterInterface, "hello.Greeter"))
|
|
156
|
+
server = Async::HTTP::Server.for(endpoint, dispatcher)
|
|
157
|
+
|
|
158
|
+
# Setup client
|
|
159
|
+
client_endpoint = Async::HTTP::Endpoint.parse("http://localhost:50051")
|
|
160
|
+
http_client = Async::HTTP::Client.new(client_endpoint)
|
|
161
|
+
client = Async::GRPC::Client.new(http_client)
|
|
162
|
+
stub = client.stub(GreeterInterface, "hello.Greeter")
|
|
163
|
+
|
|
164
|
+
# Make a call
|
|
165
|
+
request = Hello::HelloRequest.new(name: "World")
|
|
166
|
+
response = stub.say_hello(request)
|
|
167
|
+
puts response.message # => "Hello, World!"
|
|
168
|
+
|
|
169
|
+
server.run
|
|
170
|
+
end
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
|
data/context/index.yaml
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# Automatically generated context index for Utopia::Project guides.
|
|
2
|
+
# Do not edit then files in this directory directly, instead edit the guides and then run `bake utopia:project:agent:context:update`.
|
|
3
|
+
---
|
|
4
|
+
description: Client and server implementation for gRPC using Async.
|
|
5
|
+
metadata:
|
|
6
|
+
documentation_uri: https://socketry.github.io/async-grpc/
|
|
7
|
+
source_code_uri: https://github.com/socketry/async-grpc.git
|
|
8
|
+
files:
|
|
9
|
+
- path: getting-started.md
|
|
10
|
+
title: Getting Started
|
|
11
|
+
description: This guide explains how to get started with `Async::GRPC` for building
|
|
12
|
+
gRPC clients and servers.
|