Skip to content

Commit b2bea23

Browse files
move grpc-common examples to here
2 parents 60ff5c3 + 6905915 commit b2bea23

File tree

197 files changed

+21494
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

197 files changed

+21494
-0
lines changed

grpc-common/LICENSE

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Copyright (c) 2015, grpc
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are met:
6+
7+
* Redistributions of source code must retain the above copyright notice, this
8+
list of conditions and the following disclaimer.
9+
10+
* Redistributions in binary form must reproduce the above copyright notice,
11+
this list of conditions and the following disclaimer in the documentation
12+
and/or other materials provided with the distribution.
13+
14+
* Neither the name of grpc-common nor the names of its
15+
contributors may be used to endorse or promote products derived from
16+
this software without specific prior written permission.
17+
18+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+

grpc-common/PATENTS

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Additional IP Rights Grant (Patents)
2+
3+
"This implementation" means the copyrightable works distributed by
4+
Google as part of the GRPC project.
5+
6+
Google hereby grants to You a perpetual, worldwide, non-exclusive,
7+
no-charge, royalty-free, irrevocable (except as stated in this section)
8+
patent license to make, have made, use, offer to sell, sell, import,
9+
transfer and otherwise run, modify and propagate the contents of this
10+
implementation of GRPC, where such license applies only to those patent
11+
claims, both currently owned or controlled by Google and acquired in
12+
the future, licensable by Google that are necessarily infringed by this
13+
implementation of GRPC. This grant does not include claims that would be
14+
infringed only as a consequence of further modification of this
15+
implementation. If you or your agent or exclusive licensee institute or
16+
order or agree to the institution of patent litigation against any
17+
entity (including a cross-claim or counterclaim in a lawsuit) alleging
18+
that this implementation of GRPC or any code incorporated within this
19+
implementation of GRPC constitutes direct or contributory patent
20+
infringement, or inducement of patent infringement, then any patent
21+
rights granted to you under this License for this implementation of GRPC
22+
shall terminate as of the date such litigation is filed.

grpc-common/PROTOCOL-HTTP2.md

+192
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
# gRPC over HTTP2
2+
3+
## Introduction
4+
This document serves as a detailed description for an implementation of gRPC carried over HTTP2 draft 17 framing. It assumes familiarity with the HTTP2 specification.
5+
6+
## Protocol
7+
Production rules are using <a href="http://tools.ietf.org/html/rfc5234">ABNF syntax</a>.
8+
9+
### Outline
10+
11+
The following is the general sequence of message atoms in a GRPC request & response message stream
12+
13+
* Request → Request-Headers *Delimited-Message EOS
14+
* Response → (Response-Headers *Delimited-Message Trailers) / Trailers-Only
15+
16+
17+
### Requests
18+
19+
* Request → Request-Headers *Delimited-Message EOS
20+
21+
Request-Headers are delivered as HTTP2 headers in HEADERS + CONTINUATION frames.
22+
23+
* **Request-Headers** → Call-Definition *Custom-Metadata
24+
* **Call-Definition** → Method Scheme Path TE [Authority] [Timeout] [Content-Type] [Message-Type] [Message-Encoding] [Message-Accept-Encoding] [User-Agent]
25+
* **Method** → “:method POST”
26+
* **Scheme** → “:scheme ” (“http” / “https”)
27+
* **Path** → “:path” {_path identifying method within exposed API_}
28+
* **Authority** → “:authority” {_virtual host name of authority_}
29+
* **TE** → “te” “trailers” # Used to detect incompatible proxies
30+
* **Timeout** → “grpc-timeout” TimeoutValue TimeoutUnit
31+
* **TimeoutValue** → {_positive integer as ASCII string of at most 8 digits_}
32+
* **TimeoutUnit** → Hour / Minute / Second / Millisecond / Microsecond / Nanosecond
33+
* **Hour** → “H”
34+
* **Minute** → “M”
35+
* **Second** → “S”
36+
* **Millisecond** → “m”
37+
* **Microsecond** → “u”
38+
* **Nanosecond** → “n”
39+
* **Content-Type** → “content-type” “application/grpc” [(“+proto” / “+json” / {_custom_})]
40+
* **Content-Coding** → “gzip” / “deflate” / “snappy” / {_custom_}
41+
* **Message-Encoding** → “grpc-encoding” Content-Coding
42+
* **Message-Accept-Encoding** → “grpc-accept-encoding” Content-Coding *("," Content-Coding)
43+
* **User-Agent** → “user-agent” {_structured user-agent string_}
44+
* **Message-Type** → “grpc-message-type” {_type name for message schema_}
45+
* **Custom-Metadata** → Binary-Header / ASCII-Header
46+
* **Binary-Header** → {lowercase ASCII header name ending in “-bin” } {_base64 encoded value_}
47+
* **ASCII-Header** → {lowercase ASCII header name} {_value_}
48+
49+
50+
HTTP2 requires that reserved headers, ones starting with “:” appear before all other headers. Additionally implementations should send **Timeout** immediately after the reserved headers and they should send the **Call-Definition** headers before sending **Custom-Metadata**.
51+
52+
If **Timeout** is omitted a server should assume an infinite timeout. Client implementations are free to send a default minimum timeout based on their deployment requirements.
53+
54+
**Custom-Metadata** is an arbitrary set of key-value pairs defined by the application layer. Aside from transport limits on the total length of HTTP2 HEADERS the only other constraint is that header names starting with “grpc-” are reserved for future use.
55+
56+
Note that HTTP2 does not allow arbitrary octet sequences for header values so binary header values must be encoded using Base64 as per https://tools.ietf.org/html/rfc4648#section-4. Implementations MUST accept padded and un-padded values and should emit un-padded values. Applications define binary headers by having their names end with “-bin”. Runtime libraries use this suffix to detect binary headers and properly apply base64 encoding & decoding as headers are sent and received.
57+
58+
The repeated sequence of **Delimited-Message** items is delivered in DATA frames
59+
60+
* **Delimited-Message** → Compressed-Flag Message-Length Message
61+
* **Compressed-Flag** → 0 / 1 # encoded as 1 byte unsigned integer
62+
* **Message-Length** → {_length of Message_} # encoded as 4 byte unsigned integer
63+
* **Message***{binary octet}
64+
65+
A **Compressed-Flag** value of 1 indicates that the binary octet sequence of **Message** is compressed using the mechanism declared by the **Message-Encoding** header. A value of 0 indicates that no encoding of **Message** bytes has occurred. Compression contexts are NOT maintained over message boundaries, implementations must create a new context for each message in the stream. If the **Message-Encoding** header is omitted then the **Compressed-Flag** must be 0.
66+
67+
For requests, **EOS** (end-of-stream) is indicated by the presence of the END_STREAM flag on the last received DATA frame. In scenarios where the **Request** stream needs to be closed but no data remains to be sent implementations MUST send an empty DATA frame with this flag set.
68+
69+
###Responses
70+
71+
* **Response** → (Response-Headers *Delimited-Message Trailers) / Trailers-Only
72+
* **Response-Headers** → HTTP-Status [Message-Encoding] [Message-Accept-Encoding] Content-Type *Custom-Metadata
73+
* **Trailers-Only** → HTTP-Status Content-Type Trailers
74+
* **Trailers** → Status [Status-Message] *Custom-Metadata
75+
* **HTTP-Status** → “:status 200”
76+
* **Status** → “grpc-status” <status-code-as-ASCII-string>
77+
* **Status-Message** → “grpc-message” <descriptive text for status as ASCII string>
78+
79+
**Response-Headers** & **Trailers-Only** are each delivered in a single HTTP2 HEADERS frame block. Most responses are expected to have both headers and trailers but **Trailers-Only** is permitted for calls that produce an immediate error. Status must be sent in **Trailers** even if the status code is OK.
80+
81+
For responses end-of-stream is indicated by the presence of the END_STREAM flag on the last received HEADERS frame that carries **Trailers**.
82+
83+
Implementations should expect broken deployments to send non-200 HTTP status codes in responses as well as a variety of non-GRPC content-types and to omit **Status** & **Status-Message**. Implementations must synthesize a **Status** & **Status-Message** to propagate to the application layer when this occurs.
84+
85+
####Example
86+
87+
Sample unary-call showing HTTP2 framing sequence
88+
89+
**Request**
90+
91+
```
92+
HEADERS (flags = END_HEADERS)
93+
:method = POST
94+
:scheme = http
95+
:path = /google.pubsub.v2.PublisherService/CreateTopic
96+
:authority = pubsub.googleapis.com
97+
grpc-timeout = 1S
98+
content-type = application/grpc+proto
99+
grpc-encoding = gzip
100+
authorization = Bearer y235.wef315yfh138vh31hv93hv8h3v
101+
102+
DATA (flags = END_STREAM)
103+
<Delimited Message>
104+
```
105+
**Response**
106+
```
107+
HEADERS (flags = END_HEADERS)
108+
:status = 200
109+
grpc-encoding = gzip
110+
111+
DATA
112+
<Delimited Message>
113+
114+
HEADERS (flags = END_STREAM, END_HEADERS)
115+
grpc-status = 0 # OK
116+
trace-proto-bin = jher831yy13JHy3hc
117+
```
118+
####User Agents
119+
120+
While the protocol does not require a user-agent to function it is recommended that clients provide a structured user-agent string that provides a basic description of the calling library, version & platform to facilitate issue diagnosis in heterogeneous environments. The following structure is recommended to library developers
121+
```
122+
User-Agent → “grpc-” Language ?(“-” Variant) “/” Version ?( “ (“ *(AdditionalProperty “;”) “)” )
123+
```
124+
E.g.
125+
126+
```
127+
grpc-java/1.2.3
128+
grpc-ruby/1.2.3
129+
grpc-ruby-jruby/1.3.4
130+
grpc-java-android/0.9.1 (gingerbread/1.2.4; nexus5; tmobile)
131+
```
132+
####HTTP2 Transport Mapping
133+
134+
#####Stream Identification
135+
All GRPC calls need to specify an internal ID. We will use HTTP2 stream-ids as call identifiers in this scheme. NOTE: These id’s are contextual to an open HTTP2 session and will not be unique within a given process that is handling more than one HTTP2 session nor can they be used as GUIDs.
136+
137+
#####Data Frames
138+
DATA frame boundaries have no relation to **Delimited-Message** boundaries and implementations should make no assumptions about their alignment.
139+
140+
#####Errors
141+
142+
When an application or runtime error occurs during an RPC a **Status** and **Status-Message** are delivered in **Trailers**.
143+
144+
In some cases it is possible that the framing of the message stream has become corrupt and the RPC runtime will choose to use an **RST_STREAM** frame to indicate this state to its peer. RPC runtime implementations should interpret RST_STREAM as immediate full-closure of the stream and should propagate an error up to the calling application layer.
145+
146+
The following mapping from RST_STREAM error codes to GRPC error codes is applied.
147+
148+
HTTP2 Code|GRPC Code
149+
----------|-----------
150+
NO_ERROR(0)|INTERNAL - An explicit GRPC status of OK should have been sent but this might be used to aggressively lameduck in some scenarios.
151+
PROTOCOL_ERROR(1)|INTERNAL
152+
INTERNAL_ERROR(2)|INTERNAL
153+
FLOW_CONTROL_ERROR(3)|INTERNAL
154+
SETTINGS_TIMEOUT(4)|INTERNAL
155+
STREAM_CLOSED|No mapping as there is no open stream to propagate to. Implementations should log.
156+
FRAME_SIZE_ERROR|INTERNAL
157+
REFUSED_STREAM|UNAVAILABLE - Indicates that no processing occurred and the request can be retried, possibly elsewhere.
158+
CANCEL(8)|Mapped to call cancellation when sent by a client.Mapped to CANCELLED when sent by a server. Note that servers should only use this mechanism when they need to cancel a call but the payload byte sequence is incomplete.
159+
COMPRESSION_ERROR|INTERNAL
160+
CONNECT_ERROR|INTERNAL
161+
ENHANCE_YOUR_CALM|RESOURCE_EXHAUSTED ...with additional error detail provided by runtime to indicate that the exhausted resource is bandwidth.
162+
INADEQUATE_SECURITY| PERMISSION_DENIED … with additional detail indicating that permission was denied as protocol is not secure enough for call.
163+
164+
165+
#####Security
166+
167+
The HTTP2 specification mandates the use of TLS 1.2 or higher when TLS is used with HTTP2. It also places some additional constraints on the allowed ciphers in deployments to avoid known-problems as well as requiring SNI support. It is also expected that HTTP2 will be used in conjunction with proprietary transport security mechanisms about which the specification can make no meaningful recommendations.
168+
169+
#####Connection Management
170+
######GOAWAY Frame
171+
Sent by servers to clients to indicate that they will no longer accept any new streams on the associated connections. This frame includes the id of the last successfully accepted stream by the server. Clients should consider any stream initiated after the last successfully accepted stream as UNAVAILABLE and retry the call elsewhere. Clients are free to continue working with the already accepted streams until they complete or the connection is terminated.
172+
173+
Servers should send GOAWAY before terminating a connection to reliably inform clients which work has been accepted by the server and is being executed.
174+
175+
######PING Frame
176+
Both clients and servers can send a PING frame that the peer must respond to by precisely echoing what they received. This is used to assert that the connection is still live as well as providing a means to estimate end-to-end latency. If a server initiated PING does not receive a response within the deadline expected by the runtime all outstanding calls on the server will be closed with a CANCELLED status. An expired client initiated PING will cause all calls to be closed with an UNAVAILABLE status. Note that the frequency of PINGs is highly dependent on the network environment, implementations are free to adjust PING frequency based on network and application requirements.
177+
178+
######Connection failure
179+
If a detectable connection failure occurs on the client all calls will be closed with an UNAVAILABLE status. For servers open calls will be closed with a CANCELLED status.
180+
181+
182+
### Appendix A - GRPC for Protobuf
183+
184+
The service interfaces declared by protobuf are easily mapped onto GRPC by code generation extensions to protoc. The following defines the mapping to be used
185+
186+
187+
* **Path** → / Service-Name / {_method name_}
188+
* **Service-Name** → ?( {_proto package name_} "." ) {_service name_}
189+
* **Message-Type** → {_fully qualified proto message name_}
190+
* **Content-Type** → "application/grpc+proto"
191+
192+

0 commit comments

Comments
 (0)