mirror of https://github.com/grpc/grpc.git
Conflicts: src/core/security/google_default_credentials.c test/core/security/verify_jwt.cpull/2818/head
commit
d067c263b2
205 changed files with 8941 additions and 1685 deletions
@ -0,0 +1,70 @@ |
||||
GRPC Health Checking Protocol |
||||
================================ |
||||
|
||||
Health checks are used to probe whether the server is able to handle rpcs. The |
||||
client-to-server health checking can happen from point to point or via some |
||||
control system. A server may choose to reply “unhealthy” because it |
||||
is not ready to take requests, it is shutting down or some other reason. |
||||
The client can act accordingly if the response is not received within some time |
||||
window or the response says unhealthy in it. |
||||
|
||||
|
||||
A GRPC service is used as the health checking mechanism for both simple |
||||
client-to-server scenario and other control systems such as load-balancing. |
||||
Being a high |
||||
level service provides some benefits. Firstly, since it is a GRPC service |
||||
itself, doing a health check is in the same format as a normal rpc. Secondly, |
||||
it has rich semantics such as per-service health status. Thirdly, as a GRPC |
||||
service, it is able reuse all the existing billing, quota infrastructure, etc, |
||||
and thus the server has full control over the access of the health checking |
||||
service. |
||||
|
||||
## Service Definition |
||||
|
||||
The server should export a service defined in the following proto: |
||||
|
||||
``` |
||||
syntax = "proto3"; |
||||
|
||||
package grpc.health.v1alpha; |
||||
|
||||
message HealthCheckRequest { |
||||
string service = 1; |
||||
} |
||||
|
||||
message HealthCheckResponse { |
||||
enum ServingStatus { |
||||
UNKNOWN = 0; |
||||
SERVING = 1; |
||||
NOT_SERVING = 2; |
||||
} |
||||
ServingStatus status = 1; |
||||
} |
||||
|
||||
service Health { |
||||
rpc Check(HealthCheckRequest) returns (HealthCheckResponse); |
||||
} |
||||
``` |
||||
|
||||
A client can query the server’s health status by calling the `Check` method, and |
||||
a deadline should be set on the rpc. The client can optionally set the service |
||||
name it wants to query for health status. The suggested format of service name |
||||
is `package_names.ServiceName`, such as `grpc.health.v1alpha.Health`. |
||||
|
||||
The server should register all the services manually and set |
||||
the individual status, including an empty service name and its status. For each |
||||
request received, if the service name can be found in the registry, |
||||
a response must be sent back with an `OK` status and the status field should be |
||||
set to `SERVING` or `NOT_SERVING` accordingly. If the service name is not |
||||
registered, the server returns a `NOT_FOUND` GRPC status. |
||||
|
||||
The server should use an empty string as the key for server’s |
||||
overall health status, so that a client not interested in a specific service can |
||||
query the server's status with an empty request. The server can just do exact |
||||
matching of the service name without support of any kind of wildcard matching. |
||||
However, the service owner has the freedom to implement more complicated |
||||
matching semantics that both the client and server agree upon. |
||||
|
||||
A client can declare the server as unhealthy if the rpc is not finished after |
||||
some amount of time. The client should be able to handle the case where server |
||||
does not have the Health service. |
@ -0,0 +1,52 @@ |
||||
#gRPC Naming and Discovery Support |
||||
|
||||
## Overview |
||||
|
||||
gRPC supports DNS as the default name-system. A number of alternative name-systems are used in various deployments. We propose an API that is general enough to support a range of name-systems and the corresponding syntax for names. The gRPC client library in various languages will provide a plugin mechanism so resolvers for different name-systems can be plugged in. |
||||
|
||||
## Detailed Proposal |
||||
|
||||
A fully qualified, self contained name used for gRPC channel construction uses the syntax: |
||||
|
||||
``` |
||||
scheme://authority/endpoint_name |
||||
``` |
||||
|
||||
Here, scheme indicates the name-system to be used. Example schemes to be supported include: |
||||
|
||||
* `dns` |
||||
|
||||
* `zookeeper` |
||||
|
||||
* `etcd` |
||||
|
||||
Authority indicates some scheme-specific bootstrap information, e.g., for DNS, the authority may include the IP[:port] of the DNS server to use. Often, a DNS name may used as the authority, since the ability to resolve DNS names is already built into all gRPC client libraries. |
||||
|
||||
Finally, the endpoint_name indicates a concrete name to be looked up in a given name-system identified by the scheme and the authority. The syntax of endpoint name is dictated by the scheme in use. |
||||
|
||||
### Plugins |
||||
|
||||
The gRPC client library will switch on the scheme to pick the right resolver plugin and pass it the fully qualified name string. |
||||
|
||||
Resolvers should be able to contact the authority and get a resolution that they return back to the gRPC client library. The returned contents include a list of IP:port, an optional config and optional auth config data to be used for channel authentication. The plugin API allows the resolvers to continuously watch an endpoint_name and return updated resolutions as needed. |
||||
|
||||
## Zookeeper |
||||
|
||||
Apache [ZooKeeper](https://zookeeper.apache.org/) is a popular solution for building name-systems. Curator is a service discovery system built on to of ZooKeeper. We propose to organize names hierarchically as `/path/service/instance` similar to Apache Curator. |
||||
|
||||
A fully-qualified ZooKeeper name used to construct a gRPC channel will look as follows: |
||||
|
||||
``` |
||||
zookeeper://host:port/path/service/instance |
||||
``` |
||||
Here `zookeeper` is the scheme identifying the name-system. `host:port` identifies an authoritative name-server for this scheme (i.e., a Zookeeper server). The host can be an IP address or a DNS name. |
||||
Finally `/path/service/instance` is the Zookeeper name to be resolved. |
||||
|
||||
## Service Registration |
||||
|
||||
|
||||
Service providers can register their services in Zookeeper by using a Zookeeper client. |
||||
|
||||
Each service is a zookeeper node, and each instance is a child node of the corresponding service. For example, a MySQL service may have multiple instances, `/mysql/1`, `/mysql/2`, `/mysql/3`. The name of the service or instance, as well as an optional path is specified by the service provider. |
||||
|
||||
The data in service nodes is empty. Each instance node stores its address in the format of `host:port`, where host can be either hostname or IP address. |
@ -0,0 +1,183 @@ |
||||
GRPC Server Reflection Protocol |
||||
=============================== |
||||
|
||||
This document describes server reflection as an optional extension for servers |
||||
to assist clients in runtime construction of requests without having stub |
||||
information precompiled into the client. |
||||
|
||||
The primary usecase for server reflection is to write (typically) command line |
||||
debugging tools for talking to a grpc server. In particular, such a tool will |
||||
take in a method and a payload (in human readable text format) send it to the |
||||
server (typically in binary proto wire format), and then take the response and |
||||
decode it to text to present to the user. |
||||
|
||||
This broadly involves two problems: determining what formats (which protobuf |
||||
messages) a server’s method uses, and determining how to convert messages |
||||
between human readable format and the (likely binary) wire format. |
||||
|
||||
## Method reflection |
||||
|
||||
We want to be able to answer the following queries: |
||||
1. What methods does a server export? |
||||
2. For a particular method, how do we call it? |
||||
Specifically, what are the names of the methods, are those methods unary or |
||||
streaming, and what are the types of the argument and result? |
||||
|
||||
``` |
||||
#TODO(dklempner): link to an actual .proto later. |
||||
package grpc.reflection.v1alpha; |
||||
|
||||
message ListApisRequest { |
||||
} |
||||
|
||||
message ListApisResponse { |
||||
repeated google.protobuf.Api apis = 1; |
||||
} |
||||
|
||||
message GetMethodRequest { |
||||
string method = 1; |
||||
} |
||||
message GetMethodResponse { |
||||
google.protobuf.Method method = 1; |
||||
} |
||||
|
||||
service ServerReflection { |
||||
rpc ListApis (ListApisRequest) returns (ListApisResponse); |
||||
rpc GetMethod (GetMethodRequest) returns (GetMethodResponse); |
||||
} |
||||
``` |
||||
|
||||
Note that a server is under no obligation to return a complete list of all |
||||
methods it supports. For example, a reverse proxy may support server reflection |
||||
for methods implemented directly on the proxy but not enumerate all methods |
||||
supported by its backends. |
||||
|
||||
|
||||
### Open questions on method reflection |
||||
* Consider how to extend this protocol to support non-protobuf methods. |
||||
|
||||
## Argument reflection |
||||
The second half of the problem is converting between the human readable |
||||
input/output of a debugging tool and the binary format understood by the |
||||
method. |
||||
|
||||
This is obviously dependent on protocol type. At one extreme, if both the |
||||
server and the debugging tool accept JSON, there may be no need for such a |
||||
conversion in the first place. At the opposite extreme, a server using a custom |
||||
binary format has no hope of being supported by a generic system. The |
||||
intermediate interesting common case is a server which speaks binary-proto and |
||||
a debugging client which speaks either ascii-proto or json-proto. |
||||
|
||||
One approach would be to require servers directly support human readable input. |
||||
In the future method reflection may be extended to document such support, |
||||
should it become widespread or standardized. |
||||
|
||||
## Protobuf descriptors |
||||
|
||||
A second would be for the server to export its |
||||
google::protobuf::DescriptorDatabase over the wire. This is very easy to |
||||
implement in C++, and Google implementations of a similar protocol already |
||||
exist in C++, Go, and Java. |
||||
|
||||
This protocol mostly returns FileDescriptorProtos, which are a proto encoding |
||||
of a parsed .proto file. It supports four queries: |
||||
1. The FileDescriptorProto for a given file name |
||||
2. The FileDescriptorProto for the file with a given symbol |
||||
3. The FileDescriptorProto for the file with a given extension |
||||
4. The list of known extension tag numbers of a given type |
||||
|
||||
These directly correspond to the methods of |
||||
google::protobuf::DescriptorDatabase. Note that this protocol includes support |
||||
for extensions, which have been removed from proto3 but are still in widespread |
||||
use in Google’s codebase. |
||||
|
||||
Because most usecases will require also requesting the transitive dependencies |
||||
of requested files, the queries will also return all transitive dependencies of |
||||
the returned file. Should interesting usecases for non-transitive queries turn |
||||
up later, we can easily extend the protocol to support them. |
||||
|
||||
### Reverse proxy traversal |
||||
|
||||
One potential issue with naive reverse proxies is that, while any individual |
||||
server will have a consistent and valid picture of the proto DB which is |
||||
sufficient to handle incoming requests, incompatibilities will arise if the |
||||
backend servers have a mix of builds. For example, if a given message is moved |
||||
from foo.proto to bar.proto, and the client requests foo.proto from an old |
||||
server and bar.proto from a new server, the resulting database will have a |
||||
double definition. |
||||
|
||||
To solve this problem, the protocol is structured as a bidirectional stream, |
||||
ensuring all related requests go to a single server. This has the additional |
||||
benefit that overlapping recursive requests don’t require sending a lot of |
||||
redundant information, because there is a single stream to maintain context |
||||
between queries. |
||||
|
||||
``` |
||||
package grpc.reflection.v1alpha; |
||||
message DescriptorDatabaseRequest { |
||||
string host = 1; |
||||
oneof message_request { |
||||
string files_for_file_name = 3; |
||||
string files_for_symbol_name = 4; |
||||
FileContainingExtensionRequest file_containing_extension = 5; |
||||
string list_all_extensions_of_type = 6; |
||||
} |
||||
} |
||||
|
||||
message FileContainingExtensionRequest { |
||||
string base_message = 1; |
||||
int64 extension_id = 2; |
||||
} |
||||
|
||||
message DescriptorDatabaseResponse { |
||||
string valid_host = 1; |
||||
DescriptorDatabaseRequest original_request = 2; |
||||
oneof message_response { |
||||
// These are proto2 type google.protobuf.FileDescriptorProto, but |
||||
// we avoid taking a dependency on descriptor.proto, which uses |
||||
// proto2 only features, by making them opaque |
||||
// bytes instead |
||||
repeated bytes fd_proto = 4; |
||||
ListAllExtensionsResponse extensions_response = 5; |
||||
// Notably includes error code 5, NOT FOUND |
||||
int32 error_code = 6; |
||||
} |
||||
} |
||||
|
||||
message ListAllExtensionsResponse { |
||||
string base_type_name; |
||||
repeated int64 extension_number; |
||||
} |
||||
|
||||
service ProtoDescriptorDatabase { |
||||
rpc DescriptorDatabaseInfo(stream DescriptorDatabaseRequest) returns (stream DescriptorDatabaseResponse); |
||||
} |
||||
``` |
||||
|
||||
Any given request must either result in an error code or an answer, usually in |
||||
the form of a series of FileDescriptorProtos with the requested file itself |
||||
and all previously unsent transitive imports of that file. Servers may track |
||||
which FileDescriptorProtos have been sent on a given stream, for a given value |
||||
of valid_host, and avoid sending them repeatedly for overlapping requests. |
||||
|
||||
| message_request message | Result | |
||||
| files_for_file_name | transitive closure of file name | |
||||
| files_for_symbol_name | transitive closure file containing symbol | |
||||
| file_containing_extension | transitive closure of file containing a given extension number of a given symbol | |
||||
| list_all_extensions_of_type | ListAllExtensionsResponse containing all known extension numbers of a given type | |
||||
|
||||
At some point it would make sense to additionally also support any.proto’s |
||||
format. Note that known any.proto messages can be queried by symbol using this |
||||
protocol even without any such support, by parsing the url and extracting the |
||||
symbol name from it. |
||||
|
||||
## Language specific implementation thoughts |
||||
All of the information needed to implement Proto reflection is available to the |
||||
code generator, but I’m not certain we actually generate this in every |
||||
language. If the proto implementation in the language doesn’t have something |
||||
like google::protobuf::DescriptorPool the grpc implementation for that language |
||||
will need to index those FileDescriptorProtos by file and symbol and imports. |
||||
|
||||
One issue is that some grpc implementations are very loosely coupled with |
||||
protobufs; in such implementations it probably makes sense to split apart these |
||||
reflection APIs so as not to take an additional proto dependency. |
@ -1,125 +0,0 @@ |
||||
#region Copyright notice and license |
||||
|
||||
// Copyright 2015, Google Inc. |
||||
// All rights reserved. |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
#endregion |
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.IO; |
||||
using System.Security.Cryptography; |
||||
using System.Threading; |
||||
using System.Threading.Tasks; |
||||
|
||||
using Google.Apis.Auth.OAuth2; |
||||
using Google.Apis.Auth.OAuth2.Responses; |
||||
using Newtonsoft.Json.Linq; |
||||
using Org.BouncyCastle.Crypto.Parameters; |
||||
using Org.BouncyCastle.Security; |
||||
|
||||
namespace Grpc.Auth |
||||
{ |
||||
// TODO(jtattermusch): Remove this class once possible. |
||||
/// <summary> |
||||
/// A temporary placeholder for Google credential from |
||||
/// Google Auth library for .NET. It emulates the usage pattern |
||||
/// for Usable auth. |
||||
/// </summary> |
||||
public class GoogleCredential |
||||
{ |
||||
private const string GoogleApplicationCredentialsEnvName = "GOOGLE_APPLICATION_CREDENTIALS"; |
||||
private const string ClientEmailFieldName = "client_email"; |
||||
private const string PrivateKeyFieldName = "private_key"; |
||||
|
||||
private ServiceCredential credential; |
||||
|
||||
private GoogleCredential(ServiceCredential credential) |
||||
{ |
||||
this.credential = credential; |
||||
} |
||||
|
||||
public static GoogleCredential GetApplicationDefault() |
||||
{ |
||||
return new GoogleCredential(null); |
||||
} |
||||
|
||||
public bool IsCreateScopedRequired |
||||
{ |
||||
get |
||||
{ |
||||
return true; |
||||
} |
||||
} |
||||
|
||||
public GoogleCredential CreateScoped(IEnumerable<string> scopes) |
||||
{ |
||||
var credsPath = Environment.GetEnvironmentVariable(GoogleApplicationCredentialsEnvName); |
||||
if (credsPath == null) |
||||
{ |
||||
// Default to ComputeCredentials if path to JSON key is not set. |
||||
// ComputeCredential is not scoped actually, but for our use case it's |
||||
// fine to treat is as such. |
||||
return new GoogleCredential(new ComputeCredential(new ComputeCredential.Initializer())); |
||||
} |
||||
|
||||
JObject jsonCredentialParameters = JObject.Parse(File.ReadAllText(credsPath)); |
||||
string clientEmail = jsonCredentialParameters.GetValue(ClientEmailFieldName).Value<string>(); |
||||
string privateKeyString = jsonCredentialParameters.GetValue(PrivateKeyFieldName).Value<string>(); |
||||
|
||||
var serviceCredential = new ServiceAccountCredential( |
||||
new ServiceAccountCredential.Initializer(clientEmail) |
||||
{ |
||||
Scopes = scopes, |
||||
}.FromPrivateKey(privateKeyString)); |
||||
return new GoogleCredential(serviceCredential); |
||||
} |
||||
|
||||
public Task<bool> RequestAccessTokenAsync(CancellationToken taskCancellationToken) |
||||
{ |
||||
return credential.RequestAccessTokenAsync(taskCancellationToken); |
||||
} |
||||
|
||||
public TokenResponse Token |
||||
{ |
||||
get |
||||
{ |
||||
return credential.Token; |
||||
} |
||||
} |
||||
|
||||
internal ServiceCredential InternalCredential |
||||
{ |
||||
get |
||||
{ |
||||
return credential; |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,128 @@ |
||||
#region Copyright notice and license |
||||
|
||||
// Copyright 2015, Google Inc. |
||||
// All rights reserved. |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
#endregion |
||||
|
||||
using System; |
||||
using System.Diagnostics; |
||||
using System.Linq; |
||||
using System.Threading; |
||||
using System.Threading.Tasks; |
||||
using Grpc.Core; |
||||
using Grpc.Core.Internal; |
||||
using Grpc.Core.Utils; |
||||
using NUnit.Framework; |
||||
|
||||
namespace Grpc.Core.Tests |
||||
{ |
||||
public class CompressionTest |
||||
{ |
||||
MockServiceHelper helper; |
||||
Server server; |
||||
Channel channel; |
||||
|
||||
[SetUp] |
||||
public void Init() |
||||
{ |
||||
helper = new MockServiceHelper(); |
||||
|
||||
server = helper.GetServer(); |
||||
server.Start(); |
||||
channel = helper.GetChannel(); |
||||
} |
||||
|
||||
[TearDown] |
||||
public void Cleanup() |
||||
{ |
||||
channel.Dispose(); |
||||
server.ShutdownAsync().Wait(); |
||||
} |
||||
|
||||
[TestFixtureTearDown] |
||||
public void CleanupClass() |
||||
{ |
||||
GrpcEnvironment.Shutdown(); |
||||
} |
||||
|
||||
[Test] |
||||
public void WriteOptions_Unary() |
||||
{ |
||||
helper.UnaryHandler = new UnaryServerMethod<string, string>(async (request, context) => |
||||
{ |
||||
context.WriteOptions = new WriteOptions(WriteFlags.NoCompress); |
||||
return request; |
||||
}); |
||||
|
||||
var callOptions = new CallOptions(writeOptions: new WriteOptions(WriteFlags.NoCompress)); |
||||
Calls.BlockingUnaryCall(helper.CreateUnaryCall(callOptions), "abc"); |
||||
} |
||||
|
||||
[Test] |
||||
public async Task WriteOptions_DuplexStreaming() |
||||
{ |
||||
helper.DuplexStreamingHandler = new DuplexStreamingServerMethod<string, string>(async (requestStream, responseStream, context) => |
||||
{ |
||||
await requestStream.ToListAsync(); |
||||
|
||||
context.WriteOptions = new WriteOptions(WriteFlags.NoCompress); |
||||
|
||||
await context.WriteResponseHeadersAsync(new Metadata { { "ascii-header", "abcdefg" } }); |
||||
|
||||
await responseStream.WriteAsync("X"); |
||||
|
||||
responseStream.WriteOptions = null; |
||||
await responseStream.WriteAsync("Y"); |
||||
|
||||
responseStream.WriteOptions = new WriteOptions(WriteFlags.NoCompress); |
||||
await responseStream.WriteAsync("Z"); |
||||
}); |
||||
|
||||
var callOptions = new CallOptions(writeOptions: new WriteOptions(WriteFlags.NoCompress)); |
||||
var call = Calls.AsyncDuplexStreamingCall(helper.CreateDuplexStreamingCall(callOptions)); |
||||
|
||||
// check that write options from call options are propagated to request stream. |
||||
Assert.IsTrue((call.RequestStream.WriteOptions.Flags & WriteFlags.NoCompress) != 0); |
||||
|
||||
call.RequestStream.WriteOptions = new WriteOptions(); |
||||
await call.RequestStream.WriteAsync("A"); |
||||
|
||||
call.RequestStream.WriteOptions = null; |
||||
await call.RequestStream.WriteAsync("B"); |
||||
|
||||
call.RequestStream.WriteOptions = new WriteOptions(WriteFlags.NoCompress); |
||||
await call.RequestStream.WriteAsync("C"); |
||||
|
||||
await call.RequestStream.CompleteAsync(); |
||||
|
||||
await call.ResponseStream.ToListAsync(); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,153 @@ |
||||
#region Copyright notice and license |
||||
|
||||
// Copyright 2015, Google Inc. |
||||
// All rights reserved. |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
#endregion |
||||
|
||||
using System; |
||||
using System.Diagnostics; |
||||
using System.Linq; |
||||
using System.Threading; |
||||
using System.Threading.Tasks; |
||||
using Grpc.Core; |
||||
using Grpc.Core.Internal; |
||||
using Grpc.Core.Utils; |
||||
using NUnit.Framework; |
||||
|
||||
namespace Grpc.Core.Tests |
||||
{ |
||||
public class ContextPropagationTest |
||||
{ |
||||
MockServiceHelper helper; |
||||
Server server; |
||||
Channel channel; |
||||
|
||||
[SetUp] |
||||
public void Init() |
||||
{ |
||||
helper = new MockServiceHelper(); |
||||
|
||||
server = helper.GetServer(); |
||||
server.Start(); |
||||
channel = helper.GetChannel(); |
||||
} |
||||
|
||||
[TearDown] |
||||
public void Cleanup() |
||||
{ |
||||
channel.Dispose(); |
||||
server.ShutdownAsync().Wait(); |
||||
} |
||||
|
||||
[TestFixtureTearDown] |
||||
public void CleanupClass() |
||||
{ |
||||
GrpcEnvironment.Shutdown(); |
||||
} |
||||
|
||||
[Test] |
||||
public async Task PropagateCancellation() |
||||
{ |
||||
helper.UnaryHandler = new UnaryServerMethod<string, string>(async (request, context) => |
||||
{ |
||||
// check that we didn't obtain the default cancellation token. |
||||
Assert.IsTrue(context.CancellationToken.CanBeCanceled); |
||||
return "PASS"; |
||||
}); |
||||
|
||||
helper.ClientStreamingHandler = new ClientStreamingServerMethod<string, string>(async (requestStream, context) => |
||||
{ |
||||
var propagationToken = context.CreatePropagationToken(); |
||||
Assert.IsNotNull(propagationToken.ParentCall); |
||||
|
||||
var callOptions = new CallOptions(propagationToken: propagationToken); |
||||
return await Calls.AsyncUnaryCall(helper.CreateUnaryCall(callOptions), "xyz"); |
||||
}); |
||||
|
||||
var cts = new CancellationTokenSource(); |
||||
var call = Calls.AsyncClientStreamingCall(helper.CreateClientStreamingCall(new CallOptions(cancellationToken: cts.Token))); |
||||
await call.RequestStream.CompleteAsync(); |
||||
Assert.AreEqual("PASS", await call); |
||||
} |
||||
|
||||
[Test] |
||||
public async Task PropagateDeadline() |
||||
{ |
||||
var deadline = DateTime.UtcNow.AddDays(7); |
||||
helper.UnaryHandler = new UnaryServerMethod<string, string>(async (request, context) => |
||||
{ |
||||
Assert.IsTrue(context.Deadline < deadline.AddMinutes(1)); |
||||
Assert.IsTrue(context.Deadline > deadline.AddMinutes(-1)); |
||||
return "PASS"; |
||||
}); |
||||
|
||||
helper.ClientStreamingHandler = new ClientStreamingServerMethod<string, string>(async (requestStream, context) => |
||||
{ |
||||
Assert.Throws(typeof(ArgumentException), () => |
||||
{ |
||||
// Trying to override deadline while propagating deadline from parent call will throw. |
||||
Calls.BlockingUnaryCall(helper.CreateUnaryCall( |
||||
new CallOptions(deadline: DateTime.UtcNow.AddDays(8), |
||||
propagationToken: context.CreatePropagationToken())), ""); |
||||
}); |
||||
|
||||
var callOptions = new CallOptions(propagationToken: context.CreatePropagationToken()); |
||||
return await Calls.AsyncUnaryCall(helper.CreateUnaryCall(callOptions), "xyz"); |
||||
}); |
||||
|
||||
var call = Calls.AsyncClientStreamingCall(helper.CreateClientStreamingCall(new CallOptions(deadline: deadline))); |
||||
await call.RequestStream.CompleteAsync(); |
||||
Assert.AreEqual("PASS", await call); |
||||
} |
||||
|
||||
[Test] |
||||
public async Task SuppressDeadlinePropagation() |
||||
{ |
||||
helper.UnaryHandler = new UnaryServerMethod<string, string>(async (request, context) => |
||||
{ |
||||
Assert.AreEqual(DateTime.MaxValue, context.Deadline); |
||||
return "PASS"; |
||||
}); |
||||
|
||||
helper.ClientStreamingHandler = new ClientStreamingServerMethod<string, string>(async (requestStream, context) => |
||||
{ |
||||
Assert.IsTrue(context.CancellationToken.CanBeCanceled); |
||||
|
||||
var callOptions = new CallOptions(propagationToken: context.CreatePropagationToken(new ContextPropagationOptions(propagateDeadline: false))); |
||||
return await Calls.AsyncUnaryCall(helper.CreateUnaryCall(callOptions), "xyz"); |
||||
}); |
||||
|
||||
var cts = new CancellationTokenSource(); |
||||
var call = Calls.AsyncClientStreamingCall(helper.CreateClientStreamingCall(new CallOptions(deadline: DateTime.UtcNow.AddDays(7)))); |
||||
await call.RequestStream.CompleteAsync(); |
||||
Assert.AreEqual("PASS", await call); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,244 @@ |
||||
#region Copyright notice and license |
||||
|
||||
// Copyright 2015, Google Inc. |
||||
// All rights reserved. |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
#endregion |
||||
|
||||
using System; |
||||
using System.Diagnostics; |
||||
using System.Linq; |
||||
using System.Threading; |
||||
using System.Threading.Tasks; |
||||
using Grpc.Core; |
||||
using Grpc.Core.Internal; |
||||
using Grpc.Core.Utils; |
||||
using NUnit.Framework; |
||||
|
||||
namespace Grpc.Core.Tests |
||||
{ |
||||
/// <summary> |
||||
/// Allows setting up a mock service in the client-server tests easily. |
||||
/// </summary> |
||||
public class MockServiceHelper |
||||
{ |
||||
public const string ServiceName = "tests.Test"; |
||||
|
||||
public static readonly Method<string, string> UnaryMethod = new Method<string, string>( |
||||
MethodType.Unary, |
||||
ServiceName, |
||||
"Unary", |
||||
Marshallers.StringMarshaller, |
||||
Marshallers.StringMarshaller); |
||||
|
||||
public static readonly Method<string, string> ClientStreamingMethod = new Method<string, string>( |
||||
MethodType.ClientStreaming, |
||||
ServiceName, |
||||
"ClientStreaming", |
||||
Marshallers.StringMarshaller, |
||||
Marshallers.StringMarshaller); |
||||
|
||||
public static readonly Method<string, string> ServerStreamingMethod = new Method<string, string>( |
||||
MethodType.ServerStreaming, |
||||
ServiceName, |
||||
"ServerStreaming", |
||||
Marshallers.StringMarshaller, |
||||
Marshallers.StringMarshaller); |
||||
|
||||
public static readonly Method<string, string> DuplexStreamingMethod = new Method<string, string>( |
||||
MethodType.DuplexStreaming, |
||||
ServiceName, |
||||
"DuplexStreaming", |
||||
Marshallers.StringMarshaller, |
||||
Marshallers.StringMarshaller); |
||||
|
||||
readonly string host; |
||||
readonly ServerServiceDefinition serviceDefinition; |
||||
|
||||
UnaryServerMethod<string, string> unaryHandler; |
||||
ClientStreamingServerMethod<string, string> clientStreamingHandler; |
||||
ServerStreamingServerMethod<string, string> serverStreamingHandler; |
||||
DuplexStreamingServerMethod<string, string> duplexStreamingHandler; |
||||
|
||||
Server server; |
||||
Channel channel; |
||||
|
||||
public MockServiceHelper(string host = null) |
||||
{ |
||||
this.host = host ?? "localhost"; |
||||
|
||||
serviceDefinition = ServerServiceDefinition.CreateBuilder(ServiceName) |
||||
.AddMethod(UnaryMethod, (request, context) => unaryHandler(request, context)) |
||||
.AddMethod(ClientStreamingMethod, (requestStream, context) => clientStreamingHandler(requestStream, context)) |
||||
.AddMethod(ServerStreamingMethod, (request, responseStream, context) => serverStreamingHandler(request, responseStream, context)) |
||||
.AddMethod(DuplexStreamingMethod, (requestStream, responseStream, context) => duplexStreamingHandler(requestStream, responseStream, context)) |
||||
.Build(); |
||||
|
||||
var defaultStatus = new Status(StatusCode.Unknown, "Default mock implementation. Please provide your own."); |
||||
|
||||
unaryHandler = new UnaryServerMethod<string, string>(async (request, context) => |
||||
{ |
||||
context.Status = defaultStatus; |
||||
return ""; |
||||
}); |
||||
|
||||
clientStreamingHandler = new ClientStreamingServerMethod<string, string>(async (requestStream, context) => |
||||
{ |
||||
context.Status = defaultStatus; |
||||
return ""; |
||||
}); |
||||
|
||||
serverStreamingHandler = new ServerStreamingServerMethod<string, string>(async (request, responseStream, context) => |
||||
{ |
||||
context.Status = defaultStatus; |
||||
}); |
||||
|
||||
duplexStreamingHandler = new DuplexStreamingServerMethod<string, string>(async (requestStream, responseStream, context) => |
||||
{ |
||||
context.Status = defaultStatus; |
||||
}); |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Returns the default server for this service and creates one if not yet created. |
||||
/// </summary> |
||||
public Server GetServer() |
||||
{ |
||||
if (server == null) |
||||
{ |
||||
server = new Server |
||||
{ |
||||
Services = { serviceDefinition }, |
||||
Ports = { { Host, ServerPort.PickUnused, ServerCredentials.Insecure } } |
||||
}; |
||||
} |
||||
return server; |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Returns the default channel for this service and creates one if not yet created. |
||||
/// </summary> |
||||
public Channel GetChannel() |
||||
{ |
||||
if (channel == null) |
||||
{ |
||||
channel = new Channel(Host, GetServer().Ports.Single().BoundPort, Credentials.Insecure); |
||||
} |
||||
return channel; |
||||
} |
||||
|
||||
public CallInvocationDetails<string, string> CreateUnaryCall(CallOptions options = default(CallOptions)) |
||||
{ |
||||
return new CallInvocationDetails<string, string>(channel, UnaryMethod, options); |
||||
} |
||||
|
||||
public CallInvocationDetails<string, string> CreateClientStreamingCall(CallOptions options = default(CallOptions)) |
||||
{ |
||||
return new CallInvocationDetails<string, string>(channel, ClientStreamingMethod, options); |
||||
} |
||||
|
||||
public CallInvocationDetails<string, string> CreateServerStreamingCall(CallOptions options = default(CallOptions)) |
||||
{ |
||||
return new CallInvocationDetails<string, string>(channel, ServerStreamingMethod, options); |
||||
} |
||||
|
||||
public CallInvocationDetails<string, string> CreateDuplexStreamingCall(CallOptions options = default(CallOptions)) |
||||
{ |
||||
return new CallInvocationDetails<string, string>(channel, DuplexStreamingMethod, options); |
||||
} |
||||
|
||||
public string Host |
||||
{ |
||||
get |
||||
{ |
||||
return this.host; |
||||
} |
||||
} |
||||
|
||||
public ServerServiceDefinition ServiceDefinition |
||||
{ |
||||
get |
||||
{ |
||||
return this.serviceDefinition; |
||||
} |
||||
} |
||||
|
||||
public UnaryServerMethod<string, string> UnaryHandler |
||||
{ |
||||
get |
||||
{ |
||||
return this.unaryHandler; |
||||
} |
||||
|
||||
set |
||||
{ |
||||
unaryHandler = value; |
||||
} |
||||
} |
||||
|
||||
public ClientStreamingServerMethod<string, string> ClientStreamingHandler |
||||
{ |
||||
get |
||||
{ |
||||
return this.clientStreamingHandler; |
||||
} |
||||
|
||||
set |
||||
{ |
||||
clientStreamingHandler = value; |
||||
} |
||||
} |
||||
|
||||
public ServerStreamingServerMethod<string, string> ServerStreamingHandler |
||||
{ |
||||
get |
||||
{ |
||||
return this.serverStreamingHandler; |
||||
} |
||||
|
||||
set |
||||
{ |
||||
serverStreamingHandler = value; |
||||
} |
||||
} |
||||
|
||||
public DuplexStreamingServerMethod<string, string> DuplexStreamingHandler |
||||
{ |
||||
get |
||||
{ |
||||
return this.duplexStreamingHandler; |
||||
} |
||||
|
||||
set |
||||
{ |
||||
duplexStreamingHandler = value; |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,136 @@ |
||||
#region Copyright notice and license |
||||
|
||||
// Copyright 2015, Google Inc. |
||||
// All rights reserved. |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
#endregion |
||||
|
||||
using System; |
||||
using System.Diagnostics; |
||||
using System.Linq; |
||||
using System.Threading; |
||||
using System.Threading.Tasks; |
||||
using Grpc.Core; |
||||
using Grpc.Core.Internal; |
||||
using Grpc.Core.Utils; |
||||
using NUnit.Framework; |
||||
|
||||
namespace Grpc.Core.Tests |
||||
{ |
||||
/// <summary> |
||||
/// Tests for response headers support. |
||||
/// </summary> |
||||
public class ResponseHeadersTest |
||||
{ |
||||
MockServiceHelper helper; |
||||
Server server; |
||||
Channel channel; |
||||
|
||||
Metadata headers; |
||||
|
||||
[SetUp] |
||||
public void Init() |
||||
{ |
||||
helper = new MockServiceHelper(); |
||||
|
||||
server = helper.GetServer(); |
||||
server.Start(); |
||||
channel = helper.GetChannel(); |
||||
|
||||
headers = new Metadata { { "ascii-header", "abcdefg" } }; |
||||
} |
||||
|
||||
[TearDown] |
||||
public void Cleanup() |
||||
{ |
||||
channel.Dispose(); |
||||
server.ShutdownAsync().Wait(); |
||||
} |
||||
|
||||
[TestFixtureTearDown] |
||||
public void CleanupClass() |
||||
{ |
||||
GrpcEnvironment.Shutdown(); |
||||
} |
||||
|
||||
[Test] |
||||
public void WriteResponseHeaders_NullNotAllowed() |
||||
{ |
||||
helper.UnaryHandler = new UnaryServerMethod<string, string>(async (request, context) => |
||||
{ |
||||
Assert.Throws(typeof(ArgumentNullException), async () => await context.WriteResponseHeadersAsync(null)); |
||||
return "PASS"; |
||||
}); |
||||
|
||||
Assert.AreEqual("PASS", Calls.BlockingUnaryCall(helper.CreateUnaryCall(), "")); |
||||
} |
||||
|
||||
[Test] |
||||
public void WriteResponseHeaders_AllowedOnlyOnce() |
||||
{ |
||||
helper.UnaryHandler = new UnaryServerMethod<string, string>(async (request, context) => |
||||
{ |
||||
await context.WriteResponseHeadersAsync(headers); |
||||
try |
||||
{ |
||||
await context.WriteResponseHeadersAsync(headers); |
||||
Assert.Fail(); |
||||
} |
||||
catch (InvalidOperationException expected) |
||||
{ |
||||
} |
||||
return "PASS"; |
||||
}); |
||||
|
||||
Assert.AreEqual("PASS", Calls.BlockingUnaryCall(helper.CreateUnaryCall(), "")); |
||||
} |
||||
|
||||
[Test] |
||||
public async Task WriteResponseHeaders_NotAllowedAfterWrite() |
||||
{ |
||||
helper.ServerStreamingHandler = new ServerStreamingServerMethod<string, string>(async (request, responseStream, context) => |
||||
{ |
||||
await responseStream.WriteAsync("A"); |
||||
try |
||||
{ |
||||
await context.WriteResponseHeadersAsync(headers); |
||||
Assert.Fail(); |
||||
} |
||||
catch (InvalidOperationException expected) |
||||
{ |
||||
} |
||||
await responseStream.WriteAsync("B"); |
||||
}); |
||||
|
||||
var call = Calls.AsyncServerStreamingCall(helper.CreateServerStreamingCall(), ""); |
||||
var responses = await call.ResponseStream.ToListAsync(); |
||||
CollectionAssert.AreEqual(new[] { "A", "B" }, responses); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,171 @@ |
||||
#region Copyright notice and license |
||||
|
||||
// Copyright 2015, Google Inc. |
||||
// All rights reserved. |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
#endregion |
||||
|
||||
using System; |
||||
using System.Threading; |
||||
|
||||
using Grpc.Core.Internal; |
||||
using Grpc.Core.Utils; |
||||
|
||||
namespace Grpc.Core |
||||
{ |
||||
/// <summary> |
||||
/// Token for propagating context of server side handlers to child calls. |
||||
/// In situations when a backend is making calls to another backend, |
||||
/// it makes sense to propagate properties like deadline and cancellation |
||||
/// token of the server call to the child call. |
||||
/// C core provides some other contexts (like tracing context) that |
||||
/// are not accessible to C# layer, but this token still allows propagating them. |
||||
/// </summary> |
||||
public class ContextPropagationToken |
||||
{ |
||||
/// <summary> |
||||
/// Default propagation mask used by C core. |
||||
/// </summary> |
||||
private const ContextPropagationFlags DefaultCoreMask = (ContextPropagationFlags)0xffff; |
||||
|
||||
/// <summary> |
||||
/// Default propagation mask used by C# - we want to propagate deadline |
||||
/// and cancellation token by our own means. |
||||
/// </summary> |
||||
internal const ContextPropagationFlags DefaultMask = DefaultCoreMask |
||||
& ~ContextPropagationFlags.Deadline & ~ContextPropagationFlags.Cancellation; |
||||
|
||||
readonly CallSafeHandle parentCall; |
||||
readonly DateTime deadline; |
||||
readonly CancellationToken cancellationToken; |
||||
readonly ContextPropagationOptions options; |
||||
|
||||
internal ContextPropagationToken(CallSafeHandle parentCall, DateTime deadline, CancellationToken cancellationToken, ContextPropagationOptions options) |
||||
{ |
||||
this.parentCall = Preconditions.CheckNotNull(parentCall); |
||||
this.deadline = deadline; |
||||
this.cancellationToken = cancellationToken; |
||||
this.options = options ?? ContextPropagationOptions.Default; |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Gets the native handle of the parent call. |
||||
/// </summary> |
||||
internal CallSafeHandle ParentCall |
||||
{ |
||||
get |
||||
{ |
||||
return this.parentCall; |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Gets the parent call's deadline. |
||||
/// </summary> |
||||
internal DateTime ParentDeadline |
||||
{ |
||||
get |
||||
{ |
||||
return this.deadline; |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Gets the parent call's cancellation token. |
||||
/// </summary> |
||||
internal CancellationToken ParentCancellationToken |
||||
{ |
||||
get |
||||
{ |
||||
return this.cancellationToken; |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Get the context propagation options. |
||||
/// </summary> |
||||
internal ContextPropagationOptions Options |
||||
{ |
||||
get |
||||
{ |
||||
return this.options; |
||||
} |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Options for <see cref="ContextPropagationToken"/>. |
||||
/// </summary> |
||||
public class ContextPropagationOptions |
||||
{ |
||||
/// <summary> |
||||
/// The context propagation options that will be used by default. |
||||
/// </summary> |
||||
public static readonly ContextPropagationOptions Default = new ContextPropagationOptions(); |
||||
|
||||
bool propagateDeadline; |
||||
bool propagateCancellation; |
||||
|
||||
|
||||
/// <summary> |
||||
/// Creates new context propagation options. |
||||
/// </summary> |
||||
/// <param name="propagateDeadline">If set to <c>true</c> parent call's deadline will be propagated to the child call.</param> |
||||
/// <param name="propagateCancellation">If set to <c>true</c> parent call's cancellation token will be propagated to the child call.</param> |
||||
public ContextPropagationOptions(bool propagateDeadline = true, bool propagateCancellation = true) |
||||
{ |
||||
this.propagateDeadline = propagateDeadline; |
||||
this.propagateCancellation = propagateCancellation; |
||||
} |
||||
|
||||
/// <value><c>true</c> if parent call's deadline should be propagated to the child call.</value> |
||||
public bool IsPropagateDeadline |
||||
{ |
||||
get { return this.propagateDeadline; } |
||||
} |
||||
|
||||
/// <value><c>true</c> if parent call's cancellation token should be propagated to the child call.</value> |
||||
public bool IsPropagateCancellation |
||||
{ |
||||
get { return this.propagateCancellation; } |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Context propagation flags from grpc/grpc.h. |
||||
/// </summary> |
||||
[Flags] |
||||
internal enum ContextPropagationFlags |
||||
{ |
||||
Deadline = 1, |
||||
CensusStatsContext = 2, |
||||
CensusTracingContext = 4, |
||||
Cancellation = 8 |
||||
} |
||||
} |
@ -1,5 +1,37 @@ |
||||
#region Copyright notice and license |
||||
|
||||
// Copyright 2015, Google Inc. |
||||
// All rights reserved. |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
#endregion |
||||
|
||||
using System.Reflection; |
||||
using System.Runtime.CompilerServices; |
||||
|
||||
// The current version of gRPC C#. |
||||
[assembly: AssemblyVersion(Grpc.Core.VersionInfo.CurrentVersion + ".*")] |
||||
[assembly: AssemblyVersion(Grpc.Core.VersionInfo.CurrentVersion + ".0")] |
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue