mirror of https://github.com/grpc/grpc.git
Merge branch 'master' of https://github.com/grpc/grpc into grpcz_client
commit
a0649dde0e
326 changed files with 18219 additions and 3248 deletions
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,158 @@ |
|||||||
|
# Combiner Explanation |
||||||
|
## Talk by ctiller, notes by vjpai |
||||||
|
|
||||||
|
Typical way of doing critical section |
||||||
|
|
||||||
|
``` |
||||||
|
mu.lock() |
||||||
|
do_stuff() |
||||||
|
mu.unlock() |
||||||
|
``` |
||||||
|
|
||||||
|
An alternative way of doing it is |
||||||
|
|
||||||
|
``` |
||||||
|
class combiner { |
||||||
|
run(f) { |
||||||
|
mu.lock() |
||||||
|
f() |
||||||
|
mu.unlock() |
||||||
|
} |
||||||
|
mutex mu; |
||||||
|
} |
||||||
|
|
||||||
|
combiner.run(do_stuff) |
||||||
|
``` |
||||||
|
|
||||||
|
If you have two threads calling combiner, there will be some kind of |
||||||
|
queuing in place. It's called `combiner` because you can pass in more |
||||||
|
than one do_stuff at once and they will run under a common `mu`. |
||||||
|
|
||||||
|
The implementation described above has the issue that you're blocking a thread |
||||||
|
for a period of time, and this is considered harmful because it's an application thread that you're blocking. |
||||||
|
|
||||||
|
Instead, get a new property: |
||||||
|
* Keep things running in serial execution |
||||||
|
* Don't ever sleep the thread |
||||||
|
* But maybe allow things to end up running on a different thread from where they were started |
||||||
|
* This means that `do_stuff` doesn't necessarily run to completion when `combiner.run` is invoked |
||||||
|
|
||||||
|
``` |
||||||
|
class combiner { |
||||||
|
mpscq q; // multi-producer single-consumer queue can be made non-blocking |
||||||
|
state s; // is it empty or executing |
||||||
|
|
||||||
|
run(f) { |
||||||
|
if (q.push(f)) { |
||||||
|
// q.push returns true if it's the first thing |
||||||
|
while (q.pop(&f)) { // modulo some extra work to avoid races |
||||||
|
f(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
``` |
||||||
|
|
||||||
|
The basic idea is that the first one to push onto the combiner |
||||||
|
executes the work and then keeps executing functions from the queue |
||||||
|
until the combiner is drained. |
||||||
|
|
||||||
|
Our combiner does some additional work, with the motivation of write-batching. |
||||||
|
|
||||||
|
We have a second tier of `run` called `run_finally`. Anything queued |
||||||
|
onto `run_finally` runs after we have drained the queue. That means |
||||||
|
that there is essentially a finally-queue. This is not guaranteed to |
||||||
|
be final, but it's best-effort. In the process of running the finally |
||||||
|
item, we might put something onto the main combiner queue and so we'll |
||||||
|
need to re-enter. |
||||||
|
|
||||||
|
`chttp2` runs all ops in the run state except if it sees a write it puts that into a finally. That way anything else that gets put into the combiner can add to that write. |
||||||
|
|
||||||
|
``` |
||||||
|
class combiner { |
||||||
|
mpscq q; // multi-producer single-consumer queue can be made non-blocking |
||||||
|
state s; // is it empty or executing |
||||||
|
queue finally; // you can only do run_finally when you are already running something from the combiner |
||||||
|
|
||||||
|
run(f) { |
||||||
|
if (q.push(f)) { |
||||||
|
// q.push returns true if it's the first thing |
||||||
|
loop: |
||||||
|
while (q.pop(&f)) { // modulo some extra work to avoid races |
||||||
|
f(); |
||||||
|
} |
||||||
|
while (finally.pop(&f)) { |
||||||
|
f(); |
||||||
|
} |
||||||
|
goto loop; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
``` |
||||||
|
|
||||||
|
So that explains how combiners work in general. In gRPC, there is |
||||||
|
`start_batch(..., tag)` and then work only gets activated by somebody |
||||||
|
calling `cq::next` which returns a tag. This gives an API-level |
||||||
|
guarantee that there will be a thread doing polling to actually make |
||||||
|
work happen. However, some operations are not covered by a poller |
||||||
|
thread, such as cancellation that doesn't have a completion. Other |
||||||
|
callbacks that don't have a completion are the internal work that gets |
||||||
|
done before the batch gets completed. We need a condition called |
||||||
|
`covered_by_poller` that means that the item will definitely need some |
||||||
|
thread at some point to call `cq::next` . This includes those |
||||||
|
callbacks that directly cause a completion but also those that are |
||||||
|
indirectly required before getting a completion. If we can't tell for |
||||||
|
sure for a specific path, we have to assumed it is not covered by |
||||||
|
poller. |
||||||
|
|
||||||
|
The above combiner has the problem that it keeps draining for a |
||||||
|
potentially infinite amount of time and that can lead to a huge tail |
||||||
|
latency for some operations. So we can tweak it by returning to the application |
||||||
|
if we know that it is valid to do so: |
||||||
|
|
||||||
|
``` |
||||||
|
while (q.pop(&f)) { |
||||||
|
f(); |
||||||
|
if (control_can_be_returned && some_still_queued_thing_is_covered_by_poller) { |
||||||
|
offload_combiner_work_to_some_other_thread(); |
||||||
|
} |
||||||
|
} |
||||||
|
``` |
||||||
|
|
||||||
|
`offload` is more than `break`; it does `break` but also causes some |
||||||
|
other thread that is currently waiting on a poll to break out of its |
||||||
|
poll. This is done by setting up a per-polling-island work-queue |
||||||
|
(distributor) wakeup FD. The work-queue is the converse of the combiner; it |
||||||
|
tries to spray events onto as many threads as possible to get as much concurrency as possible. |
||||||
|
|
||||||
|
So `offload` really does: |
||||||
|
|
||||||
|
``` |
||||||
|
workqueue.run(continue_from_while_loop); |
||||||
|
break; |
||||||
|
``` |
||||||
|
|
||||||
|
This needs us to add another class variable for a `workqueue` |
||||||
|
(which is really conceptually a distributor). |
||||||
|
|
||||||
|
``` |
||||||
|
workqueue::run(f) { |
||||||
|
q.push(f) |
||||||
|
eventfd.wakeup() |
||||||
|
} |
||||||
|
|
||||||
|
workqueue::readable() { |
||||||
|
eventfd.consume(); |
||||||
|
q.pop(&f); |
||||||
|
f(); |
||||||
|
if (!q.empty()) { |
||||||
|
eventfd.wakeup(); // spray across as many threads as are waiting on this workqueue |
||||||
|
} |
||||||
|
} |
||||||
|
``` |
||||||
|
|
||||||
|
In principle, `run_finally` could get starved, but this hasn't |
||||||
|
happened in practice. If we were concerned about this, we could put a |
||||||
|
limit on how many things come off the regular `q` before the `finally` |
||||||
|
queue gets processed. |
||||||
|
|
@ -0,0 +1,160 @@ |
|||||||
|
# gRPC Error |
||||||
|
|
||||||
|
## Background |
||||||
|
|
||||||
|
`grpc_error` is the c-core's opaque representation of an error. It holds a |
||||||
|
collection of integers, strings, timestamps, and child errors that related to |
||||||
|
the final error. |
||||||
|
|
||||||
|
always present are: |
||||||
|
|
||||||
|
* GRPC_ERROR_STR_FILE and GRPC_ERROR_INT_FILE_LINE - the source location where |
||||||
|
the error was generated |
||||||
|
* GRPC_ERROR_STR_DESCRIPTION - a human readable description of the error |
||||||
|
* GRPC_ERROR_TIME_CREATED - a timestamp indicating when the error happened |
||||||
|
|
||||||
|
An error can also have children; these are other errors that are believed to |
||||||
|
have contributed to this one. By accumulating children, we can begin to root |
||||||
|
cause high level failures from low level failures, without having to derive |
||||||
|
execution paths from log lines. |
||||||
|
|
||||||
|
grpc_errors are refcounted objects, which means they need strict ownership |
||||||
|
semantics. An extra ref on an error can cause a memory leak, and a missing ref |
||||||
|
can cause a crash. |
||||||
|
|
||||||
|
This document serves as a detailed overview of grpc_error's ownership rules. It |
||||||
|
should help people use the errors, as well as help people debug refcount related |
||||||
|
errors. |
||||||
|
|
||||||
|
## Clarification of Ownership |
||||||
|
|
||||||
|
If a particular function is said to "own" an error, that means it has the |
||||||
|
responsibility of calling unref on the error. A function may have access to an |
||||||
|
error without ownership of it. |
||||||
|
|
||||||
|
This means the function may use the error, but must not call unref on it, since |
||||||
|
that will be done elsewhere in the code. A function that does not own an error |
||||||
|
may explicitly take ownership of it by manually calling GRPC_ERROR_REF. |
||||||
|
|
||||||
|
## Ownership Rules |
||||||
|
|
||||||
|
There are three rules of error ownership, which we will go over in detail. |
||||||
|
|
||||||
|
* If `grpc_error` is returned by a function, the caller owns a ref to that |
||||||
|
instance. |
||||||
|
* If a `grpc_error` is passed to a `grpc_closure` callback function, then that |
||||||
|
function does not own a ref to the error. |
||||||
|
* if a `grpc_error` is passed to *any other function*, then that function |
||||||
|
takes ownership of the error. |
||||||
|
|
||||||
|
### Rule 1 |
||||||
|
|
||||||
|
> If `grpc_error` is returned by a function, the caller owns a ref to that |
||||||
|
> instance.* |
||||||
|
|
||||||
|
For example, in the following code block, error1 and error2 are owned by the |
||||||
|
current function. |
||||||
|
|
||||||
|
```C |
||||||
|
grpc_error* error1 = GRPC_ERROR_CREATE_FROM_STATIC_STRING("Some error occured"); |
||||||
|
grpc_error* error2 = some_operation_that_might_fail(...); |
||||||
|
``` |
||||||
|
|
||||||
|
The current function would have to explicitly call GRPC_ERROR_UNREF on the |
||||||
|
errors, or pass them along to a function that would take over the ownership. |
||||||
|
|
||||||
|
### Rule 2 |
||||||
|
|
||||||
|
> If a `grpc_error` is passed to a `grpc_closure` callback function, then that |
||||||
|
> function does not own a ref to the error. |
||||||
|
|
||||||
|
A `grpc_closure` callback function is any function that has the signature: |
||||||
|
|
||||||
|
```C |
||||||
|
void (*cb)(grpc_exec_ctx *exec_ctx, void *arg, grpc_error *error); |
||||||
|
``` |
||||||
|
|
||||||
|
This means that the error ownership is NOT transferred when a functions calls: |
||||||
|
|
||||||
|
```C |
||||||
|
c->cb(exec_ctx, c->cb_arg, err); |
||||||
|
``` |
||||||
|
|
||||||
|
The caller is still responsible for unref-ing the error. |
||||||
|
|
||||||
|
However, the above line is currently being phased out! It is safer to invoke |
||||||
|
callbacks with `grpc_closure_run` and `grpc_closure_sched`. These functions are |
||||||
|
not callbacks, so they will take ownership of the error passed to them. |
||||||
|
|
||||||
|
```C |
||||||
|
grpc_error* error = GRPC_ERROR_CREATE_FROM_STATIC_STRING("Some error occured"); |
||||||
|
grpc_closure_run(exec_ctx, cb, error); |
||||||
|
// current function no longer has ownership of the error |
||||||
|
``` |
||||||
|
|
||||||
|
If you schedule or run a closure, but still need ownership of the error, then |
||||||
|
you must explicitly take a reference. |
||||||
|
|
||||||
|
```C |
||||||
|
grpc_error* error = GRPC_ERROR_CREATE_FROM_STATIC_STRING("Some error occured"); |
||||||
|
grpc_closure_run(exec_ctx, cb, GRPC_ERROR_REF(error)); |
||||||
|
// do some other things with the error |
||||||
|
GRPC_ERROR_UNREF(error); |
||||||
|
``` |
||||||
|
|
||||||
|
Rule 2 is more important to keep in mind when **implementing** `grpc_closure` |
||||||
|
callback functions. You must keep in mind that you do not own the error, and |
||||||
|
must not unref it. More importantly, you cannot pass it to any function that |
||||||
|
would take ownership of the error, without explicitly taking ownership yourself. |
||||||
|
For example: |
||||||
|
|
||||||
|
```C |
||||||
|
void on_some_action(grpc_exec_ctx *exec_ctx, void *arg, grpc_error *error) { |
||||||
|
// this would cause a crash, because some_function will unref the error, |
||||||
|
// and the caller of this callback will also unref it. |
||||||
|
some_function(error); |
||||||
|
|
||||||
|
// this callback function must take ownership, so it can give that |
||||||
|
// ownership to the function it is calling. |
||||||
|
some_function(GRPC_ERROR_REF(error)); |
||||||
|
} |
||||||
|
``` |
||||||
|
|
||||||
|
### Rule 3 |
||||||
|
|
||||||
|
> if a `grpc_error` is passed to *any other function*, then that function takes |
||||||
|
> ownership of the error. |
||||||
|
|
||||||
|
Take the following example: |
||||||
|
|
||||||
|
```C |
||||||
|
grpc_error* error = GRPC_ERROR_CREATE_FROM_STATIC_STRING("Some error occured"); |
||||||
|
// do some things |
||||||
|
some_function(error); |
||||||
|
// can't use error anymore! might be gone. |
||||||
|
``` |
||||||
|
|
||||||
|
When some_function is called, it takes over the ownership of the error, and it |
||||||
|
will eventually unref it. So the caller can no longer safely use the error. |
||||||
|
|
||||||
|
If the caller needed to keep using the error (or passing it to other functions), |
||||||
|
if would have to take on a reference to it. This is a common pattern seen. |
||||||
|
|
||||||
|
```C |
||||||
|
void func() { |
||||||
|
grpc_error* error = GRPC_ERROR_CREATE_FROM_STATIC_STRING("Some error"); |
||||||
|
some_function(GRPC_ERROR_REF(error)); |
||||||
|
// do things |
||||||
|
some_other_function(GRPC_ERROR_REF(error)); |
||||||
|
// do more things |
||||||
|
some_last_function(error); |
||||||
|
} |
||||||
|
``` |
||||||
|
|
||||||
|
The last call takes ownership and will eventually give the error its final |
||||||
|
unref. |
||||||
|
|
||||||
|
When **implementing** a function that takes an error (and is not a |
||||||
|
`grpc_closure` callback function), you must ensure the error is unref-ed either |
||||||
|
by doing it explicitly with GRPC_ERROR_UNREF, or by passing the error to a |
||||||
|
function that takes over the ownership. |
@ -1,7 +1,8 @@ |
|||||||
This is the static code generation variant of the Node examples. Code in these examples is pre-generated using protoc and the Node gRPC protoc plugin, and the generated code can be found in various `*_pb.js` files. The command line sequence for generating those files is as follows (assuming that `protoc` and `grpc_node_plugin` are present, and starting in the base directory of this package): |
This is the static code generation variant of the Node examples. Code in these examples is pre-generated using protoc and the Node gRPC protoc plugin, and the generated code can be found in various `*_pb.js` files. The command line sequence for generating those files is as follows (assuming that `protoc` and `grpc_node_plugin` are present, and starting in the directory which contains this README.md file): |
||||||
|
|
||||||
```sh |
```sh |
||||||
cd ../protos |
cd ../../protos |
||||||
protoc --js_out=import_style=commonjs,binary:../node/static_codegen/ --grpc_out=../node/static_codegen --plugin=protoc-gen-grpc=grpc_node_plugin helloworld.proto |
npm install -g grpc-tools |
||||||
protoc --js_out=import_style=commonjs,binary:../node/static_codegen/route_guide/ --grpc_out=../node/static_codegen/route_guide/ --plugin=protoc-gen-grpc=grpc_node_plugin route_guide.proto |
grpc_tools_node_protoc --js_out=import_style=commonjs,binary:../node/static_codegen/ --grpc_out=../node/static_codegen --plugin=protoc-gen-grpc=`which grpc_tools_node_protoc_plugin` helloworld.proto |
||||||
|
grpc_tools_node_protoc --js_out=import_style=commonjs,binary:../node/static_codegen/route_guide/ --grpc_out=../node/static_codegen/route_guide/ --plugin=protoc-gen-grpc=`which grpc_tools_node_protoc_plugin` route_guide.proto |
||||||
``` |
``` |
||||||
|
@ -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) |
@ -0,0 +1,210 @@ |
|||||||
|
/*
|
||||||
|
* |
||||||
|
* 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/ext/client_channel/retry_throttle.h" |
||||||
|
|
||||||
|
#include <limits.h> |
||||||
|
#include <string.h> |
||||||
|
|
||||||
|
#include <grpc/support/alloc.h> |
||||||
|
#include <grpc/support/atm.h> |
||||||
|
#include <grpc/support/avl.h> |
||||||
|
#include <grpc/support/string_util.h> |
||||||
|
#include <grpc/support/sync.h> |
||||||
|
|
||||||
|
//
|
||||||
|
// server_retry_throttle_data
|
||||||
|
//
|
||||||
|
|
||||||
|
struct grpc_server_retry_throttle_data { |
||||||
|
gpr_refcount refs; |
||||||
|
int max_milli_tokens; |
||||||
|
int milli_token_ratio; |
||||||
|
gpr_atm milli_tokens; |
||||||
|
// A pointer to the replacement for this grpc_server_retry_throttle_data
|
||||||
|
// entry. If non-NULL, then this entry is stale and must not be used.
|
||||||
|
// We hold a reference to the replacement.
|
||||||
|
gpr_atm replacement; |
||||||
|
}; |
||||||
|
|
||||||
|
static void get_replacement_throttle_data_if_needed( |
||||||
|
grpc_server_retry_throttle_data** throttle_data) { |
||||||
|
while (true) { |
||||||
|
grpc_server_retry_throttle_data* new_throttle_data = |
||||||
|
(grpc_server_retry_throttle_data*)gpr_atm_acq_load( |
||||||
|
&(*throttle_data)->replacement); |
||||||
|
if (new_throttle_data == NULL) return; |
||||||
|
*throttle_data = new_throttle_data; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
bool grpc_server_retry_throttle_data_record_failure( |
||||||
|
grpc_server_retry_throttle_data* throttle_data) { |
||||||
|
// First, check if we are stale and need to be replaced.
|
||||||
|
get_replacement_throttle_data_if_needed(&throttle_data); |
||||||
|
// We decrement milli_tokens by 1000 (1 token) for each failure.
|
||||||
|
const int new_value = (int)gpr_atm_no_barrier_clamped_add( |
||||||
|
&throttle_data->milli_tokens, (gpr_atm)-1000, (gpr_atm)0, |
||||||
|
(gpr_atm)throttle_data->max_milli_tokens); |
||||||
|
// Retries are allowed as long as the new value is above the threshold
|
||||||
|
// (max_milli_tokens / 2).
|
||||||
|
return new_value > throttle_data->max_milli_tokens / 2; |
||||||
|
} |
||||||
|
|
||||||
|
void grpc_server_retry_throttle_data_record_success( |
||||||
|
grpc_server_retry_throttle_data* throttle_data) { |
||||||
|
// First, check if we are stale and need to be replaced.
|
||||||
|
get_replacement_throttle_data_if_needed(&throttle_data); |
||||||
|
// We increment milli_tokens by milli_token_ratio for each success.
|
||||||
|
gpr_atm_no_barrier_clamped_add( |
||||||
|
&throttle_data->milli_tokens, (gpr_atm)throttle_data->milli_token_ratio, |
||||||
|
(gpr_atm)0, (gpr_atm)throttle_data->max_milli_tokens); |
||||||
|
} |
||||||
|
|
||||||
|
grpc_server_retry_throttle_data* grpc_server_retry_throttle_data_ref( |
||||||
|
grpc_server_retry_throttle_data* throttle_data) { |
||||||
|
gpr_ref(&throttle_data->refs); |
||||||
|
return throttle_data; |
||||||
|
} |
||||||
|
|
||||||
|
void grpc_server_retry_throttle_data_unref( |
||||||
|
grpc_server_retry_throttle_data* throttle_data) { |
||||||
|
if (gpr_unref(&throttle_data->refs)) { |
||||||
|
grpc_server_retry_throttle_data* replacement = |
||||||
|
(grpc_server_retry_throttle_data*)gpr_atm_acq_load( |
||||||
|
&throttle_data->replacement); |
||||||
|
if (replacement != NULL) { |
||||||
|
grpc_server_retry_throttle_data_unref(replacement); |
||||||
|
} |
||||||
|
gpr_free(throttle_data); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
static grpc_server_retry_throttle_data* grpc_server_retry_throttle_data_create( |
||||||
|
int max_milli_tokens, int milli_token_ratio, |
||||||
|
grpc_server_retry_throttle_data* old_throttle_data) { |
||||||
|
grpc_server_retry_throttle_data* throttle_data = |
||||||
|
gpr_malloc(sizeof(*throttle_data)); |
||||||
|
memset(throttle_data, 0, sizeof(*throttle_data)); |
||||||
|
gpr_ref_init(&throttle_data->refs, 1); |
||||||
|
throttle_data->max_milli_tokens = max_milli_tokens; |
||||||
|
throttle_data->milli_token_ratio = milli_token_ratio; |
||||||
|
int initial_milli_tokens = max_milli_tokens; |
||||||
|
// If there was a pre-existing entry for this server name, initialize
|
||||||
|
// the token count by scaling proportionately to the old data. This
|
||||||
|
// ensures that if we're already throttling retries on the old scale,
|
||||||
|
// we will start out doing the same thing on the new one.
|
||||||
|
if (old_throttle_data != NULL) { |
||||||
|
double token_fraction = |
||||||
|
(int)gpr_atm_acq_load(&old_throttle_data->milli_tokens) / |
||||||
|
(double)old_throttle_data->max_milli_tokens; |
||||||
|
initial_milli_tokens = (int)(token_fraction * max_milli_tokens); |
||||||
|
} |
||||||
|
gpr_atm_rel_store(&throttle_data->milli_tokens, |
||||||
|
(gpr_atm)initial_milli_tokens); |
||||||
|
// If there was a pre-existing entry, mark it as stale and give it a
|
||||||
|
// pointer to the new entry, which is its replacement.
|
||||||
|
if (old_throttle_data != NULL) { |
||||||
|
grpc_server_retry_throttle_data_ref(throttle_data); |
||||||
|
gpr_atm_rel_store(&old_throttle_data->replacement, (gpr_atm)throttle_data); |
||||||
|
} |
||||||
|
return throttle_data; |
||||||
|
} |
||||||
|
|
||||||
|
//
|
||||||
|
// avl vtable for string -> server_retry_throttle_data map
|
||||||
|
//
|
||||||
|
|
||||||
|
static void* copy_server_name(void* key) { return gpr_strdup(key); } |
||||||
|
|
||||||
|
static long compare_server_name(void* key1, void* key2) { |
||||||
|
return strcmp(key1, key2); |
||||||
|
} |
||||||
|
|
||||||
|
static void destroy_server_retry_throttle_data(void* value) { |
||||||
|
grpc_server_retry_throttle_data* throttle_data = value; |
||||||
|
grpc_server_retry_throttle_data_unref(throttle_data); |
||||||
|
} |
||||||
|
|
||||||
|
static void* copy_server_retry_throttle_data(void* value) { |
||||||
|
grpc_server_retry_throttle_data* throttle_data = value; |
||||||
|
return grpc_server_retry_throttle_data_ref(throttle_data); |
||||||
|
} |
||||||
|
|
||||||
|
static const gpr_avl_vtable avl_vtable = { |
||||||
|
gpr_free /* destroy_key */, copy_server_name, compare_server_name, |
||||||
|
destroy_server_retry_throttle_data, copy_server_retry_throttle_data}; |
||||||
|
|
||||||
|
//
|
||||||
|
// server_retry_throttle_map
|
||||||
|
//
|
||||||
|
|
||||||
|
static gpr_mu g_mu; |
||||||
|
static gpr_avl g_avl; |
||||||
|
|
||||||
|
void grpc_retry_throttle_map_init() { |
||||||
|
gpr_mu_init(&g_mu); |
||||||
|
g_avl = gpr_avl_create(&avl_vtable); |
||||||
|
} |
||||||
|
|
||||||
|
void grpc_retry_throttle_map_shutdown() { |
||||||
|
gpr_mu_destroy(&g_mu); |
||||||
|
gpr_avl_unref(g_avl); |
||||||
|
} |
||||||
|
|
||||||
|
grpc_server_retry_throttle_data* grpc_retry_throttle_map_get_data_for_server( |
||||||
|
const char* server_name, int max_milli_tokens, int milli_token_ratio) { |
||||||
|
gpr_mu_lock(&g_mu); |
||||||
|
grpc_server_retry_throttle_data* throttle_data = |
||||||
|
gpr_avl_get(g_avl, (char*)server_name); |
||||||
|
if (throttle_data == NULL) { |
||||||
|
// Entry not found. Create a new one.
|
||||||
|
throttle_data = grpc_server_retry_throttle_data_create( |
||||||
|
max_milli_tokens, milli_token_ratio, NULL); |
||||||
|
g_avl = gpr_avl_add(g_avl, (char*)server_name, throttle_data); |
||||||
|
} else { |
||||||
|
if (throttle_data->max_milli_tokens != max_milli_tokens || |
||||||
|
throttle_data->milli_token_ratio != milli_token_ratio) { |
||||||
|
// Entry found but with old parameters. Create a new one based on
|
||||||
|
// the original one.
|
||||||
|
throttle_data = grpc_server_retry_throttle_data_create( |
||||||
|
max_milli_tokens, milli_token_ratio, throttle_data); |
||||||
|
g_avl = gpr_avl_add(g_avl, (char*)server_name, throttle_data); |
||||||
|
} else { |
||||||
|
// Entry found. Increase refcount.
|
||||||
|
grpc_server_retry_throttle_data_ref(throttle_data); |
||||||
|
} |
||||||
|
} |
||||||
|
gpr_mu_unlock(&g_mu); |
||||||
|
return throttle_data; |
||||||
|
} |
@ -0,0 +1,65 @@ |
|||||||
|
/*
|
||||||
|
* |
||||||
|
* 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_EXT_CLIENT_CHANNEL_RETRY_THROTTLE_H |
||||||
|
#define GRPC_CORE_EXT_CLIENT_CHANNEL_RETRY_THROTTLE_H |
||||||
|
|
||||||
|
#include <stdbool.h> |
||||||
|
|
||||||
|
/// Tracks retry throttling data for an individual server name.
|
||||||
|
typedef struct grpc_server_retry_throttle_data grpc_server_retry_throttle_data; |
||||||
|
|
||||||
|
/// Records a failure. Returns true if it's okay to send a retry.
|
||||||
|
bool grpc_server_retry_throttle_data_record_failure( |
||||||
|
grpc_server_retry_throttle_data* throttle_data); |
||||||
|
/// Records a success.
|
||||||
|
void grpc_server_retry_throttle_data_record_success( |
||||||
|
grpc_server_retry_throttle_data* throttle_data); |
||||||
|
|
||||||
|
grpc_server_retry_throttle_data* grpc_server_retry_throttle_data_ref( |
||||||
|
grpc_server_retry_throttle_data* throttle_data); |
||||||
|
void grpc_server_retry_throttle_data_unref( |
||||||
|
grpc_server_retry_throttle_data* throttle_data); |
||||||
|
|
||||||
|
/// Initializes global map of failure data for each server name.
|
||||||
|
void grpc_retry_throttle_map_init(); |
||||||
|
/// Shuts down global map of failure data for each server name.
|
||||||
|
void grpc_retry_throttle_map_shutdown(); |
||||||
|
|
||||||
|
/// Returns a reference to the failure data for \a server_name, creating
|
||||||
|
/// a new entry if needed.
|
||||||
|
/// Caller must eventually unref via \a grpc_server_retry_throttle_data_unref().
|
||||||
|
grpc_server_retry_throttle_data* grpc_retry_throttle_map_get_data_for_server( |
||||||
|
const char* server_name, int max_milli_tokens, int milli_token_ratio); |
||||||
|
|
||||||
|
#endif /* GRPC_CORE_EXT_CLIENT_CHANNEL_RETRY_THROTTLE_H */ |
@ -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,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 */ |
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue