mirror of https://github.com/grpc/grpc.git
commit
68dff87bac
382 changed files with 19980 additions and 1996 deletions
@ -0,0 +1,30 @@ |
||||
{ |
||||
"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 |
||||
} |
||||
] |
||||
} |
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,25 @@ |
||||
#Errors and Cancelletion code samples for grpc-ruby |
||||
|
||||
The examples in this directory show use of grpc errors. |
||||
|
||||
On the server side, errors are returned from service |
||||
implementations by raising a certain `GRPC::BadStatus` exception. |
||||
|
||||
On the client side, GRPC errors get raised when either: |
||||
* the call completes (unary and client-streaming call types) |
||||
* the response `Enumerable` is iterated through (server-streaming and |
||||
bidi call types). |
||||
|
||||
## To run the examples here: |
||||
|
||||
Start the server: |
||||
|
||||
``` |
||||
> ruby error_examples_server.rb |
||||
``` |
||||
|
||||
Then run the client: |
||||
|
||||
``` |
||||
> ruby error_examples_client.rb |
||||
``` |
@ -0,0 +1,117 @@ |
||||
#!/usr/bin/env ruby |
||||
|
||||
# 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. |
||||
|
||||
# Sample app that connects to an error-throwing implementation of |
||||
# Route Guide service. |
||||
# |
||||
# Usage: $ path/to/route_guide_client.rb |
||||
|
||||
this_dir = File.expand_path(File.dirname(__FILE__)) |
||||
lib_dir = File.join(File.dirname(this_dir), 'lib') |
||||
$LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir) |
||||
|
||||
require 'grpc' |
||||
require 'route_guide_services_pb' |
||||
|
||||
include Routeguide |
||||
|
||||
def run_get_feature_expect_error(stub) |
||||
resp = stub.get_feature(Point.new) |
||||
end |
||||
|
||||
def run_list_features_expect_error(stub) |
||||
resps = stub.list_features(Rectangle.new) |
||||
|
||||
# NOOP iteration to pick up error |
||||
resps.each { } |
||||
end |
||||
|
||||
def run_record_route_expect_error(stub) |
||||
stub.record_route([]) |
||||
end |
||||
|
||||
def run_route_chat_expect_error(stub) |
||||
resps = stub.route_chat([]) |
||||
|
||||
# NOOP iteration to pick up error |
||||
resps.each { } |
||||
end |
||||
|
||||
def main |
||||
stub = RouteGuide::Stub.new('localhost:50051', :this_channel_is_insecure) |
||||
|
||||
begin |
||||
run_get_feature_expect_error(stub) |
||||
rescue GRPC::BadStatus => e |
||||
puts "===== GetFeature exception: =====" |
||||
puts e.inspect |
||||
puts "e.code: #{e.code}" |
||||
puts "e.details: #{e.details}" |
||||
puts "e.metadata: #{e.metadata}" |
||||
puts "=================================" |
||||
end |
||||
|
||||
begin |
||||
run_list_features_expect_error(stub) |
||||
rescue GRPC::BadStatus => e |
||||
error = true |
||||
puts "===== ListFeatures exception: =====" |
||||
puts e.inspect |
||||
puts "e.code: #{e.code}" |
||||
puts "e.details: #{e.details}" |
||||
puts "e.metadata: #{e.metadata}" |
||||
puts "=================================" |
||||
end |
||||
|
||||
begin |
||||
run_route_chat_expect_error(stub) |
||||
rescue GRPC::BadStatus => e |
||||
puts "==== RouteChat exception: ====" |
||||
puts e.inspect |
||||
puts "e.code: #{e.code}" |
||||
puts "e.details: #{e.details}" |
||||
puts "e.metadata: #{e.metadata}" |
||||
puts "=================================" |
||||
end |
||||
|
||||
begin |
||||
run_record_route_expect_error(stub) |
||||
rescue GRPC::BadStatus => e |
||||
puts "==== RecordRoute exception: ====" |
||||
puts e.inspect |
||||
puts "e.code: #{e.code}" |
||||
puts "e.details: #{e.details}" |
||||
puts "e.metadata: #{e.metadata}" |
||||
puts "=================================" |
||||
end |
||||
end |
||||
|
||||
main |
@ -0,0 +1,76 @@ |
||||
#!/usr/bin/env ruby |
||||
# -*- coding: utf-8 -*- |
||||
|
||||
# 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. |
||||
|
||||
# Error-throwing implementation of Route Guide service. |
||||
# |
||||
# Usage: $ path/to/route_guide_server.rb |
||||
|
||||
this_dir = File.expand_path(File.dirname(__FILE__)) |
||||
lib_dir = File.join(File.dirname(this_dir), 'lib') |
||||
$LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir) |
||||
|
||||
require 'grpc' |
||||
require 'route_guide_services_pb' |
||||
|
||||
include Routeguide |
||||
|
||||
include GRPC::Core::StatusCodes |
||||
|
||||
# CanellingandErrorReturningServiceImpl provides an implementation of the RouteGuide service. |
||||
class CancellingAndErrorReturningServerImpl < RouteGuide::Service |
||||
# def get_feature |
||||
# Note get_feature isn't implemented in this subclass, so the server |
||||
# will get a gRPC UNIMPLEMENTED error when it's called. |
||||
|
||||
def list_features(rectangle, _call) |
||||
raise "string appears on the client in the 'details' field of a 'GRPC::Unknown' exception" |
||||
end |
||||
|
||||
def record_route(call) |
||||
raise GRPC::BadStatus.new_status_exception(CANCELLED) |
||||
end |
||||
|
||||
def route_chat(notes) |
||||
raise GRPC::BadStatus.new_status_exception(ABORTED, details = 'arbitrary', metadata = {somekey: 'val'}) |
||||
end |
||||
end |
||||
|
||||
def main |
||||
port = '0.0.0.0:50051' |
||||
s = GRPC::RpcServer.new |
||||
s.add_http2_port(port, :this_port_is_insecure) |
||||
GRPC.logger.info("... running insecurely on #{port}") |
||||
s.handle(CancellingAndErrorReturningServerImpl.new) |
||||
s.run_till_terminated |
||||
end |
||||
|
||||
main |
@ -0,0 +1,49 @@ |
||||
# c-ares cmake file for gRPC |
||||
# |
||||
# This is currently very experimental, and unsupported. |
||||
# |
||||
# Copyright 2016, 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. |
||||
|
||||
string(TOLOWER ${CMAKE_SYSTEM_NAME} cares_system_name) |
||||
|
||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../third_party/cares) |
||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../third_party/cares/cares) |
||||
|
||||
if(${cares_system_name} MATCHES windows) |
||||
add_definitions(-DCARES_STATICLIB=1) |
||||
add_definitions(-DWIN32_LEAN_AND_MEAN=1) |
||||
else() |
||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../third_party/cares/config_${cares_system_name}) |
||||
add_definitions(-DHAVE_CONFIG_H=1) |
||||
add_definitions(-D_GNU_SOURCE=1) |
||||
endif() |
||||
|
||||
file(GLOB lib_sources ../../third_party/cares/cares/*.c) |
||||
add_library(cares ${lib_sources}) |
@ -0,0 +1,149 @@ |
||||
#!/usr/bin/env python2.7 |
||||
|
||||
# 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. |
||||
|
||||
import re |
||||
import os |
||||
import sys |
||||
import yaml |
||||
|
||||
os.chdir(os.path.dirname(sys.argv[0])+'/../..') |
||||
|
||||
out = {} |
||||
|
||||
try: |
||||
def gen_ares_build(x): |
||||
subprocess.call("third_party/cares/cares/buildconf", shell=True) |
||||
subprocess.call("third_party/cares/cares/configure", shell=True) |
||||
|
||||
def config_platform(x): |
||||
if 'linux' in sys.platform: |
||||
return 'src/cares/cares/config_linux/ares_config.h' |
||||
if 'darwin' in sys.platform: |
||||
return 'src/cares/cares/config_darwin/ares_config.h' |
||||
if not os.path.isfile('third_party/cares/cares/ares_config.h'): |
||||
gen_ares_build(x) |
||||
return 'third_party/cares/cares/ares_config.h' |
||||
|
||||
def ares_build(x): |
||||
if os.path.isfile('src/cares/cares/ares_build.h'): |
||||
return 'src/cares/cares/ares_build.h' |
||||
if not os.path.isfile('third_party/cares/cares/ares_build.h'): |
||||
gen_ares_build(x) |
||||
return 'third_party/cares/cares/ares_build.h' |
||||
|
||||
out['libs'] = [{ |
||||
'name': 'ares', |
||||
'defaults': 'ares', |
||||
'build': 'private', |
||||
'language': 'c', |
||||
'secure': 'no', |
||||
'src': [ |
||||
"third_party/cares/cares/ares__close_sockets.c", |
||||
"third_party/cares/cares/ares__get_hostent.c", |
||||
"third_party/cares/cares/ares__read_line.c", |
||||
"third_party/cares/cares/ares__timeval.c", |
||||
"third_party/cares/cares/ares_cancel.c", |
||||
"third_party/cares/cares/ares_create_query.c", |
||||
"third_party/cares/cares/ares_data.c", |
||||
"third_party/cares/cares/ares_destroy.c", |
||||
"third_party/cares/cares/ares_expand_name.c", |
||||
"third_party/cares/cares/ares_expand_string.c", |
||||
"third_party/cares/cares/ares_fds.c", |
||||
"third_party/cares/cares/ares_free_hostent.c", |
||||
"third_party/cares/cares/ares_free_string.c", |
||||
"third_party/cares/cares/ares_getenv.c", |
||||
"third_party/cares/cares/ares_gethostbyaddr.c", |
||||
"third_party/cares/cares/ares_gethostbyname.c", |
||||
"third_party/cares/cares/ares_getnameinfo.c", |
||||
"third_party/cares/cares/ares_getopt.c", |
||||
"third_party/cares/cares/ares_getsock.c", |
||||
"third_party/cares/cares/ares_init.c", |
||||
"third_party/cares/cares/ares_library_init.c", |
||||
"third_party/cares/cares/ares_llist.c", |
||||
"third_party/cares/cares/ares_mkquery.c", |
||||
"third_party/cares/cares/ares_nowarn.c", |
||||
"third_party/cares/cares/ares_options.c", |
||||
"third_party/cares/cares/ares_parse_a_reply.c", |
||||
"third_party/cares/cares/ares_parse_aaaa_reply.c", |
||||
"third_party/cares/cares/ares_parse_mx_reply.c", |
||||
"third_party/cares/cares/ares_parse_naptr_reply.c", |
||||
"third_party/cares/cares/ares_parse_ns_reply.c", |
||||
"third_party/cares/cares/ares_parse_ptr_reply.c", |
||||
"third_party/cares/cares/ares_parse_soa_reply.c", |
||||
"third_party/cares/cares/ares_parse_srv_reply.c", |
||||
"third_party/cares/cares/ares_parse_txt_reply.c", |
||||
"third_party/cares/cares/ares_platform.c", |
||||
"third_party/cares/cares/ares_process.c", |
||||
"third_party/cares/cares/ares_query.c", |
||||
"third_party/cares/cares/ares_search.c", |
||||
"third_party/cares/cares/ares_send.c", |
||||
"third_party/cares/cares/ares_strcasecmp.c", |
||||
"third_party/cares/cares/ares_strdup.c", |
||||
"third_party/cares/cares/ares_strerror.c", |
||||
"third_party/cares/cares/ares_timeout.c", |
||||
"third_party/cares/cares/ares_version.c", |
||||
"third_party/cares/cares/ares_writev.c", |
||||
"third_party/cares/cares/bitncmp.c", |
||||
"third_party/cares/cares/inet_net_pton.c", |
||||
"third_party/cares/cares/inet_ntop.c", |
||||
"third_party/cares/cares/windows_port.c", |
||||
], |
||||
'headers': [ |
||||
"third_party/cares/cares/ares.h", |
||||
"third_party/cares/cares/ares_data.h", |
||||
"third_party/cares/cares/ares_dns.h", |
||||
"third_party/cares/cares/ares_getenv.h", |
||||
"third_party/cares/cares/ares_getopt.h", |
||||
"third_party/cares/cares/ares_inet_net_pton.h", |
||||
"third_party/cares/cares/ares_iphlpapi.h", |
||||
"third_party/cares/cares/ares_ipv6.h", |
||||
"third_party/cares/cares/ares_library_init.h", |
||||
"third_party/cares/cares/ares_llist.h", |
||||
"third_party/cares/cares/ares_nowarn.h", |
||||
"third_party/cares/cares/ares_platform.h", |
||||
"third_party/cares/cares/ares_private.h", |
||||
"third_party/cares/cares/ares_rules.h", |
||||
"third_party/cares/cares/ares_setup.h", |
||||
"third_party/cares/cares/ares_strcasecmp.h", |
||||
"third_party/cares/cares/ares_strdup.h", |
||||
"third_party/cares/cares/ares_version.h", |
||||
"third_party/cares/cares/bitncmp.h", |
||||
"third_party/cares/cares/config-win32.h", |
||||
"third_party/cares/cares/setup_once.h", |
||||
"third_party/cares/ares_build.h", |
||||
"third_party/cares/config_linux/ares_config.h", |
||||
"third_party/cares/config_darwin/ares_config.h" |
||||
], |
||||
}] |
||||
except: |
||||
pass |
||||
|
||||
print yaml.dump(out) |
@ -1,4 +1,4 @@ |
||||
#Overview |
||||
# Overview |
||||
|
||||
This directory contains source code for gRPC protocol buffer compiler (*protoc*) plugins. Along with `protoc`, |
||||
these plugins are used to generate gRPC client and server stubs from `.proto` files. |
||||
|
@ -1,4 +1,4 @@ |
||||
#Overview |
||||
# Overview |
||||
|
||||
This directory contains source code for C library (a.k.a the *gRPC C core*) that provides all gRPC's core functionality through a low level API. Libraries in other languages in this repository (C++, Ruby, |
||||
Python, PHP, NodeJS, Objective-C) are layered on top of this library. |
||||
|
@ -0,0 +1,350 @@ |
||||
/*
|
||||
* |
||||
* 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. |
||||
* |
||||
*/ |
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
#if GRPC_ARES == 1 && !defined(GRPC_UV) |
||||
|
||||
#include <string.h> |
||||
|
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/host_port.h> |
||||
#include <grpc/support/string_util.h> |
||||
|
||||
#include "src/core/ext/client_channel/http_connect_handshaker.h" |
||||
#include "src/core/ext/client_channel/lb_policy_registry.h" |
||||
#include "src/core/ext/client_channel/resolver_registry.h" |
||||
#include "src/core/ext/resolver/dns/c_ares/grpc_ares_wrapper.h" |
||||
#include "src/core/lib/channel/channel_args.h" |
||||
#include "src/core/lib/iomgr/combiner.h" |
||||
#include "src/core/lib/iomgr/resolve_address.h" |
||||
#include "src/core/lib/iomgr/timer.h" |
||||
#include "src/core/lib/support/backoff.h" |
||||
#include "src/core/lib/support/env.h" |
||||
#include "src/core/lib/support/string.h" |
||||
|
||||
#define GRPC_DNS_MIN_CONNECT_TIMEOUT_SECONDS 1 |
||||
#define GRPC_DNS_INITIAL_CONNECT_BACKOFF_SECONDS 1 |
||||
#define GRPC_DNS_RECONNECT_BACKOFF_MULTIPLIER 1.6 |
||||
#define GRPC_DNS_RECONNECT_MAX_BACKOFF_SECONDS 120 |
||||
#define GRPC_DNS_RECONNECT_JITTER 0.2 |
||||
|
||||
typedef struct { |
||||
/** base class: must be first */ |
||||
grpc_resolver base; |
||||
/** name to resolve (usually the same as target_name) */ |
||||
char *name_to_resolve; |
||||
/** default port to use */ |
||||
char *default_port; |
||||
/** channel args. */ |
||||
grpc_channel_args *channel_args; |
||||
/** pollset_set to drive the name resolution process */ |
||||
grpc_pollset_set *interested_parties; |
||||
|
||||
/** Closures used by the combiner */ |
||||
grpc_closure dns_ares_on_retry_timer_locked; |
||||
grpc_closure dns_ares_on_resolved_locked; |
||||
|
||||
/** Combiner guarding the rest of the state */ |
||||
grpc_combiner *combiner; |
||||
/** are we currently resolving? */ |
||||
bool resolving; |
||||
/** which version of the result have we published? */ |
||||
int published_version; |
||||
/** which version of the result is current? */ |
||||
int resolved_version; |
||||
/** pending next completion, or NULL */ |
||||
grpc_closure *next_completion; |
||||
/** target result address for next completion */ |
||||
grpc_channel_args **target_result; |
||||
/** current (fully resolved) result */ |
||||
grpc_channel_args *resolved_result; |
||||
/** retry timer */ |
||||
bool have_retry_timer; |
||||
grpc_timer retry_timer; |
||||
/** retry backoff state */ |
||||
gpr_backoff backoff_state; |
||||
|
||||
/** currently resolving addresses */ |
||||
grpc_resolved_addresses *addresses; |
||||
} ares_dns_resolver; |
||||
|
||||
static void dns_ares_destroy(grpc_exec_ctx *exec_ctx, grpc_resolver *r); |
||||
|
||||
static void dns_ares_start_resolving_locked(grpc_exec_ctx *exec_ctx, |
||||
ares_dns_resolver *r); |
||||
static void dns_ares_maybe_finish_next_locked(grpc_exec_ctx *exec_ctx, |
||||
ares_dns_resolver *r); |
||||
|
||||
static void dns_ares_shutdown_locked(grpc_exec_ctx *exec_ctx, grpc_resolver *r); |
||||
static void dns_ares_channel_saw_error_locked(grpc_exec_ctx *exec_ctx, |
||||
grpc_resolver *r); |
||||
static void dns_ares_next_locked(grpc_exec_ctx *exec_ctx, grpc_resolver *r, |
||||
grpc_channel_args **target_result, |
||||
grpc_closure *on_complete); |
||||
|
||||
static const grpc_resolver_vtable dns_ares_resolver_vtable = { |
||||
dns_ares_destroy, dns_ares_shutdown_locked, |
||||
dns_ares_channel_saw_error_locked, dns_ares_next_locked}; |
||||
|
||||
static void dns_ares_shutdown_locked(grpc_exec_ctx *exec_ctx, |
||||
grpc_resolver *resolver) { |
||||
ares_dns_resolver *r = (ares_dns_resolver *)resolver; |
||||
if (r->have_retry_timer) { |
||||
grpc_timer_cancel(exec_ctx, &r->retry_timer); |
||||
} |
||||
if (r->next_completion != NULL) { |
||||
*r->target_result = NULL; |
||||
grpc_closure_sched( |
||||
exec_ctx, r->next_completion, |
||||
GRPC_ERROR_CREATE_FROM_STATIC_STRING("Resolver Shutdown")); |
||||
r->next_completion = NULL; |
||||
} |
||||
} |
||||
|
||||
static void dns_ares_channel_saw_error_locked(grpc_exec_ctx *exec_ctx, |
||||
grpc_resolver *resolver) { |
||||
ares_dns_resolver *r = (ares_dns_resolver *)resolver; |
||||
if (!r->resolving) { |
||||
gpr_backoff_reset(&r->backoff_state); |
||||
dns_ares_start_resolving_locked(exec_ctx, r); |
||||
} |
||||
} |
||||
|
||||
static void dns_ares_on_retry_timer_locked(grpc_exec_ctx *exec_ctx, void *arg, |
||||
grpc_error *error) { |
||||
ares_dns_resolver *r = arg; |
||||
r->have_retry_timer = false; |
||||
if (error == GRPC_ERROR_NONE) { |
||||
if (!r->resolving) { |
||||
dns_ares_start_resolving_locked(exec_ctx, r); |
||||
} |
||||
} |
||||
GRPC_RESOLVER_UNREF(exec_ctx, &r->base, "retry-timer"); |
||||
} |
||||
|
||||
static void dns_ares_on_resolved_locked(grpc_exec_ctx *exec_ctx, void *arg, |
||||
grpc_error *error) { |
||||
ares_dns_resolver *r = arg; |
||||
grpc_channel_args *result = NULL; |
||||
GPR_ASSERT(r->resolving); |
||||
r->resolving = false; |
||||
if (r->addresses != NULL) { |
||||
grpc_lb_addresses *addresses = grpc_lb_addresses_create( |
||||
r->addresses->naddrs, NULL /* user_data_vtable */); |
||||
for (size_t i = 0; i < r->addresses->naddrs; ++i) { |
||||
grpc_lb_addresses_set_address( |
||||
addresses, i, &r->addresses->addrs[i].addr, |
||||
r->addresses->addrs[i].len, false /* is_balancer */, |
||||
NULL /* balancer_name */, NULL /* user_data */); |
||||
} |
||||
grpc_arg new_arg = grpc_lb_addresses_create_channel_arg(addresses); |
||||
result = grpc_channel_args_copy_and_add(r->channel_args, &new_arg, 1); |
||||
grpc_resolved_addresses_destroy(r->addresses); |
||||
grpc_lb_addresses_destroy(exec_ctx, addresses); |
||||
} else { |
||||
gpr_timespec now = gpr_now(GPR_CLOCK_MONOTONIC); |
||||
gpr_timespec next_try = gpr_backoff_step(&r->backoff_state, now); |
||||
gpr_timespec timeout = gpr_time_sub(next_try, now); |
||||
gpr_log(GPR_INFO, "dns resolution failed (will retry): %s", |
||||
grpc_error_string(error)); |
||||
GPR_ASSERT(!r->have_retry_timer); |
||||
r->have_retry_timer = true; |
||||
GRPC_RESOLVER_REF(&r->base, "retry-timer"); |
||||
if (gpr_time_cmp(timeout, gpr_time_0(timeout.clock_type)) > 0) { |
||||
gpr_log(GPR_DEBUG, "retrying in %" PRId64 ".%09d seconds", timeout.tv_sec, |
||||
timeout.tv_nsec); |
||||
} else { |
||||
gpr_log(GPR_DEBUG, "retrying immediately"); |
||||
} |
||||
grpc_timer_init(exec_ctx, &r->retry_timer, next_try, |
||||
&r->dns_ares_on_retry_timer_locked, now); |
||||
} |
||||
if (r->resolved_result != NULL) { |
||||
grpc_channel_args_destroy(exec_ctx, r->resolved_result); |
||||
} |
||||
r->resolved_result = result; |
||||
r->resolved_version++; |
||||
dns_ares_maybe_finish_next_locked(exec_ctx, r); |
||||
GRPC_RESOLVER_UNREF(exec_ctx, &r->base, "dns-resolving"); |
||||
} |
||||
|
||||
static void dns_ares_next_locked(grpc_exec_ctx *exec_ctx, |
||||
grpc_resolver *resolver, |
||||
grpc_channel_args **target_result, |
||||
grpc_closure *on_complete) { |
||||
gpr_log(GPR_DEBUG, "dns_ares_next is called."); |
||||
ares_dns_resolver *r = (ares_dns_resolver *)resolver; |
||||
GPR_ASSERT(!r->next_completion); |
||||
r->next_completion = on_complete; |
||||
r->target_result = target_result; |
||||
if (r->resolved_version == 0 && !r->resolving) { |
||||
gpr_backoff_reset(&r->backoff_state); |
||||
dns_ares_start_resolving_locked(exec_ctx, r); |
||||
} else { |
||||
dns_ares_maybe_finish_next_locked(exec_ctx, r); |
||||
} |
||||
} |
||||
|
||||
static void dns_ares_start_resolving_locked(grpc_exec_ctx *exec_ctx, |
||||
ares_dns_resolver *r) { |
||||
GRPC_RESOLVER_REF(&r->base, "dns-resolving"); |
||||
GPR_ASSERT(!r->resolving); |
||||
r->resolving = true; |
||||
r->addresses = NULL; |
||||
grpc_resolve_address(exec_ctx, r->name_to_resolve, r->default_port, |
||||
r->interested_parties, &r->dns_ares_on_resolved_locked, |
||||
&r->addresses); |
||||
} |
||||
|
||||
static void dns_ares_maybe_finish_next_locked(grpc_exec_ctx *exec_ctx, |
||||
ares_dns_resolver *r) { |
||||
if (r->next_completion != NULL && |
||||
r->resolved_version != r->published_version) { |
||||
*r->target_result = r->resolved_result == NULL |
||||
? NULL |
||||
: grpc_channel_args_copy(r->resolved_result); |
||||
grpc_closure_sched(exec_ctx, r->next_completion, GRPC_ERROR_NONE); |
||||
r->next_completion = NULL; |
||||
r->published_version = r->resolved_version; |
||||
} |
||||
} |
||||
|
||||
static void dns_ares_destroy(grpc_exec_ctx *exec_ctx, grpc_resolver *gr) { |
||||
gpr_log(GPR_DEBUG, "dns_ares_destroy"); |
||||
ares_dns_resolver *r = (ares_dns_resolver *)gr; |
||||
if (r->resolved_result != NULL) { |
||||
grpc_channel_args_destroy(exec_ctx, r->resolved_result); |
||||
} |
||||
grpc_pollset_set_destroy(exec_ctx, r->interested_parties); |
||||
gpr_free(r->name_to_resolve); |
||||
gpr_free(r->default_port); |
||||
grpc_channel_args_destroy(exec_ctx, r->channel_args); |
||||
gpr_free(r); |
||||
} |
||||
|
||||
static grpc_resolver *dns_ares_create(grpc_exec_ctx *exec_ctx, |
||||
grpc_resolver_args *args, |
||||
const char *default_port) { |
||||
// Get name from args.
|
||||
const char *path = args->uri->path; |
||||
if (0 != strcmp(args->uri->authority, "")) { |
||||
gpr_log(GPR_ERROR, "authority based dns uri's not supported"); |
||||
return NULL; |
||||
} |
||||
if (path[0] == '/') ++path; |
||||
// Create resolver.
|
||||
ares_dns_resolver *r = gpr_zalloc(sizeof(ares_dns_resolver)); |
||||
grpc_resolver_init(&r->base, &dns_ares_resolver_vtable, args->combiner); |
||||
r->name_to_resolve = gpr_strdup(path); |
||||
r->default_port = gpr_strdup(default_port); |
||||
r->channel_args = grpc_channel_args_copy(args->args); |
||||
r->interested_parties = grpc_pollset_set_create(); |
||||
if (args->pollset_set != NULL) { |
||||
grpc_pollset_set_add_pollset_set(exec_ctx, r->interested_parties, |
||||
args->pollset_set); |
||||
} |
||||
gpr_backoff_init(&r->backoff_state, GRPC_DNS_INITIAL_CONNECT_BACKOFF_SECONDS, |
||||
GRPC_DNS_RECONNECT_BACKOFF_MULTIPLIER, |
||||
GRPC_DNS_RECONNECT_JITTER, |
||||
GRPC_DNS_MIN_CONNECT_TIMEOUT_SECONDS * 1000, |
||||
GRPC_DNS_RECONNECT_MAX_BACKOFF_SECONDS * 1000); |
||||
grpc_closure_init(&r->dns_ares_on_retry_timer_locked, |
||||
dns_ares_on_retry_timer_locked, r, |
||||
grpc_combiner_scheduler(r->base.combiner, false)); |
||||
grpc_closure_init(&r->dns_ares_on_resolved_locked, |
||||
dns_ares_on_resolved_locked, r, |
||||
grpc_combiner_scheduler(r->base.combiner, false)); |
||||
return &r->base; |
||||
} |
||||
|
||||
/*
|
||||
* FACTORY |
||||
*/ |
||||
|
||||
static void dns_ares_factory_ref(grpc_resolver_factory *factory) {} |
||||
|
||||
static void dns_ares_factory_unref(grpc_resolver_factory *factory) {} |
||||
|
||||
static grpc_resolver *dns_factory_create_resolver( |
||||
grpc_exec_ctx *exec_ctx, grpc_resolver_factory *factory, |
||||
grpc_resolver_args *args) { |
||||
return dns_ares_create(exec_ctx, args, "https"); |
||||
} |
||||
|
||||
static char *dns_ares_factory_get_default_host_name( |
||||
grpc_resolver_factory *factory, grpc_uri *uri) { |
||||
const char *path = uri->path; |
||||
if (path[0] == '/') ++path; |
||||
return gpr_strdup(path); |
||||
} |
||||
|
||||
static const grpc_resolver_factory_vtable dns_ares_factory_vtable = { |
||||
dns_ares_factory_ref, dns_ares_factory_unref, dns_factory_create_resolver, |
||||
dns_ares_factory_get_default_host_name, "dns"}; |
||||
static grpc_resolver_factory dns_resolver_factory = {&dns_ares_factory_vtable}; |
||||
|
||||
static grpc_resolver_factory *dns_ares_resolver_factory_create() { |
||||
return &dns_resolver_factory; |
||||
} |
||||
|
||||
void grpc_resolver_dns_ares_init(void) { |
||||
char *resolver = gpr_getenv("GRPC_DNS_RESOLVER"); |
||||
/* TODO(zyc): Turn on c-ares based resolver by default after the address
|
||||
sorter and the CNAME support are added. */ |
||||
if (resolver != NULL && gpr_stricmp(resolver, "ares") == 0) { |
||||
grpc_error *error = grpc_ares_init(); |
||||
if (error != GRPC_ERROR_NONE) { |
||||
GRPC_LOG_IF_ERROR("ares_library_init() failed", error); |
||||
return; |
||||
} |
||||
grpc_resolve_address = grpc_resolve_address_ares; |
||||
grpc_register_resolver_type(dns_ares_resolver_factory_create()); |
||||
} |
||||
gpr_free(resolver); |
||||
} |
||||
|
||||
void grpc_resolver_dns_ares_shutdown(void) { |
||||
char *resolver = gpr_getenv("GRPC_DNS_RESOLVER"); |
||||
if (resolver != NULL && gpr_stricmp(resolver, "ares") == 0) { |
||||
grpc_ares_cleanup(); |
||||
} |
||||
gpr_free(resolver); |
||||
} |
||||
|
||||
#else /* GRPC_ARES == 1 && !defined(GRPC_UV) */ |
||||
|
||||
void grpc_resolver_dns_ares_init(void) {} |
||||
|
||||
void grpc_resolver_dns_ares_shutdown(void) {} |
||||
|
||||
#endif /* GRPC_ARES == 1 && !defined(GRPC_UV) */ |
@ -0,0 +1,65 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2016, 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. |
||||
* |
||||
*/ |
||||
|
||||
#ifndef GRPC_CORE_EXT_RESOLVER_DNS_C_ARES_GRPC_ARES_EV_DRIVER_H |
||||
#define GRPC_CORE_EXT_RESOLVER_DNS_C_ARES_GRPC_ARES_EV_DRIVER_H |
||||
|
||||
#include <ares.h> |
||||
|
||||
#include "src/core/lib/iomgr/exec_ctx.h" |
||||
#include "src/core/lib/iomgr/pollset_set.h" |
||||
|
||||
typedef struct grpc_ares_ev_driver grpc_ares_ev_driver; |
||||
|
||||
/* Start \a ev_driver. It will keep working until all IO on its ares_channel is
|
||||
done, or grpc_ares_ev_driver_destroy() is called. It may notify the callbacks |
||||
bound to its ares_channel when necessary. */ |
||||
void grpc_ares_ev_driver_start(grpc_exec_ctx *exec_ctx, |
||||
grpc_ares_ev_driver *ev_driver); |
||||
|
||||
/* Returns the ares_channel owned by \a ev_driver. To bind a c-ares query to
|
||||
\a ev_driver, use the ares_channel owned by \a ev_driver as the arg of the |
||||
query. */ |
||||
ares_channel *grpc_ares_ev_driver_get_channel(grpc_ares_ev_driver *ev_driver); |
||||
|
||||
/* Creates a new grpc_ares_ev_driver. Returns GRPC_ERROR_NONE if \a ev_driver is
|
||||
created successfully. */ |
||||
grpc_error *grpc_ares_ev_driver_create(grpc_ares_ev_driver **ev_driver, |
||||
grpc_pollset_set *pollset_set); |
||||
|
||||
/* Destroys \a ev_driver asynchronously. Pending lookups made on \a ev_driver
|
||||
will be cancelled and their on_done callbacks will be invoked with a status |
||||
of ARES_ECANCELLED. */ |
||||
void grpc_ares_ev_driver_destroy(grpc_ares_ev_driver *ev_driver); |
||||
|
||||
#endif /* GRPC_CORE_EXT_RESOLVER_DNS_C_ARES_GRPC_ARES_EV_DRIVER_H */ |
@ -0,0 +1,319 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2016, 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. |
||||
* |
||||
*/ |
||||
#include <grpc/support/port_platform.h> |
||||
#include "src/core/lib/iomgr/port.h" |
||||
#if GRPC_ARES == 1 && defined(GRPC_POSIX_SOCKET) |
||||
|
||||
#include "src/core/ext/resolver/dns/c_ares/grpc_ares_ev_driver.h" |
||||
|
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/log.h> |
||||
#include <grpc/support/string_util.h> |
||||
#include <grpc/support/time.h> |
||||
#include <grpc/support/useful.h> |
||||
#include "src/core/ext/resolver/dns/c_ares/grpc_ares_wrapper.h" |
||||
#include "src/core/lib/iomgr/ev_posix.h" |
||||
#include "src/core/lib/iomgr/iomgr_internal.h" |
||||
#include "src/core/lib/iomgr/sockaddr_utils.h" |
||||
#include "src/core/lib/support/string.h" |
||||
|
||||
typedef struct fd_node { |
||||
/** the owner of this fd node */ |
||||
grpc_ares_ev_driver *ev_driver; |
||||
/** the grpc_fd owned by this fd node */ |
||||
grpc_fd *grpc_fd; |
||||
/** a closure wrapping on_readable_cb, which should be invoked when the
|
||||
grpc_fd in this node becomes readable. */ |
||||
grpc_closure read_closure; |
||||
/** a closure wrapping on_writable_cb, which should be invoked when the
|
||||
grpc_fd in this node becomes writable. */ |
||||
grpc_closure write_closure; |
||||
/** next fd node in the list */ |
||||
struct fd_node *next; |
||||
|
||||
/** mutex guarding the rest of the state */ |
||||
gpr_mu mu; |
||||
/** if the readable closure has been registered */ |
||||
bool readable_registered; |
||||
/** if the writable closure has been registered */ |
||||
bool writable_registered; |
||||
} fd_node; |
||||
|
||||
struct grpc_ares_ev_driver { |
||||
/** the ares_channel owned by this event driver */ |
||||
ares_channel channel; |
||||
/** pollset set for driving the IO events of the channel */ |
||||
grpc_pollset_set *pollset_set; |
||||
/** refcount of the event driver */ |
||||
gpr_refcount refs; |
||||
|
||||
/** mutex guarding the rest of the state */ |
||||
gpr_mu mu; |
||||
/** a list of grpc_fd that this event driver is currently using. */ |
||||
fd_node *fds; |
||||
/** is this event driver currently working? */ |
||||
bool working; |
||||
/** is this event driver being shut down */ |
||||
bool shutting_down; |
||||
}; |
||||
|
||||
static void grpc_ares_notify_on_event_locked(grpc_exec_ctx *exec_ctx, |
||||
grpc_ares_ev_driver *ev_driver); |
||||
|
||||
static grpc_ares_ev_driver *grpc_ares_ev_driver_ref( |
||||
grpc_ares_ev_driver *ev_driver) { |
||||
gpr_log(GPR_DEBUG, "Ref ev_driver %" PRIuPTR, (uintptr_t)ev_driver); |
||||
gpr_ref(&ev_driver->refs); |
||||
return ev_driver; |
||||
} |
||||
|
||||
static void grpc_ares_ev_driver_unref(grpc_ares_ev_driver *ev_driver) { |
||||
gpr_log(GPR_DEBUG, "Unref ev_driver %" PRIuPTR, (uintptr_t)ev_driver); |
||||
if (gpr_unref(&ev_driver->refs)) { |
||||
gpr_log(GPR_DEBUG, "destroy ev_driver %" PRIuPTR, (uintptr_t)ev_driver); |
||||
GPR_ASSERT(ev_driver->fds == NULL); |
||||
gpr_mu_destroy(&ev_driver->mu); |
||||
ares_destroy(ev_driver->channel); |
||||
gpr_free(ev_driver); |
||||
} |
||||
} |
||||
|
||||
static void fd_node_destroy(grpc_exec_ctx *exec_ctx, fd_node *fdn) { |
||||
gpr_log(GPR_DEBUG, "delete fd: %d", grpc_fd_wrapped_fd(fdn->grpc_fd)); |
||||
GPR_ASSERT(!fdn->readable_registered); |
||||
GPR_ASSERT(!fdn->writable_registered); |
||||
gpr_mu_destroy(&fdn->mu); |
||||
grpc_pollset_set_del_fd(exec_ctx, fdn->ev_driver->pollset_set, fdn->grpc_fd); |
||||
grpc_fd_shutdown(exec_ctx, fdn->grpc_fd, |
||||
GRPC_ERROR_CREATE_FROM_STATIC_STRING("fd node destroyed")); |
||||
grpc_fd_orphan(exec_ctx, fdn->grpc_fd, NULL, NULL, "c-ares query finished"); |
||||
gpr_free(fdn); |
||||
} |
||||
|
||||
grpc_error *grpc_ares_ev_driver_create(grpc_ares_ev_driver **ev_driver, |
||||
grpc_pollset_set *pollset_set) { |
||||
*ev_driver = gpr_malloc(sizeof(grpc_ares_ev_driver)); |
||||
int status = ares_init(&(*ev_driver)->channel); |
||||
gpr_log(GPR_DEBUG, "grpc_ares_ev_driver_create"); |
||||
if (status != ARES_SUCCESS) { |
||||
char *err_msg; |
||||
gpr_asprintf(&err_msg, "Failed to init ares channel. C-ares error: %s", |
||||
ares_strerror(status)); |
||||
grpc_error *err = GRPC_ERROR_CREATE_FROM_COPIED_STRING(err_msg); |
||||
gpr_free(err_msg); |
||||
gpr_free(*ev_driver); |
||||
return err; |
||||
} |
||||
gpr_mu_init(&(*ev_driver)->mu); |
||||
gpr_ref_init(&(*ev_driver)->refs, 1); |
||||
(*ev_driver)->pollset_set = pollset_set; |
||||
(*ev_driver)->fds = NULL; |
||||
(*ev_driver)->working = false; |
||||
(*ev_driver)->shutting_down = false; |
||||
return GRPC_ERROR_NONE; |
||||
} |
||||
|
||||
void grpc_ares_ev_driver_destroy(grpc_ares_ev_driver *ev_driver) { |
||||
// It's not safe to shut down remaining fds here directly, becauses
|
||||
// ares_host_callback does not provide an exec_ctx. We mark the event driver
|
||||
// as being shut down. If the event driver is working,
|
||||
// grpc_ares_notify_on_event_locked will shut down the fds; if it's not
|
||||
// working, there are no fds to shut down.
|
||||
gpr_mu_lock(&ev_driver->mu); |
||||
ev_driver->shutting_down = true; |
||||
gpr_mu_unlock(&ev_driver->mu); |
||||
grpc_ares_ev_driver_unref(ev_driver); |
||||
} |
||||
|
||||
// Search fd in the fd_node list head. This is an O(n) search, the max possible
|
||||
// value of n is ARES_GETSOCK_MAXNUM (16). n is typically 1 - 2 in our tests.
|
||||
static fd_node *pop_fd_node(fd_node **head, int fd) { |
||||
fd_node dummy_head; |
||||
dummy_head.next = *head; |
||||
fd_node *node = &dummy_head; |
||||
while (node->next != NULL) { |
||||
if (grpc_fd_wrapped_fd(node->next->grpc_fd) == fd) { |
||||
fd_node *ret = node->next; |
||||
node->next = node->next->next; |
||||
*head = dummy_head.next; |
||||
return ret; |
||||
} |
||||
node = node->next; |
||||
} |
||||
return NULL; |
||||
} |
||||
|
||||
static void on_readable_cb(grpc_exec_ctx *exec_ctx, void *arg, |
||||
grpc_error *error) { |
||||
fd_node *fdn = arg; |
||||
grpc_ares_ev_driver *ev_driver = fdn->ev_driver; |
||||
gpr_mu_lock(&fdn->mu); |
||||
fdn->readable_registered = false; |
||||
gpr_mu_unlock(&fdn->mu); |
||||
|
||||
gpr_log(GPR_DEBUG, "readable on %d", grpc_fd_wrapped_fd(fdn->grpc_fd)); |
||||
if (error == GRPC_ERROR_NONE) { |
||||
ares_process_fd(ev_driver->channel, grpc_fd_wrapped_fd(fdn->grpc_fd), |
||||
ARES_SOCKET_BAD); |
||||
} else { |
||||
// If error is not GRPC_ERROR_NONE, it means the fd has been shutdown or
|
||||
// timed out. The pending lookups made on this ev_driver will be cancelled
|
||||
// by the following ares_cancel() and the on_done callbacks will be invoked
|
||||
// with a status of ARES_ECANCELLED. The remaining file descriptors in this
|
||||
// ev_driver will be cleaned up in the follwing
|
||||
// grpc_ares_notify_on_event_locked().
|
||||
ares_cancel(ev_driver->channel); |
||||
} |
||||
gpr_mu_lock(&ev_driver->mu); |
||||
grpc_ares_notify_on_event_locked(exec_ctx, ev_driver); |
||||
gpr_mu_unlock(&ev_driver->mu); |
||||
grpc_ares_ev_driver_unref(ev_driver); |
||||
} |
||||
|
||||
static void on_writable_cb(grpc_exec_ctx *exec_ctx, void *arg, |
||||
grpc_error *error) { |
||||
fd_node *fdn = arg; |
||||
grpc_ares_ev_driver *ev_driver = fdn->ev_driver; |
||||
gpr_mu_lock(&fdn->mu); |
||||
fdn->writable_registered = false; |
||||
gpr_mu_unlock(&fdn->mu); |
||||
|
||||
gpr_log(GPR_DEBUG, "writable on %d", grpc_fd_wrapped_fd(fdn->grpc_fd)); |
||||
if (error == GRPC_ERROR_NONE) { |
||||
ares_process_fd(ev_driver->channel, ARES_SOCKET_BAD, |
||||
grpc_fd_wrapped_fd(fdn->grpc_fd)); |
||||
} else { |
||||
// If error is not GRPC_ERROR_NONE, it means the fd has been shutdown or
|
||||
// timed out. The pending lookups made on this ev_driver will be cancelled
|
||||
// by the following ares_cancel() and the on_done callbacks will be invoked
|
||||
// with a status of ARES_ECANCELLED. The remaining file descriptors in this
|
||||
// ev_driver will be cleaned up in the follwing
|
||||
// grpc_ares_notify_on_event_locked().
|
||||
ares_cancel(ev_driver->channel); |
||||
} |
||||
gpr_mu_lock(&ev_driver->mu); |
||||
grpc_ares_notify_on_event_locked(exec_ctx, ev_driver); |
||||
gpr_mu_unlock(&ev_driver->mu); |
||||
grpc_ares_ev_driver_unref(ev_driver); |
||||
} |
||||
|
||||
ares_channel *grpc_ares_ev_driver_get_channel(grpc_ares_ev_driver *ev_driver) { |
||||
return &ev_driver->channel; |
||||
} |
||||
|
||||
// Get the file descriptors used by the ev_driver's ares channel, register
|
||||
// driver_closure with these filedescriptors.
|
||||
static void grpc_ares_notify_on_event_locked(grpc_exec_ctx *exec_ctx, |
||||
grpc_ares_ev_driver *ev_driver) { |
||||
fd_node *new_list = NULL; |
||||
if (!ev_driver->shutting_down) { |
||||
ares_socket_t socks[ARES_GETSOCK_MAXNUM]; |
||||
int socks_bitmask = |
||||
ares_getsock(ev_driver->channel, socks, ARES_GETSOCK_MAXNUM); |
||||
for (size_t i = 0; i < ARES_GETSOCK_MAXNUM; i++) { |
||||
if (ARES_GETSOCK_READABLE(socks_bitmask, i) || |
||||
ARES_GETSOCK_WRITABLE(socks_bitmask, i)) { |
||||
fd_node *fdn = pop_fd_node(&ev_driver->fds, socks[i]); |
||||
// Create a new fd_node if sock[i] is not in the fd_node list.
|
||||
if (fdn == NULL) { |
||||
char *fd_name; |
||||
gpr_asprintf(&fd_name, "ares_ev_driver-%" PRIuPTR, i); |
||||
fdn = gpr_malloc(sizeof(fd_node)); |
||||
gpr_log(GPR_DEBUG, "new fd: %d", socks[i]); |
||||
fdn->grpc_fd = grpc_fd_create(socks[i], fd_name); |
||||
fdn->ev_driver = ev_driver; |
||||
fdn->readable_registered = false; |
||||
fdn->writable_registered = false; |
||||
gpr_mu_init(&fdn->mu); |
||||
grpc_closure_init(&fdn->read_closure, on_readable_cb, fdn, |
||||
grpc_schedule_on_exec_ctx); |
||||
grpc_closure_init(&fdn->write_closure, on_writable_cb, fdn, |
||||
grpc_schedule_on_exec_ctx); |
||||
grpc_pollset_set_add_fd(exec_ctx, ev_driver->pollset_set, |
||||
fdn->grpc_fd); |
||||
gpr_free(fd_name); |
||||
} |
||||
fdn->next = new_list; |
||||
new_list = fdn; |
||||
gpr_mu_lock(&fdn->mu); |
||||
// Register read_closure if the socket is readable and read_closure has
|
||||
// not been registered with this socket.
|
||||
if (ARES_GETSOCK_READABLE(socks_bitmask, i) && |
||||
!fdn->readable_registered) { |
||||
grpc_ares_ev_driver_ref(ev_driver); |
||||
gpr_log(GPR_DEBUG, "notify read on: %d", |
||||
grpc_fd_wrapped_fd(fdn->grpc_fd)); |
||||
grpc_fd_notify_on_read(exec_ctx, fdn->grpc_fd, &fdn->read_closure); |
||||
fdn->readable_registered = true; |
||||
} |
||||
// Register write_closure if the socket is writable and write_closure
|
||||
// has not been registered with this socket.
|
||||
if (ARES_GETSOCK_WRITABLE(socks_bitmask, i) && |
||||
!fdn->writable_registered) { |
||||
gpr_log(GPR_DEBUG, "notify write on: %d", |
||||
grpc_fd_wrapped_fd(fdn->grpc_fd)); |
||||
grpc_ares_ev_driver_ref(ev_driver); |
||||
grpc_fd_notify_on_write(exec_ctx, fdn->grpc_fd, &fdn->write_closure); |
||||
fdn->writable_registered = true; |
||||
} |
||||
gpr_mu_unlock(&fdn->mu); |
||||
} |
||||
} |
||||
} |
||||
// Any remaining fds in ev_driver->fds were not returned by ares_getsock() and
|
||||
// are therefore no longer in use, so they can be shut down and removed from
|
||||
// the list.
|
||||
while (ev_driver->fds != NULL) { |
||||
fd_node *cur = ev_driver->fds; |
||||
ev_driver->fds = ev_driver->fds->next; |
||||
fd_node_destroy(exec_ctx, cur); |
||||
} |
||||
ev_driver->fds = new_list; |
||||
// If the ev driver has no working fd, all the tasks are done.
|
||||
if (new_list == NULL) { |
||||
ev_driver->working = false; |
||||
gpr_log(GPR_DEBUG, "ev driver stop working"); |
||||
} |
||||
} |
||||
|
||||
void grpc_ares_ev_driver_start(grpc_exec_ctx *exec_ctx, |
||||
grpc_ares_ev_driver *ev_driver) { |
||||
gpr_mu_lock(&ev_driver->mu); |
||||
if (!ev_driver->working) { |
||||
ev_driver->working = true; |
||||
grpc_ares_notify_on_event_locked(exec_ctx, ev_driver); |
||||
} |
||||
gpr_mu_unlock(&ev_driver->mu); |
||||
} |
||||
|
||||
#endif /* GRPC_ARES == 1 && defined(GRPC_POSIX_SOCKET) */ |
@ -0,0 +1,289 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2016, 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. |
||||
* |
||||
*/ |
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
#if GRPC_ARES == 1 && !defined(GRPC_UV) |
||||
|
||||
#include "src/core/ext/resolver/dns/c_ares/grpc_ares_wrapper.h" |
||||
#include "src/core/lib/iomgr/sockaddr.h" |
||||
#include "src/core/lib/iomgr/socket_utils_posix.h" |
||||
|
||||
#include <string.h> |
||||
#include <sys/types.h> |
||||
|
||||
#include <ares.h> |
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/host_port.h> |
||||
#include <grpc/support/log.h> |
||||
#include <grpc/support/string_util.h> |
||||
#include <grpc/support/time.h> |
||||
#include <grpc/support/useful.h> |
||||
#include "src/core/ext/resolver/dns/c_ares/grpc_ares_ev_driver.h" |
||||
#include "src/core/lib/iomgr/executor.h" |
||||
#include "src/core/lib/iomgr/iomgr_internal.h" |
||||
#include "src/core/lib/iomgr/sockaddr_utils.h" |
||||
#include "src/core/lib/support/string.h" |
||||
|
||||
static gpr_once g_basic_init = GPR_ONCE_INIT; |
||||
static gpr_mu g_init_mu; |
||||
|
||||
typedef struct grpc_ares_request { |
||||
/** following members are set in grpc_resolve_address_ares_impl */ |
||||
/** host to resolve, parsed from the name to resolve */ |
||||
char *host; |
||||
/** port to fill in sockaddr_in, parsed from the name to resolve */ |
||||
char *port; |
||||
/** default port to use */ |
||||
char *default_port; |
||||
/** closure to call when the request completes */ |
||||
grpc_closure *on_done; |
||||
/** the pointer to receive the resolved addresses */ |
||||
grpc_resolved_addresses **addrs_out; |
||||
/** the evernt driver used by this request */ |
||||
grpc_ares_ev_driver *ev_driver; |
||||
/** number of ongoing queries */ |
||||
gpr_refcount pending_queries; |
||||
|
||||
/** mutex guarding the rest of the state */ |
||||
gpr_mu mu; |
||||
/** is there at least one successful query, set in on_done_cb */ |
||||
bool success; |
||||
/** the errors explaining the request failure, set in on_done_cb */ |
||||
grpc_error *error; |
||||
} grpc_ares_request; |
||||
|
||||
static void do_basic_init(void) { gpr_mu_init(&g_init_mu); } |
||||
|
||||
static uint16_t strhtons(const char *port) { |
||||
if (strcmp(port, "http") == 0) { |
||||
return htons(80); |
||||
} else if (strcmp(port, "https") == 0) { |
||||
return htons(443); |
||||
} |
||||
return htons((unsigned short)atoi(port)); |
||||
} |
||||
|
||||
static void grpc_ares_request_unref(grpc_exec_ctx *exec_ctx, |
||||
grpc_ares_request *r) { |
||||
/* If there are no pending queries, invoke on_done callback and destroy the
|
||||
request */ |
||||
if (gpr_unref(&r->pending_queries)) { |
||||
/* TODO(zyc): Sort results with RFC6724 before invoking on_done. */ |
||||
if (exec_ctx == NULL) { |
||||
/* A new exec_ctx is created here, as the c-ares interface does not
|
||||
provide one in ares_host_callback. It's safe to schedule on_done with |
||||
the newly created exec_ctx, since the caller has been warned not to |
||||
acquire locks in on_done. ares_dns_resolver is using combiner to |
||||
protect resources needed by on_done. */ |
||||
grpc_exec_ctx new_exec_ctx = GRPC_EXEC_CTX_INIT; |
||||
grpc_closure_sched(&new_exec_ctx, r->on_done, r->error); |
||||
grpc_exec_ctx_finish(&new_exec_ctx); |
||||
} else { |
||||
grpc_closure_sched(exec_ctx, r->on_done, r->error); |
||||
} |
||||
gpr_mu_destroy(&r->mu); |
||||
grpc_ares_ev_driver_destroy(r->ev_driver); |
||||
gpr_free(r->host); |
||||
gpr_free(r->port); |
||||
gpr_free(r->default_port); |
||||
gpr_free(r); |
||||
} |
||||
} |
||||
|
||||
static void on_done_cb(void *arg, int status, int timeouts, |
||||
struct hostent *hostent) { |
||||
grpc_ares_request *r = (grpc_ares_request *)arg; |
||||
gpr_mu_lock(&r->mu); |
||||
if (status == ARES_SUCCESS) { |
||||
GRPC_ERROR_UNREF(r->error); |
||||
r->error = GRPC_ERROR_NONE; |
||||
r->success = true; |
||||
grpc_resolved_addresses **addresses = r->addrs_out; |
||||
if (*addresses == NULL) { |
||||
*addresses = gpr_malloc(sizeof(grpc_resolved_addresses)); |
||||
(*addresses)->naddrs = 0; |
||||
(*addresses)->addrs = NULL; |
||||
} |
||||
size_t prev_naddr = (*addresses)->naddrs; |
||||
size_t i; |
||||
for (i = 0; hostent->h_addr_list[i] != NULL; i++) { |
||||
} |
||||
(*addresses)->naddrs += i; |
||||
(*addresses)->addrs = |
||||
gpr_realloc((*addresses)->addrs, |
||||
sizeof(grpc_resolved_address) * (*addresses)->naddrs); |
||||
for (i = prev_naddr; i < (*addresses)->naddrs; i++) { |
||||
memset(&(*addresses)->addrs[i], 0, sizeof(grpc_resolved_address)); |
||||
if (hostent->h_addrtype == AF_INET6) { |
||||
(*addresses)->addrs[i].len = sizeof(struct sockaddr_in6); |
||||
struct sockaddr_in6 *addr = |
||||
(struct sockaddr_in6 *)&(*addresses)->addrs[i].addr; |
||||
addr->sin6_family = (sa_family_t)hostent->h_addrtype; |
||||
addr->sin6_port = strhtons(r->port); |
||||
|
||||
char output[INET6_ADDRSTRLEN]; |
||||
memcpy(&addr->sin6_addr, hostent->h_addr_list[i - prev_naddr], |
||||
sizeof(struct in6_addr)); |
||||
ares_inet_ntop(AF_INET6, &addr->sin6_addr, output, INET6_ADDRSTRLEN); |
||||
gpr_log(GPR_DEBUG, |
||||
"c-ares resolver gets a AF_INET6 result: \n" |
||||
" addr: %s\n port: %s\n sin6_scope_id: %d\n", |
||||
output, r->port, addr->sin6_scope_id); |
||||
} else { |
||||
(*addresses)->addrs[i].len = sizeof(struct sockaddr_in); |
||||
struct sockaddr_in *addr = |
||||
(struct sockaddr_in *)&(*addresses)->addrs[i].addr; |
||||
memcpy(&addr->sin_addr, hostent->h_addr_list[i - prev_naddr], |
||||
sizeof(struct in_addr)); |
||||
addr->sin_family = (sa_family_t)hostent->h_addrtype; |
||||
addr->sin_port = strhtons(r->port); |
||||
|
||||
char output[INET_ADDRSTRLEN]; |
||||
ares_inet_ntop(AF_INET, &addr->sin_addr, output, INET_ADDRSTRLEN); |
||||
gpr_log(GPR_DEBUG, |
||||
"c-ares resolver gets a AF_INET result: \n" |
||||
" addr: %s\n port: %s\n", |
||||
output, r->port); |
||||
} |
||||
} |
||||
} else if (!r->success) { |
||||
char *error_msg; |
||||
gpr_asprintf(&error_msg, "C-ares status is not ARES_SUCCESS: %s", |
||||
ares_strerror(status)); |
||||
grpc_error *error = GRPC_ERROR_CREATE_FROM_COPIED_STRING(error_msg); |
||||
gpr_free(error_msg); |
||||
if (r->error == GRPC_ERROR_NONE) { |
||||
r->error = error; |
||||
} else { |
||||
r->error = grpc_error_add_child(error, r->error); |
||||
} |
||||
} |
||||
gpr_mu_unlock(&r->mu); |
||||
grpc_ares_request_unref(NULL, r); |
||||
} |
||||
|
||||
void grpc_resolve_address_ares_impl(grpc_exec_ctx *exec_ctx, const char *name, |
||||
const char *default_port, |
||||
grpc_pollset_set *interested_parties, |
||||
grpc_closure *on_done, |
||||
grpc_resolved_addresses **addrs) { |
||||
/* TODO(zyc): Enable tracing after #9603 is checked in */ |
||||
/* if (grpc_dns_trace) {
|
||||
gpr_log(GPR_DEBUG, "resolve_address (blocking): name=%s, default_port=%s", |
||||
name, default_port); |
||||
} */ |
||||
|
||||
/* parse name, splitting it into host and port parts */ |
||||
char *host; |
||||
char *port; |
||||
gpr_split_host_port(name, &host, &port); |
||||
if (host == NULL) { |
||||
grpc_error *err = grpc_error_set_str( |
||||
GRPC_ERROR_CREATE_FROM_STATIC_STRING("unparseable host:port"), |
||||
GRPC_ERROR_STR_TARGET_ADDRESS, grpc_slice_from_copied_string(name)); |
||||
grpc_closure_sched(exec_ctx, on_done, err); |
||||
goto error_cleanup; |
||||
} else if (port == NULL) { |
||||
if (default_port == NULL) { |
||||
grpc_error *err = grpc_error_set_str( |
||||
GRPC_ERROR_CREATE_FROM_STATIC_STRING("no port in name"), |
||||
GRPC_ERROR_STR_TARGET_ADDRESS, grpc_slice_from_copied_string(name)); |
||||
grpc_closure_sched(exec_ctx, on_done, err); |
||||
goto error_cleanup; |
||||
} |
||||
port = gpr_strdup(default_port); |
||||
} |
||||
|
||||
grpc_ares_ev_driver *ev_driver; |
||||
grpc_error *err = grpc_ares_ev_driver_create(&ev_driver, interested_parties); |
||||
if (err != GRPC_ERROR_NONE) { |
||||
GRPC_LOG_IF_ERROR("grpc_ares_ev_driver_create() failed", err); |
||||
goto error_cleanup; |
||||
} |
||||
|
||||
grpc_ares_request *r = gpr_malloc(sizeof(grpc_ares_request)); |
||||
gpr_mu_init(&r->mu); |
||||
r->ev_driver = ev_driver; |
||||
r->on_done = on_done; |
||||
r->addrs_out = addrs; |
||||
r->default_port = gpr_strdup(default_port); |
||||
r->port = port; |
||||
r->host = host; |
||||
r->success = false; |
||||
r->error = GRPC_ERROR_NONE; |
||||
ares_channel *channel = grpc_ares_ev_driver_get_channel(r->ev_driver); |
||||
gpr_ref_init(&r->pending_queries, 2); |
||||
if (grpc_ipv6_loopback_available()) { |
||||
gpr_ref(&r->pending_queries); |
||||
ares_gethostbyname(*channel, r->host, AF_INET6, on_done_cb, r); |
||||
} |
||||
ares_gethostbyname(*channel, r->host, AF_INET, on_done_cb, r); |
||||
/* TODO(zyc): Handle CNAME records here. */ |
||||
grpc_ares_ev_driver_start(exec_ctx, r->ev_driver); |
||||
grpc_ares_request_unref(exec_ctx, r); |
||||
return; |
||||
|
||||
error_cleanup: |
||||
gpr_free(host); |
||||
gpr_free(port); |
||||
} |
||||
|
||||
void (*grpc_resolve_address_ares)( |
||||
grpc_exec_ctx *exec_ctx, const char *name, const char *default_port, |
||||
grpc_pollset_set *interested_parties, grpc_closure *on_done, |
||||
grpc_resolved_addresses **addrs) = grpc_resolve_address_ares_impl; |
||||
|
||||
grpc_error *grpc_ares_init(void) { |
||||
gpr_once_init(&g_basic_init, do_basic_init); |
||||
gpr_mu_lock(&g_init_mu); |
||||
int status = ares_library_init(ARES_LIB_INIT_ALL); |
||||
gpr_mu_unlock(&g_init_mu); |
||||
|
||||
if (status != ARES_SUCCESS) { |
||||
char *error_msg; |
||||
gpr_asprintf(&error_msg, "ares_library_init failed: %s", |
||||
ares_strerror(status)); |
||||
grpc_error *error = GRPC_ERROR_CREATE_FROM_COPIED_STRING(error_msg); |
||||
gpr_free(error_msg); |
||||
return error; |
||||
} |
||||
return GRPC_ERROR_NONE; |
||||
} |
||||
|
||||
void grpc_ares_cleanup(void) { |
||||
gpr_mu_lock(&g_init_mu); |
||||
ares_library_cleanup(); |
||||
gpr_mu_unlock(&g_init_mu); |
||||
} |
||||
|
||||
#endif /* GRPC_ARES == 1 && !defined(GRPC_UV) */ |
@ -0,0 +1,63 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2016, 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. |
||||
* |
||||
*/ |
||||
|
||||
#ifndef GRPC_CORE_EXT_RESOLVER_DNS_C_ARES_GRPC_ARES_WRAPPER_H |
||||
#define GRPC_CORE_EXT_RESOLVER_DNS_C_ARES_GRPC_ARES_WRAPPER_H |
||||
|
||||
#include "src/core/lib/iomgr/exec_ctx.h" |
||||
#include "src/core/lib/iomgr/iomgr.h" |
||||
#include "src/core/lib/iomgr/polling_entity.h" |
||||
#include "src/core/lib/iomgr/resolve_address.h" |
||||
|
||||
/* Asynchronously resolve addr. Use \a default_port if a port isn't designated
|
||||
in addr, otherwise use the port in addr. grpc_ares_init() must be called at |
||||
least once before this function. \a on_done may be called directly in this |
||||
function without being scheduled with \a exec_ctx, it must not try to acquire |
||||
locks that are being held by the caller. */ |
||||
extern void (*grpc_resolve_address_ares)(grpc_exec_ctx *exec_ctx, |
||||
const char *addr, |
||||
const char *default_port, |
||||
grpc_pollset_set *interested_parties, |
||||
grpc_closure *on_done, |
||||
grpc_resolved_addresses **addresses); |
||||
|
||||
/* Initialize gRPC ares wrapper. Must be called at least once before
|
||||
grpc_resolve_address_ares(). */ |
||||
grpc_error *grpc_ares_init(void); |
||||
|
||||
/* Uninitialized gRPC ares wrapper. If there was more than one previous call to
|
||||
grpc_ares_init(), this function uninitializes the gRPC ares wrapper only if |
||||
it has been called the same number of times as grpc_ares_init(). */ |
||||
void grpc_ares_cleanup(void); |
||||
|
||||
#endif /* GRPC_CORE_EXT_RESOLVER_DNS_C_ARES_GRPC_ARES_WRAPPER_H */ |
@ -0,0 +1,386 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2017, 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. |
||||
* |
||||
*/ |
||||
|
||||
#include "src/core/lib/channel/message_size_filter.h" |
||||
|
||||
#include <limits.h> |
||||
#include <string.h> |
||||
|
||||
#include "src/core/lib/channel/channel_args.h" |
||||
#include "src/core/lib/iomgr/timer.h" |
||||
#include "src/core/lib/transport/http2_errors.h" |
||||
#include "src/core/lib/transport/service_config.h" |
||||
|
||||
#define DEFAULT_MAX_CONNECTION_AGE_MS INT_MAX |
||||
#define DEFAULT_MAX_CONNECTION_AGE_GRACE_MS INT_MAX |
||||
#define DEFAULT_MAX_CONNECTION_IDLE_MS INT_MAX |
||||
|
||||
typedef struct channel_data { |
||||
/* We take a reference to the channel stack for the timer callback */ |
||||
grpc_channel_stack* channel_stack; |
||||
/* Guards access to max_age_timer, max_age_timer_pending, max_age_grace_timer
|
||||
and max_age_grace_timer_pending */ |
||||
gpr_mu max_age_timer_mu; |
||||
/* True if the max_age timer callback is currently pending */ |
||||
bool max_age_timer_pending; |
||||
/* True if the max_age_grace timer callback is currently pending */ |
||||
bool max_age_grace_timer_pending; |
||||
/* The timer for checking if the channel has reached its max age */ |
||||
grpc_timer max_age_timer; |
||||
/* The timer for checking if the max-aged channel has uesed up the grace
|
||||
period */ |
||||
grpc_timer max_age_grace_timer; |
||||
/* The timer for checking if the channel's idle duration reaches
|
||||
max_connection_idle */ |
||||
grpc_timer max_idle_timer; |
||||
/* Allowed max time a channel may have no outstanding rpcs */ |
||||
gpr_timespec max_connection_idle; |
||||
/* Allowed max time a channel may exist */ |
||||
gpr_timespec max_connection_age; |
||||
/* Allowed grace period after the channel reaches its max age */ |
||||
gpr_timespec max_connection_age_grace; |
||||
/* Closure to run when the channel's idle duration reaches max_connection_idle
|
||||
and should be closed gracefully */ |
||||
grpc_closure close_max_idle_channel; |
||||
/* Closure to run when the channel reaches its max age and should be closed
|
||||
gracefully */ |
||||
grpc_closure close_max_age_channel; |
||||
/* Closure to run the channel uses up its max age grace time and should be
|
||||
closed forcibly */ |
||||
grpc_closure force_close_max_age_channel; |
||||
/* Closure to run when the init fo channel stack is done and the max_idle
|
||||
timer should be started */ |
||||
grpc_closure start_max_idle_timer_after_init; |
||||
/* Closure to run when the init fo channel stack is done and the max_age timer
|
||||
should be started */ |
||||
grpc_closure start_max_age_timer_after_init; |
||||
/* Closure to run when the goaway op is finished and the max_age_timer */ |
||||
grpc_closure start_max_age_grace_timer_after_goaway_op; |
||||
/* Closure to run when the channel connectivity state changes */ |
||||
grpc_closure channel_connectivity_changed; |
||||
/* Records the current connectivity state */ |
||||
grpc_connectivity_state connectivity_state; |
||||
/* Number of active calls */ |
||||
gpr_atm call_count; |
||||
} channel_data; |
||||
|
||||
/* Increase the nubmer of active calls. Before the increasement, if there are no
|
||||
calls, the max_idle_timer should be cancelled. */ |
||||
static void increase_call_count(grpc_exec_ctx* exec_ctx, channel_data* chand) { |
||||
if (gpr_atm_full_fetch_add(&chand->call_count, 1) == 0) { |
||||
grpc_timer_cancel(exec_ctx, &chand->max_idle_timer); |
||||
} |
||||
} |
||||
|
||||
/* Decrease the nubmer of active calls. After the decrement, if there are no
|
||||
calls, the max_idle_timer should be started. */ |
||||
static void decrease_call_count(grpc_exec_ctx* exec_ctx, channel_data* chand) { |
||||
if (gpr_atm_full_fetch_add(&chand->call_count, -1) == 1) { |
||||
GRPC_CHANNEL_STACK_REF(chand->channel_stack, "max_age max_idle_timer"); |
||||
grpc_timer_init( |
||||
exec_ctx, &chand->max_idle_timer, |
||||
gpr_time_add(gpr_now(GPR_CLOCK_MONOTONIC), chand->max_connection_idle), |
||||
&chand->close_max_idle_channel, gpr_now(GPR_CLOCK_MONOTONIC)); |
||||
} |
||||
} |
||||
|
||||
static void start_max_idle_timer_after_init(grpc_exec_ctx* exec_ctx, void* arg, |
||||
grpc_error* error) { |
||||
channel_data* chand = arg; |
||||
/* Decrease call_count. If there are no active calls at this time,
|
||||
max_idle_timer will start here. If the number of active calls is not 0, |
||||
max_idle_timer will start after all the active calls end. */ |
||||
decrease_call_count(exec_ctx, chand); |
||||
GRPC_CHANNEL_STACK_UNREF(exec_ctx, chand->channel_stack, |
||||
"max_age start_max_idle_timer_after_init"); |
||||
} |
||||
|
||||
static void start_max_age_timer_after_init(grpc_exec_ctx* exec_ctx, void* arg, |
||||
grpc_error* error) { |
||||
channel_data* chand = arg; |
||||
gpr_mu_lock(&chand->max_age_timer_mu); |
||||
chand->max_age_timer_pending = true; |
||||
GRPC_CHANNEL_STACK_REF(chand->channel_stack, "max_age max_age_timer"); |
||||
grpc_timer_init( |
||||
exec_ctx, &chand->max_age_timer, |
||||
gpr_time_add(gpr_now(GPR_CLOCK_MONOTONIC), chand->max_connection_age), |
||||
&chand->close_max_age_channel, gpr_now(GPR_CLOCK_MONOTONIC)); |
||||
gpr_mu_unlock(&chand->max_age_timer_mu); |
||||
grpc_transport_op* op = grpc_make_transport_op(NULL); |
||||
op->on_connectivity_state_change = &chand->channel_connectivity_changed, |
||||
op->connectivity_state = &chand->connectivity_state; |
||||
grpc_channel_next_op(exec_ctx, |
||||
grpc_channel_stack_element(chand->channel_stack, 0), op); |
||||
GRPC_CHANNEL_STACK_UNREF(exec_ctx, chand->channel_stack, |
||||
"max_age start_max_age_timer_after_init"); |
||||
} |
||||
|
||||
static void start_max_age_grace_timer_after_goaway_op(grpc_exec_ctx* exec_ctx, |
||||
void* arg, |
||||
grpc_error* error) { |
||||
channel_data* chand = arg; |
||||
gpr_mu_lock(&chand->max_age_timer_mu); |
||||
chand->max_age_grace_timer_pending = true; |
||||
GRPC_CHANNEL_STACK_REF(chand->channel_stack, "max_age max_age_grace_timer"); |
||||
grpc_timer_init(exec_ctx, &chand->max_age_grace_timer, |
||||
gpr_time_add(gpr_now(GPR_CLOCK_MONOTONIC), |
||||
chand->max_connection_age_grace), |
||||
&chand->force_close_max_age_channel, |
||||
gpr_now(GPR_CLOCK_MONOTONIC)); |
||||
gpr_mu_unlock(&chand->max_age_timer_mu); |
||||
GRPC_CHANNEL_STACK_UNREF(exec_ctx, chand->channel_stack, |
||||
"max_age start_max_age_grace_timer_after_goaway_op"); |
||||
} |
||||
|
||||
static void close_max_idle_channel(grpc_exec_ctx* exec_ctx, void* arg, |
||||
grpc_error* error) { |
||||
channel_data* chand = arg; |
||||
gpr_atm_no_barrier_fetch_add(&chand->call_count, 1); |
||||
if (error == GRPC_ERROR_NONE) { |
||||
grpc_transport_op* op = grpc_make_transport_op(NULL); |
||||
op->goaway_error = |
||||
grpc_error_set_int(GRPC_ERROR_CREATE_FROM_STATIC_STRING("max_idle"), |
||||
GRPC_ERROR_INT_HTTP2_ERROR, GRPC_HTTP2_NO_ERROR); |
||||
grpc_channel_element* elem = |
||||
grpc_channel_stack_element(chand->channel_stack, 0); |
||||
elem->filter->start_transport_op(exec_ctx, elem, op); |
||||
} else if (error != GRPC_ERROR_CANCELLED) { |
||||
GRPC_LOG_IF_ERROR("close_max_idle_channel", error); |
||||
} |
||||
GRPC_CHANNEL_STACK_UNREF(exec_ctx, chand->channel_stack, |
||||
"max_age max_idle_timer"); |
||||
} |
||||
|
||||
static void close_max_age_channel(grpc_exec_ctx* exec_ctx, void* arg, |
||||
grpc_error* error) { |
||||
channel_data* chand = arg; |
||||
gpr_mu_lock(&chand->max_age_timer_mu); |
||||
chand->max_age_timer_pending = false; |
||||
gpr_mu_unlock(&chand->max_age_timer_mu); |
||||
if (error == GRPC_ERROR_NONE) { |
||||
GRPC_CHANNEL_STACK_REF(chand->channel_stack, |
||||
"max_age start_max_age_grace_timer_after_goaway_op"); |
||||
grpc_transport_op* op = grpc_make_transport_op( |
||||
&chand->start_max_age_grace_timer_after_goaway_op); |
||||
op->goaway_error = |
||||
grpc_error_set_int(GRPC_ERROR_CREATE_FROM_STATIC_STRING("max_age"), |
||||
GRPC_ERROR_INT_HTTP2_ERROR, GRPC_HTTP2_NO_ERROR); |
||||
grpc_channel_element* elem = |
||||
grpc_channel_stack_element(chand->channel_stack, 0); |
||||
elem->filter->start_transport_op(exec_ctx, elem, op); |
||||
} else if (error != GRPC_ERROR_CANCELLED) { |
||||
GRPC_LOG_IF_ERROR("close_max_age_channel", error); |
||||
} |
||||
GRPC_CHANNEL_STACK_UNREF(exec_ctx, chand->channel_stack, |
||||
"max_age max_age_timer"); |
||||
} |
||||
|
||||
static void force_close_max_age_channel(grpc_exec_ctx* exec_ctx, void* arg, |
||||
grpc_error* error) { |
||||
channel_data* chand = arg; |
||||
gpr_mu_lock(&chand->max_age_timer_mu); |
||||
chand->max_age_grace_timer_pending = false; |
||||
gpr_mu_unlock(&chand->max_age_timer_mu); |
||||
if (error == GRPC_ERROR_NONE) { |
||||
grpc_transport_op* op = grpc_make_transport_op(NULL); |
||||
op->disconnect_with_error = |
||||
GRPC_ERROR_CREATE_FROM_STATIC_STRING("Channel reaches max age"); |
||||
grpc_channel_element* elem = |
||||
grpc_channel_stack_element(chand->channel_stack, 0); |
||||
elem->filter->start_transport_op(exec_ctx, elem, op); |
||||
} else if (error != GRPC_ERROR_CANCELLED) { |
||||
GRPC_LOG_IF_ERROR("force_close_max_age_channel", error); |
||||
} |
||||
GRPC_CHANNEL_STACK_UNREF(exec_ctx, chand->channel_stack, |
||||
"max_age max_age_grace_timer"); |
||||
} |
||||
|
||||
static void channel_connectivity_changed(grpc_exec_ctx* exec_ctx, void* arg, |
||||
grpc_error* error) { |
||||
channel_data* chand = arg; |
||||
if (chand->connectivity_state != GRPC_CHANNEL_SHUTDOWN) { |
||||
grpc_transport_op* op = grpc_make_transport_op(NULL); |
||||
op->on_connectivity_state_change = &chand->channel_connectivity_changed, |
||||
op->connectivity_state = &chand->connectivity_state; |
||||
grpc_channel_next_op( |
||||
exec_ctx, grpc_channel_stack_element(chand->channel_stack, 0), op); |
||||
} else { |
||||
gpr_mu_lock(&chand->max_age_timer_mu); |
||||
if (chand->max_age_timer_pending) { |
||||
grpc_timer_cancel(exec_ctx, &chand->max_age_timer); |
||||
chand->max_age_timer_pending = false; |
||||
} |
||||
if (chand->max_age_grace_timer_pending) { |
||||
grpc_timer_cancel(exec_ctx, &chand->max_age_grace_timer); |
||||
chand->max_age_grace_timer_pending = false; |
||||
} |
||||
gpr_mu_unlock(&chand->max_age_timer_mu); |
||||
/* If there are no active calls, this increasement will cancel
|
||||
max_idle_timer, and prevent max_idle_timer from being started in the |
||||
future. */ |
||||
increase_call_count(exec_ctx, chand); |
||||
} |
||||
} |
||||
|
||||
/* Constructor for call_data. */ |
||||
static grpc_error* init_call_elem(grpc_exec_ctx* exec_ctx, |
||||
grpc_call_element* elem, |
||||
const grpc_call_element_args* args) { |
||||
channel_data* chand = elem->channel_data; |
||||
increase_call_count(exec_ctx, chand); |
||||
return GRPC_ERROR_NONE; |
||||
} |
||||
|
||||
/* Destructor for call_data. */ |
||||
static void destroy_call_elem(grpc_exec_ctx* exec_ctx, grpc_call_element* elem, |
||||
const grpc_call_final_info* final_info, |
||||
grpc_closure* ignored) { |
||||
channel_data* chand = elem->channel_data; |
||||
decrease_call_count(exec_ctx, chand); |
||||
} |
||||
|
||||
/* Constructor for channel_data. */ |
||||
static grpc_error* init_channel_elem(grpc_exec_ctx* exec_ctx, |
||||
grpc_channel_element* elem, |
||||
grpc_channel_element_args* args) { |
||||
channel_data* chand = elem->channel_data; |
||||
gpr_mu_init(&chand->max_age_timer_mu); |
||||
chand->max_age_timer_pending = false; |
||||
chand->max_age_grace_timer_pending = false; |
||||
chand->channel_stack = args->channel_stack; |
||||
chand->max_connection_age = |
||||
DEFAULT_MAX_CONNECTION_AGE_MS == INT_MAX |
||||
? gpr_inf_future(GPR_TIMESPAN) |
||||
: gpr_time_from_millis(DEFAULT_MAX_CONNECTION_AGE_MS, GPR_TIMESPAN); |
||||
chand->max_connection_age_grace = |
||||
DEFAULT_MAX_CONNECTION_AGE_GRACE_MS == INT_MAX |
||||
? gpr_inf_future(GPR_TIMESPAN) |
||||
: gpr_time_from_millis(DEFAULT_MAX_CONNECTION_AGE_GRACE_MS, |
||||
GPR_TIMESPAN); |
||||
chand->max_connection_idle = |
||||
DEFAULT_MAX_CONNECTION_IDLE_MS == INT_MAX |
||||
? gpr_inf_future(GPR_TIMESPAN) |
||||
: gpr_time_from_millis(DEFAULT_MAX_CONNECTION_IDLE_MS, GPR_TIMESPAN); |
||||
for (size_t i = 0; i < args->channel_args->num_args; ++i) { |
||||
if (0 == strcmp(args->channel_args->args[i].key, |
||||
GRPC_ARG_MAX_CONNECTION_AGE_MS)) { |
||||
const int value = grpc_channel_arg_get_integer( |
||||
&args->channel_args->args[i], |
||||
(grpc_integer_options){DEFAULT_MAX_CONNECTION_AGE_MS, 1, INT_MAX}); |
||||
chand->max_connection_age = |
||||
value == INT_MAX ? gpr_inf_future(GPR_TIMESPAN) |
||||
: gpr_time_from_millis(value, GPR_TIMESPAN); |
||||
} else if (0 == strcmp(args->channel_args->args[i].key, |
||||
GRPC_ARG_MAX_CONNECTION_AGE_GRACE_MS)) { |
||||
const int value = grpc_channel_arg_get_integer( |
||||
&args->channel_args->args[i], |
||||
(grpc_integer_options){DEFAULT_MAX_CONNECTION_AGE_GRACE_MS, 0, |
||||
INT_MAX}); |
||||
chand->max_connection_age_grace = |
||||
value == INT_MAX ? gpr_inf_future(GPR_TIMESPAN) |
||||
: gpr_time_from_millis(value, GPR_TIMESPAN); |
||||
} else if (0 == strcmp(args->channel_args->args[i].key, |
||||
GRPC_ARG_MAX_CONNECTION_IDLE_MS)) { |
||||
const int value = grpc_channel_arg_get_integer( |
||||
&args->channel_args->args[i], |
||||
(grpc_integer_options){DEFAULT_MAX_CONNECTION_IDLE_MS, 1, INT_MAX}); |
||||
chand->max_connection_idle = |
||||
value == INT_MAX ? gpr_inf_future(GPR_TIMESPAN) |
||||
: gpr_time_from_millis(value, GPR_TIMESPAN); |
||||
} |
||||
} |
||||
grpc_closure_init(&chand->close_max_idle_channel, close_max_idle_channel, |
||||
chand, grpc_schedule_on_exec_ctx); |
||||
grpc_closure_init(&chand->close_max_age_channel, close_max_age_channel, chand, |
||||
grpc_schedule_on_exec_ctx); |
||||
grpc_closure_init(&chand->force_close_max_age_channel, |
||||
force_close_max_age_channel, chand, |
||||
grpc_schedule_on_exec_ctx); |
||||
grpc_closure_init(&chand->start_max_idle_timer_after_init, |
||||
start_max_idle_timer_after_init, chand, |
||||
grpc_schedule_on_exec_ctx); |
||||
grpc_closure_init(&chand->start_max_age_timer_after_init, |
||||
start_max_age_timer_after_init, chand, |
||||
grpc_schedule_on_exec_ctx); |
||||
grpc_closure_init(&chand->start_max_age_grace_timer_after_goaway_op, |
||||
start_max_age_grace_timer_after_goaway_op, chand, |
||||
grpc_schedule_on_exec_ctx); |
||||
grpc_closure_init(&chand->channel_connectivity_changed, |
||||
channel_connectivity_changed, chand, |
||||
grpc_schedule_on_exec_ctx); |
||||
|
||||
if (gpr_time_cmp(chand->max_connection_age, gpr_inf_future(GPR_TIMESPAN)) != |
||||
0) { |
||||
/* When the channel reaches its max age, we send down an op with
|
||||
goaway_error set. However, we can't send down any ops until after the |
||||
channel stack is fully initialized. If we start the timer here, we have |
||||
no guarantee that the timer won't pop before channel stack initialization |
||||
is finished. To avoid that problem, we create a closure to start the |
||||
timer, and we schedule that closure to be run after call stack |
||||
initialization is done. */ |
||||
GRPC_CHANNEL_STACK_REF(chand->channel_stack, |
||||
"max_age start_max_age_timer_after_init"); |
||||
grpc_closure_sched(exec_ctx, &chand->start_max_age_timer_after_init, |
||||
GRPC_ERROR_NONE); |
||||
} |
||||
|
||||
/* Initialize the number of calls as 1, so that the max_idle_timer will not
|
||||
start until start_max_idle_timer_after_init is invoked. */ |
||||
gpr_atm_rel_store(&chand->call_count, 1); |
||||
if (gpr_time_cmp(chand->max_connection_idle, gpr_inf_future(GPR_TIMESPAN)) != |
||||
0) { |
||||
GRPC_CHANNEL_STACK_REF(chand->channel_stack, |
||||
"max_age start_max_idle_timer_after_init"); |
||||
grpc_closure_sched(exec_ctx, &chand->start_max_idle_timer_after_init, |
||||
GRPC_ERROR_NONE); |
||||
} |
||||
return GRPC_ERROR_NONE; |
||||
} |
||||
|
||||
/* Destructor for channel_data. */ |
||||
static void destroy_channel_elem(grpc_exec_ctx* exec_ctx, |
||||
grpc_channel_element* elem) {} |
||||
|
||||
const grpc_channel_filter grpc_max_age_filter = { |
||||
grpc_call_next_op, |
||||
grpc_channel_next_op, |
||||
0, /* sizeof_call_data */ |
||||
init_call_elem, |
||||
grpc_call_stack_ignore_set_pollset_or_pollset_set, |
||||
destroy_call_elem, |
||||
sizeof(channel_data), |
||||
init_channel_elem, |
||||
destroy_channel_elem, |
||||
grpc_call_next_get_peer, |
||||
grpc_channel_next_get_info, |
||||
"max_age"}; |
@ -0,0 +1,39 @@ |
||||
//
|
||||
// Copyright 2017, 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.
|
||||
//
|
||||
|
||||
#ifndef GRPC_CORE_LIB_CHANNEL_MAX_AGE_FILTER_H |
||||
#define GRPC_CORE_LIB_CHANNEL_MAX_AGE_FILTER_H |
||||
|
||||
#include "src/core/lib/channel/channel_stack.h" |
||||
|
||||
extern const grpc_channel_filter grpc_max_age_filter; |
||||
|
||||
#endif /* GRPC_CORE_LIB_CHANNEL_MAX_AGE_FILTER_H */ |
@ -0,0 +1,110 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2017, 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. |
||||
* |
||||
*/ |
||||
|
||||
#include "src/core/lib/iomgr/port.h" |
||||
|
||||
#ifdef GRPC_POSIX_SOCKET |
||||
|
||||
#include "src/core/lib/iomgr/socket_factory_posix.h" |
||||
|
||||
#include <grpc/impl/codegen/grpc_types.h> |
||||
#include <grpc/support/sync.h> |
||||
#include <grpc/support/useful.h> |
||||
|
||||
void grpc_socket_factory_init(grpc_socket_factory *factory, |
||||
const grpc_socket_factory_vtable *vtable) { |
||||
factory->vtable = vtable; |
||||
gpr_ref_init(&factory->refcount, 1); |
||||
} |
||||
|
||||
int grpc_socket_factory_socket(grpc_socket_factory *factory, int domain, |
||||
int type, int protocol) { |
||||
return factory->vtable->socket(factory, domain, type, protocol); |
||||
} |
||||
|
||||
int grpc_socket_factory_bind(grpc_socket_factory *factory, int sockfd, |
||||
const grpc_resolved_address *addr) { |
||||
return factory->vtable->bind(factory, sockfd, addr); |
||||
} |
||||
|
||||
int grpc_socket_factory_compare(grpc_socket_factory *a, |
||||
grpc_socket_factory *b) { |
||||
int c = GPR_ICMP(a, b); |
||||
if (c != 0) { |
||||
grpc_socket_factory *sma = a; |
||||
grpc_socket_factory *smb = b; |
||||
c = GPR_ICMP(sma->vtable, smb->vtable); |
||||
if (c == 0) { |
||||
c = sma->vtable->compare(sma, smb); |
||||
} |
||||
} |
||||
return c; |
||||
} |
||||
|
||||
grpc_socket_factory *grpc_socket_factory_ref(grpc_socket_factory *factory) { |
||||
gpr_ref(&factory->refcount); |
||||
return factory; |
||||
} |
||||
|
||||
void grpc_socket_factory_unref(grpc_socket_factory *factory) { |
||||
if (gpr_unref(&factory->refcount)) { |
||||
factory->vtable->destroy(factory); |
||||
} |
||||
} |
||||
|
||||
static void *socket_factory_arg_copy(void *p) { |
||||
return grpc_socket_factory_ref(p); |
||||
} |
||||
|
||||
static void socket_factory_arg_destroy(grpc_exec_ctx *exec_ctx, void *p) { |
||||
grpc_socket_factory_unref(p); |
||||
} |
||||
|
||||
static int socket_factory_cmp(void *a, void *b) { |
||||
return grpc_socket_factory_compare((grpc_socket_factory *)a, |
||||
(grpc_socket_factory *)b); |
||||
} |
||||
|
||||
static const grpc_arg_pointer_vtable socket_factory_arg_vtable = { |
||||
socket_factory_arg_copy, socket_factory_arg_destroy, socket_factory_cmp}; |
||||
|
||||
grpc_arg grpc_socket_factory_to_arg(grpc_socket_factory *factory) { |
||||
grpc_arg arg; |
||||
arg.type = GRPC_ARG_POINTER; |
||||
arg.key = GRPC_ARG_SOCKET_FACTORY; |
||||
arg.value.pointer.vtable = &socket_factory_arg_vtable; |
||||
arg.value.pointer.p = factory; |
||||
return arg; |
||||
} |
||||
|
||||
#endif |
@ -0,0 +1,90 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2017, 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. |
||||
* |
||||
*/ |
||||
|
||||
#ifndef GRPC_CORE_LIB_IOMGR_SOCKET_FACTORY_POSIX_H |
||||
#define GRPC_CORE_LIB_IOMGR_SOCKET_FACTORY_POSIX_H |
||||
|
||||
#include <grpc/impl/codegen/grpc_types.h> |
||||
#include <grpc/support/sync.h> |
||||
#include "src/core/lib/iomgr/resolve_address.h" |
||||
|
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
/** The virtual table of grpc_socket_factory */ |
||||
typedef struct { |
||||
/** Replacement for socket(2) */ |
||||
int (*socket)(grpc_socket_factory *factory, int domain, int type, |
||||
int protocol); |
||||
/** Replacement for bind(2) */ |
||||
int (*bind)(grpc_socket_factory *factory, int sockfd, |
||||
const grpc_resolved_address *addr); |
||||
/** Compare socket factory \a a and \a b */ |
||||
int (*compare)(grpc_socket_factory *a, grpc_socket_factory *b); |
||||
/** Destroys the socket factory instance */ |
||||
void (*destroy)(grpc_socket_factory *factory); |
||||
} grpc_socket_factory_vtable; |
||||
|
||||
/** The Socket Factory interface allows changes on socket options */ |
||||
struct grpc_socket_factory { |
||||
const grpc_socket_factory_vtable *vtable; |
||||
gpr_refcount refcount; |
||||
}; |
||||
|
||||
/** called by concrete implementations to initialize the base struct */ |
||||
void grpc_socket_factory_init(grpc_socket_factory *factory, |
||||
const grpc_socket_factory_vtable *vtable); |
||||
|
||||
/** Wrap \a factory as a grpc_arg */ |
||||
grpc_arg grpc_socket_factory_to_arg(grpc_socket_factory *factory); |
||||
|
||||
/** Perform the equivalent of a socket(2) operation using \a factory */ |
||||
int grpc_socket_factory_socket(grpc_socket_factory *factory, int domain, |
||||
int type, int protocol); |
||||
|
||||
/** Perform the equivalent of a bind(2) operation using \a factory */ |
||||
int grpc_socket_factory_bind(grpc_socket_factory *factory, int sockfd, |
||||
const grpc_resolved_address *addr); |
||||
|
||||
/** Compare if \a a and \a b are the same factory or have same settings */ |
||||
int grpc_socket_factory_compare(grpc_socket_factory *a, grpc_socket_factory *b); |
||||
|
||||
grpc_socket_factory *grpc_socket_factory_ref(grpc_socket_factory *factory); |
||||
void grpc_socket_factory_unref(grpc_socket_factory *factory); |
||||
|
||||
#ifdef __cplusplus |
||||
} |
||||
#endif |
||||
|
||||
#endif /* GRPC_CORE_LIB_IOMGR_SOCKET_FACTORY_POSIX_H */ |
@ -0,0 +1,77 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2017, 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. |
||||
* |
||||
*/ |
||||
|
||||
#include "src/core/lib/surface/completion_queue_factory.h" |
||||
#include "src/core/lib/surface/completion_queue.h" |
||||
|
||||
#include <grpc/support/log.h> |
||||
|
||||
/* TODO (sreek) - Currently this does not use the attributes arg. This will be
|
||||
added in a future PR */ |
||||
static grpc_completion_queue* default_create( |
||||
const grpc_completion_queue_factory* factory, |
||||
const grpc_completion_queue_attributes* attributes) { |
||||
return grpc_completion_queue_create(NULL); |
||||
} |
||||
|
||||
static grpc_completion_queue_factory_vtable default_vtable = {default_create}; |
||||
|
||||
static const grpc_completion_queue_factory g_default_cq_factory = { |
||||
"Default Factory", NULL, &default_vtable}; |
||||
|
||||
const grpc_completion_queue_factory* grpc_completion_queue_factory_lookup( |
||||
const grpc_completion_queue_attributes* attributes) { |
||||
/* As we add more fields to grpc_completion_queue_attributes, we may have to
|
||||
change this assert to: |
||||
GPR_ASSERT (attributes->version >= 1 && |
||||
attributes->version <= GRPC_CQ_CURRENT_VERSION) */ |
||||
GPR_ASSERT(attributes->version == 1); |
||||
|
||||
/* The default factory can handle version 1 of the attributes structure. We
|
||||
may have to change this as more fields are added to the structure */ |
||||
return &g_default_cq_factory; |
||||
} |
||||
|
||||
grpc_completion_queue* grpc_completion_queue_create_for_next(void* reserved) { |
||||
GPR_ASSERT(!reserved); |
||||
grpc_completion_queue_attributes attr = {1, GRPC_CQ_NEXT, |
||||
GRPC_CQ_DEFAULT_POLLING}; |
||||
return g_default_cq_factory.vtable->create(&g_default_cq_factory, &attr); |
||||
} |
||||
|
||||
grpc_completion_queue* grpc_completion_queue_create_for_pluck(void* reserved) { |
||||
GPR_ASSERT(!reserved); |
||||
grpc_completion_queue_attributes attr = {1, GRPC_CQ_PLUCK, |
||||
GRPC_CQ_DEFAULT_POLLING}; |
||||
return g_default_cq_factory.vtable->create(&g_default_cq_factory, &attr); |
||||
} |
@ -0,0 +1,51 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2017, 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. |
||||
* |
||||
*/ |
||||
|
||||
#ifndef GRPC_CORE_LIB_SURFACE_COMPLETION_QUEUE_FACTORY_H |
||||
#define GRPC_CORE_LIB_SURFACE_COMPLETION_QUEUE_FACTORY_H |
||||
|
||||
#include <grpc/grpc.h> |
||||
#include "src/core/lib/surface/completion_queue.h" |
||||
|
||||
typedef struct grpc_completion_queue_factory_vtable { |
||||
grpc_completion_queue* (*create)(const grpc_completion_queue_factory*, |
||||
const grpc_completion_queue_attributes*); |
||||
} grpc_completion_queue_factory_vtable; |
||||
|
||||
struct grpc_completion_queue_factory { |
||||
const char* name; |
||||
void* data; /* Factory specific data */ |
||||
grpc_completion_queue_factory_vtable* vtable; |
||||
}; |
||||
|
||||
#endif /* GRPC_CORE_LIB_SURFACE_COMPLETION_QUEUE_FACTORY_H */ |
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue