mirror of https://github.com/grpc/grpc.git
commit
afb6c98679
840 changed files with 30008 additions and 19880 deletions
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,41 @@ |
||||
# gRPC Versioning Guide |
||||
|
||||
## Versioning Overview |
||||
|
||||
All gRPC implementations use a three-part version number (`vX.Y.Z`) and strictly follow [semantic versioning](https://semver.org/), which defines the semantics of major, minor and patch components of the version number. In addition to that, gRPC versions evolve according to these rules: |
||||
- **Major version bumps** only happen on rare occasions. In order to qualify for a major version bump, certain criteria described later in this document need to be met. Most importantly, a major version increase must not break wire compatibility with other gRPC implementations so that existing gRPC libraries remain fully interoperable. |
||||
- **Minor version bumps** happen approx. every 6 weeks as part of the normal release cycle as defined by the gRPC release process. A new release branch named vMAJOR.MINOR.PATCH) is cut every 6 weeks based on the [release schedule](https://github.com/grpc/grpc/blob/master/doc/grpc_release_schedule.md). |
||||
- **Patch version bump** corresponds to bugfixes done on release branch. |
||||
|
||||
There are also a few extra rules regarding adding new gRPC implementations (e.g. adding support for a new language) |
||||
- New implementations start at v0.x.y version and until they reach 1.0, they are considered not ready for production workloads. Breaking API changes are allowed in the 0.x releases as the library is not considered stable yet. |
||||
- The "1.0" release has semantics of GA (generally available) and being production ready. Requirements to reach this milestone are at least these |
||||
- basic RPC features are feature complete and tested |
||||
- implementation is tested for interoperability with other languages |
||||
- Public API is declared stable |
||||
- Once a gRPC library reaches 1.0 (or higher version), the normal rules for versioning apply. |
||||
|
||||
## Policy for updating the major version number |
||||
|
||||
To avoid user confusion and simplify reasoning, the gRPC releases in different languages try to stay synchronized in terms of major and minor version (all languages follow the same release schedule). Nevertheless, because we also strictly follow semantic versioning, there are circumstances in which a gRPC implementation needs to break the version synchronicity and do a major version bump independently of other languages. |
||||
|
||||
### Situations when it's ok to do a major version bump |
||||
- **change forced by the language ecosystem:** when the language itself or its standard libraries that we depend on make a breaking change (something which is out of our control), reacting with updating gRPC APIs may be the only adequate response. |
||||
- **voluntary change:** Even in non-forced situations, there might be circumstances in which a breaking API change makes sense and represents a net win, but as a rule of thumb breaking changes are very disruptive for users, cause user fragmentation and incur high maintenance costs. Therefore, breaking API changes should be very rare events that need to be considered with extreme care and the bar for accepting such changes is intentionally set very high. |
||||
Example scenarios where a breaking API change might be adequate: |
||||
- fixing a security problem which requires changes to API (need to consider the non-breaking alternatives first) |
||||
- the change leads to very significant gains to security, usability or development velocity. These gains need to be clearly documented and claims need to be supported by evidence (ideally by numbers). Costs to the ecosystem (impact on users, dev team etc.) need to be taken into account and the change still needs to be a net positive after subtracting the costs. |
||||
|
||||
All proposals to make a breaking change need to be documented as a gRFC document (in the grpc/proposal repository) that covers at least these areas: |
||||
- Description of the proposal including an explanation why the proposed change is one of the very rare events where a breaking change is introduced. |
||||
- Migration costs (= what does it mean for the users to migrate to the new API, what are the costs and risks associated with it) |
||||
- Pros of the change (what is gained and how) |
||||
- Cons of the change (e.g. user confusion, lost users and user trust, work needed, added maintenance costs) |
||||
- Plan for supporting users still using the old major version (in case migration to the new major version is not trivial or not everyone can migrate easily) |
||||
|
||||
Note that while major version bump allows changing APIs used by the users, it must not impact the interoperability of the implementation with other gRPC implementations and the previous major version released. That means that **no backward incompatible protocol changes are allowed**: old clients must continue interoperating correctly with new servers and new servers with old clients. |
||||
|
||||
### Situations that DON'T warrant a major version bump |
||||
- Because other languages do so. This is not a good enough reason because |
||||
doing a major version bump has high potential for disturbing and confusing the users of that language and fragmenting the user base and that is a bigger threat than having language implementations at different major version (provided the state is well documented). Having some languages at different major version seems to be unavoidable anyway (due to forced version bumps), unless we bump some languages artificially. |
||||
- "I don't like this API": In retrospect, some API decisions made in the past necessarily turn out more lucky than others, but without strong reasons that would be in favor of changing the API and without enough supporting evidence (see previous section), other strategy than making a breaking API change needs to be used. Possible options: Expand the API to make it useful again; mark API as deprecated while keeping its functionality and providing a new better API. |
@ -0,0 +1,65 @@ |
||||
# Copyright 2019 The gRPC Authors |
||||
# |
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
# |
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
# |
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
|
||||
filegroup( |
||||
name = "_credentials_files", |
||||
testonly = 1, |
||||
srcs = [ |
||||
"credentials/localhost.key", |
||||
"credentials/localhost.crt", |
||||
"credentials/root.crt", |
||||
], |
||||
) |
||||
|
||||
py_library( |
||||
name = "_credentials", |
||||
testonly = 1, |
||||
srcs = ["_credentials.py"], |
||||
data = [":_credentials_files"], |
||||
) |
||||
|
||||
py_binary( |
||||
name = "customized_auth_client", |
||||
testonly = 1, |
||||
srcs = ["customized_auth_client.py"], |
||||
deps = [ |
||||
":_credentials", |
||||
"//src/python/grpcio/grpc:grpcio", |
||||
"//examples:py_helloworld", |
||||
], |
||||
) |
||||
|
||||
py_binary( |
||||
name = "customized_auth_server", |
||||
testonly = 1, |
||||
srcs = ["customized_auth_server.py"], |
||||
deps = [ |
||||
":_credentials", |
||||
"//src/python/grpcio/grpc:grpcio", |
||||
"//examples:py_helloworld", |
||||
|
||||
], |
||||
) |
||||
|
||||
py_test( |
||||
name = "_auth_example_test", |
||||
srcs = ["test/_auth_example_test.py"], |
||||
deps = [ |
||||
"//src/python/grpcio/grpc:grpcio", |
||||
"//examples:py_helloworld", |
||||
":customized_auth_client", |
||||
":customized_auth_server", |
||||
":_credentials", |
||||
], |
||||
) |
@ -0,0 +1,112 @@ |
||||
# Authentication Extension Example in gRPC Python |
||||
|
||||
## Check Our Guide First |
||||
|
||||
For most common usage of authentication in gRPC Python, please see our |
||||
[Authentication](https://grpc.io/docs/guides/auth/) guide's Python section. The |
||||
Guide includes following scenarios: |
||||
|
||||
1. Server SSL credential setup |
||||
2. Client SSL credential setup |
||||
3. Authenticate with Google using a JWT |
||||
4. Authenticate with Google using an Oauth2 token |
||||
|
||||
Also, the guide talks about gRPC specific credential types. |
||||
|
||||
### Channel credentials |
||||
|
||||
Channel credentials are attached to a `Channel` object, the most common use case |
||||
are SSL credentials. |
||||
|
||||
### Call credentials |
||||
|
||||
Call credentials are attached to a `Call` object (corresponding to an RPC). |
||||
Under the hood, the call credentials is a function that takes in information of |
||||
the RPC and modify metadata through callback. |
||||
|
||||
## About This Example |
||||
|
||||
This example focuses on extending gRPC authentication mechanism: |
||||
1) Customize authentication plugin; |
||||
2) Composite client side credentials; |
||||
3) Validation through interceptor on server side. |
||||
|
||||
## AuthMetadataPlugin: Manipulate metadata for each call |
||||
|
||||
Unlike TLS/SSL based authentication, the authentication extension in gRPC Python |
||||
lives at a much higher level of networking. It relies on the transmission of |
||||
metadata (HTTP Header) between client and server, instead of alternating the |
||||
transport protocol. |
||||
|
||||
gRPC Python provides a way to intercept an RPC and append authentication related |
||||
metadata through |
||||
[`AuthMetadataPlugin`](https://grpc.github.io/grpc/python/grpc.html#grpc.AuthMetadataPlugin). |
||||
Those in need of a custom authentication method may simply provide a concrete |
||||
implementation of the following interface: |
||||
|
||||
```Python |
||||
class AuthMetadataPlugin: |
||||
"""A specification for custom authentication.""" |
||||
|
||||
def __call__(self, context, callback): |
||||
"""Implements authentication by passing metadata to a callback. |
||||
|
||||
Implementations of this method must not block. |
||||
|
||||
Args: |
||||
context: An AuthMetadataContext providing information on the RPC that |
||||
the plugin is being called to authenticate. |
||||
callback: An AuthMetadataPluginCallback to be invoked either |
||||
synchronously or asynchronously. |
||||
""" |
||||
``` |
||||
|
||||
Then pass the instance of the concrete implementation to |
||||
`grpc.metadata_call_credentials` function to be converted into a |
||||
`CallCredentials` object. Please NOTE that it is possible to pass a Python |
||||
function object directly, but we recommend to inherit from the base class to |
||||
ensure implementation correctness. |
||||
|
||||
|
||||
```Python |
||||
def metadata_call_credentials(metadata_plugin, name=None): |
||||
"""Construct CallCredentials from an AuthMetadataPlugin. |
||||
|
||||
Args: |
||||
metadata_plugin: An AuthMetadataPlugin to use for authentication. |
||||
name: An optional name for the plugin. |
||||
|
||||
Returns: |
||||
A CallCredentials. |
||||
""" |
||||
``` |
||||
|
||||
The `CallCredentials` object can be passed directly into an RPC like: |
||||
|
||||
```Python |
||||
call_credentials = grpc.metadata_call_credentials(my_foo_plugin) |
||||
stub.FooRpc(request, credentials=call_credentials) |
||||
``` |
||||
|
||||
Or you can use `ChannelCredentials` and `CallCredentials` at the same time by |
||||
combining them: |
||||
|
||||
```Python |
||||
channel_credentials = ... |
||||
call_credentials = ... |
||||
composite_credentials = grpc.composite_channel_credentials( |
||||
channel_credential, |
||||
call_credentials) |
||||
channel = grpc.secure_channel(server_address, composite_credentials) |
||||
``` |
||||
|
||||
It is also possible to apply multiple `CallCredentials` to a single RPC: |
||||
|
||||
```Python |
||||
call_credentials_foo = ... |
||||
call_credentials_bar = ... |
||||
call_credentials = grpc.composite_call_credentials( |
||||
call_credentials_foo, |
||||
call_credentials_bar) |
||||
stub.FooRpc(request, credentials=call_credentials) |
||||
``` |
@ -0,0 +1,31 @@ |
||||
# Copyright 2019 The gRPC Authors |
||||
# |
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
# |
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
# |
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
"""Loading SSL credentials for gRPC Python authentication example.""" |
||||
|
||||
from __future__ import absolute_import |
||||
from __future__ import division |
||||
from __future__ import print_function |
||||
|
||||
import os |
||||
|
||||
|
||||
def _load_credential_from_file(filepath): |
||||
real_path = os.path.join(os.path.dirname(__file__), filepath) |
||||
with open(real_path, 'rb') as f: |
||||
return f.read() |
||||
|
||||
|
||||
SERVER_CERTIFICATE = _load_credential_from_file('credentials/localhost.crt') |
||||
SERVER_CERTIFICATE_KEY = _load_credential_from_file('credentials/localhost.key') |
||||
ROOT_CERTIFICATE = _load_credential_from_file('credentials/root.crt') |
@ -0,0 +1,19 @@ |
||||
-----BEGIN CERTIFICATE----- |
||||
MIIDFjCCAf4CCQCzrLIhrWa55zANBgkqhkiG9w0BAQsFADBCMQswCQYDVQQGEwJV |
||||
UzETMBEGA1UECAwKQ2FsaWZvcm5pYTEPMA0GA1UECgwGR29vZ2xlMQ0wCwYDVQQL |
||||
DARnUlBDMCAXDTE5MDYyNDIyMjIzM1oYDzIxMTkwNTMxMjIyMjMzWjBWMQswCQYD |
||||
VQQGEwJVUzETMBEGA1UECAwKQ2FsaWZvcm5pYTEPMA0GA1UECgwGR29vZ2xlMQ0w |
||||
CwYDVQQLDARnUlBDMRIwEAYDVQQDDAlsb2NhbGhvc3QwggEiMA0GCSqGSIb3DQEB |
||||
AQUAA4IBDwAwggEKAoIBAQCtCW0TjugnIUu8BEVIYvdMP+/2GENQDjZhZ8eKR5C6 |
||||
toDGbgjsDtt/GxISAg4cg70fIvy0XolnGPZodvfHDM4lJ7yHBOdZD8TXQoE6okR7 |
||||
HZuLUJ20M0pXgWqtRewKRUjuYsSDXBnzLiZw1dcv9nGpo+Bqa8NonpiGRRpEkshF |
||||
D6T9KU9Ts/x+wMQBIra2Gj0UMh79jPhUuxcYAQA0JQGivnOtdwuPiumpnUT8j8h6 |
||||
tWg5l01EsCZWJecCF85KnGpJEVYPyPqBqGsy0nGS9plGotOWF87+jyUQt+KD63xA |
||||
aBmTro86mKDDKEK4JvzjVeMGz2UbVcLPiiZnErTFaiXJAgMBAAEwDQYJKoZIhvcN |
||||
AQELBQADggEBAKsDgOPCWp5WCy17vJbRlgfgk05sVNIHZtzrmdswjBmvSg8MUpep |
||||
XqcPNUpsljAXsf9UM5IFEMRdilUsFGWvHjBEtNAW8WUK9UV18WRuU//0w1Mp5HAN |
||||
xUEKb4BoyZr65vlCnTR+AR5c9FfPvLibhr5qHs2RA8Y3GyLOcGqBWed87jhdQLCc |
||||
P1bxB+96le5JeXq0tw215lxonI2/3ZYVK4/ok9gwXrQoWm8YieJqitk/ZQ4S17/4 |
||||
pynHtDfdxLn23EXeGx+UTxJGfpRmhEZdJ+MN7QGYoomzx5qS5XoYKxRNrDlirJpr |
||||
OqXIn8E1it+6d5gOZfuHawcNGhRLplE/pfA= |
||||
-----END CERTIFICATE----- |
@ -0,0 +1,27 @@ |
||||
-----BEGIN RSA PRIVATE KEY----- |
||||
MIIEogIBAAKCAQEArQltE47oJyFLvARFSGL3TD/v9hhDUA42YWfHikeQuraAxm4I |
||||
7A7bfxsSEgIOHIO9HyL8tF6JZxj2aHb3xwzOJSe8hwTnWQ/E10KBOqJEex2bi1Cd |
||||
tDNKV4FqrUXsCkVI7mLEg1wZ8y4mcNXXL/ZxqaPgamvDaJ6YhkUaRJLIRQ+k/SlP |
||||
U7P8fsDEASK2tho9FDIe/Yz4VLsXGAEANCUBor5zrXcLj4rpqZ1E/I/IerVoOZdN |
||||
RLAmViXnAhfOSpxqSRFWD8j6gahrMtJxkvaZRqLTlhfO/o8lELfig+t8QGgZk66P |
||||
OpigwyhCuCb841XjBs9lG1XCz4omZxK0xWolyQIDAQABAoIBADeq/Kh6JT3RfGf0 |
||||
h8WN8TlaqHxnueAbcmtL0+oss+cdp7gu1jf7X6o4r0uT1a5ew40s2Fe+wj2kzkE1 |
||||
ZOlouTlC22gkr7j7Vbxa7PBMG/Pvxoa/XL0IczZLsGImSJXVTG1E4SvRiZeulTdf |
||||
1GbdxhtpWV1jZe5Wd4Na3+SHxF5S7m3PrHiZlYdz1ND+8XZs1NlL9+ej72qSFul9 |
||||
t/QjMWJ9pky/Wad5abnRLRyOsg+BsgnXbkUy2rD89ZxFMLda9pzXo3TPyAlBHonr |
||||
mkEsE4eRMWMpjBM79JbeyDdHn/cs/LjAZrzeDf7ugXr2CHQpKaM5O0PsNHezJII9 |
||||
L5kCfzECgYEA4M/rz1UP1/BJoSqigUlSs0tPAg8a5UlkVsh6Osuq72IPNo8qg/Fw |
||||
oV/IiIS+q+obRcFj1Od3PGdTpCJwW5dzd2fXBQGmGdj0HucnCrs13RtBh91JiF5i |
||||
y/YYI9KfgOG2ZT9gG68T0gTs6jRrS3Qd83npqjrkJqMOd7s00MK9tUcCgYEAxQq7 |
||||
T541oCYHSBRIIb0IrR25krZy9caxzCqPDwOcuuhaCqCiaq+ATvOWlSfgecm4eH0K |
||||
PCH0xlWxG0auPEwm4pA8+/WR/XJwscPZMuoht1EoKy1his4eKx/s7hHNeO6KOF0V |
||||
Y/zqIiuZnEwUoKbn7EqqNFSTT65PJKyGsICJFG8CgYAfaw9yl1myfQNdQb8aQGwN |
||||
YJ33FLNWje427qeeZe5KrDKiFloDvI9YDjHRWnPnRL1w/zj7fSm9yFb5HlMDieP6 |
||||
MQnsyjEzdY2QcA+VwVoiv3dmDHgFVeOKy6bOAtaFxYWfGr9MvygO9t9BT/gawGyb |
||||
JVORlc9i0vDnrMMR1dV7awKBgBpTWLtGc/u1mPt0Wj7HtsUKV6TWY32a0l5owTxM |
||||
S0BdksogtBJ06DukJ9Y9wawD23WdnyRxlPZ6tHLkeprrwbY7dypioOKvy4a0l+xJ |
||||
g7+uRCOgqIuXBkjUtx8HmeAyXp0xMo5tWArAsIFFWOwt4IadYygitJvMuh44PraO |
||||
NcJZAoGADEiV0dheXUCVr8DrtSom8DQMj92/G/FIYjXL8OUhh0+F+YlYP0+F8PEU |
||||
yYIWEqL/S5tVKYshimUXQa537JcRKsTVJBG/ZKD2kuqgOc72zQy3oplimXeJDCXY |
||||
h2eAQ0u8GN6tN9C4t8Kp4a3y6FGsxgu+UTxdnL3YQ+yHAVhtCzo= |
||||
-----END RSA PRIVATE KEY----- |
@ -0,0 +1,20 @@ |
||||
-----BEGIN CERTIFICATE----- |
||||
MIIDWTCCAkGgAwIBAgIJAPOConZMwykwMA0GCSqGSIb3DQEBCwUAMEIxCzAJBgNV |
||||
BAYTAlVTMRMwEQYDVQQIDApDYWxpZm9ybmlhMQ8wDQYDVQQKDAZHb29nbGUxDTAL |
||||
BgNVBAsMBGdSUEMwIBcNMTkwNjI0MjIyMDA3WhgPMjExOTA1MzEyMjIwMDdaMEIx |
||||
CzAJBgNVBAYTAlVTMRMwEQYDVQQIDApDYWxpZm9ybmlhMQ8wDQYDVQQKDAZHb29n |
||||
bGUxDTALBgNVBAsMBGdSUEMwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIB |
||||
AQCwqei3TfyLidnQNDJ2lierMYo229K92DuORni7nSjJQ59Jc3dNMsmqGQJjCD8o |
||||
6mTlKM/oCbs27Wpx+OxcOLvT95j2kiDGca1fCvaMdguIod09SWiyMpv/hp0trLv7 |
||||
NJIKHznath6rHYX2Ii3fZ1yCPzyQbEPSAA+GNpoNm1v1ZWmWKke9v7vLlS3inNlW |
||||
Mt9jepK7DrtbNZnVDjeItnppBSbVYRMxIyNHkepFbqXx5TpkCvl4M4XQZw9bfSxQ |
||||
i3WZ3q+T1Tw//OUdPNc+OfMhu0MA0QoMwikskP0NaIC3dbJZ5Ogx0RcnaB4E+9C6 |
||||
O/znUEh3WuKVl5HXBF+UwWoFAgMBAAGjUDBOMB0GA1UdDgQWBBRm3JIgzgK4G97J |
||||
fbMGatWMZc7V3jAfBgNVHSMEGDAWgBRm3JIgzgK4G97JfbMGatWMZc7V3jAMBgNV |
||||
HRMEBTADAQH/MA0GCSqGSIb3DQEBCwUAA4IBAQCNiV8x41if094ry2srS0YucpiN |
||||
3rTPk08FOLsENTMYai524TGXJti1P6ofGr5KXCL0uxTByHE3fEiMMud2TIY5iHQo |
||||
Y4mzDTTcb+Q7yKHwYZMlcp6nO8W+NeY5t+S0JPHhb8deKWepcN2UpXBUYQLw7AiE |
||||
l96T9Gi+vC9h/XE5IVwHFQXTxf5UYzXtW1nfapvrOONg/ms41dgmrRKIi+knWfiJ |
||||
FdHpHX2sfDAoJtnpEISX+nxRGNVTLY64utXWm4yxaZJshvy2s8zWJgRg7rtwAhTT |
||||
Np9E9MnihXLEmDI4Co9XlLPJyZFmqImsbmVuKFeQOCiLAoPJaMI2lbi7fiTo |
||||
-----END CERTIFICATE----- |
@ -0,0 +1,105 @@ |
||||
# Copyright 2019 The gRPC Authors |
||||
# |
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
# |
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
# |
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
"""Client of the Python example of customizing authentication mechanism.""" |
||||
|
||||
from __future__ import absolute_import |
||||
from __future__ import division |
||||
from __future__ import print_function |
||||
|
||||
import argparse |
||||
import contextlib |
||||
import logging |
||||
|
||||
import grpc |
||||
from examples import helloworld_pb2 |
||||
from examples import helloworld_pb2_grpc |
||||
from examples.python.auth import _credentials |
||||
|
||||
_LOGGER = logging.getLogger(__name__) |
||||
_LOGGER.setLevel(logging.INFO) |
||||
|
||||
_ONE_DAY_IN_SECONDS = 60 * 60 * 24 |
||||
|
||||
_SERVER_ADDR_TEMPLATE = 'localhost:%d' |
||||
_SIGNATURE_HEADER_KEY = 'x-signature' |
||||
|
||||
|
||||
class AuthGateway(grpc.AuthMetadataPlugin): |
||||
|
||||
def __call__(self, context, callback): |
||||
"""Implements authentication by passing metadata to a callback. |
||||
|
||||
Implementations of this method must not block. |
||||
|
||||
Args: |
||||
context: An AuthMetadataContext providing information on the RPC that |
||||
the plugin is being called to authenticate. |
||||
callback: An AuthMetadataPluginCallback to be invoked either |
||||
synchronously or asynchronously. |
||||
""" |
||||
# Example AuthMetadataContext object: |
||||
# AuthMetadataContext( |
||||
# service_url=u'https://localhost:50051/helloworld.Greeter', |
||||
# method_name=u'SayHello') |
||||
signature = context.method_name[::-1] |
||||
callback(((_SIGNATURE_HEADER_KEY, signature),), None) |
||||
|
||||
|
||||
@contextlib.contextmanager |
||||
def create_client_channel(addr): |
||||
# Call credential object will be invoked for every single RPC |
||||
call_credentials = grpc.metadata_call_credentials( |
||||
AuthGateway(), name='auth gateway') |
||||
# Channel credential will be valid for the entire channel |
||||
channel_credential = grpc.ssl_channel_credentials( |
||||
_credentials.ROOT_CERTIFICATE) |
||||
# Combining channel credentials and call credentials together |
||||
composite_credentials = grpc.composite_channel_credentials( |
||||
channel_credential, |
||||
call_credentials, |
||||
) |
||||
channel = grpc.secure_channel(addr, composite_credentials) |
||||
yield channel |
||||
|
||||
|
||||
def send_rpc(channel): |
||||
stub = helloworld_pb2_grpc.GreeterStub(channel) |
||||
request = helloworld_pb2.HelloRequest(name='you') |
||||
try: |
||||
response = stub.SayHello(request) |
||||
except grpc.RpcError as rpc_error: |
||||
_LOGGER.error('Received error: %s', rpc_error) |
||||
return rpc_error |
||||
else: |
||||
_LOGGER.info('Received message: %s', response) |
||||
return response |
||||
|
||||
|
||||
def main(): |
||||
parser = argparse.ArgumentParser() |
||||
parser.add_argument( |
||||
'--port', |
||||
nargs='?', |
||||
type=int, |
||||
default=50051, |
||||
help='the address of server') |
||||
args = parser.parse_args() |
||||
|
||||
with create_client_channel(_SERVER_ADDR_TEMPLATE % args.port) as channel: |
||||
send_rpc(channel) |
||||
|
||||
|
||||
if __name__ == '__main__': |
||||
logging.basicConfig(level=logging.INFO) |
||||
main() |
@ -0,0 +1,110 @@ |
||||
# Copyright 2019 The gRPC Authors |
||||
# |
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
# |
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
# |
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
"""Server of the Python example of customizing authentication mechanism.""" |
||||
|
||||
from __future__ import absolute_import |
||||
from __future__ import division |
||||
from __future__ import print_function |
||||
|
||||
import argparse |
||||
import contextlib |
||||
import logging |
||||
import time |
||||
from concurrent import futures |
||||
|
||||
import grpc |
||||
from examples import helloworld_pb2 |
||||
from examples import helloworld_pb2_grpc |
||||
from examples.python.auth import _credentials |
||||
|
||||
_LOGGER = logging.getLogger(__name__) |
||||
_LOGGER.setLevel(logging.INFO) |
||||
|
||||
_ONE_DAY_IN_SECONDS = 60 * 60 * 24 |
||||
|
||||
_LISTEN_ADDRESS_TEMPLATE = 'localhost:%d' |
||||
_SIGNATURE_HEADER_KEY = 'x-signature' |
||||
|
||||
|
||||
class SignatureValidationInterceptor(grpc.ServerInterceptor): |
||||
|
||||
def __init__(self): |
||||
|
||||
def abort(ignored_request, context): |
||||
context.abort(grpc.StatusCode.UNAUTHENTICATED, 'Invalid signature') |
||||
|
||||
self._abortion = grpc.unary_unary_rpc_method_handler(abort) |
||||
|
||||
def intercept_service(self, continuation, handler_call_details): |
||||
# Example HandlerCallDetails object: |
||||
# _HandlerCallDetails( |
||||
# method=u'/helloworld.Greeter/SayHello', |
||||
# invocation_metadata=...) |
||||
method_name = handler_call_details.method.split('/')[-1] |
||||
expected_metadata = (_SIGNATURE_HEADER_KEY, method_name[::-1]) |
||||
if expected_metadata in handler_call_details.invocation_metadata: |
||||
return continuation(handler_call_details) |
||||
else: |
||||
return self._abortion |
||||
|
||||
|
||||
class SimpleGreeter(helloworld_pb2_grpc.GreeterServicer): |
||||
|
||||
def SayHello(self, request, unused_context): |
||||
return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name) |
||||
|
||||
|
||||
@contextlib.contextmanager |
||||
def run_server(port): |
||||
# Bind interceptor to server |
||||
server = grpc.server( |
||||
futures.ThreadPoolExecutor(), |
||||
interceptors=(SignatureValidationInterceptor(),)) |
||||
helloworld_pb2_grpc.add_GreeterServicer_to_server(SimpleGreeter(), server) |
||||
|
||||
# Loading credentials |
||||
server_credentials = grpc.ssl_server_credentials((( |
||||
_credentials.SERVER_CERTIFICATE_KEY, |
||||
_credentials.SERVER_CERTIFICATE, |
||||
),)) |
||||
|
||||
# Pass down credentials |
||||
port = server.add_secure_port(_LISTEN_ADDRESS_TEMPLATE % port, |
||||
server_credentials) |
||||
|
||||
server.start() |
||||
try: |
||||
yield port |
||||
finally: |
||||
server.stop(0) |
||||
|
||||
|
||||
def main(): |
||||
parser = argparse.ArgumentParser() |
||||
parser.add_argument( |
||||
'--port', nargs='?', type=int, default=50051, help='the listening port') |
||||
args = parser.parse_args() |
||||
|
||||
with run_server(args.port) as port: |
||||
logging.info('Server is listening at port :%d', port) |
||||
try: |
||||
while True: |
||||
time.sleep(_ONE_DAY_IN_SECONDS) |
||||
except KeyboardInterrupt: |
||||
pass |
||||
|
||||
|
||||
if __name__ == '__main__': |
||||
logging.basicConfig(level=logging.INFO) |
||||
main() |
@ -0,0 +1,56 @@ |
||||
# Copyright 2019 The gRPC Authors |
||||
# |
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
# |
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
# |
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
"""Test for gRPC Python authentication example.""" |
||||
|
||||
from __future__ import absolute_import |
||||
from __future__ import division |
||||
from __future__ import print_function |
||||
|
||||
import unittest |
||||
|
||||
import grpc |
||||
from examples.python.auth import _credentials |
||||
from examples.python.auth import customized_auth_client |
||||
from examples.python.auth import customized_auth_server |
||||
|
||||
_SERVER_ADDR_TEMPLATE = 'localhost:%d' |
||||
|
||||
|
||||
class AuthExampleTest(unittest.TestCase): |
||||
|
||||
def test_successful_call(self): |
||||
with customized_auth_server.run_server(0) as port: |
||||
with customized_auth_client.create_client_channel( |
||||
_SERVER_ADDR_TEMPLATE % port) as channel: |
||||
customized_auth_client.send_rpc(channel) |
||||
# No unhandled exception raised, test passed! |
||||
|
||||
def test_no_channel_credential(self): |
||||
with customized_auth_server.run_server(0) as port: |
||||
with grpc.insecure_channel(_SERVER_ADDR_TEMPLATE % port) as channel: |
||||
resp = customized_auth_client.send_rpc(channel) |
||||
self.assertEqual(resp.code(), grpc.StatusCode.UNAVAILABLE) |
||||
|
||||
def test_no_call_credential(self): |
||||
with customized_auth_server.run_server(0) as port: |
||||
channel_credential = grpc.ssl_channel_credentials( |
||||
_credentials.ROOT_CERTIFICATE) |
||||
with grpc.secure_channel(_SERVER_ADDR_TEMPLATE % port, |
||||
channel_credential) as channel: |
||||
resp = customized_auth_client.send_rpc(channel) |
||||
self.assertEqual(resp.code(), grpc.StatusCode.UNAUTHENTICATED) |
||||
|
||||
|
||||
if __name__ == '__main__': |
||||
unittest.main(verbosity=2) |
@ -0,0 +1,44 @@ |
||||
# Copyright 2019 The gRPC Authors |
||||
# |
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
# |
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
# |
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
|
||||
py_binary( |
||||
name = "server", |
||||
srcs = ["server.py"], |
||||
deps = [ |
||||
"//src/python/grpcio/grpc:grpcio", |
||||
"//examples:py_helloworld", |
||||
], |
||||
srcs_version = "PY2AND3", |
||||
) |
||||
|
||||
py_binary( |
||||
name = "client", |
||||
srcs = ["client.py"], |
||||
deps = [ |
||||
"//src/python/grpcio/grpc:grpcio", |
||||
"//examples:py_helloworld", |
||||
], |
||||
srcs_version = "PY2AND3", |
||||
) |
||||
|
||||
py_test( |
||||
name = "test/compression_example_test", |
||||
srcs = ["test/compression_example_test.py"], |
||||
srcs_version = "PY2AND3", |
||||
data = [ |
||||
":client", |
||||
":server", |
||||
], |
||||
size = "small", |
||||
) |
@ -0,0 +1,58 @@ |
||||
## Compression with gRPC Python |
||||
|
||||
gRPC offers lossless compression options in order to decrease the number of bits |
||||
transferred over the wire. Three levels of compression are available: |
||||
|
||||
- `grpc.Compression.NoCompression` - No compression is applied to the payload. (default) |
||||
- `grpc.Compression.Deflate` - The "Deflate" algorithm is applied to the payload. |
||||
- `grpc.Compression.Gzip` - The Gzip algorithm is applied to the payload. |
||||
|
||||
The default option on both clients and servers is `grpc.Compression.NoCompression`. |
||||
|
||||
See [the gRPC Compression Spec](https://github.com/grpc/grpc/blob/master/doc/compression.md) |
||||
for more information. |
||||
|
||||
### Client Side Compression |
||||
|
||||
Compression may be set at two levels on the client side. |
||||
|
||||
#### At the channel level |
||||
|
||||
```python |
||||
with grpc.insecure_channel('foo.bar:1234', compression=grpc.Compression.Gzip) as channel: |
||||
use_channel(channel) |
||||
``` |
||||
|
||||
#### At the call level |
||||
|
||||
Setting the compression method at the call level will override any settings on |
||||
the channel level. |
||||
|
||||
```python |
||||
stub = helloworld_pb2_grpc.GreeterStub(channel) |
||||
response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'), |
||||
compression=grpc.Compression.Deflate) |
||||
``` |
||||
|
||||
|
||||
### Server Side Compression |
||||
|
||||
Additionally, compression may be set at two levels on the server side. |
||||
|
||||
#### On the entire server |
||||
|
||||
```python |
||||
server = grpc.server(futures.ThreadPoolExecutor(), |
||||
compression=grpc.Compression.Gzip) |
||||
``` |
||||
|
||||
#### For an individual RPC |
||||
|
||||
```python |
||||
def SayHello(self, request, context): |
||||
context.set_response_compression(grpc.Compression.NoCompression) |
||||
return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name) |
||||
``` |
||||
|
||||
Setting the compression method for an individual RPC will override any setting |
||||
supplied at server creation time. |
@ -0,0 +1,76 @@ |
||||
# Copyright 2019 the gRPC authors. |
||||
# |
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
# |
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
# |
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
"""An example of compression on the client side with gRPC.""" |
||||
|
||||
from __future__ import absolute_import |
||||
from __future__ import division |
||||
from __future__ import print_function |
||||
|
||||
import argparse |
||||
import logging |
||||
import grpc |
||||
|
||||
from examples import helloworld_pb2 |
||||
from examples import helloworld_pb2_grpc |
||||
|
||||
_DESCRIPTION = 'A client capable of compression.' |
||||
_COMPRESSION_OPTIONS = { |
||||
"none": grpc.Compression.NoCompression, |
||||
"deflate": grpc.Compression.Deflate, |
||||
"gzip": grpc.Compression.Gzip, |
||||
} |
||||
|
||||
_LOGGER = logging.getLogger(__name__) |
||||
|
||||
|
||||
def run_client(channel_compression, call_compression, target): |
||||
with grpc.insecure_channel( |
||||
target, compression=channel_compression) as channel: |
||||
stub = helloworld_pb2_grpc.GreeterStub(channel) |
||||
response = stub.SayHello( |
||||
helloworld_pb2.HelloRequest(name='you'), |
||||
compression=call_compression, |
||||
wait_for_ready=True) |
||||
print("Response: {}".format(response)) |
||||
|
||||
|
||||
def main(): |
||||
parser = argparse.ArgumentParser(description=_DESCRIPTION) |
||||
parser.add_argument( |
||||
'--channel_compression', |
||||
default='none', |
||||
nargs='?', |
||||
choices=_COMPRESSION_OPTIONS.keys(), |
||||
help='The compression method to use for the channel.') |
||||
parser.add_argument( |
||||
'--call_compression', |
||||
default='none', |
||||
nargs='?', |
||||
choices=_COMPRESSION_OPTIONS.keys(), |
||||
help='The compression method to use for an individual call.') |
||||
parser.add_argument( |
||||
'--server', |
||||
default='localhost:50051', |
||||
type=str, |
||||
nargs='?', |
||||
help='The host-port pair at which to reach the server.') |
||||
args = parser.parse_args() |
||||
channel_compression = _COMPRESSION_OPTIONS[args.channel_compression] |
||||
call_compression = _COMPRESSION_OPTIONS[args.call_compression] |
||||
run_client(channel_compression, call_compression, args.server) |
||||
|
||||
|
||||
if __name__ == "__main__": |
||||
logging.basicConfig() |
||||
main() |
@ -0,0 +1,109 @@ |
||||
# Copyright 2019 the gRPC authors. |
||||
# |
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
# |
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
# |
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
"""An example of compression on the server side with gRPC.""" |
||||
|
||||
from __future__ import absolute_import |
||||
from __future__ import division |
||||
from __future__ import print_function |
||||
|
||||
from concurrent import futures |
||||
import argparse |
||||
import logging |
||||
import threading |
||||
import time |
||||
import grpc |
||||
|
||||
from examples import helloworld_pb2 |
||||
from examples import helloworld_pb2_grpc |
||||
|
||||
_DESCRIPTION = 'A server capable of compression.' |
||||
_COMPRESSION_OPTIONS = { |
||||
"none": grpc.Compression.NoCompression, |
||||
"deflate": grpc.Compression.Deflate, |
||||
"gzip": grpc.Compression.Gzip, |
||||
} |
||||
_LOGGER = logging.getLogger(__name__) |
||||
|
||||
_SERVER_HOST = 'localhost' |
||||
_ONE_DAY_IN_SECONDS = 60 * 60 * 24 |
||||
|
||||
|
||||
class Greeter(helloworld_pb2_grpc.GreeterServicer): |
||||
|
||||
def __init__(self, no_compress_every_n): |
||||
super(Greeter, self).__init__() |
||||
self._no_compress_every_n = 0 |
||||
self._request_counter = 0 |
||||
self._counter_lock = threading.RLock() |
||||
|
||||
def _should_suppress_compression(self): |
||||
suppress_compression = False |
||||
with self._counter_lock: |
||||
if self._no_compress_every_n and self._request_counter % self._no_compress_every_n == 0: |
||||
suppress_compression = True |
||||
self._request_counter += 1 |
||||
return suppress_compression |
||||
|
||||
def SayHello(self, request, context): |
||||
if self._should_suppress_compression(): |
||||
context.set_response_compression(grpc.Compression.NoCompression) |
||||
return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name) |
||||
|
||||
|
||||
def run_server(server_compression, no_compress_every_n, port): |
||||
server = grpc.server( |
||||
futures.ThreadPoolExecutor(), |
||||
compression=server_compression, |
||||
options=(('grpc.so_reuseport', 1),)) |
||||
helloworld_pb2_grpc.add_GreeterServicer_to_server( |
||||
Greeter(no_compress_every_n), server) |
||||
address = '{}:{}'.format(_SERVER_HOST, port) |
||||
server.add_insecure_port(address) |
||||
server.start() |
||||
print("Server listening at '{}'".format(address)) |
||||
try: |
||||
while True: |
||||
time.sleep(_ONE_DAY_IN_SECONDS) |
||||
except KeyboardInterrupt: |
||||
server.stop(None) |
||||
|
||||
|
||||
def main(): |
||||
parser = argparse.ArgumentParser(description=_DESCRIPTION) |
||||
parser.add_argument( |
||||
'--server_compression', |
||||
default='none', |
||||
nargs='?', |
||||
choices=_COMPRESSION_OPTIONS.keys(), |
||||
help='The default compression method for the server.') |
||||
parser.add_argument( |
||||
'--no_compress_every_n', |
||||
type=int, |
||||
default=0, |
||||
nargs='?', |
||||
help='If set, every nth reply will be uncompressed.') |
||||
parser.add_argument( |
||||
'--port', |
||||
type=int, |
||||
default=50051, |
||||
nargs='?', |
||||
help='The port on which the server will listen.') |
||||
args = parser.parse_args() |
||||
run_server(_COMPRESSION_OPTIONS[args.server_compression], |
||||
args.no_compress_every_n, args.port) |
||||
|
||||
|
||||
if __name__ == "__main__": |
||||
logging.basicConfig() |
||||
main() |
@ -0,0 +1,62 @@ |
||||
# Copyright 2019 the gRPC authors. |
||||
# |
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
# |
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
# |
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
"""Test for compression example.""" |
||||
|
||||
import contextlib |
||||
import os |
||||
import socket |
||||
import subprocess |
||||
import unittest |
||||
|
||||
_BINARY_DIR = os.path.realpath( |
||||
os.path.join(os.path.dirname(os.path.abspath(__file__)), '..')) |
||||
_SERVER_PATH = os.path.join(_BINARY_DIR, 'server') |
||||
_CLIENT_PATH = os.path.join(_BINARY_DIR, 'client') |
||||
|
||||
|
||||
@contextlib.contextmanager |
||||
def _get_port(): |
||||
sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) |
||||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) |
||||
if sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT) == 0: |
||||
raise RuntimeError("Failed to set SO_REUSEPORT.") |
||||
sock.bind(('', 0)) |
||||
try: |
||||
yield sock.getsockname()[1] |
||||
finally: |
||||
sock.close() |
||||
|
||||
|
||||
class CompressionExampleTest(unittest.TestCase): |
||||
|
||||
def test_compression_example(self): |
||||
with _get_port() as test_port: |
||||
server_process = subprocess.Popen((_SERVER_PATH, '--port', |
||||
str(test_port), |
||||
'--server_compression', 'gzip')) |
||||
try: |
||||
server_target = 'localhost:{}'.format(test_port) |
||||
client_process = subprocess.Popen( |
||||
(_CLIENT_PATH, '--server', server_target, |
||||
'--channel_compression', 'gzip')) |
||||
client_return_code = client_process.wait() |
||||
self.assertEqual(0, client_return_code) |
||||
self.assertIsNone(server_process.poll()) |
||||
finally: |
||||
server_process.kill() |
||||
server_process.wait() |
||||
|
||||
|
||||
if __name__ == '__main__': |
||||
unittest.main(verbosity=2) |
@ -0,0 +1,59 @@ |
||||
# Copyright 2019 The gRPC Authors |
||||
# |
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
# |
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
# |
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
|
||||
load("@grpc_python_dependencies//:requirements.bzl", "requirement") |
||||
|
||||
py_binary( |
||||
name = "debug_server", |
||||
testonly = 1, |
||||
srcs = ["debug_server.py"], |
||||
deps = [ |
||||
"//src/python/grpcio/grpc:grpcio", |
||||
"//src/python/grpcio_channelz/grpc_channelz/v1:grpc_channelz", |
||||
"//examples:py_helloworld", |
||||
], |
||||
) |
||||
|
||||
py_binary( |
||||
name = "send_message", |
||||
testonly = 1, |
||||
srcs = ["send_message.py"], |
||||
deps = [ |
||||
"//src/python/grpcio/grpc:grpcio", |
||||
"//examples:py_helloworld", |
||||
], |
||||
) |
||||
|
||||
py_binary( |
||||
name = "get_stats", |
||||
testonly = 1, |
||||
srcs = ["get_stats.py"], |
||||
deps = [ |
||||
"//src/python/grpcio/grpc:grpcio", |
||||
"//src/python/grpcio_channelz/grpc_channelz/v1:grpc_channelz", |
||||
], |
||||
) |
||||
|
||||
py_test( |
||||
name = "_debug_example_test", |
||||
srcs = ["test/_debug_example_test.py"], |
||||
deps = [ |
||||
"//src/python/grpcio/grpc:grpcio", |
||||
"//src/python/grpcio_channelz/grpc_channelz/v1:grpc_channelz", |
||||
"//examples:py_helloworld", |
||||
":debug_server", |
||||
":send_message", |
||||
":get_stats", |
||||
], |
||||
) |
@ -0,0 +1,68 @@ |
||||
# gRPC Python Debug Example |
||||
|
||||
This example demonstrate the usage of Channelz. For a better looking website, |
||||
the [gdebug](https://github.com/grpc/grpc-experiments/tree/master/gdebug) uses |
||||
gRPC-Web protocol and will serve all useful information in web pages. |
||||
|
||||
## Channelz: Live Channel Tracing |
||||
|
||||
Channelz is a channel tracing feature. It will track statistics like how many |
||||
messages have been sent, how many of them failed, what are the connected |
||||
sockets. Since it is implemented in C-Core and has low-overhead, it is |
||||
recommended to turn on for production services. See [Channelz design |
||||
doc](https://github.com/grpc/proposal/blob/master/A14-channelz.md). |
||||
|
||||
## How to enable tracing log |
||||
The tracing log generation might have larger overhead, especially when you try |
||||
to trace transport. It would result in replicating the traffic loads. However, |
||||
it is still the most powerful tool when you need to dive in. |
||||
|
||||
### The Most Verbose Tracing Log |
||||
|
||||
Specify environment variables, then run your application: |
||||
|
||||
``` |
||||
GRPC_VERBOSITY=debug |
||||
GRPC_TRACE=all |
||||
``` |
||||
|
||||
For more granularity, please see |
||||
[environment_variables](https://github.com/grpc/grpc/blob/master/doc/environment_variables.md). |
||||
|
||||
### Debug Transport Protocol |
||||
|
||||
``` |
||||
GRPC_VERBOSITY=debug |
||||
GRPC_TRACE=tcp,http,secure_endpoint,transport_security |
||||
``` |
||||
|
||||
### Debug Connection Behavior |
||||
|
||||
``` |
||||
GRPC_VERBOSITY=debug |
||||
GRPC_TRACE=call_error,connectivity_state,pick_first,round_robin,glb |
||||
``` |
||||
|
||||
## How to debug your application? |
||||
|
||||
`pdb` is a debugging tool that is available for Python interpreters natively. |
||||
You can set breakpoint, and execute commands while the application is stopped. |
||||
|
||||
The simplest usage is add a single line in the place you want to inspect: |
||||
`import pdb; pdb.set_trace()`. When interpreter see this line, it would pop out |
||||
a interactive command line interface for you to inspect the application state. |
||||
|
||||
For more detailed usage, see https://docs.python.org/3/library/pdb.html. |
||||
|
||||
**Caveat**: gRPC Python uses C-Extension under-the-hood, so `pdb` may not be |
||||
able to trace through the whole stack. |
||||
|
||||
## gRPC Command Line Tool |
||||
|
||||
`grpc_cli` is a handy tool to interact with gRPC backend easily. Imageine you can |
||||
inspect what service does a server provide without writing any code, and make |
||||
gRPC calls just like `curl`. |
||||
|
||||
The installation guide: https://github.com/grpc/grpc/blob/master/doc/command_line_tool.md#code-location |
||||
The usage guide: https://github.com/grpc/grpc/blob/master/doc/command_line_tool.md#usage |
||||
The source code: https://github.com/grpc/grpc/blob/master/test/cpp/util/grpc_cli.cc |
@ -0,0 +1,90 @@ |
||||
# Copyright 2019 The gRPC Authors |
||||
# |
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
# |
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
# |
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
"""The Python example of utilizing Channelz feature.""" |
||||
|
||||
from __future__ import absolute_import |
||||
from __future__ import division |
||||
from __future__ import print_function |
||||
|
||||
import argparse |
||||
import logging |
||||
import time |
||||
from concurrent import futures |
||||
import random |
||||
|
||||
import grpc |
||||
from grpc_channelz.v1 import channelz |
||||
|
||||
from examples import helloworld_pb2 |
||||
from examples import helloworld_pb2_grpc |
||||
|
||||
_LOGGER = logging.getLogger(__name__) |
||||
_LOGGER.setLevel(logging.INFO) |
||||
|
||||
_ONE_DAY_IN_SECONDS = 60 * 60 * 24 |
||||
_RANDOM_FAILURE_RATE = 0.3 |
||||
|
||||
|
||||
class FaultInjectGreeter(helloworld_pb2_grpc.GreeterServicer): |
||||
|
||||
def __init__(self, failure_rate): |
||||
self._failure_rate = failure_rate |
||||
|
||||
def SayHello(self, request, context): |
||||
if random.random() < self._failure_rate: |
||||
context.abort(grpc.StatusCode.UNAVAILABLE, |
||||
'Randomly injected failure.') |
||||
return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name) |
||||
|
||||
|
||||
def create_server(addr, failure_rate): |
||||
server = grpc.server(futures.ThreadPoolExecutor()) |
||||
helloworld_pb2_grpc.add_GreeterServicer_to_server( |
||||
FaultInjectGreeter(failure_rate), server) |
||||
|
||||
# Add Channelz Servicer to the gRPC server |
||||
channelz.add_channelz_servicer(server) |
||||
|
||||
server.add_insecure_port(addr) |
||||
return server |
||||
|
||||
|
||||
def main(): |
||||
parser = argparse.ArgumentParser() |
||||
parser.add_argument( |
||||
'--addr', |
||||
nargs=1, |
||||
type=str, |
||||
default='[::]:50051', |
||||
help='the address to listen on') |
||||
parser.add_argument( |
||||
'--failure_rate', |
||||
nargs=1, |
||||
type=float, |
||||
default=0.3, |
||||
help='a float indicates the percentage of failed message injections') |
||||
args = parser.parse_args() |
||||
|
||||
server = create_server(addr=args.addr, failure_rate=args.failure_rate) |
||||
server.start() |
||||
try: |
||||
while True: |
||||
time.sleep(_ONE_DAY_IN_SECONDS) |
||||
except KeyboardInterrupt: |
||||
server.stop(0) |
||||
|
||||
|
||||
if __name__ == '__main__': |
||||
logging.basicConfig(level=logging.INFO) |
||||
main() |
@ -0,0 +1,49 @@ |
||||
# Copyright 2019 The gRPC Authors |
||||
# |
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
# |
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
# |
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
"""Poll statistics from the server.""" |
||||
|
||||
from __future__ import absolute_import |
||||
from __future__ import division |
||||
from __future__ import print_function |
||||
|
||||
import logging |
||||
import argparse |
||||
import grpc |
||||
from grpc_channelz.v1 import channelz_pb2 |
||||
from grpc_channelz.v1 import channelz_pb2_grpc |
||||
|
||||
|
||||
def run(addr): |
||||
with grpc.insecure_channel(addr) as channel: |
||||
channelz_stub = channelz_pb2_grpc.ChannelzStub(channel) |
||||
response = channelz_stub.GetServers( |
||||
channelz_pb2.GetServersRequest(start_server_id=0)) |
||||
print('Info for all servers: %s' % response) |
||||
|
||||
|
||||
def main(): |
||||
parser = argparse.ArgumentParser() |
||||
parser.add_argument( |
||||
'--addr', |
||||
nargs=1, |
||||
type=str, |
||||
default='[::]:50051', |
||||
help='the address to request') |
||||
args = parser.parse_args() |
||||
run(addr=args.addr) |
||||
|
||||
|
||||
if __name__ == '__main__': |
||||
logging.basicConfig() |
||||
main() |
@ -0,0 +1,64 @@ |
||||
# Copyright 2019 The gRPC Authors |
||||
# |
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
# |
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
# |
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
"""Send multiple greeting messages to the backend.""" |
||||
|
||||
from __future__ import absolute_import |
||||
from __future__ import division |
||||
from __future__ import print_function |
||||
|
||||
import logging |
||||
import argparse |
||||
import grpc |
||||
from examples import helloworld_pb2 |
||||
from examples import helloworld_pb2_grpc |
||||
|
||||
|
||||
def process(stub, request): |
||||
try: |
||||
response = stub.SayHello(request) |
||||
except grpc.RpcError as rpc_error: |
||||
print('Received error: %s' % rpc_error) |
||||
else: |
||||
print('Received message: %s' % response) |
||||
|
||||
|
||||
def run(addr, n): |
||||
with grpc.insecure_channel(addr) as channel: |
||||
stub = helloworld_pb2_grpc.GreeterStub(channel) |
||||
request = helloworld_pb2.HelloRequest(name='you') |
||||
for _ in range(n): |
||||
process(stub, request) |
||||
|
||||
|
||||
def main(): |
||||
parser = argparse.ArgumentParser() |
||||
parser.add_argument( |
||||
'--addr', |
||||
nargs=1, |
||||
type=str, |
||||
default='[::]:50051', |
||||
help='the address to request') |
||||
parser.add_argument( |
||||
'-n', |
||||
nargs=1, |
||||
type=int, |
||||
default=10, |
||||
help='an integer for number of messages to sent') |
||||
args = parser.parse_args() |
||||
run(addr=args.addr, n=args.n) |
||||
|
||||
|
||||
if __name__ == '__main__': |
||||
logging.basicConfig() |
||||
main() |
@ -0,0 +1,53 @@ |
||||
# Copyright 2019 The gRPC Authors |
||||
# |
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
# |
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
# |
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
"""Test for gRPC Python debug example.""" |
||||
|
||||
from __future__ import absolute_import |
||||
from __future__ import division |
||||
from __future__ import print_function |
||||
|
||||
import logging |
||||
import unittest |
||||
|
||||
from examples.python.debug import debug_server |
||||
from examples.python.debug import send_message |
||||
from examples.python.debug import get_stats |
||||
|
||||
_LOGGER = logging.getLogger(__name__) |
||||
_LOGGER.setLevel(logging.INFO) |
||||
|
||||
_FAILURE_RATE = 0.5 |
||||
_NUMBER_OF_MESSAGES = 100 |
||||
|
||||
_ADDR_TEMPLATE = 'localhost:%d' |
||||
|
||||
|
||||
class DebugExampleTest(unittest.TestCase): |
||||
|
||||
def test_channelz_example(self): |
||||
server = debug_server.create_server( |
||||
addr='[::]:0', failure_rate=_FAILURE_RATE) |
||||
port = server.add_insecure_port('[::]:0') |
||||
server.start() |
||||
address = _ADDR_TEMPLATE % port |
||||
|
||||
send_message.run(addr=address, n=_NUMBER_OF_MESSAGES) |
||||
get_stats.run(addr=address) |
||||
server.stop(None) |
||||
# No unhandled exception raised, test passed! |
||||
|
||||
|
||||
if __name__ == '__main__': |
||||
logging.basicConfig() |
||||
unittest.main(verbosity=2) |
@ -0,0 +1,127 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2015 gRPC authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
* |
||||
*/ |
||||
|
||||
#ifndef GRPCPP_CHANNEL_IMPL_H |
||||
#define GRPCPP_CHANNEL_IMPL_H |
||||
|
||||
#include <memory> |
||||
#include <mutex> |
||||
|
||||
#include <grpc/grpc.h> |
||||
#include <grpcpp/impl/call.h> |
||||
#include <grpcpp/impl/codegen/channel_interface.h> |
||||
#include <grpcpp/impl/codegen/client_interceptor.h> |
||||
#include <grpcpp/impl/codegen/completion_queue_impl.h> |
||||
#include <grpcpp/impl/codegen/config.h> |
||||
#include <grpcpp/impl/codegen/grpc_library.h> |
||||
#include <grpcpp/impl/codegen/sync.h> |
||||
|
||||
struct grpc_channel; |
||||
|
||||
namespace grpc { |
||||
|
||||
std::shared_ptr<::grpc_impl::Channel> CreateChannelInternal( |
||||
const grpc::string& host, grpc_channel* c_channel, |
||||
std::vector< |
||||
std::unique_ptr<experimental::ClientInterceptorFactoryInterface>> |
||||
interceptor_creators); |
||||
} // namespace grpc
|
||||
namespace grpc_impl { |
||||
|
||||
namespace experimental { |
||||
/// Resets the channel's connection backoff.
|
||||
/// TODO(roth): Once we see whether this proves useful, either create a gRFC
|
||||
/// and change this to be a method of the Channel class, or remove it.
|
||||
void ChannelResetConnectionBackoff(Channel* channel); |
||||
} // namespace experimental
|
||||
|
||||
/// Channels represent a connection to an endpoint. Created by \a CreateChannel.
|
||||
class Channel final : public ::grpc::ChannelInterface, |
||||
public ::grpc::internal::CallHook, |
||||
public std::enable_shared_from_this<Channel>, |
||||
private ::grpc::GrpcLibraryCodegen { |
||||
public: |
||||
~Channel(); |
||||
|
||||
/// Get the current channel state. If the channel is in IDLE and
|
||||
/// \a try_to_connect is set to true, try to connect.
|
||||
grpc_connectivity_state GetState(bool try_to_connect) override; |
||||
|
||||
/// Returns the LB policy name, or the empty string if not yet available.
|
||||
grpc::string GetLoadBalancingPolicyName() const; |
||||
|
||||
/// Returns the service config in JSON form, or the empty string if
|
||||
/// not available.
|
||||
grpc::string GetServiceConfigJSON() const; |
||||
|
||||
private: |
||||
template <class InputMessage, class OutputMessage> |
||||
friend class ::grpc::internal::BlockingUnaryCallImpl; |
||||
friend void experimental::ChannelResetConnectionBackoff(Channel* channel); |
||||
friend std::shared_ptr<Channel> grpc::CreateChannelInternal( |
||||
const grpc::string& host, grpc_channel* c_channel, |
||||
std::vector<std::unique_ptr< |
||||
::grpc::experimental::ClientInterceptorFactoryInterface>> |
||||
interceptor_creators); |
||||
friend class ::grpc::internal::InterceptedChannel; |
||||
Channel(const grpc::string& host, grpc_channel* c_channel, |
||||
std::vector<std::unique_ptr< |
||||
::grpc::experimental::ClientInterceptorFactoryInterface>> |
||||
interceptor_creators); |
||||
|
||||
::grpc::internal::Call CreateCall(const ::grpc::internal::RpcMethod& method, |
||||
::grpc_impl::ClientContext* context, |
||||
::grpc_impl::CompletionQueue* cq) override; |
||||
void PerformOpsOnCall(::grpc::internal::CallOpSetInterface* ops, |
||||
::grpc::internal::Call* call) override; |
||||
void* RegisterMethod(const char* method) override; |
||||
|
||||
void NotifyOnStateChangeImpl(grpc_connectivity_state last_observed, |
||||
gpr_timespec deadline, |
||||
::grpc_impl::CompletionQueue* cq, |
||||
void* tag) override; |
||||
bool WaitForStateChangeImpl(grpc_connectivity_state last_observed, |
||||
gpr_timespec deadline) override; |
||||
|
||||
::grpc_impl::CompletionQueue* CallbackCQ() override; |
||||
|
||||
::grpc::internal::Call CreateCallInternal( |
||||
const ::grpc::internal::RpcMethod& method, |
||||
::grpc_impl::ClientContext* context, ::grpc_impl::CompletionQueue* cq, |
||||
size_t interceptor_pos) override; |
||||
|
||||
const grpc::string host_; |
||||
grpc_channel* const c_channel_; // owned
|
||||
|
||||
// mu_ protects callback_cq_ (the per-channel callbackable completion queue)
|
||||
grpc::internal::Mutex mu_; |
||||
|
||||
// callback_cq_ references the callbackable completion queue associated
|
||||
// with this channel (if any). It is set on the first call to CallbackCQ().
|
||||
// It is _not owned_ by the channel; ownership belongs with its internal
|
||||
// shutdown callback tag (invoked when the CQ is fully shutdown).
|
||||
::grpc_impl::CompletionQueue* callback_cq_ = nullptr; |
||||
|
||||
std::vector< |
||||
std::unique_ptr<::grpc::experimental::ClientInterceptorFactoryInterface>> |
||||
interceptor_creators_; |
||||
}; |
||||
|
||||
} // namespace grpc_impl
|
||||
|
||||
#endif // GRPCPP_CHANNEL_IMPL_H
|
@ -0,0 +1,24 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2019 gRPC authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
* |
||||
*/ |
||||
|
||||
#ifndef GRPCPP_COMPLETION_QUEUE_IMPL_H |
||||
#define GRPCPP_COMPLETION_QUEUE_IMPL_H |
||||
|
||||
#include <grpcpp/impl/codegen/completion_queue_impl.h> |
||||
|
||||
#endif // GRPCPP_COMPLETION_QUEUE_IMPL_H
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,315 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2015 gRPC authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
* |
||||
*/ |
||||
|
||||
#ifndef GRPCPP_IMPL_CODEGEN_ASYNC_UNARY_CALL_IMPL_H |
||||
#define GRPCPP_IMPL_CODEGEN_ASYNC_UNARY_CALL_IMPL_H |
||||
|
||||
#include <assert.h> |
||||
#include <grpcpp/impl/codegen/call.h> |
||||
#include <grpcpp/impl/codegen/channel_interface.h> |
||||
#include <grpcpp/impl/codegen/client_context_impl.h> |
||||
#include <grpcpp/impl/codegen/server_context_impl.h> |
||||
#include <grpcpp/impl/codegen/service_type.h> |
||||
#include <grpcpp/impl/codegen/status.h> |
||||
|
||||
namespace grpc_impl { |
||||
|
||||
/// An interface relevant for async client side unary RPCs (which send
|
||||
/// one request message to a server and receive one response message).
|
||||
template <class R> |
||||
class ClientAsyncResponseReaderInterface { |
||||
public: |
||||
virtual ~ClientAsyncResponseReaderInterface() {} |
||||
|
||||
/// Start the call that was set up by the constructor, but only if the
|
||||
/// constructor was invoked through the "Prepare" API which doesn't actually
|
||||
/// start the call
|
||||
virtual void StartCall() = 0; |
||||
|
||||
/// Request notification of the reading of initial metadata. Completion
|
||||
/// will be notified by \a tag on the associated completion queue.
|
||||
/// This call is optional, but if it is used, it cannot be used concurrently
|
||||
/// with or after the \a Finish method.
|
||||
///
|
||||
/// \param[in] tag Tag identifying this request.
|
||||
virtual void ReadInitialMetadata(void* tag) = 0; |
||||
|
||||
/// Request to receive the server's response \a msg and final \a status for
|
||||
/// the call, and to notify \a tag on this call's completion queue when
|
||||
/// finished.
|
||||
///
|
||||
/// This function will return when either:
|
||||
/// - when the server's response message and status have been received.
|
||||
/// - when the server has returned a non-OK status (no message expected in
|
||||
/// this case).
|
||||
/// - when the call failed for some reason and the library generated a
|
||||
/// non-OK status.
|
||||
///
|
||||
/// \param[in] tag Tag identifying this request.
|
||||
/// \param[out] status To be updated with the operation status.
|
||||
/// \param[out] msg To be filled in with the server's response message.
|
||||
virtual void Finish(R* msg, ::grpc::Status* status, void* tag) = 0; |
||||
}; |
||||
|
||||
namespace internal { |
||||
template <class R> |
||||
class ClientAsyncResponseReaderFactory { |
||||
public: |
||||
/// Start a call and write the request out if \a start is set.
|
||||
/// \a tag will be notified on \a cq when the call has been started (i.e.
|
||||
/// intitial metadata sent) and \a request has been written out.
|
||||
/// If \a start is not set, the actual call must be initiated by StartCall
|
||||
/// Note that \a context will be used to fill in custom initial metadata
|
||||
/// used to send to the server when starting the call.
|
||||
template <class W> |
||||
static ClientAsyncResponseReader<R>* Create( |
||||
::grpc::ChannelInterface* channel, ::grpc_impl::CompletionQueue* cq, |
||||
const ::grpc::internal::RpcMethod& method, |
||||
::grpc_impl::ClientContext* context, const W& request, bool start) { |
||||
::grpc::internal::Call call = channel->CreateCall(method, context, cq); |
||||
return new (::grpc::g_core_codegen_interface->grpc_call_arena_alloc( |
||||
call.call(), sizeof(ClientAsyncResponseReader<R>))) |
||||
ClientAsyncResponseReader<R>(call, context, request, start); |
||||
} |
||||
}; |
||||
} // namespace internal
|
||||
|
||||
/// Async API for client-side unary RPCs, where the message response
|
||||
/// received from the server is of type \a R.
|
||||
template <class R> |
||||
class ClientAsyncResponseReader final |
||||
: public ClientAsyncResponseReaderInterface<R> { |
||||
public: |
||||
// always allocated against a call arena, no memory free required
|
||||
static void operator delete(void* ptr, std::size_t size) { |
||||
assert(size == sizeof(ClientAsyncResponseReader)); |
||||
} |
||||
|
||||
// This operator should never be called as the memory should be freed as part
|
||||
// of the arena destruction. It only exists to provide a matching operator
|
||||
// delete to the operator new so that some compilers will not complain (see
|
||||
// https://github.com/grpc/grpc/issues/11301) Note at the time of adding this
|
||||
// there are no tests catching the compiler warning.
|
||||
static void operator delete(void*, void*) { assert(0); } |
||||
|
||||
void StartCall() override { |
||||
assert(!started_); |
||||
started_ = true; |
||||
StartCallInternal(); |
||||
} |
||||
|
||||
/// See \a ClientAsyncResponseReaderInterface::ReadInitialMetadata for
|
||||
/// semantics.
|
||||
///
|
||||
/// Side effect:
|
||||
/// - the \a ClientContext associated with this call is updated with
|
||||
/// possible initial and trailing metadata sent from the server.
|
||||
void ReadInitialMetadata(void* tag) override { |
||||
assert(started_); |
||||
GPR_CODEGEN_ASSERT(!context_->initial_metadata_received_); |
||||
|
||||
single_buf.set_output_tag(tag); |
||||
single_buf.RecvInitialMetadata(context_); |
||||
call_.PerformOps(&single_buf); |
||||
initial_metadata_read_ = true; |
||||
} |
||||
|
||||
/// See \a ClientAysncResponseReaderInterface::Finish for semantics.
|
||||
///
|
||||
/// Side effect:
|
||||
/// - the \a ClientContext associated with this call is updated with
|
||||
/// possible initial and trailing metadata sent from the server.
|
||||
void Finish(R* msg, ::grpc::Status* status, void* tag) override { |
||||
assert(started_); |
||||
if (initial_metadata_read_) { |
||||
finish_buf.set_output_tag(tag); |
||||
finish_buf.RecvMessage(msg); |
||||
finish_buf.AllowNoMessage(); |
||||
finish_buf.ClientRecvStatus(context_, status); |
||||
call_.PerformOps(&finish_buf); |
||||
} else { |
||||
single_buf.set_output_tag(tag); |
||||
single_buf.RecvInitialMetadata(context_); |
||||
single_buf.RecvMessage(msg); |
||||
single_buf.AllowNoMessage(); |
||||
single_buf.ClientRecvStatus(context_, status); |
||||
call_.PerformOps(&single_buf); |
||||
} |
||||
} |
||||
|
||||
private: |
||||
friend class internal::ClientAsyncResponseReaderFactory<R>; |
||||
::grpc_impl::ClientContext* const context_; |
||||
::grpc::internal::Call call_; |
||||
bool started_; |
||||
bool initial_metadata_read_ = false; |
||||
|
||||
template <class W> |
||||
ClientAsyncResponseReader(::grpc::internal::Call call, |
||||
::grpc_impl::ClientContext* context, |
||||
const W& request, bool start) |
||||
: context_(context), call_(call), started_(start) { |
||||
// Bind the metadata at time of StartCallInternal but set up the rest here
|
||||
// TODO(ctiller): don't assert
|
||||
GPR_CODEGEN_ASSERT(single_buf.SendMessage(request).ok()); |
||||
single_buf.ClientSendClose(); |
||||
if (start) StartCallInternal(); |
||||
} |
||||
|
||||
void StartCallInternal() { |
||||
single_buf.SendInitialMetadata(&context_->send_initial_metadata_, |
||||
context_->initial_metadata_flags()); |
||||
} |
||||
|
||||
// disable operator new
|
||||
static void* operator new(std::size_t size); |
||||
static void* operator new(std::size_t size, void* p) { return p; } |
||||
|
||||
::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata, |
||||
::grpc::internal::CallOpSendMessage, |
||||
::grpc::internal::CallOpClientSendClose, |
||||
::grpc::internal::CallOpRecvInitialMetadata, |
||||
::grpc::internal::CallOpRecvMessage<R>, |
||||
::grpc::internal::CallOpClientRecvStatus> |
||||
single_buf; |
||||
::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvMessage<R>, |
||||
::grpc::internal::CallOpClientRecvStatus> |
||||
finish_buf; |
||||
}; |
||||
|
||||
/// Async server-side API for handling unary calls, where the single
|
||||
/// response message sent to the client is of type \a W.
|
||||
template <class W> |
||||
class ServerAsyncResponseWriter final |
||||
: public ::grpc::internal::ServerAsyncStreamingInterface { |
||||
public: |
||||
explicit ServerAsyncResponseWriter(::grpc_impl::ServerContext* ctx) |
||||
: call_(nullptr, nullptr, nullptr), ctx_(ctx) {} |
||||
|
||||
/// See \a ServerAsyncStreamingInterface::SendInitialMetadata for semantics.
|
||||
///
|
||||
/// Side effect:
|
||||
/// The initial metadata that will be sent to the client from this op will
|
||||
/// be taken from the \a ServerContext associated with the call.
|
||||
///
|
||||
/// \param[in] tag Tag identifying this request.
|
||||
void SendInitialMetadata(void* tag) override { |
||||
GPR_CODEGEN_ASSERT(!ctx_->sent_initial_metadata_); |
||||
|
||||
meta_buf_.set_output_tag(tag); |
||||
meta_buf_.SendInitialMetadata(&ctx_->initial_metadata_, |
||||
ctx_->initial_metadata_flags()); |
||||
if (ctx_->compression_level_set()) { |
||||
meta_buf_.set_compression_level(ctx_->compression_level()); |
||||
} |
||||
ctx_->sent_initial_metadata_ = true; |
||||
call_.PerformOps(&meta_buf_); |
||||
} |
||||
|
||||
/// Indicate that the stream is to be finished and request notification
|
||||
/// when the server has sent the appropriate signals to the client to
|
||||
/// end the call. Should not be used concurrently with other operations.
|
||||
///
|
||||
/// \param[in] tag Tag identifying this request.
|
||||
/// \param[in] status To be sent to the client as the result of the call.
|
||||
/// \param[in] msg Message to be sent to the client.
|
||||
///
|
||||
/// Side effect:
|
||||
/// - also sends initial metadata if not already sent (using the
|
||||
/// \a ServerContext associated with this call).
|
||||
///
|
||||
/// Note: if \a status has a non-OK code, then \a msg will not be sent,
|
||||
/// and the client will receive only the status with possible trailing
|
||||
/// metadata.
|
||||
void Finish(const W& msg, const ::grpc::Status& status, void* tag) { |
||||
finish_buf_.set_output_tag(tag); |
||||
finish_buf_.set_core_cq_tag(&finish_buf_); |
||||
if (!ctx_->sent_initial_metadata_) { |
||||
finish_buf_.SendInitialMetadata(&ctx_->initial_metadata_, |
||||
ctx_->initial_metadata_flags()); |
||||
if (ctx_->compression_level_set()) { |
||||
finish_buf_.set_compression_level(ctx_->compression_level()); |
||||
} |
||||
ctx_->sent_initial_metadata_ = true; |
||||
} |
||||
// The response is dropped if the status is not OK.
|
||||
if (status.ok()) { |
||||
finish_buf_.ServerSendStatus(&ctx_->trailing_metadata_, |
||||
finish_buf_.SendMessage(msg)); |
||||
} else { |
||||
finish_buf_.ServerSendStatus(&ctx_->trailing_metadata_, status); |
||||
} |
||||
call_.PerformOps(&finish_buf_); |
||||
} |
||||
|
||||
/// Indicate that the stream is to be finished with a non-OK status,
|
||||
/// and request notification for when the server has finished sending the
|
||||
/// appropriate signals to the client to end the call.
|
||||
/// Should not be used concurrently with other operations.
|
||||
///
|
||||
/// \param[in] tag Tag identifying this request.
|
||||
/// \param[in] status To be sent to the client as the result of the call.
|
||||
/// - Note: \a status must have a non-OK code.
|
||||
///
|
||||
/// Side effect:
|
||||
/// - also sends initial metadata if not already sent (using the
|
||||
/// \a ServerContext associated with this call).
|
||||
void FinishWithError(const ::grpc::Status& status, void* tag) { |
||||
GPR_CODEGEN_ASSERT(!status.ok()); |
||||
finish_buf_.set_output_tag(tag); |
||||
if (!ctx_->sent_initial_metadata_) { |
||||
finish_buf_.SendInitialMetadata(&ctx_->initial_metadata_, |
||||
ctx_->initial_metadata_flags()); |
||||
if (ctx_->compression_level_set()) { |
||||
finish_buf_.set_compression_level(ctx_->compression_level()); |
||||
} |
||||
ctx_->sent_initial_metadata_ = true; |
||||
} |
||||
finish_buf_.ServerSendStatus(&ctx_->trailing_metadata_, status); |
||||
call_.PerformOps(&finish_buf_); |
||||
} |
||||
|
||||
private: |
||||
void BindCall(::grpc::internal::Call* call) override { call_ = *call; } |
||||
|
||||
::grpc::internal::Call call_; |
||||
::grpc_impl::ServerContext* ctx_; |
||||
::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata> |
||||
meta_buf_; |
||||
::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata, |
||||
::grpc::internal::CallOpSendMessage, |
||||
::grpc::internal::CallOpServerSendStatus> |
||||
finish_buf_; |
||||
}; |
||||
|
||||
} // namespace grpc_impl
|
||||
|
||||
namespace std { |
||||
template <class R> |
||||
class default_delete<::grpc_impl::ClientAsyncResponseReader<R>> { |
||||
public: |
||||
void operator()(void* p) {} |
||||
}; |
||||
template <class R> |
||||
class default_delete<::grpc_impl::ClientAsyncResponseReaderInterface<R>> { |
||||
public: |
||||
void operator()(void* p) {} |
||||
}; |
||||
} // namespace std
|
||||
|
||||
#endif // GRPCPP_IMPL_CODEGEN_ASYNC_UNARY_CALL_IMPL_H
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,493 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2015 gRPC authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
* |
||||
*/ |
||||
|
||||
/// A ClientContext allows the person implementing a service client to:
|
||||
///
|
||||
/// - Add custom metadata key-value pairs that will propagated to the server
|
||||
/// side.
|
||||
/// - Control call settings such as compression and authentication.
|
||||
/// - Initial and trailing metadata coming from the server.
|
||||
/// - Get performance metrics (ie, census).
|
||||
///
|
||||
/// Context settings are only relevant to the call they are invoked with, that
|
||||
/// is to say, they aren't sticky. Some of these settings, such as the
|
||||
/// compression options, can be made persistent at channel construction time
|
||||
/// (see \a grpc::CreateCustomChannel).
|
||||
///
|
||||
/// \warning ClientContext instances should \em not be reused across rpcs.
|
||||
|
||||
#ifndef GRPCPP_IMPL_CODEGEN_CLIENT_CONTEXT_IMPL_H |
||||
#define GRPCPP_IMPL_CODEGEN_CLIENT_CONTEXT_IMPL_H |
||||
|
||||
#include <map> |
||||
#include <memory> |
||||
#include <mutex> |
||||
#include <string> |
||||
|
||||
#include <grpc/impl/codegen/compression_types.h> |
||||
#include <grpc/impl/codegen/propagation_bits.h> |
||||
#include <grpcpp/impl/codegen/client_interceptor.h> |
||||
#include <grpcpp/impl/codegen/config.h> |
||||
#include <grpcpp/impl/codegen/core_codegen_interface.h> |
||||
#include <grpcpp/impl/codegen/create_auth_context.h> |
||||
#include <grpcpp/impl/codegen/metadata_map.h> |
||||
#include <grpcpp/impl/codegen/rpc_method.h> |
||||
#include <grpcpp/impl/codegen/security/auth_context.h> |
||||
#include <grpcpp/impl/codegen/slice.h> |
||||
#include <grpcpp/impl/codegen/status.h> |
||||
#include <grpcpp/impl/codegen/string_ref.h> |
||||
#include <grpcpp/impl/codegen/sync.h> |
||||
#include <grpcpp/impl/codegen/time.h> |
||||
|
||||
struct census_context; |
||||
struct grpc_call; |
||||
|
||||
namespace grpc { |
||||
|
||||
class ChannelInterface; |
||||
|
||||
namespace internal { |
||||
class RpcMethod; |
||||
template <class InputMessage, class OutputMessage> |
||||
class BlockingUnaryCallImpl; |
||||
class CallOpClientRecvStatus; |
||||
class CallOpRecvInitialMetadata; |
||||
} // namespace internal
|
||||
|
||||
namespace testing { |
||||
class InteropClientContextInspector; |
||||
} // namespace testing
|
||||
} // namespace grpc
|
||||
namespace grpc_impl { |
||||
|
||||
namespace internal { |
||||
template <class InputMessage, class OutputMessage> |
||||
class CallbackUnaryCallImpl; |
||||
template <class Request, class Response> |
||||
class ClientCallbackReaderWriterImpl; |
||||
template <class Response> |
||||
class ClientCallbackReaderImpl; |
||||
template <class Request> |
||||
class ClientCallbackWriterImpl; |
||||
class ClientCallbackUnaryImpl; |
||||
} // namespace internal
|
||||
|
||||
class CallCredentials; |
||||
class Channel; |
||||
class CompletionQueue; |
||||
class ServerContext; |
||||
template <class R> |
||||
class ClientReader; |
||||
template <class W> |
||||
class ClientWriter; |
||||
template <class W, class R> |
||||
class ClientReaderWriter; |
||||
template <class R> |
||||
class ClientAsyncReader; |
||||
template <class W> |
||||
class ClientAsyncWriter; |
||||
template <class W, class R> |
||||
class ClientAsyncReaderWriter; |
||||
template <class R> |
||||
class ClientAsyncResponseReader; |
||||
|
||||
/// Options for \a ClientContext::FromServerContext specifying which traits from
|
||||
/// the \a ServerContext to propagate (copy) from it into a new \a
|
||||
/// ClientContext.
|
||||
///
|
||||
/// \see ClientContext::FromServerContext
|
||||
class PropagationOptions { |
||||
public: |
||||
PropagationOptions() : propagate_(GRPC_PROPAGATE_DEFAULTS) {} |
||||
|
||||
PropagationOptions& enable_deadline_propagation() { |
||||
propagate_ |= GRPC_PROPAGATE_DEADLINE; |
||||
return *this; |
||||
} |
||||
|
||||
PropagationOptions& disable_deadline_propagation() { |
||||
propagate_ &= ~GRPC_PROPAGATE_DEADLINE; |
||||
return *this; |
||||
} |
||||
|
||||
PropagationOptions& enable_census_stats_propagation() { |
||||
propagate_ |= GRPC_PROPAGATE_CENSUS_STATS_CONTEXT; |
||||
return *this; |
||||
} |
||||
|
||||
PropagationOptions& disable_census_stats_propagation() { |
||||
propagate_ &= ~GRPC_PROPAGATE_CENSUS_STATS_CONTEXT; |
||||
return *this; |
||||
} |
||||
|
||||
PropagationOptions& enable_census_tracing_propagation() { |
||||
propagate_ |= GRPC_PROPAGATE_CENSUS_TRACING_CONTEXT; |
||||
return *this; |
||||
} |
||||
|
||||
PropagationOptions& disable_census_tracing_propagation() { |
||||
propagate_ &= ~GRPC_PROPAGATE_CENSUS_TRACING_CONTEXT; |
||||
return *this; |
||||
} |
||||
|
||||
PropagationOptions& enable_cancellation_propagation() { |
||||
propagate_ |= GRPC_PROPAGATE_CANCELLATION; |
||||
return *this; |
||||
} |
||||
|
||||
PropagationOptions& disable_cancellation_propagation() { |
||||
propagate_ &= ~GRPC_PROPAGATE_CANCELLATION; |
||||
return *this; |
||||
} |
||||
|
||||
uint32_t c_bitmask() const { return propagate_; } |
||||
|
||||
private: |
||||
uint32_t propagate_; |
||||
}; |
||||
|
||||
/// A ClientContext allows the person implementing a service client to:
|
||||
///
|
||||
/// - Add custom metadata key-value pairs that will propagated to the server
|
||||
/// side.
|
||||
/// - Control call settings such as compression and authentication.
|
||||
/// - Initial and trailing metadata coming from the server.
|
||||
/// - Get performance metrics (ie, census).
|
||||
///
|
||||
/// Context settings are only relevant to the call they are invoked with, that
|
||||
/// is to say, they aren't sticky. Some of these settings, such as the
|
||||
/// compression options, can be made persistent at channel construction time
|
||||
/// (see \a grpc::CreateCustomChannel).
|
||||
///
|
||||
/// \warning ClientContext instances should \em not be reused across rpcs.
|
||||
/// \warning The ClientContext instance used for creating an rpc must remain
|
||||
/// alive and valid for the lifetime of the rpc.
|
||||
class ClientContext { |
||||
public: |
||||
ClientContext(); |
||||
~ClientContext(); |
||||
|
||||
/// Create a new \a ClientContext as a child of an incoming server call,
|
||||
/// according to \a options (\see PropagationOptions).
|
||||
///
|
||||
/// \param server_context The source server context to use as the basis for
|
||||
/// constructing the client context.
|
||||
/// \param options The options controlling what to copy from the \a
|
||||
/// server_context.
|
||||
///
|
||||
/// \return A newly constructed \a ClientContext instance based on \a
|
||||
/// server_context, with traits propagated (copied) according to \a options.
|
||||
static std::unique_ptr<ClientContext> FromServerContext( |
||||
const grpc_impl::ServerContext& server_context, |
||||
PropagationOptions options = PropagationOptions()); |
||||
|
||||
/// Add the (\a meta_key, \a meta_value) pair to the metadata associated with
|
||||
/// a client call. These are made available at the server side by the \a
|
||||
/// grpc::ServerContext::client_metadata() method.
|
||||
///
|
||||
/// \warning This method should only be called before invoking the rpc.
|
||||
///
|
||||
/// \param meta_key The metadata key. If \a meta_value is binary data, it must
|
||||
/// end in "-bin".
|
||||
/// \param meta_value The metadata value. If its value is binary, the key name
|
||||
/// must end in "-bin".
|
||||
///
|
||||
/// Metadata must conform to the following format:
|
||||
/// Custom-Metadata -> Binary-Header / ASCII-Header
|
||||
/// Binary-Header -> {Header-Name "-bin" } {binary value}
|
||||
/// ASCII-Header -> Header-Name ASCII-Value
|
||||
/// Header-Name -> 1*( %x30-39 / %x61-7A / "_" / "-" / ".") ; 0-9 a-z _ - .
|
||||
/// ASCII-Value -> 1*( %x20-%x7E ) ; space and printable ASCII
|
||||
void AddMetadata(const grpc::string& meta_key, |
||||
const grpc::string& meta_value); |
||||
|
||||
/// Return a collection of initial metadata key-value pairs. Note that keys
|
||||
/// may happen more than once (ie, a \a std::multimap is returned).
|
||||
///
|
||||
/// \warning This method should only be called after initial metadata has been
|
||||
/// received. For streaming calls, see \a
|
||||
/// ClientReaderInterface::WaitForInitialMetadata().
|
||||
///
|
||||
/// \return A multimap of initial metadata key-value pairs from the server.
|
||||
const std::multimap<grpc::string_ref, grpc::string_ref>& |
||||
GetServerInitialMetadata() const { |
||||
GPR_CODEGEN_ASSERT(initial_metadata_received_); |
||||
return *recv_initial_metadata_.map(); |
||||
} |
||||
|
||||
/// Return a collection of trailing metadata key-value pairs. Note that keys
|
||||
/// may happen more than once (ie, a \a std::multimap is returned).
|
||||
///
|
||||
/// \warning This method is only callable once the stream has finished.
|
||||
///
|
||||
/// \return A multimap of metadata trailing key-value pairs from the server.
|
||||
const std::multimap<grpc::string_ref, grpc::string_ref>& |
||||
GetServerTrailingMetadata() const { |
||||
// TODO(yangg) check finished
|
||||
return *trailing_metadata_.map(); |
||||
} |
||||
|
||||
/// Set the deadline for the client call.
|
||||
///
|
||||
/// \warning This method should only be called before invoking the rpc.
|
||||
///
|
||||
/// \param deadline the deadline for the client call. Units are determined by
|
||||
/// the type used. The deadline is an absolute (not relative) time.
|
||||
template <typename T> |
||||
void set_deadline(const T& deadline) { |
||||
grpc::TimePoint<T> deadline_tp(deadline); |
||||
deadline_ = deadline_tp.raw_time(); |
||||
} |
||||
|
||||
/// EXPERIMENTAL: Indicate that this request is idempotent.
|
||||
/// By default, RPCs are assumed to <i>not</i> be idempotent.
|
||||
///
|
||||
/// If true, the gRPC library assumes that it's safe to initiate
|
||||
/// this RPC multiple times.
|
||||
void set_idempotent(bool idempotent) { idempotent_ = idempotent; } |
||||
|
||||
/// EXPERIMENTAL: Set this request to be cacheable.
|
||||
/// If set, grpc is free to use the HTTP GET verb for sending the request,
|
||||
/// with the possibility of receiving a cached response.
|
||||
void set_cacheable(bool cacheable) { cacheable_ = cacheable; } |
||||
|
||||
/// EXPERIMENTAL: Trigger wait-for-ready or not on this request.
|
||||
/// See https://github.com/grpc/grpc/blob/master/doc/wait-for-ready.md.
|
||||
/// If set, if an RPC is made when a channel's connectivity state is
|
||||
/// TRANSIENT_FAILURE or CONNECTING, the call will not "fail fast",
|
||||
/// and the channel will wait until the channel is READY before making the
|
||||
/// call.
|
||||
void set_wait_for_ready(bool wait_for_ready) { |
||||
wait_for_ready_ = wait_for_ready; |
||||
wait_for_ready_explicitly_set_ = true; |
||||
} |
||||
|
||||
/// DEPRECATED: Use set_wait_for_ready() instead.
|
||||
void set_fail_fast(bool fail_fast) { set_wait_for_ready(!fail_fast); } |
||||
|
||||
/// Return the deadline for the client call.
|
||||
std::chrono::system_clock::time_point deadline() const { |
||||
return grpc::Timespec2Timepoint(deadline_); |
||||
} |
||||
|
||||
/// Return a \a gpr_timespec representation of the client call's deadline.
|
||||
gpr_timespec raw_deadline() const { return deadline_; } |
||||
|
||||
/// Set the per call authority header (see
|
||||
/// https://tools.ietf.org/html/rfc7540#section-8.1.2.3).
|
||||
void set_authority(const grpc::string& authority) { authority_ = authority; } |
||||
|
||||
/// Return the authentication context for this client call.
|
||||
///
|
||||
/// \see grpc::AuthContext.
|
||||
std::shared_ptr<const grpc::AuthContext> auth_context() const { |
||||
if (auth_context_.get() == nullptr) { |
||||
auth_context_ = grpc::CreateAuthContext(call_); |
||||
} |
||||
return auth_context_; |
||||
} |
||||
|
||||
/// Set credentials for the client call.
|
||||
///
|
||||
/// A credentials object encapsulates all the state needed by a client to
|
||||
/// authenticate with a server and make various assertions, e.g., about the
|
||||
/// client’s identity, role, or whether it is authorized to make a particular
|
||||
/// call.
|
||||
///
|
||||
/// \see https://grpc.io/docs/guides/auth.html
|
||||
void set_credentials( |
||||
const std::shared_ptr<grpc_impl::CallCredentials>& creds) { |
||||
creds_ = creds; |
||||
} |
||||
|
||||
/// Return the compression algorithm the client call will request be used.
|
||||
/// Note that the gRPC runtime may decide to ignore this request, for example,
|
||||
/// due to resource constraints.
|
||||
grpc_compression_algorithm compression_algorithm() const { |
||||
return compression_algorithm_; |
||||
} |
||||
|
||||
/// Set \a algorithm to be the compression algorithm used for the client call.
|
||||
///
|
||||
/// \param algorithm The compression algorithm used for the client call.
|
||||
void set_compression_algorithm(grpc_compression_algorithm algorithm); |
||||
|
||||
/// Flag whether the initial metadata should be \a corked
|
||||
///
|
||||
/// If \a corked is true, then the initial metadata will be coalesced with the
|
||||
/// write of first message in the stream. As a result, any tag set for the
|
||||
/// initial metadata operation (starting a client-streaming or bidi-streaming
|
||||
/// RPC) will not actually be sent to the completion queue or delivered
|
||||
/// via Next.
|
||||
///
|
||||
/// \param corked The flag indicating whether the initial metadata is to be
|
||||
/// corked or not.
|
||||
void set_initial_metadata_corked(bool corked) { |
||||
initial_metadata_corked_ = corked; |
||||
} |
||||
|
||||
/// Return the peer uri in a string.
|
||||
///
|
||||
/// \warning This value is never authenticated or subject to any security
|
||||
/// related code. It must not be used for any authentication related
|
||||
/// functionality. Instead, use auth_context.
|
||||
///
|
||||
/// \return The call's peer URI.
|
||||
grpc::string peer() const; |
||||
|
||||
/// Get and set census context.
|
||||
void set_census_context(struct census_context* ccp) { census_context_ = ccp; } |
||||
struct census_context* census_context() const { |
||||
return census_context_; |
||||
} |
||||
|
||||
/// Send a best-effort out-of-band cancel on the call associated with
|
||||
/// this client context. The call could be in any stage; e.g., if it is
|
||||
/// already finished, it may still return success.
|
||||
///
|
||||
/// There is no guarantee the call will be cancelled.
|
||||
///
|
||||
/// Note that TryCancel() does not change any of the tags that are pending
|
||||
/// on the completion queue. All pending tags will still be delivered
|
||||
/// (though their ok result may reflect the effect of cancellation).
|
||||
void TryCancel(); |
||||
|
||||
/// Global Callbacks
|
||||
///
|
||||
/// Can be set exactly once per application to install hooks whenever
|
||||
/// a client context is constructed and destructed.
|
||||
class GlobalCallbacks { |
||||
public: |
||||
virtual ~GlobalCallbacks() {} |
||||
virtual void DefaultConstructor(ClientContext* context) = 0; |
||||
virtual void Destructor(ClientContext* context) = 0; |
||||
}; |
||||
static void SetGlobalCallbacks(GlobalCallbacks* callbacks); |
||||
|
||||
/// Should be used for framework-level extensions only.
|
||||
/// Applications never need to call this method.
|
||||
grpc_call* c_call() { return call_; } |
||||
|
||||
/// EXPERIMENTAL debugging API
|
||||
///
|
||||
/// if status is not ok() for an RPC, this will return a detailed string
|
||||
/// of the gRPC Core error that led to the failure. It should not be relied
|
||||
/// upon for anything other than gaining more debug data in failure cases.
|
||||
grpc::string debug_error_string() const { return debug_error_string_; } |
||||
|
||||
private: |
||||
// Disallow copy and assign.
|
||||
ClientContext(const ClientContext&); |
||||
ClientContext& operator=(const ClientContext&); |
||||
|
||||
friend class ::grpc::testing::InteropClientContextInspector; |
||||
friend class ::grpc::internal::CallOpClientRecvStatus; |
||||
friend class ::grpc::internal::CallOpRecvInitialMetadata; |
||||
friend class ::grpc_impl::Channel; |
||||
template <class R> |
||||
friend class ::grpc_impl::ClientReader; |
||||
template <class W> |
||||
friend class ::grpc_impl::ClientWriter; |
||||
template <class W, class R> |
||||
friend class ::grpc_impl::ClientReaderWriter; |
||||
template <class R> |
||||
friend class ::grpc_impl::ClientAsyncReader; |
||||
template <class W> |
||||
friend class ::grpc_impl::ClientAsyncWriter; |
||||
template <class W, class R> |
||||
friend class ::grpc_impl::ClientAsyncReaderWriter; |
||||
template <class R> |
||||
friend class ::grpc_impl::ClientAsyncResponseReader; |
||||
template <class InputMessage, class OutputMessage> |
||||
friend class ::grpc::internal::BlockingUnaryCallImpl; |
||||
template <class InputMessage, class OutputMessage> |
||||
friend class ::grpc_impl::internal::CallbackUnaryCallImpl; |
||||
template <class Request, class Response> |
||||
friend class ::grpc_impl::internal::ClientCallbackReaderWriterImpl; |
||||
template <class Response> |
||||
friend class ::grpc_impl::internal::ClientCallbackReaderImpl; |
||||
template <class Request> |
||||
friend class ::grpc_impl::internal::ClientCallbackWriterImpl; |
||||
friend class ::grpc_impl::internal::ClientCallbackUnaryImpl; |
||||
|
||||
// Used by friend class CallOpClientRecvStatus
|
||||
void set_debug_error_string(const grpc::string& debug_error_string) { |
||||
debug_error_string_ = debug_error_string; |
||||
} |
||||
|
||||
grpc_call* call() const { return call_; } |
||||
void set_call(grpc_call* call, |
||||
const std::shared_ptr<::grpc_impl::Channel>& channel); |
||||
|
||||
grpc::experimental::ClientRpcInfo* set_client_rpc_info( |
||||
const char* method, grpc::internal::RpcMethod::RpcType type, |
||||
grpc::ChannelInterface* channel, |
||||
const std::vector<std::unique_ptr< |
||||
grpc::experimental::ClientInterceptorFactoryInterface>>& creators, |
||||
size_t interceptor_pos) { |
||||
rpc_info_ = grpc::experimental::ClientRpcInfo(this, type, method, channel); |
||||
rpc_info_.RegisterInterceptors(creators, interceptor_pos); |
||||
return &rpc_info_; |
||||
} |
||||
|
||||
uint32_t initial_metadata_flags() const { |
||||
return (idempotent_ ? GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST : 0) | |
||||
(wait_for_ready_ ? GRPC_INITIAL_METADATA_WAIT_FOR_READY : 0) | |
||||
(cacheable_ ? GRPC_INITIAL_METADATA_CACHEABLE_REQUEST : 0) | |
||||
(wait_for_ready_explicitly_set_ |
||||
? GRPC_INITIAL_METADATA_WAIT_FOR_READY_EXPLICITLY_SET |
||||
: 0) | |
||||
(initial_metadata_corked_ ? GRPC_INITIAL_METADATA_CORKED : 0); |
||||
} |
||||
|
||||
grpc::string authority() { return authority_; } |
||||
|
||||
void SendCancelToInterceptors(); |
||||
|
||||
bool initial_metadata_received_; |
||||
bool wait_for_ready_; |
||||
bool wait_for_ready_explicitly_set_; |
||||
bool idempotent_; |
||||
bool cacheable_; |
||||
std::shared_ptr<::grpc_impl::Channel> channel_; |
||||
grpc::internal::Mutex mu_; |
||||
grpc_call* call_; |
||||
bool call_canceled_; |
||||
gpr_timespec deadline_; |
||||
grpc::string authority_; |
||||
std::shared_ptr<grpc_impl::CallCredentials> creds_; |
||||
mutable std::shared_ptr<const grpc::AuthContext> auth_context_; |
||||
struct census_context* census_context_; |
||||
std::multimap<grpc::string, grpc::string> send_initial_metadata_; |
||||
mutable grpc::internal::MetadataMap recv_initial_metadata_; |
||||
mutable grpc::internal::MetadataMap trailing_metadata_; |
||||
|
||||
grpc_call* propagate_from_call_; |
||||
PropagationOptions propagation_options_; |
||||
|
||||
grpc_compression_algorithm compression_algorithm_; |
||||
bool initial_metadata_corked_; |
||||
|
||||
grpc::string debug_error_string_; |
||||
|
||||
grpc::experimental::ClientRpcInfo rpc_info_; |
||||
}; |
||||
|
||||
} // namespace grpc_impl
|
||||
|
||||
#endif // GRPCPP_IMPL_CODEGEN_CLIENT_CONTEXT_IMPL_H
|
@ -0,0 +1,420 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2015-2016 gRPC authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
* |
||||
*/ |
||||
|
||||
/// A completion queue implements a concurrent producer-consumer queue, with
|
||||
/// two main API-exposed methods: \a Next and \a AsyncNext. These
|
||||
/// methods are the essential component of the gRPC C++ asynchronous API.
|
||||
/// There is also a \a Shutdown method to indicate that a given completion queue
|
||||
/// will no longer have regular events. This must be called before the
|
||||
/// completion queue is destroyed.
|
||||
/// All completion queue APIs are thread-safe and may be used concurrently with
|
||||
/// any other completion queue API invocation; it is acceptable to have
|
||||
/// multiple threads calling \a Next or \a AsyncNext on the same or different
|
||||
/// completion queues, or to call these methods concurrently with a \a Shutdown
|
||||
/// elsewhere.
|
||||
/// \remark{All other API calls on completion queue should be completed before
|
||||
/// a completion queue destructor is called.}
|
||||
#ifndef GRPCPP_IMPL_CODEGEN_COMPLETION_QUEUE_IMPL_H |
||||
#define GRPCPP_IMPL_CODEGEN_COMPLETION_QUEUE_IMPL_H |
||||
|
||||
#include <grpc/impl/codegen/atm.h> |
||||
#include <grpcpp/impl/codegen/completion_queue_tag.h> |
||||
#include <grpcpp/impl/codegen/core_codegen_interface.h> |
||||
#include <grpcpp/impl/codegen/grpc_library.h> |
||||
#include <grpcpp/impl/codegen/status.h> |
||||
#include <grpcpp/impl/codegen/time.h> |
||||
|
||||
struct grpc_completion_queue; |
||||
|
||||
namespace grpc_impl { |
||||
|
||||
class Channel; |
||||
class Server; |
||||
class ServerBuilder; |
||||
class ServerContext; |
||||
template <class R> |
||||
class ClientReader; |
||||
template <class W> |
||||
class ClientWriter; |
||||
template <class W, class R> |
||||
class ClientReaderWriter; |
||||
template <class R> |
||||
class ServerReader; |
||||
template <class W> |
||||
class ServerWriter; |
||||
namespace internal { |
||||
template <class W, class R> |
||||
class ServerReaderWriterBody; |
||||
} // namespace internal
|
||||
} // namespace grpc_impl
|
||||
namespace grpc { |
||||
|
||||
class ChannelInterface; |
||||
class ServerInterface; |
||||
|
||||
namespace internal { |
||||
class CompletionQueueTag; |
||||
class RpcMethod; |
||||
template <class ServiceType, class RequestType, class ResponseType> |
||||
class RpcMethodHandler; |
||||
template <class ServiceType, class RequestType, class ResponseType> |
||||
class ClientStreamingHandler; |
||||
template <class ServiceType, class RequestType, class ResponseType> |
||||
class ServerStreamingHandler; |
||||
template <class ServiceType, class RequestType, class ResponseType> |
||||
class BidiStreamingHandler; |
||||
template <class Streamer, bool WriteNeeded> |
||||
class TemplatedBidiStreamingHandler; |
||||
template <StatusCode code> |
||||
class ErrorMethodHandler; |
||||
template <class InputMessage, class OutputMessage> |
||||
class BlockingUnaryCallImpl; |
||||
template <class Op1, class Op2, class Op3, class Op4, class Op5, class Op6> |
||||
class CallOpSet; |
||||
} // namespace internal
|
||||
|
||||
extern CoreCodegenInterface* g_core_codegen_interface; |
||||
|
||||
} // namespace grpc
|
||||
|
||||
namespace grpc_impl { |
||||
|
||||
/// A thin wrapper around \ref grpc_completion_queue (see \ref
|
||||
/// src/core/lib/surface/completion_queue.h).
|
||||
/// See \ref doc/cpp/perf_notes.md for notes on best practices for high
|
||||
/// performance servers.
|
||||
class CompletionQueue : private ::grpc::GrpcLibraryCodegen { |
||||
public: |
||||
/// Default constructor. Implicitly creates a \a grpc_completion_queue
|
||||
/// instance.
|
||||
CompletionQueue() |
||||
: CompletionQueue(grpc_completion_queue_attributes{ |
||||
GRPC_CQ_CURRENT_VERSION, GRPC_CQ_NEXT, GRPC_CQ_DEFAULT_POLLING, |
||||
nullptr}) {} |
||||
|
||||
/// Wrap \a take, taking ownership of the instance.
|
||||
///
|
||||
/// \param take The completion queue instance to wrap. Ownership is taken.
|
||||
explicit CompletionQueue(grpc_completion_queue* take); |
||||
|
||||
/// Destructor. Destroys the owned wrapped completion queue / instance.
|
||||
~CompletionQueue() { |
||||
::grpc::g_core_codegen_interface->grpc_completion_queue_destroy(cq_); |
||||
} |
||||
|
||||
/// Tri-state return for AsyncNext: SHUTDOWN, GOT_EVENT, TIMEOUT.
|
||||
enum NextStatus { |
||||
SHUTDOWN, ///< The completion queue has been shutdown and fully-drained
|
||||
GOT_EVENT, ///< Got a new event; \a tag will be filled in with its
|
||||
///< associated value; \a ok indicating its success.
|
||||
TIMEOUT ///< deadline was reached.
|
||||
}; |
||||
|
||||
/// Read from the queue, blocking until an event is available or the queue is
|
||||
/// shutting down.
|
||||
///
|
||||
/// \param tag [out] Updated to point to the read event's tag.
|
||||
/// \param ok [out] true if read a successful event, false otherwise.
|
||||
///
|
||||
/// Note that each tag sent to the completion queue (through RPC operations
|
||||
/// or alarms) will be delivered out of the completion queue by a call to
|
||||
/// Next (or a related method), regardless of whether the operation succeeded
|
||||
/// or not. Success here means that this operation completed in the normal
|
||||
/// valid manner.
|
||||
///
|
||||
/// Server-side RPC request: \a ok indicates that the RPC has indeed
|
||||
/// been started. If it is false, the server has been Shutdown
|
||||
/// before this particular call got matched to an incoming RPC.
|
||||
///
|
||||
/// Client-side StartCall/RPC invocation: \a ok indicates that the RPC is
|
||||
/// going to go to the wire. If it is false, it not going to the wire. This
|
||||
/// would happen if the channel is either permanently broken or
|
||||
/// transiently broken but with the fail-fast option. (Note that async unary
|
||||
/// RPCs don't post a CQ tag at this point, nor do client-streaming
|
||||
/// or bidi-streaming RPCs that have the initial metadata corked option set.)
|
||||
///
|
||||
/// Client-side Write, Client-side WritesDone, Server-side Write,
|
||||
/// Server-side Finish, Server-side SendInitialMetadata (which is
|
||||
/// typically included in Write or Finish when not done explicitly):
|
||||
/// \a ok means that the data/metadata/status/etc is going to go to the
|
||||
/// wire. If it is false, it not going to the wire because the call
|
||||
/// is already dead (i.e., canceled, deadline expired, other side
|
||||
/// dropped the channel, etc).
|
||||
///
|
||||
/// Client-side Read, Server-side Read, Client-side
|
||||
/// RecvInitialMetadata (which is typically included in Read if not
|
||||
/// done explicitly): \a ok indicates whether there is a valid message
|
||||
/// that got read. If not, you know that there are certainly no more
|
||||
/// messages that can ever be read from this stream. For the client-side
|
||||
/// operations, this only happens because the call is dead. For the
|
||||
/// server-sider operation, though, this could happen because the client
|
||||
/// has done a WritesDone already.
|
||||
///
|
||||
/// Client-side Finish: \a ok should always be true
|
||||
///
|
||||
/// Server-side AsyncNotifyWhenDone: \a ok should always be true
|
||||
///
|
||||
/// Alarm: \a ok is true if it expired, false if it was canceled
|
||||
///
|
||||
/// \return true if got an event, false if the queue is fully drained and
|
||||
/// shut down.
|
||||
bool Next(void** tag, bool* ok) { |
||||
return (AsyncNextInternal(tag, ok, |
||||
::grpc::g_core_codegen_interface->gpr_inf_future( |
||||
GPR_CLOCK_REALTIME)) != SHUTDOWN); |
||||
} |
||||
|
||||
/// Read from the queue, blocking up to \a deadline (or the queue's shutdown).
|
||||
/// Both \a tag and \a ok are updated upon success (if an event is available
|
||||
/// within the \a deadline). A \a tag points to an arbitrary location usually
|
||||
/// employed to uniquely identify an event.
|
||||
///
|
||||
/// \param tag [out] Upon success, updated to point to the event's tag.
|
||||
/// \param ok [out] Upon success, true if a successful event, false otherwise
|
||||
/// See documentation for CompletionQueue::Next for explanation of ok
|
||||
/// \param deadline [in] How long to block in wait for an event.
|
||||
///
|
||||
/// \return The type of event read.
|
||||
template <typename T> |
||||
NextStatus AsyncNext(void** tag, bool* ok, const T& deadline) { |
||||
::grpc::TimePoint<T> deadline_tp(deadline); |
||||
return AsyncNextInternal(tag, ok, deadline_tp.raw_time()); |
||||
} |
||||
|
||||
/// EXPERIMENTAL
|
||||
/// First executes \a F, then reads from the queue, blocking up to
|
||||
/// \a deadline (or the queue's shutdown).
|
||||
/// Both \a tag and \a ok are updated upon success (if an event is available
|
||||
/// within the \a deadline). A \a tag points to an arbitrary location usually
|
||||
/// employed to uniquely identify an event.
|
||||
///
|
||||
/// \param f [in] Function to execute before calling AsyncNext on this queue.
|
||||
/// \param tag [out] Upon success, updated to point to the event's tag.
|
||||
/// \param ok [out] Upon success, true if read a regular event, false
|
||||
/// otherwise.
|
||||
/// \param deadline [in] How long to block in wait for an event.
|
||||
///
|
||||
/// \return The type of event read.
|
||||
template <typename T, typename F> |
||||
NextStatus DoThenAsyncNext(F&& f, void** tag, bool* ok, const T& deadline) { |
||||
CompletionQueueTLSCache cache = CompletionQueueTLSCache(this); |
||||
f(); |
||||
if (cache.Flush(tag, ok)) { |
||||
return GOT_EVENT; |
||||
} else { |
||||
return AsyncNext(tag, ok, deadline); |
||||
} |
||||
} |
||||
|
||||
/// Request the shutdown of the queue.
|
||||
///
|
||||
/// \warning This method must be called at some point if this completion queue
|
||||
/// is accessed with Next or AsyncNext. \a Next will not return false
|
||||
/// until this method has been called and all pending tags have been drained.
|
||||
/// (Likewise for \a AsyncNext returning \a NextStatus::SHUTDOWN .)
|
||||
/// Only once either one of these methods does that (that is, once the queue
|
||||
/// has been \em drained) can an instance of this class be destroyed.
|
||||
/// Also note that applications must ensure that no work is enqueued on this
|
||||
/// completion queue after this method is called.
|
||||
void Shutdown(); |
||||
|
||||
/// Returns a \em raw pointer to the underlying \a grpc_completion_queue
|
||||
/// instance.
|
||||
///
|
||||
/// \warning Remember that the returned instance is owned. No transfer of
|
||||
/// owership is performed.
|
||||
grpc_completion_queue* cq() { return cq_; } |
||||
|
||||
protected: |
||||
/// Private constructor of CompletionQueue only visible to friend classes
|
||||
CompletionQueue(const grpc_completion_queue_attributes& attributes) { |
||||
cq_ = ::grpc::g_core_codegen_interface->grpc_completion_queue_create( |
||||
::grpc::g_core_codegen_interface->grpc_completion_queue_factory_lookup( |
||||
&attributes), |
||||
&attributes, NULL); |
||||
InitialAvalanching(); // reserve this for the future shutdown
|
||||
} |
||||
|
||||
private: |
||||
// Friend synchronous wrappers so that they can access Pluck(), which is
|
||||
// a semi-private API geared towards the synchronous implementation.
|
||||
template <class R> |
||||
friend class ::grpc_impl::ClientReader; |
||||
template <class W> |
||||
friend class ::grpc_impl::ClientWriter; |
||||
template <class W, class R> |
||||
friend class ::grpc_impl::ClientReaderWriter; |
||||
template <class R> |
||||
friend class ::grpc_impl::ServerReader; |
||||
template <class W> |
||||
friend class ::grpc_impl::ServerWriter; |
||||
template <class W, class R> |
||||
friend class ::grpc_impl::internal::ServerReaderWriterBody; |
||||
template <class ServiceType, class RequestType, class ResponseType> |
||||
friend class ::grpc::internal::RpcMethodHandler; |
||||
template <class ServiceType, class RequestType, class ResponseType> |
||||
friend class ::grpc::internal::ClientStreamingHandler; |
||||
template <class ServiceType, class RequestType, class ResponseType> |
||||
friend class ::grpc::internal::ServerStreamingHandler; |
||||
template <class Streamer, bool WriteNeeded> |
||||
friend class ::grpc::internal::TemplatedBidiStreamingHandler; |
||||
template <::grpc::StatusCode code> |
||||
friend class ::grpc::internal::ErrorMethodHandler; |
||||
friend class ::grpc_impl::Server; |
||||
friend class ::grpc_impl::ServerContext; |
||||
friend class ::grpc::ServerInterface; |
||||
template <class InputMessage, class OutputMessage> |
||||
friend class ::grpc::internal::BlockingUnaryCallImpl; |
||||
|
||||
// Friends that need access to constructor for callback CQ
|
||||
friend class ::grpc_impl::Channel; |
||||
|
||||
// For access to Register/CompleteAvalanching
|
||||
template <class Op1, class Op2, class Op3, class Op4, class Op5, class Op6> |
||||
friend class ::grpc::internal::CallOpSet; |
||||
|
||||
/// EXPERIMENTAL
|
||||
/// Creates a Thread Local cache to store the first event
|
||||
/// On this completion queue queued from this thread. Once
|
||||
/// initialized, it must be flushed on the same thread.
|
||||
class CompletionQueueTLSCache { |
||||
public: |
||||
CompletionQueueTLSCache(CompletionQueue* cq); |
||||
~CompletionQueueTLSCache(); |
||||
bool Flush(void** tag, bool* ok); |
||||
|
||||
private: |
||||
CompletionQueue* cq_; |
||||
bool flushed_; |
||||
}; |
||||
|
||||
NextStatus AsyncNextInternal(void** tag, bool* ok, gpr_timespec deadline); |
||||
|
||||
/// Wraps \a grpc_completion_queue_pluck.
|
||||
/// \warning Must not be mixed with calls to \a Next.
|
||||
bool Pluck(::grpc::internal::CompletionQueueTag* tag) { |
||||
auto deadline = |
||||
::grpc::g_core_codegen_interface->gpr_inf_future(GPR_CLOCK_REALTIME); |
||||
while (true) { |
||||
auto ev = ::grpc::g_core_codegen_interface->grpc_completion_queue_pluck( |
||||
cq_, tag, deadline, nullptr); |
||||
bool ok = ev.success != 0; |
||||
void* ignored = tag; |
||||
if (tag->FinalizeResult(&ignored, &ok)) { |
||||
GPR_CODEGEN_ASSERT(ignored == tag); |
||||
return ok; |
||||
} |
||||
} |
||||
} |
||||
|
||||
/// Performs a single polling pluck on \a tag.
|
||||
/// \warning Must not be mixed with calls to \a Next.
|
||||
///
|
||||
/// TODO: sreek - This calls tag->FinalizeResult() even if the cq_ is already
|
||||
/// shutdown. This is most likely a bug and if it is a bug, then change this
|
||||
/// implementation to simple call the other TryPluck function with a zero
|
||||
/// timeout. i.e:
|
||||
/// TryPluck(tag, gpr_time_0(GPR_CLOCK_REALTIME))
|
||||
void TryPluck(::grpc::internal::CompletionQueueTag* tag) { |
||||
auto deadline = |
||||
::grpc::g_core_codegen_interface->gpr_time_0(GPR_CLOCK_REALTIME); |
||||
auto ev = ::grpc::g_core_codegen_interface->grpc_completion_queue_pluck( |
||||
cq_, tag, deadline, nullptr); |
||||
if (ev.type == GRPC_QUEUE_TIMEOUT) return; |
||||
bool ok = ev.success != 0; |
||||
void* ignored = tag; |
||||
// the tag must be swallowed if using TryPluck
|
||||
GPR_CODEGEN_ASSERT(!tag->FinalizeResult(&ignored, &ok)); |
||||
} |
||||
|
||||
/// Performs a single polling pluck on \a tag. Calls tag->FinalizeResult if
|
||||
/// the pluck() was successful and returned the tag.
|
||||
///
|
||||
/// This exects tag->FinalizeResult (if called) to return 'false' i.e expects
|
||||
/// that the tag is internal not something that is returned to the user.
|
||||
void TryPluck(::grpc::internal::CompletionQueueTag* tag, |
||||
gpr_timespec deadline) { |
||||
auto ev = ::grpc::g_core_codegen_interface->grpc_completion_queue_pluck( |
||||
cq_, tag, deadline, nullptr); |
||||
if (ev.type == GRPC_QUEUE_TIMEOUT || ev.type == GRPC_QUEUE_SHUTDOWN) { |
||||
return; |
||||
} |
||||
|
||||
bool ok = ev.success != 0; |
||||
void* ignored = tag; |
||||
GPR_CODEGEN_ASSERT(!tag->FinalizeResult(&ignored, &ok)); |
||||
} |
||||
|
||||
/// Manage state of avalanching operations : completion queue tags that
|
||||
/// trigger other completion queue operations. The underlying core completion
|
||||
/// queue should not really shutdown until all avalanching operations have
|
||||
/// been finalized. Note that we maintain the requirement that an avalanche
|
||||
/// registration must take place before CQ shutdown (which must be maintained
|
||||
/// elsehwere)
|
||||
void InitialAvalanching() { |
||||
gpr_atm_rel_store(&avalanches_in_flight_, static_cast<gpr_atm>(1)); |
||||
} |
||||
void RegisterAvalanching() { |
||||
gpr_atm_no_barrier_fetch_add(&avalanches_in_flight_, |
||||
static_cast<gpr_atm>(1)); |
||||
} |
||||
void CompleteAvalanching() { |
||||
if (gpr_atm_no_barrier_fetch_add(&avalanches_in_flight_, |
||||
static_cast<gpr_atm>(-1)) == 1) { |
||||
::grpc::g_core_codegen_interface->grpc_completion_queue_shutdown(cq_); |
||||
} |
||||
} |
||||
|
||||
grpc_completion_queue* cq_; // owned
|
||||
|
||||
gpr_atm avalanches_in_flight_; |
||||
}; |
||||
|
||||
/// A specific type of completion queue used by the processing of notifications
|
||||
/// by servers. Instantiated by \a ServerBuilder.
|
||||
class ServerCompletionQueue : public CompletionQueue { |
||||
public: |
||||
bool IsFrequentlyPolled() { return polling_type_ != GRPC_CQ_NON_LISTENING; } |
||||
|
||||
protected: |
||||
/// Default constructor
|
||||
ServerCompletionQueue() : polling_type_(GRPC_CQ_DEFAULT_POLLING) {} |
||||
|
||||
private: |
||||
/// \param completion_type indicates whether this is a NEXT or CALLBACK
|
||||
/// completion queue.
|
||||
/// \param polling_type Informs the GRPC library about the type of polling
|
||||
/// allowed on this completion queue. See grpc_cq_polling_type's description
|
||||
/// in grpc_types.h for more details.
|
||||
/// \param shutdown_cb is the shutdown callback used for CALLBACK api queues
|
||||
ServerCompletionQueue(grpc_cq_completion_type completion_type, |
||||
grpc_cq_polling_type polling_type, |
||||
grpc_experimental_completion_queue_functor* shutdown_cb) |
||||
: CompletionQueue(grpc_completion_queue_attributes{ |
||||
GRPC_CQ_CURRENT_VERSION, completion_type, polling_type, |
||||
shutdown_cb}), |
||||
polling_type_(polling_type) {} |
||||
|
||||
grpc_cq_polling_type polling_type_; |
||||
friend class ::grpc_impl::ServerBuilder; |
||||
friend class ::grpc_impl::Server; |
||||
}; |
||||
|
||||
} // namespace grpc_impl
|
||||
|
||||
#endif // GRPCPP_IMPL_CODEGEN_COMPLETION_QUEUE_IMPL_H
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,380 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2019 gRPC authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
* |
||||
*/ |
||||
|
||||
#ifndef GRPCPP_IMPL_CODEGEN_SERVER_CONTEXT_IMPL_H |
||||
#define GRPCPP_IMPL_CODEGEN_SERVER_CONTEXT_IMPL_H |
||||
#include <map> |
||||
#include <memory> |
||||
#include <vector> |
||||
|
||||
#include <grpc/impl/codegen/compression_types.h> |
||||
|
||||
#include <grpcpp/impl/codegen/call.h> |
||||
#include <grpcpp/impl/codegen/call_op_set.h> |
||||
#include <grpcpp/impl/codegen/callback_common.h> |
||||
#include <grpcpp/impl/codegen/completion_queue_tag.h> |
||||
#include <grpcpp/impl/codegen/config.h> |
||||
#include <grpcpp/impl/codegen/create_auth_context.h> |
||||
#include <grpcpp/impl/codegen/metadata_map.h> |
||||
#include <grpcpp/impl/codegen/security/auth_context.h> |
||||
#include <grpcpp/impl/codegen/server_interceptor.h> |
||||
#include <grpcpp/impl/codegen/string_ref.h> |
||||
#include <grpcpp/impl/codegen/time.h> |
||||
|
||||
struct grpc_metadata; |
||||
struct grpc_call; |
||||
struct census_context; |
||||
|
||||
namespace grpc_impl { |
||||
class ClientContext; |
||||
class CompletionQueue; |
||||
class Server; |
||||
template <class W, class R> |
||||
class ServerAsyncReader; |
||||
template <class W> |
||||
class ServerAsyncWriter; |
||||
template <class W> |
||||
class ServerAsyncResponseWriter; |
||||
template <class W, class R> |
||||
class ServerAsyncReaderWriter; |
||||
template <class R> |
||||
class ServerReader; |
||||
template <class W> |
||||
class ServerWriter; |
||||
|
||||
namespace internal { |
||||
template <class ServiceType, class RequestType, class ResponseType> |
||||
class BidiStreamingHandler; |
||||
template <class RequestType, class ResponseType> |
||||
class CallbackUnaryHandler; |
||||
template <class RequestType, class ResponseType> |
||||
class CallbackClientStreamingHandler; |
||||
template <class RequestType, class ResponseType> |
||||
class CallbackServerStreamingHandler; |
||||
template <class RequestType, class ResponseType> |
||||
class CallbackBidiHandler; |
||||
template <class W, class R> |
||||
class ServerReaderWriterBody; |
||||
class ServerReactor; |
||||
} // namespace internal
|
||||
|
||||
} // namespace grpc_impl
|
||||
namespace grpc { |
||||
class GenericServerContext; |
||||
class ServerInterface; |
||||
|
||||
namespace internal { |
||||
template <class ServiceType, class RequestType, class ResponseType> |
||||
class ClientStreamingHandler; |
||||
template <class ServiceType, class RequestType, class ResponseType> |
||||
class ServerStreamingHandler; |
||||
template <class ServiceType, class RequestType, class ResponseType> |
||||
class RpcMethodHandler; |
||||
template <class Streamer, bool WriteNeeded> |
||||
class TemplatedBidiStreamingHandler; |
||||
template <StatusCode code> |
||||
class ErrorMethodHandler; |
||||
class Call; |
||||
} // namespace internal
|
||||
|
||||
class ServerInterface; |
||||
namespace testing { |
||||
class InteropServerContextInspector; |
||||
class ServerContextTestSpouse; |
||||
} // namespace testing
|
||||
} // namespace grpc
|
||||
|
||||
namespace grpc_impl { |
||||
/// A ServerContext allows the person implementing a service handler to:
|
||||
///
|
||||
/// - Add custom initial and trailing metadata key-value pairs that will
|
||||
/// propagated to the client side.
|
||||
/// - Control call settings such as compression and authentication.
|
||||
/// - Access metadata coming from the client.
|
||||
/// - Get performance metrics (ie, census).
|
||||
///
|
||||
/// Context settings are only relevant to the call handler they are supplied to,
|
||||
/// that is to say, they aren't sticky across multiple calls. Some of these
|
||||
/// settings, such as the compression options, can be made persistent at server
|
||||
/// construction time by specifying the appropriate \a ChannelArguments
|
||||
/// to a \a grpc::ServerBuilder, via \a ServerBuilder::AddChannelArgument.
|
||||
///
|
||||
/// \warning ServerContext instances should \em not be reused across rpcs.
|
||||
class ServerContext { |
||||
public: |
||||
ServerContext(); // for async calls
|
||||
~ServerContext(); |
||||
|
||||
/// Return the deadline for the server call.
|
||||
std::chrono::system_clock::time_point deadline() const { |
||||
return ::grpc::Timespec2Timepoint(deadline_); |
||||
} |
||||
|
||||
/// Return a \a gpr_timespec representation of the server call's deadline.
|
||||
gpr_timespec raw_deadline() const { return deadline_; } |
||||
|
||||
/// Add the (\a key, \a value) pair to the initial metadata
|
||||
/// associated with a server call. These are made available at the client side
|
||||
/// by the \a grpc::ClientContext::GetServerInitialMetadata() method.
|
||||
///
|
||||
/// \warning This method should only be called before sending initial metadata
|
||||
/// to the client (which can happen explicitly, or implicitly when sending a
|
||||
/// a response message or status to the client).
|
||||
///
|
||||
/// \param key The metadata key. If \a value is binary data, it must
|
||||
/// end in "-bin".
|
||||
/// \param value The metadata value. If its value is binary, the key name
|
||||
/// must end in "-bin".
|
||||
///
|
||||
/// Metadata must conform to the following format:
|
||||
/// Custom-Metadata -> Binary-Header / ASCII-Header
|
||||
/// Binary-Header -> {Header-Name "-bin" } {binary value}
|
||||
/// ASCII-Header -> Header-Name ASCII-Value
|
||||
/// Header-Name -> 1*( %x30-39 / %x61-7A / "_" / "-" / ".") ; 0-9 a-z _ - .
|
||||
/// ASCII-Value -> 1*( %x20-%x7E ) ; space and printable ASCII
|
||||
void AddInitialMetadata(const grpc::string& key, const grpc::string& value); |
||||
|
||||
/// Add the (\a key, \a value) pair to the initial metadata
|
||||
/// associated with a server call. These are made available at the client
|
||||
/// side by the \a grpc::ClientContext::GetServerTrailingMetadata() method.
|
||||
///
|
||||
/// \warning This method should only be called before sending trailing
|
||||
/// metadata to the client (which happens when the call is finished and a
|
||||
/// status is sent to the client).
|
||||
///
|
||||
/// \param key The metadata key. If \a value is binary data,
|
||||
/// it must end in "-bin".
|
||||
/// \param value The metadata value. If its value is binary, the key name
|
||||
/// must end in "-bin".
|
||||
///
|
||||
/// Metadata must conform to the following format:
|
||||
/// Custom-Metadata -> Binary-Header / ASCII-Header
|
||||
/// Binary-Header -> {Header-Name "-bin" } {binary value}
|
||||
/// ASCII-Header -> Header-Name ASCII-Value
|
||||
/// Header-Name -> 1*( %x30-39 / %x61-7A / "_" / "-" / ".") ; 0-9 a-z _ - .
|
||||
/// ASCII-Value -> 1*( %x20-%x7E ) ; space and printable ASCII
|
||||
void AddTrailingMetadata(const grpc::string& key, const grpc::string& value); |
||||
|
||||
/// IsCancelled is always safe to call when using sync or callback API.
|
||||
/// When using async API, it is only safe to call IsCancelled after
|
||||
/// the AsyncNotifyWhenDone tag has been delivered.
|
||||
bool IsCancelled() const; |
||||
|
||||
/// Cancel the Call from the server. This is a best-effort API and
|
||||
/// depending on when it is called, the RPC may still appear successful to
|
||||
/// the client.
|
||||
/// For example, if TryCancel() is called on a separate thread, it might race
|
||||
/// with the server handler which might return success to the client before
|
||||
/// TryCancel() was even started by the thread.
|
||||
///
|
||||
/// It is the caller's responsibility to prevent such races and ensure that if
|
||||
/// TryCancel() is called, the serverhandler must return Status::CANCELLED.
|
||||
/// The only exception is that if the serverhandler is already returning an
|
||||
/// error status code, it is ok to not return Status::CANCELLED even if
|
||||
/// TryCancel() was called.
|
||||
///
|
||||
/// Note that TryCancel() does not change any of the tags that are pending
|
||||
/// on the completion queue. All pending tags will still be delivered
|
||||
/// (though their ok result may reflect the effect of cancellation).
|
||||
void TryCancel() const; |
||||
|
||||
/// Return a collection of initial metadata key-value pairs sent from the
|
||||
/// client. Note that keys may happen more than
|
||||
/// once (ie, a \a std::multimap is returned).
|
||||
///
|
||||
/// It is safe to use this method after initial metadata has been received,
|
||||
/// Calls always begin with the client sending initial metadata, so this is
|
||||
/// safe to access as soon as the call has begun on the server side.
|
||||
///
|
||||
/// \return A multimap of initial metadata key-value pairs from the server.
|
||||
const std::multimap<grpc::string_ref, grpc::string_ref>& client_metadata() |
||||
const { |
||||
return *client_metadata_.map(); |
||||
} |
||||
|
||||
/// Return the compression algorithm to be used by the server call.
|
||||
grpc_compression_level compression_level() const { |
||||
return compression_level_; |
||||
} |
||||
|
||||
/// Set \a level to be the compression level used for the server call.
|
||||
///
|
||||
/// \param level The compression level used for the server call.
|
||||
void set_compression_level(grpc_compression_level level) { |
||||
compression_level_set_ = true; |
||||
compression_level_ = level; |
||||
} |
||||
|
||||
/// Return a bool indicating whether the compression level for this call
|
||||
/// has been set (either implicitly or through a previous call to
|
||||
/// \a set_compression_level.
|
||||
bool compression_level_set() const { return compression_level_set_; } |
||||
|
||||
/// Return the compression algorithm the server call will request be used.
|
||||
/// Note that the gRPC runtime may decide to ignore this request, for example,
|
||||
/// due to resource constraints, or if the server is aware the client doesn't
|
||||
/// support the requested algorithm.
|
||||
grpc_compression_algorithm compression_algorithm() const { |
||||
return compression_algorithm_; |
||||
} |
||||
/// Set \a algorithm to be the compression algorithm used for the server call.
|
||||
///
|
||||
/// \param algorithm The compression algorithm used for the server call.
|
||||
void set_compression_algorithm(grpc_compression_algorithm algorithm); |
||||
|
||||
/// Set the serialized load reporting costs in \a cost_data for the call.
|
||||
void SetLoadReportingCosts(const std::vector<grpc::string>& cost_data); |
||||
|
||||
/// Return the authentication context for this server call.
|
||||
///
|
||||
/// \see grpc::AuthContext.
|
||||
std::shared_ptr<const ::grpc::AuthContext> auth_context() const { |
||||
if (auth_context_.get() == nullptr) { |
||||
auth_context_ = ::grpc::CreateAuthContext(call_); |
||||
} |
||||
return auth_context_; |
||||
} |
||||
|
||||
/// Return the peer uri in a string.
|
||||
/// WARNING: this value is never authenticated or subject to any security
|
||||
/// related code. It must not be used for any authentication related
|
||||
/// functionality. Instead, use auth_context.
|
||||
grpc::string peer() const; |
||||
|
||||
/// Get the census context associated with this server call.
|
||||
const struct census_context* census_context() const; |
||||
|
||||
/// Async only. Has to be called before the rpc starts.
|
||||
/// Returns the tag in completion queue when the rpc finishes.
|
||||
/// IsCancelled() can then be called to check whether the rpc was cancelled.
|
||||
/// TODO(vjpai): Fix this so that the tag is returned even if the call never
|
||||
/// starts (https://github.com/grpc/grpc/issues/10136).
|
||||
void AsyncNotifyWhenDone(void* tag) { |
||||
has_notify_when_done_tag_ = true; |
||||
async_notify_when_done_tag_ = tag; |
||||
} |
||||
|
||||
/// Should be used for framework-level extensions only.
|
||||
/// Applications never need to call this method.
|
||||
grpc_call* c_call() { return call_; } |
||||
|
||||
private: |
||||
friend class ::grpc::testing::InteropServerContextInspector; |
||||
friend class ::grpc::testing::ServerContextTestSpouse; |
||||
friend class ::grpc::ServerInterface; |
||||
friend class ::grpc_impl::Server; |
||||
template <class W, class R> |
||||
friend class ::grpc_impl::ServerAsyncReader; |
||||
template <class W> |
||||
friend class ::grpc_impl::ServerAsyncWriter; |
||||
template <class W> |
||||
friend class ::grpc_impl::ServerAsyncResponseWriter; |
||||
template <class W, class R> |
||||
friend class ::grpc_impl::ServerAsyncReaderWriter; |
||||
template <class R> |
||||
friend class ::grpc_impl::ServerReader; |
||||
template <class W> |
||||
friend class ::grpc_impl::ServerWriter; |
||||
template <class W, class R> |
||||
friend class ::grpc_impl::internal::ServerReaderWriterBody; |
||||
template <class ServiceType, class RequestType, class ResponseType> |
||||
friend class ::grpc::internal::RpcMethodHandler; |
||||
template <class ServiceType, class RequestType, class ResponseType> |
||||
friend class ::grpc::internal::ClientStreamingHandler; |
||||
template <class ServiceType, class RequestType, class ResponseType> |
||||
friend class ::grpc::internal::ServerStreamingHandler; |
||||
template <class Streamer, bool WriteNeeded> |
||||
friend class ::grpc::internal::TemplatedBidiStreamingHandler; |
||||
template <class RequestType, class ResponseType> |
||||
friend class ::grpc_impl::internal::CallbackUnaryHandler; |
||||
template <class RequestType, class ResponseType> |
||||
friend class ::grpc_impl::internal::CallbackClientStreamingHandler; |
||||
template <class RequestType, class ResponseType> |
||||
friend class ::grpc_impl::internal::CallbackServerStreamingHandler; |
||||
template <class RequestType, class ResponseType> |
||||
friend class ::grpc_impl::internal::CallbackBidiHandler; |
||||
template <::grpc::StatusCode code> |
||||
friend class ::grpc::internal::ErrorMethodHandler; |
||||
friend class ::grpc_impl::ClientContext; |
||||
friend class ::grpc::GenericServerContext; |
||||
|
||||
/// Prevent copying.
|
||||
ServerContext(const ServerContext&); |
||||
ServerContext& operator=(const ServerContext&); |
||||
|
||||
class CompletionOp; |
||||
|
||||
void BeginCompletionOp(::grpc::internal::Call* call, |
||||
std::function<void(bool)> callback, |
||||
::grpc_impl::internal::ServerReactor* reactor); |
||||
/// Return the tag queued by BeginCompletionOp()
|
||||
::grpc::internal::CompletionQueueTag* GetCompletionOpTag(); |
||||
|
||||
ServerContext(gpr_timespec deadline, grpc_metadata_array* arr); |
||||
|
||||
void set_call(grpc_call* call) { call_ = call; } |
||||
|
||||
void BindDeadlineAndMetadata(gpr_timespec deadline, grpc_metadata_array* arr); |
||||
|
||||
void Clear(); |
||||
|
||||
void Setup(gpr_timespec deadline); |
||||
|
||||
uint32_t initial_metadata_flags() const { return 0; } |
||||
|
||||
void SetCancelCallback(std::function<void()> callback); |
||||
void ClearCancelCallback(); |
||||
|
||||
::grpc::experimental::ServerRpcInfo* set_server_rpc_info( |
||||
const char* method, ::grpc::internal::RpcMethod::RpcType type, |
||||
const std::vector<std::unique_ptr< |
||||
::grpc::experimental::ServerInterceptorFactoryInterface>>& creators) { |
||||
if (creators.size() != 0) { |
||||
rpc_info_ = new ::grpc::experimental::ServerRpcInfo(this, method, type); |
||||
rpc_info_->RegisterInterceptors(creators); |
||||
} |
||||
return rpc_info_; |
||||
} |
||||
|
||||
CompletionOp* completion_op_; |
||||
bool has_notify_when_done_tag_; |
||||
void* async_notify_when_done_tag_; |
||||
::grpc::internal::CallbackWithSuccessTag completion_tag_; |
||||
|
||||
gpr_timespec deadline_; |
||||
grpc_call* call_; |
||||
::grpc_impl::CompletionQueue* cq_; |
||||
bool sent_initial_metadata_; |
||||
mutable std::shared_ptr<const ::grpc::AuthContext> auth_context_; |
||||
mutable ::grpc::internal::MetadataMap client_metadata_; |
||||
std::multimap<grpc::string, grpc::string> initial_metadata_; |
||||
std::multimap<grpc::string, grpc::string> trailing_metadata_; |
||||
|
||||
bool compression_level_set_; |
||||
grpc_compression_level compression_level_; |
||||
grpc_compression_algorithm compression_algorithm_; |
||||
|
||||
::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata, |
||||
::grpc::internal::CallOpSendMessage> |
||||
pending_ops_; |
||||
bool has_pending_ops_; |
||||
|
||||
::grpc::experimental::ServerRpcInfo* rpc_info_; |
||||
}; |
||||
} // namespace grpc_impl
|
||||
#endif // GRPCPP_IMPL_CODEGEN_SERVER_CONTEXT_IMPL_H
|
@ -0,0 +1,944 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2019 gRPC authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
#ifndef GRPCPP_IMPL_CODEGEN_SYNC_STREAM_IMPL_H |
||||
#define GRPCPP_IMPL_CODEGEN_SYNC_STREAM_IMPL_H |
||||
|
||||
#include <grpcpp/impl/codegen/call.h> |
||||
#include <grpcpp/impl/codegen/channel_interface.h> |
||||
#include <grpcpp/impl/codegen/client_context_impl.h> |
||||
#include <grpcpp/impl/codegen/completion_queue_impl.h> |
||||
#include <grpcpp/impl/codegen/core_codegen_interface.h> |
||||
#include <grpcpp/impl/codegen/server_context_impl.h> |
||||
#include <grpcpp/impl/codegen/service_type.h> |
||||
#include <grpcpp/impl/codegen/status.h> |
||||
|
||||
namespace grpc_impl { |
||||
|
||||
namespace internal { |
||||
/// Common interface for all synchronous client side streaming.
|
||||
class ClientStreamingInterface { |
||||
public: |
||||
virtual ~ClientStreamingInterface() {} |
||||
|
||||
/// Block waiting until the stream finishes and a final status of the call is
|
||||
/// available.
|
||||
///
|
||||
/// It is appropriate to call this method when both:
|
||||
/// * the calling code (client-side) has no more message to send
|
||||
/// (this can be declared implicitly by calling this method, or
|
||||
/// explicitly through an earlier call to <i>WritesDone</i> method of the
|
||||
/// class in use, e.g. \a ClientWriterInterface::WritesDone or
|
||||
/// \a ClientReaderWriterInterface::WritesDone).
|
||||
/// * there are no more messages to be received from the server (which can
|
||||
/// be known implicitly, or explicitly from an earlier call to \a
|
||||
/// ReaderInterface::Read that returned "false").
|
||||
///
|
||||
/// This function will return either:
|
||||
/// - when all incoming messages have been read and the server has
|
||||
/// returned status.
|
||||
/// - when the server has returned a non-OK status.
|
||||
/// - OR when the call failed for some reason and the library generated a
|
||||
/// status.
|
||||
///
|
||||
/// Return values:
|
||||
/// - \a Status contains the status code, message and details for the call
|
||||
/// - the \a ClientContext associated with this call is updated with
|
||||
/// possible trailing metadata sent from the server.
|
||||
virtual ::grpc::Status Finish() = 0; |
||||
}; |
||||
|
||||
/// Common interface for all synchronous server side streaming.
|
||||
class ServerStreamingInterface { |
||||
public: |
||||
virtual ~ServerStreamingInterface() {} |
||||
|
||||
/// Block to send initial metadata to client.
|
||||
/// This call is optional, but if it is used, it cannot be used concurrently
|
||||
/// with or after the \a Finish method.
|
||||
///
|
||||
/// The initial metadata that will be sent to the client will be
|
||||
/// taken from the \a ServerContext associated with the call.
|
||||
virtual void SendInitialMetadata() = 0; |
||||
}; |
||||
|
||||
/// An interface that yields a sequence of messages of type \a R.
|
||||
template <class R> |
||||
class ReaderInterface { |
||||
public: |
||||
virtual ~ReaderInterface() {} |
||||
|
||||
/// Get an upper bound on the next message size available for reading on this
|
||||
/// stream.
|
||||
virtual bool NextMessageSize(uint32_t* sz) = 0; |
||||
|
||||
/// Block to read a message and parse to \a msg. Returns \a true on success.
|
||||
/// This is thread-safe with respect to \a Write or \WritesDone methods on
|
||||
/// the same stream. It should not be called concurrently with another \a
|
||||
/// Read on the same stream as the order of delivery will not be defined.
|
||||
///
|
||||
/// \param[out] msg The read message.
|
||||
///
|
||||
/// \return \a false when there will be no more incoming messages, either
|
||||
/// because the other side has called \a WritesDone() or the stream has failed
|
||||
/// (or been cancelled).
|
||||
virtual bool Read(R* msg) = 0; |
||||
}; |
||||
|
||||
/// An interface that can be fed a sequence of messages of type \a W.
|
||||
template <class W> |
||||
class WriterInterface { |
||||
public: |
||||
virtual ~WriterInterface() {} |
||||
|
||||
/// Block to write \a msg to the stream with WriteOptions \a options.
|
||||
/// This is thread-safe with respect to \a ReaderInterface::Read
|
||||
///
|
||||
/// \param msg The message to be written to the stream.
|
||||
/// \param options The WriteOptions affecting the write operation.
|
||||
///
|
||||
/// \return \a true on success, \a false when the stream has been closed.
|
||||
virtual bool Write(const W& msg, ::grpc::WriteOptions options) = 0; |
||||
|
||||
/// Block to write \a msg to the stream with default write options.
|
||||
/// This is thread-safe with respect to \a ReaderInterface::Read
|
||||
///
|
||||
/// \param msg The message to be written to the stream.
|
||||
///
|
||||
/// \return \a true on success, \a false when the stream has been closed.
|
||||
inline bool Write(const W& msg) { return Write(msg, ::grpc::WriteOptions()); } |
||||
|
||||
/// Write \a msg and coalesce it with the writing of trailing metadata, using
|
||||
/// WriteOptions \a options.
|
||||
///
|
||||
/// For client, WriteLast is equivalent of performing Write and WritesDone in
|
||||
/// a single step. \a msg and trailing metadata are coalesced and sent on wire
|
||||
/// by calling this function. For server, WriteLast buffers the \a msg.
|
||||
/// The writing of \a msg is held until the service handler returns,
|
||||
/// where \a msg and trailing metadata are coalesced and sent on wire.
|
||||
/// Note that WriteLast can only buffer \a msg up to the flow control window
|
||||
/// size. If \a msg size is larger than the window size, it will be sent on
|
||||
/// wire without buffering.
|
||||
///
|
||||
/// \param[in] msg The message to be written to the stream.
|
||||
/// \param[in] options The WriteOptions to be used to write this message.
|
||||
void WriteLast(const W& msg, ::grpc::WriteOptions options) { |
||||
Write(msg, options.set_last_message()); |
||||
} |
||||
}; |
||||
|
||||
} // namespace internal
|
||||
|
||||
/// Client-side interface for streaming reads of message of type \a R.
|
||||
template <class R> |
||||
class ClientReaderInterface : public internal::ClientStreamingInterface, |
||||
public internal::ReaderInterface<R> { |
||||
public: |
||||
/// Block to wait for initial metadata from server. The received metadata
|
||||
/// can only be accessed after this call returns. Should only be called before
|
||||
/// the first read. Calling this method is optional, and if it is not called
|
||||
/// the metadata will be available in ClientContext after the first read.
|
||||
virtual void WaitForInitialMetadata() = 0; |
||||
}; |
||||
|
||||
namespace internal { |
||||
template <class R> |
||||
class ClientReaderFactory { |
||||
public: |
||||
template <class W> |
||||
static ClientReader<R>* Create(::grpc::ChannelInterface* channel, |
||||
const ::grpc::internal::RpcMethod& method, |
||||
::grpc_impl::ClientContext* context, |
||||
const W& request) { |
||||
return new ClientReader<R>(channel, method, context, request); |
||||
} |
||||
}; |
||||
} // namespace internal
|
||||
|
||||
/// Synchronous (blocking) client-side API for doing server-streaming RPCs,
|
||||
/// where the stream of messages coming from the server has messages
|
||||
/// of type \a R.
|
||||
template <class R> |
||||
class ClientReader final : public ClientReaderInterface<R> { |
||||
public: |
||||
/// See the \a ClientStreamingInterface.WaitForInitialMetadata method for
|
||||
/// semantics.
|
||||
///
|
||||
// Side effect:
|
||||
/// Once complete, the initial metadata read from
|
||||
/// the server will be accessible through the \a ClientContext used to
|
||||
/// construct this object.
|
||||
void WaitForInitialMetadata() override { |
||||
GPR_CODEGEN_ASSERT(!context_->initial_metadata_received_); |
||||
|
||||
::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvInitialMetadata> |
||||
ops; |
||||
ops.RecvInitialMetadata(context_); |
||||
call_.PerformOps(&ops); |
||||
cq_.Pluck(&ops); /// status ignored
|
||||
} |
||||
|
||||
bool NextMessageSize(uint32_t* sz) override { |
||||
*sz = call_.max_receive_message_size(); |
||||
return true; |
||||
} |
||||
|
||||
/// See the \a ReaderInterface.Read method for semantics.
|
||||
/// Side effect:
|
||||
/// This also receives initial metadata from the server, if not
|
||||
/// already received (if initial metadata is received, it can be then
|
||||
/// accessed through the \a ClientContext associated with this call).
|
||||
bool Read(R* msg) override { |
||||
::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvInitialMetadata, |
||||
::grpc::internal::CallOpRecvMessage<R>> |
||||
ops; |
||||
if (!context_->initial_metadata_received_) { |
||||
ops.RecvInitialMetadata(context_); |
||||
} |
||||
ops.RecvMessage(msg); |
||||
call_.PerformOps(&ops); |
||||
return cq_.Pluck(&ops) && ops.got_message; |
||||
} |
||||
|
||||
/// See the \a ClientStreamingInterface.Finish method for semantics.
|
||||
///
|
||||
/// Side effect:
|
||||
/// The \a ClientContext associated with this call is updated with
|
||||
/// possible metadata received from the server.
|
||||
::grpc::Status Finish() override { |
||||
::grpc::internal::CallOpSet<::grpc::internal::CallOpClientRecvStatus> ops; |
||||
::grpc::Status status; |
||||
ops.ClientRecvStatus(context_, &status); |
||||
call_.PerformOps(&ops); |
||||
GPR_CODEGEN_ASSERT(cq_.Pluck(&ops)); |
||||
return status; |
||||
} |
||||
|
||||
private: |
||||
friend class internal::ClientReaderFactory<R>; |
||||
::grpc_impl::ClientContext* context_; |
||||
::grpc_impl::CompletionQueue cq_; |
||||
::grpc::internal::Call call_; |
||||
|
||||
/// Block to create a stream and write the initial metadata and \a request
|
||||
/// out. Note that \a context will be used to fill in custom initial
|
||||
/// metadata used to send to the server when starting the call.
|
||||
template <class W> |
||||
ClientReader(::grpc::ChannelInterface* channel, |
||||
const ::grpc::internal::RpcMethod& method, |
||||
::grpc_impl::ClientContext* context, const W& request) |
||||
: context_(context), |
||||
cq_(grpc_completion_queue_attributes{ |
||||
GRPC_CQ_CURRENT_VERSION, GRPC_CQ_PLUCK, GRPC_CQ_DEFAULT_POLLING, |
||||
nullptr}), // Pluckable cq
|
||||
call_(channel->CreateCall(method, context, &cq_)) { |
||||
::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata, |
||||
::grpc::internal::CallOpSendMessage, |
||||
::grpc::internal::CallOpClientSendClose> |
||||
ops; |
||||
ops.SendInitialMetadata(&context->send_initial_metadata_, |
||||
context->initial_metadata_flags()); |
||||
// TODO(ctiller): don't assert
|
||||
GPR_CODEGEN_ASSERT(ops.SendMessagePtr(&request).ok()); |
||||
ops.ClientSendClose(); |
||||
call_.PerformOps(&ops); |
||||
cq_.Pluck(&ops); |
||||
} |
||||
}; |
||||
|
||||
/// Client-side interface for streaming writes of message type \a W.
|
||||
template <class W> |
||||
class ClientWriterInterface : public internal::ClientStreamingInterface, |
||||
public internal::WriterInterface<W> { |
||||
public: |
||||
/// Half close writing from the client. (signal that the stream of messages
|
||||
/// coming from the client is complete).
|
||||
/// Blocks until currently-pending writes are completed.
|
||||
/// Thread safe with respect to \a ReaderInterface::Read operations only
|
||||
///
|
||||
/// \return Whether the writes were successful.
|
||||
virtual bool WritesDone() = 0; |
||||
}; |
||||
|
||||
namespace internal { |
||||
template <class W> |
||||
class ClientWriterFactory { |
||||
public: |
||||
template <class R> |
||||
static ClientWriter<W>* Create(::grpc::ChannelInterface* channel, |
||||
const ::grpc::internal::RpcMethod& method, |
||||
::grpc_impl::ClientContext* context, |
||||
R* response) { |
||||
return new ClientWriter<W>(channel, method, context, response); |
||||
} |
||||
}; |
||||
} // namespace internal
|
||||
|
||||
/// Synchronous (blocking) client-side API for doing client-streaming RPCs,
|
||||
/// where the outgoing message stream coming from the client has messages of
|
||||
/// type \a W.
|
||||
template <class W> |
||||
class ClientWriter : public ClientWriterInterface<W> { |
||||
public: |
||||
/// See the \a ClientStreamingInterface.WaitForInitialMetadata method for
|
||||
/// semantics.
|
||||
///
|
||||
// Side effect:
|
||||
/// Once complete, the initial metadata read from the server will be
|
||||
/// accessible through the \a ClientContext used to construct this object.
|
||||
void WaitForInitialMetadata() { |
||||
GPR_CODEGEN_ASSERT(!context_->initial_metadata_received_); |
||||
|
||||
::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvInitialMetadata> |
||||
ops; |
||||
ops.RecvInitialMetadata(context_); |
||||
call_.PerformOps(&ops); |
||||
cq_.Pluck(&ops); // status ignored
|
||||
} |
||||
|
||||
/// See the WriterInterface.Write(const W& msg, WriteOptions options) method
|
||||
/// for semantics.
|
||||
///
|
||||
/// Side effect:
|
||||
/// Also sends initial metadata if not already sent (using the
|
||||
/// \a ClientContext associated with this call).
|
||||
using internal::WriterInterface<W>::Write; |
||||
bool Write(const W& msg, ::grpc::WriteOptions options) override { |
||||
::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata, |
||||
::grpc::internal::CallOpSendMessage, |
||||
::grpc::internal::CallOpClientSendClose> |
||||
ops; |
||||
|
||||
if (options.is_last_message()) { |
||||
options.set_buffer_hint(); |
||||
ops.ClientSendClose(); |
||||
} |
||||
if (context_->initial_metadata_corked_) { |
||||
ops.SendInitialMetadata(&context_->send_initial_metadata_, |
||||
context_->initial_metadata_flags()); |
||||
context_->set_initial_metadata_corked(false); |
||||
} |
||||
if (!ops.SendMessagePtr(&msg, options).ok()) { |
||||
return false; |
||||
} |
||||
|
||||
call_.PerformOps(&ops); |
||||
return cq_.Pluck(&ops); |
||||
} |
||||
|
||||
bool WritesDone() override { |
||||
::grpc::internal::CallOpSet<::grpc::internal::CallOpClientSendClose> ops; |
||||
ops.ClientSendClose(); |
||||
call_.PerformOps(&ops); |
||||
return cq_.Pluck(&ops); |
||||
} |
||||
|
||||
/// See the ClientStreamingInterface.Finish method for semantics.
|
||||
/// Side effects:
|
||||
/// - Also receives initial metadata if not already received.
|
||||
/// - Attempts to fill in the \a response parameter passed
|
||||
/// to the constructor of this instance with the response
|
||||
/// message from the server.
|
||||
::grpc::Status Finish() override { |
||||
::grpc::Status status; |
||||
if (!context_->initial_metadata_received_) { |
||||
finish_ops_.RecvInitialMetadata(context_); |
||||
} |
||||
finish_ops_.ClientRecvStatus(context_, &status); |
||||
call_.PerformOps(&finish_ops_); |
||||
GPR_CODEGEN_ASSERT(cq_.Pluck(&finish_ops_)); |
||||
return status; |
||||
} |
||||
|
||||
private: |
||||
friend class internal::ClientWriterFactory<W>; |
||||
|
||||
/// Block to create a stream (i.e. send request headers and other initial
|
||||
/// metadata to the server). Note that \a context will be used to fill
|
||||
/// in custom initial metadata. \a response will be filled in with the
|
||||
/// single expected response message from the server upon a successful
|
||||
/// call to the \a Finish method of this instance.
|
||||
template <class R> |
||||
ClientWriter(::grpc::ChannelInterface* channel, |
||||
const ::grpc::internal::RpcMethod& method, |
||||
::grpc_impl::ClientContext* context, R* response) |
||||
: context_(context), |
||||
cq_(grpc_completion_queue_attributes{ |
||||
GRPC_CQ_CURRENT_VERSION, GRPC_CQ_PLUCK, GRPC_CQ_DEFAULT_POLLING, |
||||
nullptr}), // Pluckable cq
|
||||
call_(channel->CreateCall(method, context, &cq_)) { |
||||
finish_ops_.RecvMessage(response); |
||||
finish_ops_.AllowNoMessage(); |
||||
|
||||
if (!context_->initial_metadata_corked_) { |
||||
::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata> |
||||
ops; |
||||
ops.SendInitialMetadata(&context->send_initial_metadata_, |
||||
context->initial_metadata_flags()); |
||||
call_.PerformOps(&ops); |
||||
cq_.Pluck(&ops); |
||||
} |
||||
} |
||||
|
||||
::grpc_impl::ClientContext* context_; |
||||
::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvInitialMetadata, |
||||
::grpc::internal::CallOpGenericRecvMessage, |
||||
::grpc::internal::CallOpClientRecvStatus> |
||||
finish_ops_; |
||||
::grpc_impl::CompletionQueue cq_; |
||||
::grpc::internal::Call call_; |
||||
}; |
||||
|
||||
/// Client-side interface for bi-directional streaming with
|
||||
/// client-to-server stream messages of type \a W and
|
||||
/// server-to-client stream messages of type \a R.
|
||||
template <class W, class R> |
||||
class ClientReaderWriterInterface : public internal::ClientStreamingInterface, |
||||
public internal::WriterInterface<W>, |
||||
public internal::ReaderInterface<R> { |
||||
public: |
||||
/// Block to wait for initial metadata from server. The received metadata
|
||||
/// can only be accessed after this call returns. Should only be called before
|
||||
/// the first read. Calling this method is optional, and if it is not called
|
||||
/// the metadata will be available in ClientContext after the first read.
|
||||
virtual void WaitForInitialMetadata() = 0; |
||||
|
||||
/// Half close writing from the client. (signal that the stream of messages
|
||||
/// coming from the clinet is complete).
|
||||
/// Blocks until currently-pending writes are completed.
|
||||
/// Thread-safe with respect to \a ReaderInterface::Read
|
||||
///
|
||||
/// \return Whether the writes were successful.
|
||||
virtual bool WritesDone() = 0; |
||||
}; |
||||
|
||||
namespace internal { |
||||
template <class W, class R> |
||||
class ClientReaderWriterFactory { |
||||
public: |
||||
static ClientReaderWriter<W, R>* Create( |
||||
::grpc::ChannelInterface* channel, |
||||
const ::grpc::internal::RpcMethod& method, |
||||
::grpc_impl::ClientContext* context) { |
||||
return new ClientReaderWriter<W, R>(channel, method, context); |
||||
} |
||||
}; |
||||
} // namespace internal
|
||||
|
||||
/// Synchronous (blocking) client-side API for bi-directional streaming RPCs,
|
||||
/// where the outgoing message stream coming from the client has messages of
|
||||
/// type \a W, and the incoming messages stream coming from the server has
|
||||
/// messages of type \a R.
|
||||
template <class W, class R> |
||||
class ClientReaderWriter final : public ClientReaderWriterInterface<W, R> { |
||||
public: |
||||
/// Block waiting to read initial metadata from the server.
|
||||
/// This call is optional, but if it is used, it cannot be used concurrently
|
||||
/// with or after the \a Finish method.
|
||||
///
|
||||
/// Once complete, the initial metadata read from the server will be
|
||||
/// accessible through the \a ClientContext used to construct this object.
|
||||
void WaitForInitialMetadata() override { |
||||
GPR_CODEGEN_ASSERT(!context_->initial_metadata_received_); |
||||
|
||||
::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvInitialMetadata> |
||||
ops; |
||||
ops.RecvInitialMetadata(context_); |
||||
call_.PerformOps(&ops); |
||||
cq_.Pluck(&ops); // status ignored
|
||||
} |
||||
|
||||
bool NextMessageSize(uint32_t* sz) override { |
||||
*sz = call_.max_receive_message_size(); |
||||
return true; |
||||
} |
||||
|
||||
/// See the \a ReaderInterface.Read method for semantics.
|
||||
/// Side effect:
|
||||
/// Also receives initial metadata if not already received (updates the \a
|
||||
/// ClientContext associated with this call in that case).
|
||||
bool Read(R* msg) override { |
||||
::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvInitialMetadata, |
||||
::grpc::internal::CallOpRecvMessage<R>> |
||||
ops; |
||||
if (!context_->initial_metadata_received_) { |
||||
ops.RecvInitialMetadata(context_); |
||||
} |
||||
ops.RecvMessage(msg); |
||||
call_.PerformOps(&ops); |
||||
return cq_.Pluck(&ops) && ops.got_message; |
||||
} |
||||
|
||||
/// See the \a WriterInterface.Write method for semantics.
|
||||
///
|
||||
/// Side effect:
|
||||
/// Also sends initial metadata if not already sent (using the
|
||||
/// \a ClientContext associated with this call to fill in values).
|
||||
using internal::WriterInterface<W>::Write; |
||||
bool Write(const W& msg, ::grpc::WriteOptions options) override { |
||||
::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata, |
||||
::grpc::internal::CallOpSendMessage, |
||||
::grpc::internal::CallOpClientSendClose> |
||||
ops; |
||||
|
||||
if (options.is_last_message()) { |
||||
options.set_buffer_hint(); |
||||
ops.ClientSendClose(); |
||||
} |
||||
if (context_->initial_metadata_corked_) { |
||||
ops.SendInitialMetadata(&context_->send_initial_metadata_, |
||||
context_->initial_metadata_flags()); |
||||
context_->set_initial_metadata_corked(false); |
||||
} |
||||
if (!ops.SendMessagePtr(&msg, options).ok()) { |
||||
return false; |
||||
} |
||||
|
||||
call_.PerformOps(&ops); |
||||
return cq_.Pluck(&ops); |
||||
} |
||||
|
||||
bool WritesDone() override { |
||||
::grpc::internal::CallOpSet<::grpc::internal::CallOpClientSendClose> ops; |
||||
ops.ClientSendClose(); |
||||
call_.PerformOps(&ops); |
||||
return cq_.Pluck(&ops); |
||||
} |
||||
|
||||
/// See the ClientStreamingInterface.Finish method for semantics.
|
||||
///
|
||||
/// Side effect:
|
||||
/// - the \a ClientContext associated with this call is updated with
|
||||
/// possible trailing metadata sent from the server.
|
||||
::grpc::Status Finish() override { |
||||
::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvInitialMetadata, |
||||
::grpc::internal::CallOpClientRecvStatus> |
||||
ops; |
||||
if (!context_->initial_metadata_received_) { |
||||
ops.RecvInitialMetadata(context_); |
||||
} |
||||
::grpc::Status status; |
||||
ops.ClientRecvStatus(context_, &status); |
||||
call_.PerformOps(&ops); |
||||
GPR_CODEGEN_ASSERT(cq_.Pluck(&ops)); |
||||
return status; |
||||
} |
||||
|
||||
private: |
||||
friend class internal::ClientReaderWriterFactory<W, R>; |
||||
|
||||
::grpc_impl::ClientContext* context_; |
||||
::grpc_impl::CompletionQueue cq_; |
||||
::grpc::internal::Call call_; |
||||
|
||||
/// Block to create a stream and write the initial metadata and \a request
|
||||
/// out. Note that \a context will be used to fill in custom initial metadata
|
||||
/// used to send to the server when starting the call.
|
||||
ClientReaderWriter(::grpc::ChannelInterface* channel, |
||||
const ::grpc::internal::RpcMethod& method, |
||||
::grpc_impl::ClientContext* context) |
||||
: context_(context), |
||||
cq_(grpc_completion_queue_attributes{ |
||||
GRPC_CQ_CURRENT_VERSION, GRPC_CQ_PLUCK, GRPC_CQ_DEFAULT_POLLING, |
||||
nullptr}), // Pluckable cq
|
||||
call_(channel->CreateCall(method, context, &cq_)) { |
||||
if (!context_->initial_metadata_corked_) { |
||||
::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata> |
||||
ops; |
||||
ops.SendInitialMetadata(&context->send_initial_metadata_, |
||||
context->initial_metadata_flags()); |
||||
call_.PerformOps(&ops); |
||||
cq_.Pluck(&ops); |
||||
} |
||||
} |
||||
}; |
||||
|
||||
/// Server-side interface for streaming reads of message of type \a R.
|
||||
template <class R> |
||||
class ServerReaderInterface : public internal::ServerStreamingInterface, |
||||
public internal::ReaderInterface<R> {}; |
||||
|
||||
/// Synchronous (blocking) server-side API for doing client-streaming RPCs,
|
||||
/// where the incoming message stream coming from the client has messages of
|
||||
/// type \a R.
|
||||
template <class R> |
||||
class ServerReader final : public ServerReaderInterface<R> { |
||||
public: |
||||
/// See the \a ServerStreamingInterface.SendInitialMetadata method
|
||||
/// for semantics. Note that initial metadata will be affected by the
|
||||
/// \a ServerContext associated with this call.
|
||||
void SendInitialMetadata() override { |
||||
GPR_CODEGEN_ASSERT(!ctx_->sent_initial_metadata_); |
||||
|
||||
::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata> |
||||
ops; |
||||
ops.SendInitialMetadata(&ctx_->initial_metadata_, |
||||
ctx_->initial_metadata_flags()); |
||||
if (ctx_->compression_level_set()) { |
||||
ops.set_compression_level(ctx_->compression_level()); |
||||
} |
||||
ctx_->sent_initial_metadata_ = true; |
||||
call_->PerformOps(&ops); |
||||
call_->cq()->Pluck(&ops); |
||||
} |
||||
|
||||
bool NextMessageSize(uint32_t* sz) override { |
||||
*sz = call_->max_receive_message_size(); |
||||
return true; |
||||
} |
||||
|
||||
bool Read(R* msg) override { |
||||
::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvMessage<R>> ops; |
||||
ops.RecvMessage(msg); |
||||
call_->PerformOps(&ops); |
||||
return call_->cq()->Pluck(&ops) && ops.got_message; |
||||
} |
||||
|
||||
private: |
||||
::grpc::internal::Call* const call_; |
||||
ServerContext* const ctx_; |
||||
|
||||
template <class ServiceType, class RequestType, class ResponseType> |
||||
friend class ::grpc::internal::ClientStreamingHandler; |
||||
|
||||
ServerReader(::grpc::internal::Call* call, ::grpc_impl::ServerContext* ctx) |
||||
: call_(call), ctx_(ctx) {} |
||||
}; |
||||
|
||||
/// Server-side interface for streaming writes of message of type \a W.
|
||||
template <class W> |
||||
class ServerWriterInterface : public internal::ServerStreamingInterface, |
||||
public internal::WriterInterface<W> {}; |
||||
|
||||
/// Synchronous (blocking) server-side API for doing for doing a
|
||||
/// server-streaming RPCs, where the outgoing message stream coming from the
|
||||
/// server has messages of type \a W.
|
||||
template <class W> |
||||
class ServerWriter final : public ServerWriterInterface<W> { |
||||
public: |
||||
/// See the \a ServerStreamingInterface.SendInitialMetadata method
|
||||
/// for semantics.
|
||||
/// Note that initial metadata will be affected by the
|
||||
/// \a ServerContext associated with this call.
|
||||
void SendInitialMetadata() override { |
||||
GPR_CODEGEN_ASSERT(!ctx_->sent_initial_metadata_); |
||||
|
||||
::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata> |
||||
ops; |
||||
ops.SendInitialMetadata(&ctx_->initial_metadata_, |
||||
ctx_->initial_metadata_flags()); |
||||
if (ctx_->compression_level_set()) { |
||||
ops.set_compression_level(ctx_->compression_level()); |
||||
} |
||||
ctx_->sent_initial_metadata_ = true; |
||||
call_->PerformOps(&ops); |
||||
call_->cq()->Pluck(&ops); |
||||
} |
||||
|
||||
/// See the \a WriterInterface.Write method for semantics.
|
||||
///
|
||||
/// Side effect:
|
||||
/// Also sends initial metadata if not already sent (using the
|
||||
/// \a ClientContext associated with this call to fill in values).
|
||||
using internal::WriterInterface<W>::Write; |
||||
bool Write(const W& msg, ::grpc::WriteOptions options) override { |
||||
if (options.is_last_message()) { |
||||
options.set_buffer_hint(); |
||||
} |
||||
|
||||
if (!ctx_->pending_ops_.SendMessagePtr(&msg, options).ok()) { |
||||
return false; |
||||
} |
||||
if (!ctx_->sent_initial_metadata_) { |
||||
ctx_->pending_ops_.SendInitialMetadata(&ctx_->initial_metadata_, |
||||
ctx_->initial_metadata_flags()); |
||||
if (ctx_->compression_level_set()) { |
||||
ctx_->pending_ops_.set_compression_level(ctx_->compression_level()); |
||||
} |
||||
ctx_->sent_initial_metadata_ = true; |
||||
} |
||||
call_->PerformOps(&ctx_->pending_ops_); |
||||
// if this is the last message we defer the pluck until AFTER we start
|
||||
// the trailing md op. This prevents hangs. See
|
||||
// https://github.com/grpc/grpc/issues/11546
|
||||
if (options.is_last_message()) { |
||||
ctx_->has_pending_ops_ = true; |
||||
return true; |
||||
} |
||||
ctx_->has_pending_ops_ = false; |
||||
return call_->cq()->Pluck(&ctx_->pending_ops_); |
||||
} |
||||
|
||||
private: |
||||
::grpc::internal::Call* const call_; |
||||
::grpc_impl::ServerContext* const ctx_; |
||||
|
||||
template <class ServiceType, class RequestType, class ResponseType> |
||||
friend class ::grpc::internal::ServerStreamingHandler; |
||||
|
||||
ServerWriter(::grpc::internal::Call* call, ::grpc_impl::ServerContext* ctx) |
||||
: call_(call), ctx_(ctx) {} |
||||
}; |
||||
|
||||
/// Server-side interface for bi-directional streaming.
|
||||
template <class W, class R> |
||||
class ServerReaderWriterInterface : public internal::ServerStreamingInterface, |
||||
public internal::WriterInterface<W>, |
||||
public internal::ReaderInterface<R> {}; |
||||
|
||||
/// Actual implementation of bi-directional streaming
|
||||
namespace internal { |
||||
template <class W, class R> |
||||
class ServerReaderWriterBody final { |
||||
public: |
||||
ServerReaderWriterBody(grpc::internal::Call* call, |
||||
::grpc_impl::ServerContext* ctx) |
||||
: call_(call), ctx_(ctx) {} |
||||
|
||||
void SendInitialMetadata() { |
||||
GPR_CODEGEN_ASSERT(!ctx_->sent_initial_metadata_); |
||||
|
||||
grpc::internal::CallOpSet<grpc::internal::CallOpSendInitialMetadata> ops; |
||||
ops.SendInitialMetadata(&ctx_->initial_metadata_, |
||||
ctx_->initial_metadata_flags()); |
||||
if (ctx_->compression_level_set()) { |
||||
ops.set_compression_level(ctx_->compression_level()); |
||||
} |
||||
ctx_->sent_initial_metadata_ = true; |
||||
call_->PerformOps(&ops); |
||||
call_->cq()->Pluck(&ops); |
||||
} |
||||
|
||||
bool NextMessageSize(uint32_t* sz) { |
||||
*sz = call_->max_receive_message_size(); |
||||
return true; |
||||
} |
||||
|
||||
bool Read(R* msg) { |
||||
::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvMessage<R>> ops; |
||||
ops.RecvMessage(msg); |
||||
call_->PerformOps(&ops); |
||||
return call_->cq()->Pluck(&ops) && ops.got_message; |
||||
} |
||||
|
||||
bool Write(const W& msg, ::grpc::WriteOptions options) { |
||||
if (options.is_last_message()) { |
||||
options.set_buffer_hint(); |
||||
} |
||||
if (!ctx_->pending_ops_.SendMessagePtr(&msg, options).ok()) { |
||||
return false; |
||||
} |
||||
if (!ctx_->sent_initial_metadata_) { |
||||
ctx_->pending_ops_.SendInitialMetadata(&ctx_->initial_metadata_, |
||||
ctx_->initial_metadata_flags()); |
||||
if (ctx_->compression_level_set()) { |
||||
ctx_->pending_ops_.set_compression_level(ctx_->compression_level()); |
||||
} |
||||
ctx_->sent_initial_metadata_ = true; |
||||
} |
||||
call_->PerformOps(&ctx_->pending_ops_); |
||||
// if this is the last message we defer the pluck until AFTER we start
|
||||
// the trailing md op. This prevents hangs. See
|
||||
// https://github.com/grpc/grpc/issues/11546
|
||||
if (options.is_last_message()) { |
||||
ctx_->has_pending_ops_ = true; |
||||
return true; |
||||
} |
||||
ctx_->has_pending_ops_ = false; |
||||
return call_->cq()->Pluck(&ctx_->pending_ops_); |
||||
} |
||||
|
||||
private: |
||||
grpc::internal::Call* const call_; |
||||
::grpc_impl::ServerContext* const ctx_; |
||||
}; |
||||
|
||||
} // namespace internal
|
||||
|
||||
/// Synchronous (blocking) server-side API for a bidirectional
|
||||
/// streaming call, where the incoming message stream coming from the client has
|
||||
/// messages of type \a R, and the outgoing message streaming coming from
|
||||
/// the server has messages of type \a W.
|
||||
template <class W, class R> |
||||
class ServerReaderWriter final : public ServerReaderWriterInterface<W, R> { |
||||
public: |
||||
/// See the \a ServerStreamingInterface.SendInitialMetadata method
|
||||
/// for semantics. Note that initial metadata will be affected by the
|
||||
/// \a ServerContext associated with this call.
|
||||
void SendInitialMetadata() override { body_.SendInitialMetadata(); } |
||||
|
||||
bool NextMessageSize(uint32_t* sz) override { |
||||
return body_.NextMessageSize(sz); |
||||
} |
||||
|
||||
bool Read(R* msg) override { return body_.Read(msg); } |
||||
|
||||
/// See the \a WriterInterface.Write(const W& msg, WriteOptions options)
|
||||
/// method for semantics.
|
||||
/// Side effect:
|
||||
/// Also sends initial metadata if not already sent (using the \a
|
||||
/// ServerContext associated with this call).
|
||||
using internal::WriterInterface<W>::Write; |
||||
bool Write(const W& msg, ::grpc::WriteOptions options) override { |
||||
return body_.Write(msg, options); |
||||
} |
||||
|
||||
private: |
||||
internal::ServerReaderWriterBody<W, R> body_; |
||||
|
||||
friend class ::grpc::internal::TemplatedBidiStreamingHandler< |
||||
ServerReaderWriter<W, R>, false>; |
||||
ServerReaderWriter(::grpc::internal::Call* call, |
||||
::grpc_impl::ServerContext* ctx) |
||||
: body_(call, ctx) {} |
||||
}; |
||||
|
||||
/// A class to represent a flow-controlled unary call. This is something
|
||||
/// of a hybrid between conventional unary and streaming. This is invoked
|
||||
/// through a unary call on the client side, but the server responds to it
|
||||
/// as though it were a single-ping-pong streaming call. The server can use
|
||||
/// the \a NextMessageSize method to determine an upper-bound on the size of
|
||||
/// the message. A key difference relative to streaming: ServerUnaryStreamer
|
||||
/// must have exactly 1 Read and exactly 1 Write, in that order, to function
|
||||
/// correctly. Otherwise, the RPC is in error.
|
||||
template <class RequestType, class ResponseType> |
||||
class ServerUnaryStreamer final |
||||
: public ServerReaderWriterInterface<ResponseType, RequestType> { |
||||
public: |
||||
/// Block to send initial metadata to client.
|
||||
/// Implicit input parameter:
|
||||
/// - the \a ServerContext associated with this call will be used for
|
||||
/// sending initial metadata.
|
||||
void SendInitialMetadata() override { body_.SendInitialMetadata(); } |
||||
|
||||
/// Get an upper bound on the request message size from the client.
|
||||
bool NextMessageSize(uint32_t* sz) override { |
||||
return body_.NextMessageSize(sz); |
||||
} |
||||
|
||||
/// Read a message of type \a R into \a msg. Completion will be notified by \a
|
||||
/// tag on the associated completion queue.
|
||||
/// This is thread-safe with respect to \a Write or \a WritesDone methods. It
|
||||
/// should not be called concurrently with other streaming APIs
|
||||
/// on the same stream. It is not meaningful to call it concurrently
|
||||
/// with another \a ReaderInterface::Read on the same stream since reads on
|
||||
/// the same stream are delivered in order.
|
||||
///
|
||||
/// \param[out] msg Where to eventually store the read message.
|
||||
/// \param[in] tag The tag identifying the operation.
|
||||
bool Read(RequestType* request) override { |
||||
if (read_done_) { |
||||
return false; |
||||
} |
||||
read_done_ = true; |
||||
return body_.Read(request); |
||||
} |
||||
|
||||
/// Block to write \a msg to the stream with WriteOptions \a options.
|
||||
/// This is thread-safe with respect to \a ReaderInterface::Read
|
||||
///
|
||||
/// \param msg The message to be written to the stream.
|
||||
/// \param options The WriteOptions affecting the write operation.
|
||||
///
|
||||
/// \return \a true on success, \a false when the stream has been closed.
|
||||
using internal::WriterInterface<ResponseType>::Write; |
||||
bool Write(const ResponseType& response, |
||||
::grpc::WriteOptions options) override { |
||||
if (write_done_ || !read_done_) { |
||||
return false; |
||||
} |
||||
write_done_ = true; |
||||
return body_.Write(response, options); |
||||
} |
||||
|
||||
private: |
||||
internal::ServerReaderWriterBody<ResponseType, RequestType> body_; |
||||
bool read_done_; |
||||
bool write_done_; |
||||
|
||||
friend class ::grpc::internal::TemplatedBidiStreamingHandler< |
||||
ServerUnaryStreamer<RequestType, ResponseType>, true>; |
||||
ServerUnaryStreamer(::grpc::internal::Call* call, |
||||
::grpc_impl::ServerContext* ctx) |
||||
: body_(call, ctx), read_done_(false), write_done_(false) {} |
||||
}; |
||||
|
||||
/// A class to represent a flow-controlled server-side streaming call.
|
||||
/// This is something of a hybrid between server-side and bidi streaming.
|
||||
/// This is invoked through a server-side streaming call on the client side,
|
||||
/// but the server responds to it as though it were a bidi streaming call that
|
||||
/// must first have exactly 1 Read and then any number of Writes.
|
||||
template <class RequestType, class ResponseType> |
||||
class ServerSplitStreamer final |
||||
: public ServerReaderWriterInterface<ResponseType, RequestType> { |
||||
public: |
||||
/// Block to send initial metadata to client.
|
||||
/// Implicit input parameter:
|
||||
/// - the \a ServerContext associated with this call will be used for
|
||||
/// sending initial metadata.
|
||||
void SendInitialMetadata() override { body_.SendInitialMetadata(); } |
||||
|
||||
/// Get an upper bound on the request message size from the client.
|
||||
bool NextMessageSize(uint32_t* sz) override { |
||||
return body_.NextMessageSize(sz); |
||||
} |
||||
|
||||
/// Read a message of type \a R into \a msg. Completion will be notified by \a
|
||||
/// tag on the associated completion queue.
|
||||
/// This is thread-safe with respect to \a Write or \a WritesDone methods. It
|
||||
/// should not be called concurrently with other streaming APIs
|
||||
/// on the same stream. It is not meaningful to call it concurrently
|
||||
/// with another \a ReaderInterface::Read on the same stream since reads on
|
||||
/// the same stream are delivered in order.
|
||||
///
|
||||
/// \param[out] msg Where to eventually store the read message.
|
||||
/// \param[in] tag The tag identifying the operation.
|
||||
bool Read(RequestType* request) override { |
||||
if (read_done_) { |
||||
return false; |
||||
} |
||||
read_done_ = true; |
||||
return body_.Read(request); |
||||
} |
||||
|
||||
/// Block to write \a msg to the stream with WriteOptions \a options.
|
||||
/// This is thread-safe with respect to \a ReaderInterface::Read
|
||||
///
|
||||
/// \param msg The message to be written to the stream.
|
||||
/// \param options The WriteOptions affecting the write operation.
|
||||
///
|
||||
/// \return \a true on success, \a false when the stream has been closed.
|
||||
using internal::WriterInterface<ResponseType>::Write; |
||||
bool Write(const ResponseType& response, |
||||
::grpc::WriteOptions options) override { |
||||
return read_done_ && body_.Write(response, options); |
||||
} |
||||
|
||||
private: |
||||
internal::ServerReaderWriterBody<ResponseType, RequestType> body_; |
||||
bool read_done_; |
||||
|
||||
friend class ::grpc::internal::TemplatedBidiStreamingHandler< |
||||
ServerSplitStreamer<RequestType, ResponseType>, false>; |
||||
ServerSplitStreamer(::grpc::internal::Call* call, |
||||
::grpc_impl::ServerContext* ctx) |
||||
: body_(call, ctx), read_done_(false) {} |
||||
}; |
||||
|
||||
} // namespace grpc_impl
|
||||
|
||||
#endif // GRPCPP_IMPL_CODEGEN_SYNC_STREAM_IMPL_H
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue