mirror of https://github.com/grpc/grpc.git
commit
afdc59712d
317 changed files with 15616 additions and 4783 deletions
@ -1,30 +0,0 @@ |
||||
{ |
||||
"version": "0.2.0", |
||||
"configurations": [ |
||||
{ |
||||
"type": "node", |
||||
"request": "launch", |
||||
"name": "Mocha Tests", |
||||
"cwd": "${workspaceRoot}", |
||||
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/mocha", |
||||
"windows": { |
||||
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/mocha.cmd" |
||||
}, |
||||
"runtimeArgs": [ |
||||
"-u", |
||||
"tdd", |
||||
"--timeout", |
||||
"999999", |
||||
"--colors", |
||||
"${workspaceRoot}/src/node/test" |
||||
], |
||||
"internalConsoleOptions": "openOnSessionStart" |
||||
}, |
||||
{ |
||||
"type": "node", |
||||
"request": "attach", |
||||
"name": "Attach to Process", |
||||
"port": 5858 |
||||
} |
||||
] |
||||
} |
@ -0,0 +1,32 @@ |
||||
# 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_library( |
||||
name = "wait_for_ready_example", |
||||
testonly = 1, |
||||
srcs = ["wait_for_ready_example.py"], |
||||
deps = [ |
||||
"//src/python/grpcio/grpc:grpcio", |
||||
"//examples:py_helloworld", |
||||
], |
||||
) |
||||
|
||||
py_test( |
||||
name = "test/_wait_for_ready_example_test", |
||||
srcs = ["test/_wait_for_ready_example_test.py"], |
||||
deps = [":wait_for_ready_example",], |
||||
size = "small", |
||||
) |
@ -0,0 +1,32 @@ |
||||
# gRPC Python Example for Wait-for-ready |
||||
|
||||
The default behavior of an RPC is to fail instantly if the server is not ready yet. This example demonstrates how to change that behavior. |
||||
|
||||
|
||||
### Definition of 'wait-for-ready' semantics |
||||
> If an RPC is issued but the channel is in TRANSIENT_FAILURE or SHUTDOWN states, the RPC is unable to be transmitted promptly. By default, gRPC implementations SHOULD fail such RPCs immediately. This is known as "fail fast," but the usage of the term is historical. RPCs SHOULD NOT fail as a result of the channel being in other states (CONNECTING, READY, or IDLE). |
||||
> |
||||
> gRPC implementations MAY provide a per-RPC option to not fail RPCs as a result of the channel being in TRANSIENT_FAILURE state. Instead, the implementation queues the RPCs until the channel is READY. This is known as "wait for ready." The RPCs SHOULD still fail before READY if there are unrelated reasons, such as the channel is SHUTDOWN or the RPC's deadline is reached. |
||||
> |
||||
> From https://github.com/grpc/grpc/blob/master/doc/wait-for-ready.md |
||||
|
||||
|
||||
### Use cases for 'wait-for-ready' |
||||
|
||||
When developers spin up gRPC clients and servers at the same time, it is very like to fail first couple RPC calls due to unavailability of the server. If developers failed to prepare for this situation, the result can be catastrophic. But with 'wait-for-ready' semantics, developers can initialize the client and server in any order, especially useful in testing. |
||||
|
||||
Also, developers may ensure the server is up before starting client. But in some cases like transient network failure may result in a temporary unavailability of the server. With 'wait-for-ready' semantics, those RPC calls will automatically wait until the server is ready to accept incoming requests. |
||||
|
||||
|
||||
### DEMO Snippets |
||||
|
||||
```Python |
||||
# Per RPC level |
||||
stub = ...Stub(...) |
||||
|
||||
stub.important_transaction_1(..., wait_for_ready=True) |
||||
stub.unimportant_transaction_2(...) |
||||
stub.important_transaction_3(..., wait_for_ready=True) |
||||
stub.unimportant_transaction_4(...) |
||||
# The unimportant transactions can be status report, or health check, etc. |
||||
``` |
@ -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. |
||||
"""Tests of the wait-for-ready example.""" |
||||
|
||||
import unittest |
||||
import logging |
||||
|
||||
from examples.python.wait_for_ready import wait_for_ready_example |
||||
|
||||
|
||||
class WaitForReadyExampleTest(unittest.TestCase): |
||||
|
||||
def test_wait_for_ready_example(self): |
||||
wait_for_ready_example.main() |
||||
# No unhandled exception raised, no deadlock, test passed! |
||||
|
||||
|
||||
if __name__ == '__main__': |
||||
logging.basicConfig() |
||||
unittest.main(verbosity=2) |
@ -0,0 +1,114 @@ |
||||
# 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 wait-for-ready flag.""" |
||||
|
||||
from __future__ import print_function |
||||
import logging |
||||
from concurrent import futures |
||||
from contextlib import contextmanager |
||||
import socket |
||||
import threading |
||||
|
||||
import grpc |
||||
|
||||
from examples.protos import helloworld_pb2 |
||||
from examples.protos import helloworld_pb2_grpc |
||||
|
||||
_LOGGER = logging.getLogger(__name__) |
||||
_LOGGER.setLevel(logging.INFO) |
||||
|
||||
_ONE_DAY_IN_SECONDS = 60 * 60 * 24 |
||||
|
||||
|
||||
@contextmanager |
||||
def get_free_loopback_tcp_port(): |
||||
tcp_socket = socket.socket(socket.AF_INET6) |
||||
tcp_socket.bind(('', 0)) |
||||
address_tuple = tcp_socket.getsockname() |
||||
yield "[::1]:%s" % (address_tuple[1]) |
||||
tcp_socket.close() |
||||
|
||||
|
||||
class Greeter(helloworld_pb2_grpc.GreeterServicer): |
||||
|
||||
def SayHello(self, request, unused_context): |
||||
return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name) |
||||
|
||||
|
||||
def create_server(server_address): |
||||
server = grpc.server(futures.ThreadPoolExecutor()) |
||||
helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server) |
||||
bound_port = server.add_insecure_port(server_address) |
||||
assert bound_port == int(server_address.split(':')[-1]) |
||||
return server |
||||
|
||||
|
||||
def process(stub, wait_for_ready=None): |
||||
try: |
||||
response = stub.SayHello( |
||||
helloworld_pb2.HelloRequest(name='you'), |
||||
wait_for_ready=wait_for_ready) |
||||
message = response.message |
||||
except grpc.RpcError as rpc_error: |
||||
assert rpc_error.code() == grpc.StatusCode.UNAVAILABLE |
||||
assert not wait_for_ready |
||||
message = rpc_error |
||||
else: |
||||
assert wait_for_ready |
||||
_LOGGER.info("Wait-for-ready %s, client received: %s", "enabled" |
||||
if wait_for_ready else "disabled", message) |
||||
|
||||
|
||||
def main(): |
||||
# Pick a random free port |
||||
with get_free_loopback_tcp_port() as server_address: |
||||
|
||||
# Register connectivity event to notify main thread |
||||
transient_failure_event = threading.Event() |
||||
|
||||
def wait_for_transient_failure(channel_connectivity): |
||||
if channel_connectivity == grpc.ChannelConnectivity.TRANSIENT_FAILURE: |
||||
transient_failure_event.set() |
||||
|
||||
# Create gRPC channel |
||||
channel = grpc.insecure_channel(server_address) |
||||
channel.subscribe(wait_for_transient_failure) |
||||
stub = helloworld_pb2_grpc.GreeterStub(channel) |
||||
|
||||
# Fire an RPC without wait_for_ready |
||||
thread_disabled_wait_for_ready = threading.Thread( |
||||
target=process, args=(stub, False)) |
||||
thread_disabled_wait_for_ready.start() |
||||
# Fire an RPC with wait_for_ready |
||||
thread_enabled_wait_for_ready = threading.Thread( |
||||
target=process, args=(stub, True)) |
||||
thread_enabled_wait_for_ready.start() |
||||
|
||||
# Wait for the channel entering TRANSIENT FAILURE state. |
||||
transient_failure_event.wait() |
||||
server = create_server(server_address) |
||||
server.start() |
||||
|
||||
# Expected to fail with StatusCode.UNAVAILABLE. |
||||
thread_disabled_wait_for_ready.join() |
||||
# Expected to success. |
||||
thread_enabled_wait_for_ready.join() |
||||
|
||||
server.stop(None) |
||||
channel.close() |
||||
|
||||
|
||||
if __name__ == '__main__': |
||||
logging.basicConfig(level=logging.INFO) |
||||
main() |
@ -0,0 +1,55 @@ |
||||
/*
|
||||
* |
||||
* 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_MESSAGE_ALLOCATOR_H |
||||
#define GRPCPP_IMPL_CODEGEN_MESSAGE_ALLOCATOR_H |
||||
|
||||
namespace grpc { |
||||
namespace experimental { |
||||
|
||||
// This is per rpc struct for the allocator. We can potentially put the grpc
|
||||
// call arena in here in the future.
|
||||
template <typename RequestT, typename ResponseT> |
||||
struct RpcAllocatorInfo { |
||||
RequestT* request; |
||||
ResponseT* response; |
||||
// per rpc allocator internal state. MessageAllocator can set it when
|
||||
// AllocateMessages is called and use it later.
|
||||
void* allocator_state; |
||||
}; |
||||
|
||||
// Implementations need to be thread-safe
|
||||
template <typename RequestT, typename ResponseT> |
||||
class MessageAllocator { |
||||
public: |
||||
virtual ~MessageAllocator() = default; |
||||
// Allocate both request and response
|
||||
virtual void AllocateMessages( |
||||
RpcAllocatorInfo<RequestT, ResponseT>* info) = 0; |
||||
// Optional: deallocate request early, called by
|
||||
// ServerCallbackRpcController::ReleaseRequest
|
||||
virtual void DeallocateRequest(RpcAllocatorInfo<RequestT, ResponseT>* info) {} |
||||
// Deallocate response and request (if applicable)
|
||||
virtual void DeallocateMessages( |
||||
RpcAllocatorInfo<RequestT, ResponseT>* info) = 0; |
||||
}; |
||||
|
||||
} // namespace experimental
|
||||
} // namespace grpc
|
||||
|
||||
#endif // GRPCPP_IMPL_CODEGEN_MESSAGE_ALLOCATOR_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_SUPPORT_MESSAGE_ALLOCATOR_H |
||||
#define GRPCPP_SUPPORT_MESSAGE_ALLOCATOR_H |
||||
|
||||
#include <grpcpp/impl/codegen/message_allocator.h> |
||||
|
||||
#endif // GRPCPP_SUPPORT_MESSAGE_ALLOCATOR_H
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue