aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorGravatar Sree Kuchibhotla <sreek@google.com>2017-05-10 19:35:03 -0700
committerGravatar Sree Kuchibhotla <sreek@google.com>2017-05-10 19:35:03 -0700
commit6e510f700db26731e0d2314316b3ce30e79a728f (patch)
tree6c82c4463fc7c414e11e0af86707e57d5d32998d /src
parent4afb4db49807f1811d08b6f41c857a0e238844cc (diff)
parentdd2f97045cb5ef37b8719d3b0ba0f6f57ca697b3 (diff)
Merge branch 'master' into cq_mpsc_based
Diffstat (limited to 'src')
-rwxr-xr-xsrc/boringssl/gen_build_yaml.py2
-rw-r--r--src/compiler/cpp_generator.cc6
-rw-r--r--src/core/ext/filters/client_channel/client_channel.c114
-rw-r--r--src/core/ext/filters/client_channel/lb_policy.h2
-rw-r--r--src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.c20
-rw-r--r--src/core/ext/transport/chttp2/transport/chttp2_transport.c45
-rw-r--r--src/core/ext/transport/chttp2/transport/internal.h11
-rw-r--r--src/core/ext/transport/chttp2/transport/parsing.c8
-rw-r--r--src/core/ext/transport/chttp2/transport/writing.c36
-rw-r--r--src/core/ext/transport/cronet/transport/cronet_transport.c4
-rw-r--r--src/core/lib/iomgr/port.h1
-rw-r--r--src/core/lib/support/cpu_linux.c8
-rw-r--r--src/core/lib/support/wrap_memcpy.c4
-rw-r--r--src/core/lib/transport/bdp_estimator.c19
-rw-r--r--src/core/lib/transport/bdp_estimator.h3
-rw-r--r--src/cpp/common/core_codegen.cc14
-rw-r--r--src/node/ext/call.cc1
-rw-r--r--src/node/ext/channel.cc1
-rw-r--r--src/node/ext/completion_queue.cc (renamed from src/node/ext/completion_queue_uv.cc)4
-rw-r--r--src/node/ext/completion_queue_async_worker.h86
-rw-r--r--src/node/ext/completion_queue_threadpool.cc180
-rw-r--r--src/node/ext/node_grpc.cc5
-rw-r--r--src/node/ext/server.cc59
-rw-r--r--src/node/ext/server_generic.cc75
-rw-r--r--src/node/ext/server_uv.cc120
-rw-r--r--src/node/index.js11
-rw-r--r--src/node/src/client.js790
-rw-r--r--src/node/src/common.js67
-rw-r--r--src/node/src/metadata.js13
-rw-r--r--src/php/composer.json2
-rw-r--r--src/proto/grpc/testing/control.proto3
-rw-r--r--src/proto/grpc/testing/services.proto17
-rw-r--r--src/python/grpcio/commands.py4
-rw-r--r--src/python/grpcio/grpc/__init__.py411
-rw-r--r--src/python/grpcio/grpc/_cython/_cygrpc/completion_queue.pyx.pxi11
-rw-r--r--src/python/grpcio/grpc/_cython/_cygrpc/grpc.pxi22
-rw-r--r--src/python/grpcio/grpc/_cython/_cygrpc/server.pyx.pxi2
-rw-r--r--src/python/grpcio_health_checking/setup.py2
-rw-r--r--src/python/grpcio_reflection/setup.py2
-rw-r--r--src/python/grpcio_tests/setup.py2
40 files changed, 1027 insertions, 1160 deletions
diff --git a/src/boringssl/gen_build_yaml.py b/src/boringssl/gen_build_yaml.py
index c53beb0da5..d01c2b4ec5 100755
--- a/src/boringssl/gen_build_yaml.py
+++ b/src/boringssl/gen_build_yaml.py
@@ -138,7 +138,7 @@ class Grpc(object):
{
'name': 'boringssl_%s' % os.path.basename(test[0]),
'args': [map_testarg(arg) for arg in test[1:]],
- 'exclude_configs': ['asan'],
+ 'exclude_configs': ['asan', 'ubsan'],
'ci_platforms': ['linux', 'mac', 'posix', 'windows'],
'platforms': ['linux', 'mac', 'posix', 'windows'],
'flaky': False,
diff --git a/src/compiler/cpp_generator.cc b/src/compiler/cpp_generator.cc
index d3e71625d6..a1a0258c68 100644
--- a/src/compiler/cpp_generator.cc
+++ b/src/compiler/cpp_generator.cc
@@ -804,6 +804,12 @@ void PrintHeaderService(grpc_generator::Printer *printer,
" public:\n");
printer->Indent();
+ // Service metadata
+ printer->Print(*vars,
+ "static constexpr char const* service_full_name() {\n"
+ " return \"$Package$$Service$\";\n"
+ "}\n");
+
// Client side
printer->Print(
"class StubInterface {\n"
diff --git a/src/core/ext/filters/client_channel/client_channel.c b/src/core/ext/filters/client_channel/client_channel.c
index 95578d989c..24843d52e9 100644
--- a/src/core/ext/filters/client_channel/client_channel.c
+++ b/src/core/ext/filters/client_channel/client_channel.c
@@ -760,12 +760,6 @@ static void cc_destroy_channel_elem(grpc_exec_ctx *exec_ctx,
#define CANCELLED_CALL ((grpc_subchannel_call *)1)
-typedef enum {
- /* zero so that it can be default-initialized */
- GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING = 0,
- GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL
-} subchannel_creation_phase;
-
/** Call data. Holds a pointer to grpc_subchannel_call and the
associated machinery to create such a pointer.
Handles queueing of stream ops until a call object is ready, waiting
@@ -793,7 +787,7 @@ typedef struct client_channel_call_data {
gpr_atm subchannel_call;
gpr_arena *arena;
- subchannel_creation_phase creation_phase;
+ bool pick_pending;
grpc_connected_subchannel *connected_subchannel;
grpc_call_context_element subchannel_call_context[GRPC_CONTEXT_COUNT];
grpc_polling_entity *pollent;
@@ -915,11 +909,10 @@ static void subchannel_ready_locked(grpc_exec_ctx *exec_ctx, void *arg,
grpc_call_element *elem = arg;
call_data *calld = elem->call_data;
channel_data *chand = elem->channel_data;
- GPR_ASSERT(calld->creation_phase ==
- GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL);
+ GPR_ASSERT(calld->pick_pending);
+ calld->pick_pending = false;
grpc_polling_entity_del_from_pollset_set(exec_ctx, calld->pollent,
chand->interested_parties);
- calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING;
if (calld->connected_subchannel == NULL) {
gpr_atm_no_barrier_store(&calld->subchannel_call, 1);
fail_locked(exec_ctx, calld,
@@ -988,8 +981,7 @@ static bool pick_subchannel_locked(
grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
grpc_metadata_batch *initial_metadata, uint32_t initial_metadata_flags,
grpc_connected_subchannel **connected_subchannel,
- grpc_call_context_element *subchannel_call_context, grpc_closure *on_ready,
- grpc_error *error);
+ grpc_call_context_element *subchannel_call_context, grpc_closure *on_ready);
static void continue_picking_locked(grpc_exec_ctx *exec_ctx, void *arg,
grpc_error *error) {
@@ -999,52 +991,51 @@ static void continue_picking_locked(grpc_exec_ctx *exec_ctx, void *arg,
} else if (error != GRPC_ERROR_NONE) {
grpc_closure_sched(exec_ctx, cpa->on_ready, GRPC_ERROR_REF(error));
} else {
- if (pick_subchannel_locked(
- exec_ctx, cpa->elem, cpa->initial_metadata,
- cpa->initial_metadata_flags, cpa->connected_subchannel,
- cpa->subchannel_call_context, cpa->on_ready, GRPC_ERROR_NONE)) {
+ if (pick_subchannel_locked(exec_ctx, cpa->elem, cpa->initial_metadata,
+ cpa->initial_metadata_flags,
+ cpa->connected_subchannel,
+ cpa->subchannel_call_context, cpa->on_ready)) {
grpc_closure_sched(exec_ctx, cpa->on_ready, GRPC_ERROR_NONE);
}
}
gpr_free(cpa);
}
+static void cancel_pick_locked(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
+ grpc_error *error) {
+ channel_data *chand = elem->channel_data;
+ call_data *calld = elem->call_data;
+ if (chand->lb_policy != NULL) {
+ grpc_lb_policy_cancel_pick_locked(exec_ctx, chand->lb_policy,
+ &calld->connected_subchannel,
+ GRPC_ERROR_REF(error));
+ }
+ for (grpc_closure *closure = chand->waiting_for_config_closures.head;
+ closure != NULL; closure = closure->next_data.next) {
+ continue_picking_args *cpa = closure->cb_arg;
+ if (cpa->connected_subchannel == &calld->connected_subchannel) {
+ cpa->connected_subchannel = NULL;
+ grpc_closure_sched(exec_ctx, cpa->on_ready,
+ GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(
+ "Pick cancelled", &error, 1));
+ }
+ }
+ GRPC_ERROR_UNREF(error);
+}
+
static bool pick_subchannel_locked(
grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
grpc_metadata_batch *initial_metadata, uint32_t initial_metadata_flags,
grpc_connected_subchannel **connected_subchannel,
- grpc_call_context_element *subchannel_call_context, grpc_closure *on_ready,
- grpc_error *error) {
+ grpc_call_context_element *subchannel_call_context,
+ grpc_closure *on_ready) {
GPR_TIMER_BEGIN("pick_subchannel", 0);
channel_data *chand = elem->channel_data;
call_data *calld = elem->call_data;
- continue_picking_args *cpa;
- grpc_closure *closure;
GPR_ASSERT(connected_subchannel);
- if (initial_metadata == NULL) {
- if (chand->lb_policy != NULL) {
- grpc_lb_policy_cancel_pick_locked(exec_ctx, chand->lb_policy,
- connected_subchannel,
- GRPC_ERROR_REF(error));
- }
- for (closure = chand->waiting_for_config_closures.head; closure != NULL;
- closure = closure->next_data.next) {
- cpa = closure->cb_arg;
- if (cpa->connected_subchannel == connected_subchannel) {
- cpa->connected_subchannel = NULL;
- grpc_closure_sched(exec_ctx, cpa->on_ready,
- GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(
- "Pick cancelled", &error, 1));
- }
- }
- GPR_TIMER_END("pick_subchannel", 0);
- GRPC_ERROR_UNREF(error);
- return true;
- }
- GPR_ASSERT(error == GRPC_ERROR_NONE);
if (chand->lb_policy != NULL) {
apply_final_configuration_locked(exec_ctx, elem);
grpc_lb_policy *lb_policy = chand->lb_policy;
@@ -1067,8 +1058,7 @@ static bool pick_subchannel_locked(
}
}
const grpc_lb_policy_pick_args inputs = {
- initial_metadata, initial_metadata_flags, &calld->lb_token_mdelem,
- gpr_inf_future(GPR_CLOCK_MONOTONIC)};
+ initial_metadata, initial_metadata_flags, &calld->lb_token_mdelem};
// Wrap the user-provided callback in order to hold a strong reference to
// the LB policy for the duration of the pick.
@@ -1101,7 +1091,7 @@ static bool pick_subchannel_locked(
&chand->on_resolver_result_changed);
}
if (chand->resolver != NULL) {
- cpa = gpr_malloc(sizeof(*cpa));
+ continue_picking_args *cpa = gpr_malloc(sizeof(*cpa));
cpa->initial_metadata = initial_metadata;
cpa->initial_metadata_flags = initial_metadata_flags;
cpa->connected_subchannel = connected_subchannel;
@@ -1157,16 +1147,13 @@ static void start_transport_stream_op_batch_locked_inner(
error to the caller when the first op does get passed down. */
calld->cancel_error =
GRPC_ERROR_REF(op->payload->cancel_stream.cancel_error);
- switch (calld->creation_phase) {
- case GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING:
- fail_locked(exec_ctx, calld,
- GRPC_ERROR_REF(op->payload->cancel_stream.cancel_error));
- break;
- case GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL:
- pick_subchannel_locked(
- exec_ctx, elem, NULL, 0, &calld->connected_subchannel, NULL, NULL,
- GRPC_ERROR_REF(op->payload->cancel_stream.cancel_error));
- break;
+ if (calld->pick_pending) {
+ cancel_pick_locked(
+ exec_ctx, elem,
+ GRPC_ERROR_REF(op->payload->cancel_stream.cancel_error));
+ } else {
+ fail_locked(exec_ctx, calld,
+ GRPC_ERROR_REF(op->payload->cancel_stream.cancel_error));
}
grpc_transport_stream_op_batch_finish_with_failure(
exec_ctx, op,
@@ -1176,9 +1163,9 @@ static void start_transport_stream_op_batch_locked_inner(
}
}
/* if we don't have a subchannel, try to get one */
- if (calld->creation_phase == GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING &&
- calld->connected_subchannel == NULL && op->send_initial_metadata) {
- calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL;
+ if (!calld->pick_pending && calld->connected_subchannel == NULL &&
+ op->send_initial_metadata) {
+ calld->pick_pending = true;
grpc_closure_init(&calld->next_step, subchannel_ready_locked, elem,
grpc_combiner_scheduler(chand->combiner, true));
GRPC_CALL_STACK_REF(calld->owning_call, "pick_subchannel");
@@ -1190,8 +1177,8 @@ static void start_transport_stream_op_batch_locked_inner(
op->payload->send_initial_metadata.send_initial_metadata,
op->payload->send_initial_metadata.send_initial_metadata_flags,
&calld->connected_subchannel, calld->subchannel_call_context,
- &calld->next_step, GRPC_ERROR_NONE)) {
- calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING;
+ &calld->next_step)) {
+ calld->pick_pending = false;
GRPC_CALL_STACK_UNREF(exec_ctx, calld->owning_call, "pick_subchannel");
} else {
grpc_polling_entity_add_to_pollset_set(exec_ctx, calld->pollent,
@@ -1199,8 +1186,7 @@ static void start_transport_stream_op_batch_locked_inner(
}
}
/* if we've got a subchannel, then let's ask it to create a call */
- if (calld->creation_phase == GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING &&
- calld->connected_subchannel != NULL) {
+ if (!calld->pick_pending && calld->connected_subchannel != NULL) {
grpc_subchannel_call *subchannel_call = NULL;
const grpc_connected_subchannel_call_args call_args = {
.pollent = calld->pollent,
@@ -1357,7 +1343,7 @@ static void cc_destroy_call_elem(grpc_exec_ctx *exec_ctx,
then_schedule_closure = NULL;
GRPC_SUBCHANNEL_CALL_UNREF(exec_ctx, call, "client_channel_destroy_call");
}
- GPR_ASSERT(calld->creation_phase == GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING);
+ GPR_ASSERT(!calld->pick_pending);
GPR_ASSERT(calld->waiting_ops_count == 0);
if (calld->connected_subchannel != NULL) {
GRPC_CONNECTED_SUBCHANNEL_UNREF(exec_ctx, calld->connected_subchannel,
@@ -1464,12 +1450,12 @@ static void watch_connectivity_state_locked(grpc_exec_ctx *exec_ctx, void *arg,
void grpc_client_channel_watch_connectivity_state(
grpc_exec_ctx *exec_ctx, grpc_channel_element *elem, grpc_pollset *pollset,
- grpc_connectivity_state *state, grpc_closure *on_complete) {
+ grpc_connectivity_state *state, grpc_closure *closure) {
channel_data *chand = elem->channel_data;
external_connectivity_watcher *w = gpr_malloc(sizeof(*w));
w->chand = chand;
w->pollset = pollset;
- w->on_complete = on_complete;
+ w->on_complete = closure;
w->state = state;
grpc_pollset_set_add_pollset(exec_ctx, chand->interested_parties, pollset);
GRPC_CHANNEL_STACK_REF(w->chand->owning_stack,
diff --git a/src/core/ext/filters/client_channel/lb_policy.h b/src/core/ext/filters/client_channel/lb_policy.h
index fefcb4912c..184b2ef720 100644
--- a/src/core/ext/filters/client_channel/lb_policy.h
+++ b/src/core/ext/filters/client_channel/lb_policy.h
@@ -62,8 +62,6 @@ typedef struct grpc_lb_policy_pick_args {
uint32_t initial_metadata_flags;
/** Storage for LB token in \a initial_metadata, or NULL if not used */
grpc_linked_mdelem *lb_token_mdelem_storage;
- /** Deadline for the call to the LB server */
- gpr_timespec deadline;
} grpc_lb_policy_pick_args;
struct grpc_lb_policy_vtable {
diff --git a/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.c b/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.c
index 37468101f0..695be4fdf2 100644
--- a/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.c
+++ b/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.c
@@ -95,8 +95,7 @@
headers. Therefore, sockaddr.h must always be included first */
#include "src/core/lib/iomgr/sockaddr.h"
-#include <errno.h>
-
+#include <limits.h>
#include <string.h>
#include <grpc/byte_buffer_reader.h>
@@ -310,8 +309,8 @@ typedef struct glb_lb_policy {
grpc_client_channel_factory *cc_factory;
grpc_channel_args *args;
- /** deadline for the LB's call */
- gpr_timespec deadline;
+ /** timeout in milliseconds for the LB call. 0 means no deadline. */
+ int lb_call_timeout_ms;
/** for communicating with the LB server */
grpc_channel *lb_channel;
@@ -917,6 +916,10 @@ static grpc_lb_policy *glb_create(grpc_exec_ctx *exec_ctx,
glb_policy->cc_factory = args->client_channel_factory;
GPR_ASSERT(glb_policy->cc_factory != NULL);
+ arg = grpc_channel_args_find(args->args, GRPC_ARG_GRPCLB_CALL_TIMEOUT_MS);
+ glb_policy->lb_call_timeout_ms =
+ grpc_channel_arg_get_integer(arg, (grpc_integer_options){0, 0, INT_MAX});
+
// Make sure that GRPC_ARG_LB_POLICY_NAME is set in channel args,
// since we use this to trigger the client_load_reporting filter.
grpc_arg new_arg;
@@ -1089,7 +1092,6 @@ static int glb_pick_locked(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol,
}
glb_lb_policy *glb_policy = (glb_lb_policy *)pol;
- glb_policy->deadline = pick_args->deadline;
bool pick_done;
if (glb_policy->rr_policy != NULL) {
@@ -1275,11 +1277,17 @@ static void lb_call_init_locked(grpc_exec_ctx *exec_ctx,
* glb_policy->base.interested_parties, which is comprised of the polling
* entities from \a client_channel. */
grpc_slice host = grpc_slice_from_copied_string(glb_policy->server_name);
+ gpr_timespec deadline =
+ glb_policy->lb_call_timeout_ms == 0
+ ? gpr_inf_future(GPR_CLOCK_MONOTONIC)
+ : gpr_time_add(gpr_now(GPR_CLOCK_MONOTONIC),
+ gpr_time_from_millis(glb_policy->lb_call_timeout_ms,
+ GPR_TIMESPAN));
glb_policy->lb_call = grpc_channel_create_pollset_set_call(
exec_ctx, glb_policy->lb_channel, NULL, GRPC_PROPAGATE_DEFAULTS,
glb_policy->base.interested_parties,
GRPC_MDSTR_SLASH_GRPC_DOT_LB_DOT_V1_DOT_LOADBALANCER_SLASH_BALANCELOAD,
- &host, glb_policy->deadline, NULL);
+ &host, deadline, NULL);
grpc_slice_unref_internal(exec_ctx, host);
if (glb_policy->client_stats != NULL) {
diff --git a/src/core/ext/transport/chttp2/transport/chttp2_transport.c b/src/core/ext/transport/chttp2/transport/chttp2_transport.c
index d6b79bd492..3c5216e104 100644
--- a/src/core/ext/transport/chttp2/transport/chttp2_transport.c
+++ b/src/core/ext/transport/chttp2/transport/chttp2_transport.c
@@ -884,14 +884,23 @@ static void write_action_begin_locked(grpc_exec_ctx *exec_ctx, void *gt,
GPR_TIMER_BEGIN("write_action_begin_locked", 0);
grpc_chttp2_transport *t = gt;
GPR_ASSERT(t->write_state != GRPC_CHTTP2_WRITE_STATE_IDLE);
- if (!t->closed && grpc_chttp2_begin_write(exec_ctx, t)) {
- set_write_state(exec_ctx, t, GRPC_CHTTP2_WRITE_STATE_WRITING,
- "begin writing");
- grpc_closure_sched(exec_ctx, &t->write_action, GRPC_ERROR_NONE);
- } else {
- set_write_state(exec_ctx, t, GRPC_CHTTP2_WRITE_STATE_IDLE,
- "begin writing nothing");
- GRPC_CHTTP2_UNREF_TRANSPORT(exec_ctx, t, "writing");
+ switch (t->closed ? GRPC_CHTTP2_NOTHING_TO_WRITE
+ : grpc_chttp2_begin_write(exec_ctx, t)) {
+ case GRPC_CHTTP2_NOTHING_TO_WRITE:
+ set_write_state(exec_ctx, t, GRPC_CHTTP2_WRITE_STATE_IDLE,
+ "begin writing nothing");
+ GRPC_CHTTP2_UNREF_TRANSPORT(exec_ctx, t, "writing");
+ break;
+ case GRPC_CHTTP2_PARTIAL_WRITE:
+ set_write_state(exec_ctx, t, GRPC_CHTTP2_WRITE_STATE_WRITING_WITH_MORE,
+ "begin writing partial");
+ grpc_closure_sched(exec_ctx, &t->write_action, GRPC_ERROR_NONE);
+ break;
+ case GRPC_CHTTP2_FULL_WRITE:
+ set_write_state(exec_ctx, t, GRPC_CHTTP2_WRITE_STATE_WRITING,
+ "begin writing");
+ grpc_closure_sched(exec_ctx, &t->write_action, GRPC_ERROR_NONE);
+ break;
}
GPR_TIMER_END("write_action_begin_locked", 0);
}
@@ -2130,27 +2139,29 @@ static void end_all_the_calls(grpc_exec_ctx *exec_ctx, grpc_chttp2_transport *t,
static void update_bdp(grpc_exec_ctx *exec_ctx, grpc_chttp2_transport *t,
double bdp_dbl) {
- uint32_t bdp;
- if (bdp_dbl <= 0) {
- bdp = 0;
- } else if (bdp_dbl > UINT32_MAX) {
- bdp = UINT32_MAX;
+ int32_t bdp;
+ const int32_t kMinBDP = 128;
+ if (bdp_dbl <= kMinBDP) {
+ bdp = kMinBDP;
+ } else if (bdp_dbl > INT32_MAX) {
+ bdp = INT32_MAX;
} else {
- bdp = (uint32_t)(bdp_dbl);
+ bdp = (int32_t)(bdp_dbl);
}
int64_t delta =
(int64_t)bdp -
(int64_t)t->settings[GRPC_LOCAL_SETTINGS]
[GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE];
- if (delta == 0 || (bdp != 0 && delta > -1024 && delta < 1024)) {
+ if (delta == 0 || (delta > -bdp / 10 && delta < bdp / 10)) {
return;
}
if (grpc_bdp_estimator_trace) {
gpr_log(GPR_DEBUG, "%s: update initial window size to %d", t->peer_string,
(int)bdp);
}
- push_setting(exec_ctx, t, GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE, bdp);
- push_setting(exec_ctx, t, GRPC_CHTTP2_SETTINGS_MAX_FRAME_SIZE, bdp);
+ push_setting(exec_ctx, t, GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE,
+ (uint32_t)bdp);
+ push_setting(exec_ctx, t, GRPC_CHTTP2_SETTINGS_MAX_FRAME_SIZE, (uint32_t)bdp);
}
static grpc_error *try_http_parsing(grpc_exec_ctx *exec_ctx,
diff --git a/src/core/ext/transport/chttp2/transport/internal.h b/src/core/ext/transport/chttp2/transport/internal.h
index 0aaa4aebe5..733e9900ef 100644
--- a/src/core/ext/transport/chttp2/transport/internal.h
+++ b/src/core/ext/transport/chttp2/transport/internal.h
@@ -552,9 +552,14 @@ void grpc_chttp2_initiate_write(grpc_exec_ctx *exec_ctx,
grpc_chttp2_transport *t,
bool covered_by_poller, const char *reason);
-/** Someone is unlocking the transport mutex: check to see if writes
- are required, and frame them if so */
-bool grpc_chttp2_begin_write(grpc_exec_ctx *exec_ctx, grpc_chttp2_transport *t);
+typedef enum {
+ GRPC_CHTTP2_NOTHING_TO_WRITE,
+ GRPC_CHTTP2_PARTIAL_WRITE,
+ GRPC_CHTTP2_FULL_WRITE,
+} grpc_chttp2_begin_write_result;
+
+grpc_chttp2_begin_write_result grpc_chttp2_begin_write(
+ grpc_exec_ctx *exec_ctx, grpc_chttp2_transport *t);
void grpc_chttp2_end_write(grpc_exec_ctx *exec_ctx, grpc_chttp2_transport *t,
grpc_error *error);
diff --git a/src/core/ext/transport/chttp2/transport/parsing.c b/src/core/ext/transport/chttp2/transport/parsing.c
index 638b137316..b0dd685116 100644
--- a/src/core/ext/transport/chttp2/transport/parsing.c
+++ b/src/core/ext/transport/chttp2/transport/parsing.c
@@ -418,11 +418,9 @@ static grpc_error *update_incoming_window(grpc_exec_ctx *exec_ctx,
GRPC_CHTTP2_FLOW_DEBIT_STREAM_INCOMING_WINDOW_DELTA("parse", t, s,
incoming_frame_size);
- if ((int64_t)t->settings[GRPC_SENT_SETTINGS]
- [GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE] +
- (int64_t)s->incoming_window_delta - (int64_t)s->announce_window <=
- (int64_t)t->settings[GRPC_SENT_SETTINGS]
- [GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE] /
+ if ((int64_t)s->incoming_window_delta - (int64_t)s->announce_window <=
+ -(int64_t)t->settings[GRPC_SENT_SETTINGS]
+ [GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE] /
2) {
grpc_chttp2_become_writable(exec_ctx, t, s,
GRPC_CHTTP2_STREAM_WRITE_INITIATE_UNCOVERED,
diff --git a/src/core/ext/transport/chttp2/transport/writing.c b/src/core/ext/transport/chttp2/transport/writing.c
index 069780ae5a..60db567fb3 100644
--- a/src/core/ext/transport/chttp2/transport/writing.c
+++ b/src/core/ext/transport/chttp2/transport/writing.c
@@ -160,19 +160,22 @@ static bool stream_ref_if_not_destroyed(gpr_refcount *r) {
return true;
}
+/* How many bytes of incoming flow control would we like to advertise */
uint32_t grpc_chttp2_target_incoming_window(grpc_chttp2_transport *t) {
- return (uint32_t)GPR_MAX(
+ return (uint32_t)GPR_MIN(
(int64_t)((1u << 31) - 1),
t->stream_total_over_incoming_window +
- (int64_t)GPR_MAX(
- t->settings[GRPC_SENT_SETTINGS]
- [GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE] -
- t->stream_total_under_incoming_window,
- 0));
+ t->settings[GRPC_SENT_SETTINGS]
+ [GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE]);
}
-bool grpc_chttp2_begin_write(grpc_exec_ctx *exec_ctx,
- grpc_chttp2_transport *t) {
+/* How many bytes would we like to put on the wire during a single syscall */
+static uint32_t target_write_size(grpc_chttp2_transport *t) {
+ return 1024 * 1024;
+}
+
+grpc_chttp2_begin_write_result grpc_chttp2_begin_write(
+ grpc_exec_ctx *exec_ctx, grpc_chttp2_transport *t) {
grpc_chttp2_stream *s;
GPR_TIMER_BEGIN("grpc_chttp2_begin_write", 0);
@@ -206,9 +209,20 @@ bool grpc_chttp2_begin_write(grpc_exec_ctx *exec_ctx,
}
}
+ bool partial_write = false;
+
/* for each grpc_chttp2_stream that's become writable, frame it's data
(according to available window sizes) and add to the output buffer */
- while (grpc_chttp2_list_pop_writable_stream(t, &s)) {
+ while (true) {
+ if (t->outbuf.length > target_write_size(t)) {
+ partial_write = true;
+ break;
+ }
+
+ if (!grpc_chttp2_list_pop_writable_stream(t, &s)) {
+ break;
+ }
+
bool sent_initial_metadata = s->sent_initial_metadata;
bool now_writing = false;
@@ -395,7 +409,9 @@ bool grpc_chttp2_begin_write(grpc_exec_ctx *exec_ctx,
GPR_TIMER_END("grpc_chttp2_begin_write", 0);
- return t->outbuf.count > 0;
+ return t->outbuf.count > 0 ? (partial_write ? GRPC_CHTTP2_PARTIAL_WRITE
+ : GRPC_CHTTP2_FULL_WRITE)
+ : GRPC_CHTTP2_NOTHING_TO_WRITE;
}
void grpc_chttp2_end_write(grpc_exec_ctx *exec_ctx, grpc_chttp2_transport *t,
diff --git a/src/core/ext/transport/cronet/transport/cronet_transport.c b/src/core/ext/transport/cronet/transport/cronet_transport.c
index d4e89d6a6c..67974b0b6a 100644
--- a/src/core/ext/transport/cronet/transport/cronet_transport.c
+++ b/src/core/ext/transport/cronet/transport/cronet_transport.c
@@ -886,6 +886,10 @@ static bool op_can_be_run(grpc_transport_stream_op_batch *curr_op,
!stream_state->state_op_done[OP_RECV_MESSAGE]) {
CRONET_LOG(GPR_DEBUG, "Because");
result = false;
+ } else if (curr_op->cancel_stream &&
+ !stream_state->state_callback_received[OP_CANCELED]) {
+ CRONET_LOG(GPR_DEBUG, "Because");
+ result = false;
} else if (curr_op->recv_trailing_metadata) {
/* We aren't done with trailing metadata yet */
if (!stream_state->state_op_done[OP_RECV_TRAILING_METADATA]) {
diff --git a/src/core/lib/iomgr/port.h b/src/core/lib/iomgr/port.h
index 269dc35003..2a553f4114 100644
--- a/src/core/lib/iomgr/port.h
+++ b/src/core/lib/iomgr/port.h
@@ -88,6 +88,7 @@
#ifndef __GLIBC__
#define GRPC_LINUX_EPOLL 1
#define GRPC_LINUX_EVENTFD 1
+#define GRPC_MSG_IOVLEN_TYPE int
#endif
#ifndef GRPC_LINUX_EVENTFD
#define GRPC_POSIX_NO_SPECIAL_WAKEUP_FD 1
diff --git a/src/core/lib/support/cpu_linux.c b/src/core/lib/support/cpu_linux.c
index 1e50f59823..b826dde160 100644
--- a/src/core/lib/support/cpu_linux.c
+++ b/src/core/lib/support/cpu_linux.c
@@ -67,16 +67,16 @@ unsigned gpr_cpu_num_cores(void) {
}
unsigned gpr_cpu_current_cpu(void) {
-#ifdef __GLIBC__
+#ifdef GPR_MUSL_LIBC_COMPAT
+ // sched_getcpu() is undefined on musl
+ return 0;
+#else
int cpu = sched_getcpu();
if (cpu < 0) {
gpr_log(GPR_ERROR, "Error determining current CPU: %s\n", strerror(errno));
return 0;
}
return (unsigned)cpu;
-#else
- // sched_getcpu() is undefined on musl
- return 0;
#endif
}
diff --git a/src/core/lib/support/wrap_memcpy.c b/src/core/lib/support/wrap_memcpy.c
index 050cc6db5e..deb8d6b198 100644
--- a/src/core/lib/support/wrap_memcpy.c
+++ b/src/core/lib/support/wrap_memcpy.c
@@ -31,6 +31,8 @@
*
*/
+#include <grpc/support/port_platform.h>
+
#include <string.h>
/* Provide a wrapped memcpy for targets that need to be backwards
@@ -40,7 +42,7 @@
*/
#ifdef __linux__
-#if defined(__x86_64__) && defined(__GNU_LIBRARY__)
+#if defined(__x86_64__) && !defined(GPR_MUSL_LIBC_COMPAT)
__asm__(".symver memcpy,memcpy@GLIBC_2.2.5");
void *__wrap_memcpy(void *destination, const void *source, size_t num) {
return memcpy(destination, source, num);
diff --git a/src/core/lib/transport/bdp_estimator.c b/src/core/lib/transport/bdp_estimator.c
index e1483677fd..536694214e 100644
--- a/src/core/lib/transport/bdp_estimator.c
+++ b/src/core/lib/transport/bdp_estimator.c
@@ -44,6 +44,7 @@ void grpc_bdp_estimator_init(grpc_bdp_estimator *estimator, const char *name) {
estimator->estimate = 65536;
estimator->ping_state = GRPC_BDP_PING_UNSCHEDULED;
estimator->name = name;
+ estimator->bw_est = 0;
}
bool grpc_bdp_estimator_get_estimate(grpc_bdp_estimator *estimator,
@@ -84,16 +85,26 @@ void grpc_bdp_estimator_start_ping(grpc_bdp_estimator *estimator) {
GPR_ASSERT(estimator->ping_state == GRPC_BDP_PING_SCHEDULED);
estimator->ping_state = GRPC_BDP_PING_STARTED;
estimator->accumulator = 0;
+ estimator->ping_start_time = gpr_now(GPR_CLOCK_MONOTONIC);
}
void grpc_bdp_estimator_complete_ping(grpc_bdp_estimator *estimator) {
+ gpr_timespec dt_ts =
+ gpr_time_sub(gpr_now(GPR_CLOCK_MONOTONIC), estimator->ping_start_time);
+ double dt = (double)dt_ts.tv_sec + 1e-9 * (double)dt_ts.tv_nsec;
+ double bw = dt > 0 ? ((double)estimator->accumulator / dt) : 0;
if (grpc_bdp_estimator_trace) {
- gpr_log(GPR_DEBUG, "bdp[%s]:complete acc=%" PRId64 " est=%" PRId64,
- estimator->name, estimator->accumulator, estimator->estimate);
+ gpr_log(GPR_DEBUG, "bdp[%s]:complete acc=%" PRId64 " est=%" PRId64
+ " dt=%lf bw=%lfMbs bw_est=%lfMbs",
+ estimator->name, estimator->accumulator, estimator->estimate, dt,
+ bw / 125000.0, estimator->bw_est / 125000.0);
}
GPR_ASSERT(estimator->ping_state == GRPC_BDP_PING_STARTED);
- if (estimator->accumulator > 2 * estimator->estimate / 3) {
- estimator->estimate *= 2;
+ if (estimator->accumulator > 2 * estimator->estimate / 3 &&
+ bw > estimator->bw_est) {
+ estimator->estimate =
+ GPR_MAX(estimator->accumulator, estimator->estimate * 2);
+ estimator->bw_est = bw;
if (grpc_bdp_estimator_trace) {
gpr_log(GPR_DEBUG, "bdp[%s]: estimate increased to %" PRId64,
estimator->name, estimator->estimate);
diff --git a/src/core/lib/transport/bdp_estimator.h b/src/core/lib/transport/bdp_estimator.h
index df8d1f6fc0..752bee1abd 100644
--- a/src/core/lib/transport/bdp_estimator.h
+++ b/src/core/lib/transport/bdp_estimator.h
@@ -34,6 +34,7 @@
#ifndef GRPC_CORE_LIB_TRANSPORT_BDP_ESTIMATOR_H
#define GRPC_CORE_LIB_TRANSPORT_BDP_ESTIMATOR_H
+#include <grpc/support/time.h>
#include <stdbool.h>
#include <stdint.h>
@@ -52,6 +53,8 @@ typedef struct grpc_bdp_estimator {
grpc_bdp_estimator_ping_state ping_state;
int64_t accumulator;
int64_t estimate;
+ gpr_timespec ping_start_time;
+ double bw_est;
const char *name;
} grpc_bdp_estimator;
diff --git a/src/cpp/common/core_codegen.cc b/src/cpp/common/core_codegen.cc
index f679a33945..2b484c0a08 100644
--- a/src/cpp/common/core_codegen.cc
+++ b/src/cpp/common/core_codegen.cc
@@ -134,6 +134,12 @@ grpc_byte_buffer* CoreCodegen::grpc_raw_byte_buffer_create(grpc_slice* slice,
return ::grpc_raw_byte_buffer_create(slice, nslices);
}
+grpc_slice CoreCodegen::grpc_slice_new_with_user_data(void* p, size_t len,
+ void (*destroy)(void*),
+ void* user_data) {
+ return ::grpc_slice_new_with_user_data(p, len, destroy, user_data);
+}
+
grpc_slice CoreCodegen::grpc_empty_slice() { return ::grpc_empty_slice(); }
grpc_slice CoreCodegen::grpc_slice_malloc(size_t length) {
@@ -144,10 +150,18 @@ void CoreCodegen::grpc_slice_unref(grpc_slice slice) {
::grpc_slice_unref(slice);
}
+grpc_slice CoreCodegen::grpc_slice_ref(grpc_slice slice) {
+ return ::grpc_slice_ref(slice);
+}
+
grpc_slice CoreCodegen::grpc_slice_split_tail(grpc_slice* s, size_t split) {
return ::grpc_slice_split_tail(s, split);
}
+grpc_slice CoreCodegen::grpc_slice_split_head(grpc_slice* s, size_t split) {
+ return ::grpc_slice_split_head(s, split);
+}
+
grpc_slice CoreCodegen::grpc_slice_from_static_buffer(const void* buffer,
size_t length) {
return ::grpc_slice_from_static_buffer(buffer, length);
diff --git a/src/node/ext/call.cc b/src/node/ext/call.cc
index fe0c80e642..49179ab359 100644
--- a/src/node/ext/call.cc
+++ b/src/node/ext/call.cc
@@ -42,7 +42,6 @@
#include "call_credentials.h"
#include "channel.h"
#include "completion_queue.h"
-#include "completion_queue_async_worker.h"
#include "grpc/grpc.h"
#include "grpc/grpc_security.h"
#include "grpc/support/alloc.h"
diff --git a/src/node/ext/channel.cc b/src/node/ext/channel.cc
index be04cf421d..eb6bc0f53f 100644
--- a/src/node/ext/channel.cc
+++ b/src/node/ext/channel.cc
@@ -41,7 +41,6 @@
#include "channel.h"
#include "channel_credentials.h"
#include "completion_queue.h"
-#include "completion_queue_async_worker.h"
#include "grpc/grpc.h"
#include "grpc/grpc_security.h"
#include "timeval.h"
diff --git a/src/node/ext/completion_queue_uv.cc b/src/node/ext/completion_queue.cc
index 9b60911d1e..d01abad757 100644
--- a/src/node/ext/completion_queue_uv.cc
+++ b/src/node/ext/completion_queue.cc
@@ -31,8 +31,6 @@
*
*/
-#ifdef GRPC_UV
-
#include <grpc/grpc.h>
#include <node.h>
#include <uv.h>
@@ -95,5 +93,3 @@ void CompletionQueueInit(Local<Object> exports) {
} // namespace node
} // namespace grpc
-
-#endif /* GRPC_UV */
diff --git a/src/node/ext/completion_queue_async_worker.h b/src/node/ext/completion_queue_async_worker.h
deleted file mode 100644
index 6e54116765..0000000000
--- a/src/node/ext/completion_queue_async_worker.h
+++ /dev/null
@@ -1,86 +0,0 @@
-/*
- *
- * 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.
- *
- */
-
-#ifndef NET_GRPC_NODE_COMPLETION_QUEUE_ASYNC_WORKER_H_
-#define NET_GRPC_NODE_COMPLETION_QUEUE_ASYNC_WORKER_H_
-#include <nan.h>
-
-#include "grpc/grpc.h"
-
-namespace grpc {
-namespace node {
-
-/* A worker that asynchronously calls completion_queue_next, and queues onto the
- node event loop a call to the function stored in the event's tag. */
-class CompletionQueueAsyncWorker : public Nan::AsyncWorker {
- public:
- CompletionQueueAsyncWorker();
-
- ~CompletionQueueAsyncWorker();
- /* Calls completion_queue_next with the provided deadline, and stores the
- event if there was one or sets an error message if there was not */
- void Execute();
-
- /* Returns the completion queue attached to this class */
- static grpc_completion_queue *GetQueue();
-
- /* Convenience function to create a worker with the given arguments and queue
- it to run asynchronously */
- static void Next();
-
- /* Initialize the CompletionQueueAsyncWorker class */
- static void Init(v8::Local<v8::Object> exports);
-
- protected:
- /* Called when Execute has succeeded (completed without setting an error
- message). Calls the saved callback with the event that came from
- completion_queue_next */
- void HandleOKCallback();
-
- void HandleErrorCallback();
-
- private:
- grpc_event result;
-
- static grpc_completion_queue *queue;
-
- // Number of grpc_completion_queue_next calls in the thread pool
- static int current_threads;
- // Number of grpc_completion_queue_next calls waiting to enter the thread pool
- static int waiting_next_calls;
-};
-
-} // namespace node
-} // namespace grpc
-
-#endif // NET_GRPC_NODE_COMPLETION_QUEUE_ASYNC_WORKER_H_
diff --git a/src/node/ext/completion_queue_threadpool.cc b/src/node/ext/completion_queue_threadpool.cc
deleted file mode 100644
index 72df5d1d65..0000000000
--- a/src/node/ext/completion_queue_threadpool.cc
+++ /dev/null
@@ -1,180 +0,0 @@
-/*
- *
- * 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.
- *
- */
-
-/* I don't like using #ifndef, but I don't see a better way to do this */
-#ifndef GRPC_UV
-
-#include <nan.h>
-#include <node.h>
-
-#include "call.h"
-#include "completion_queue.h"
-#include "grpc/grpc.h"
-#include "grpc/support/log.h"
-#include "grpc/support/time.h"
-
-namespace grpc {
-namespace node {
-
-namespace {
-
-/* A worker that asynchronously calls completion_queue_next, and queues onto the
- node event loop a call to the function stored in the event's tag. */
-class CompletionQueueAsyncWorker : public Nan::AsyncWorker {
- public:
- CompletionQueueAsyncWorker();
-
- ~CompletionQueueAsyncWorker();
- /* Calls completion_queue_next with the provided deadline, and stores the
- event if there was one or sets an error message if there was not */
- void Execute();
-
- /* Returns the completion queue attached to this class */
- static grpc_completion_queue *GetQueue();
-
- /* Convenience function to create a worker with the given arguments and queue
- it to run asynchronously */
- static void Next();
-
- /* Initialize the CompletionQueueAsyncWorker class */
- static void Init(v8::Local<v8::Object> exports);
-
- protected:
- /* Called when Execute has succeeded (completed without setting an error
- message). Calls the saved callback with the event that came from
- completion_queue_next */
- void HandleOKCallback();
-
- void HandleErrorCallback();
-
- private:
- static void TryAddWorker();
-
- grpc_event result;
-
- static grpc_completion_queue *queue;
-
- // Number of grpc_completion_queue_next calls in the thread pool
- static int current_threads;
- // Number of grpc_completion_queue_next calls waiting to enter the thread pool
- static int waiting_next_calls;
-};
-
-const int max_queue_threads = 2;
-
-using v8::Function;
-using v8::Local;
-using v8::Object;
-using v8::Value;
-
-grpc_completion_queue *CompletionQueueAsyncWorker::queue;
-
-// Invariants: current_threads <= max_queue_threads
-// (current_threads == max_queue_threads) || (waiting_next_calls == 0)
-
-int CompletionQueueAsyncWorker::current_threads;
-int CompletionQueueAsyncWorker::waiting_next_calls;
-
-CompletionQueueAsyncWorker::CompletionQueueAsyncWorker()
- : Nan::AsyncWorker(NULL) {}
-
-CompletionQueueAsyncWorker::~CompletionQueueAsyncWorker() {}
-
-void CompletionQueueAsyncWorker::Execute() {
- result = grpc_completion_queue_next(queue, gpr_inf_future(GPR_CLOCK_REALTIME),
- NULL);
- if (!result.success) {
- SetErrorMessage("The async function encountered an error");
- }
-}
-
-grpc_completion_queue *CompletionQueueAsyncWorker::GetQueue() { return queue; }
-
-void CompletionQueueAsyncWorker::TryAddWorker() {
- if (current_threads < max_queue_threads && waiting_next_calls > 0) {
- current_threads += 1;
- waiting_next_calls -= 1;
- CompletionQueueAsyncWorker *worker = new CompletionQueueAsyncWorker();
- Nan::AsyncQueueWorker(worker);
- }
- GPR_ASSERT(current_threads <= max_queue_threads);
- GPR_ASSERT((current_threads == max_queue_threads) ||
- (waiting_next_calls == 0));
-}
-
-void CompletionQueueAsyncWorker::Next() {
- waiting_next_calls += 1;
- TryAddWorker();
-}
-
-void CompletionQueueAsyncWorker::Init(Local<Object> exports) {
- Nan::HandleScope scope;
- current_threads = 0;
- waiting_next_calls = 0;
- queue = grpc_completion_queue_create_for_next(NULL);
-}
-
-void CompletionQueueAsyncWorker::HandleOKCallback() {
- Nan::HandleScope scope;
- current_threads -= 1;
- TryAddWorker();
- CompleteTag(result.tag, NULL);
-
- DestroyTag(result.tag);
-}
-
-void CompletionQueueAsyncWorker::HandleErrorCallback() {
- Nan::HandleScope scope;
- current_threads -= 1;
- TryAddWorker();
- CompleteTag(result.tag, ErrorMessage());
-
- DestroyTag(result.tag);
-}
-
-} // namespace
-
-grpc_completion_queue *GetCompletionQueue() {
- return CompletionQueueAsyncWorker::GetQueue();
-}
-
-void CompletionQueueNext() { CompletionQueueAsyncWorker::Next(); }
-
-void CompletionQueueInit(Local<Object> exports) {
- CompletionQueueAsyncWorker::Init(exports);
-}
-
-} // namespace node
-} // namespace grpc
-
-#endif /* GRPC_UV */
diff --git a/src/node/ext/node_grpc.cc b/src/node/ext/node_grpc.cc
index 076f1ed424..e193e82179 100644
--- a/src/node/ext/node_grpc.cc
+++ b/src/node/ext/node_grpc.cc
@@ -43,18 +43,15 @@
#include "grpc/support/time.h"
// TODO(murgatroid99): Remove this when the endpoint API becomes public
-#ifdef GRPC_UV
extern "C" {
#include "src/core/lib/iomgr/pollset_uv.h"
}
-#endif
#include "call.h"
#include "call_credentials.h"
#include "channel.h"
#include "channel_credentials.h"
#include "completion_queue.h"
-#include "completion_queue_async_worker.h"
#include "server.h"
#include "server_credentials.h"
#include "slice.h"
@@ -432,9 +429,7 @@ void init(Local<Object> exports) {
InitWriteFlags(exports);
InitLogConstants(exports);
-#ifdef GRPC_UV
grpc_pollset_work_run_loop = 0;
-#endif
grpc::node::Call::Init(exports);
grpc::node::CallCredentials::Init(exports);
diff --git a/src/node/ext/server.cc b/src/node/ext/server.cc
index 1871a32452..a885a9f268 100644
--- a/src/node/ext/server.cc
+++ b/src/node/ext/server.cc
@@ -41,7 +41,6 @@
#include <vector>
#include "call.h"
#include "completion_queue.h"
-#include "completion_queue_async_worker.h"
#include "grpc/grpc.h"
#include "grpc/grpc_security.h"
#include "grpc/support/log.h"
@@ -78,6 +77,30 @@ using v8::Value;
Nan::Callback *Server::constructor;
Persistent<FunctionTemplate> Server::fun_tpl;
+static Callback *shutdown_callback = NULL;
+
+class ServerShutdownOp : public Op {
+ public:
+ ServerShutdownOp(grpc_server *server) : server(server) {}
+
+ ~ServerShutdownOp() {}
+
+ Local<Value> GetNodeValue() const { return Nan::Null(); }
+
+ bool ParseOp(Local<Value> value, grpc_op *out) { return true; }
+ bool IsFinalOp() { return false; }
+ void OnComplete(bool success) {
+ /* Because cancel_all_calls was called, we assume that shutdown_and_notify
+ completes successfully */
+ grpc_server_destroy(server);
+ }
+
+ grpc_server *server;
+
+ protected:
+ std::string GetTypeString() const { return "shutdown"; }
+};
+
class NewCallOp : public Op {
public:
NewCallOp() {
@@ -149,6 +172,10 @@ class TryShutdownOp : public Op {
server_persist;
};
+Server::Server(grpc_server *server) : wrapped_server(server) {}
+
+Server::~Server() { this->ShutdownServer(); }
+
void Server::Init(Local<Object> exports) {
HandleScope scope;
Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
@@ -177,6 +204,36 @@ void Server::DestroyWrappedServer() {
}
}
+NAN_METHOD(ServerShutdownCallback) {
+ if (!info[0]->IsNull()) {
+ return Nan::ThrowError("forceShutdown failed somehow");
+ }
+}
+
+void Server::ShutdownServer() {
+ Nan::HandleScope scope;
+ if (this->wrapped_server != NULL) {
+ if (shutdown_callback == NULL) {
+ Local<FunctionTemplate> callback_tpl =
+ Nan::New<FunctionTemplate>(ServerShutdownCallback);
+ shutdown_callback =
+ new Callback(Nan::GetFunction(callback_tpl).ToLocalChecked());
+ }
+
+ ServerShutdownOp *op = new ServerShutdownOp(this->wrapped_server);
+ unique_ptr<OpVec> ops(new OpVec());
+ ops->push_back(unique_ptr<Op>(op));
+
+ grpc_server_shutdown_and_notify(
+ this->wrapped_server, GetCompletionQueue(),
+ new struct tag(new Callback(**shutdown_callback), ops.release(), NULL,
+ Nan::Null()));
+ grpc_server_cancel_all_calls(this->wrapped_server);
+ CompletionQueueNext();
+ this->wrapped_server = NULL;
+ }
+}
+
NAN_METHOD(Server::New) {
/* If this is not a constructor call, make a constructor call and return
the result */
diff --git a/src/node/ext/server_generic.cc b/src/node/ext/server_generic.cc
deleted file mode 100644
index 088273d527..0000000000
--- a/src/node/ext/server_generic.cc
+++ /dev/null
@@ -1,75 +0,0 @@
-/*
- *
- * 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_UV
-
-#include "server.h"
-
-#include <nan.h>
-#include <node.h>
-#include "grpc/grpc.h"
-#include "grpc/support/time.h"
-
-namespace grpc {
-namespace node {
-
-Server::Server(grpc_server *server) : wrapped_server(server) {
- grpc_completion_queue_attributes attrs = {
- GRPC_CQ_CURRENT_VERSION, GRPC_CQ_PLUCK, GRPC_CQ_NON_LISTENING};
- shutdown_queue = grpc_completion_queue_create(
- grpc_completion_queue_factory_lookup(&attrs), &attrs, NULL);
- grpc_server_register_completion_queue(server, shutdown_queue, NULL);
-}
-
-Server::~Server() {
- this->ShutdownServer();
- grpc_completion_queue_shutdown(this->shutdown_queue);
- grpc_completion_queue_destroy(this->shutdown_queue);
-}
-
-void Server::ShutdownServer() {
- if (this->wrapped_server != NULL) {
- grpc_server_shutdown_and_notify(this->wrapped_server, this->shutdown_queue,
- NULL);
- grpc_server_cancel_all_calls(this->wrapped_server);
- grpc_completion_queue_pluck(this->shutdown_queue, NULL,
- gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
- grpc_server_destroy(this->wrapped_server);
- this->wrapped_server = NULL;
- }
-}
-
-} // namespace grpc
-} // namespace node
-
-#endif /* GRPC_UV */
diff --git a/src/node/ext/server_uv.cc b/src/node/ext/server_uv.cc
deleted file mode 100644
index 709921b7fc..0000000000
--- a/src/node/ext/server_uv.cc
+++ /dev/null
@@ -1,120 +0,0 @@
-/*
- *
- * 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.
- *
- */
-
-#ifdef GRPC_UV
-
-#include "server.h"
-
-#include <nan.h>
-#include <node.h>
-#include "grpc/grpc.h"
-#include "grpc/support/time.h"
-
-#include "call.h"
-#include "completion_queue.h"
-
-namespace grpc {
-namespace node {
-
-using Nan::Callback;
-using Nan::MaybeLocal;
-
-using v8::External;
-using v8::Function;
-using v8::FunctionTemplate;
-using v8::Local;
-using v8::Object;
-using v8::Value;
-
-static Callback *shutdown_callback = NULL;
-
-class ServerShutdownOp : public Op {
- public:
- ServerShutdownOp(grpc_server *server) : server(server) {}
-
- ~ServerShutdownOp() {}
-
- Local<Value> GetNodeValue() const { return Nan::Null(); }
-
- bool ParseOp(Local<Value> value, grpc_op *out) { return true; }
- bool IsFinalOp() { return false; }
- void OnComplete(bool success) {
- /* Because cancel_all_calls was called, we assume that shutdown_and_notify
- completes successfully */
- grpc_server_destroy(server);
- }
-
- grpc_server *server;
-
- protected:
- std::string GetTypeString() const { return "shutdown"; }
-};
-
-Server::Server(grpc_server *server) : wrapped_server(server) {}
-
-Server::~Server() { this->ShutdownServer(); }
-
-NAN_METHOD(ServerShutdownCallback) {
- if (!info[0]->IsNull()) {
- return Nan::ThrowError("forceShutdown failed somehow");
- }
-}
-
-void Server::ShutdownServer() {
- Nan::HandleScope scope;
- if (this->wrapped_server != NULL) {
- if (shutdown_callback == NULL) {
- Local<FunctionTemplate> callback_tpl =
- Nan::New<FunctionTemplate>(ServerShutdownCallback);
- shutdown_callback =
- new Callback(Nan::GetFunction(callback_tpl).ToLocalChecked());
- }
-
- ServerShutdownOp *op = new ServerShutdownOp(this->wrapped_server);
- unique_ptr<OpVec> ops(new OpVec());
- ops->push_back(unique_ptr<Op>(op));
-
- grpc_server_shutdown_and_notify(
- this->wrapped_server, GetCompletionQueue(),
- new struct tag(new Callback(**shutdown_callback), ops.release(), NULL,
- Nan::Null()));
- grpc_server_cancel_all_calls(this->wrapped_server);
- CompletionQueueNext();
- this->wrapped_server = NULL;
- }
-}
-
-} // namespace grpc
-} // namespace node
-
-#endif /* GRPC_UV */
diff --git a/src/node/index.js b/src/node/index.js
index 071bfd7927..76ab1744b0 100644
--- a/src/node/index.js
+++ b/src/node/index.js
@@ -31,6 +31,10 @@
*
*/
+/**
+ * @module
+ */
+
'use strict';
var path = require('path');
@@ -256,5 +260,10 @@ exports.getClientChannel = client.getClientChannel;
exports.waitForClientReady = client.waitForClientReady;
exports.closeClient = function closeClient(client_obj) {
- client.getClientChannel(client_obj).close();
+ client.Client.prototype.close.apply(client_obj);
};
+
+/**
+ * @see module:src/client.Client
+ */
+exports.Client = client.Client;
diff --git a/src/node/src/client.js b/src/node/src/client.js
index 1aaf35c16c..43502da6af 100644
--- a/src/node/src/client.js
+++ b/src/node/src/client.js
@@ -37,7 +37,7 @@
* This module contains the factory method for creating Client classes, and the
* method calling code for all types of methods.
*
- * For example, to create a client and call a method on it:
+ * @example <caption>Create a client and call a method on it</caption>
*
* var proto_obj = grpc.load(proto_file_path);
* var Client = proto_obj.package.subpackage.ServiceName;
@@ -68,14 +68,33 @@ var Duplex = stream.Duplex;
var util = require('util');
var version = require('../../../package.json').version;
+util.inherits(ClientUnaryCall, EventEmitter);
+
+/**
+ * An EventEmitter. Used for unary calls
+ * @constructor
+ * @extends external:EventEmitter
+ * @param {grpc.Call} call The call object associated with the request
+ */
+function ClientUnaryCall(call) {
+ EventEmitter.call(this);
+ this.call = call;
+}
+
util.inherits(ClientWritableStream, Writable);
/**
* A stream that the client can write to. Used for calls that are streaming from
* the client side.
* @constructor
+ * @extends external:Writable
+ * @borrows module:src/client~ClientUnaryCall#cancel as
+ * module:src/client~ClientWritableStream#cancel
+ * @borrows module:src/client~ClientUnaryCall#getPeer as
+ * module:src/client~ClientWritableStream#getPeer
* @param {grpc.Call} call The call object to send data with
- * @param {function(*):Buffer=} serialize Serialization function for writes.
+ * @param {module:src/common~serialize=} [serialize=identity] Serialization
+ * function for writes.
*/
function ClientWritableStream(call, serialize) {
Writable.call(this, {objectMode: true});
@@ -134,8 +153,14 @@ util.inherits(ClientReadableStream, Readable);
* A stream that the client can read from. Used for calls that are streaming
* from the server side.
* @constructor
+ * @extends external:Readable
+ * @borrows module:src/client~ClientUnaryCall#cancel as
+ * module:src/client~ClientReadableStream#cancel
+ * @borrows module:src/client~ClientUnaryCall#getPeer as
+ * module:src/client~ClientReadableStream#getPeer
* @param {grpc.Call} call The call object to read data with
- * @param {function(Buffer):*=} deserialize Deserialization function for reads
+ * @param {module:src/common~deserialize=} [deserialize=identity]
+ * Deserialization function for reads
*/
function ClientReadableStream(call, deserialize) {
Readable.call(this, {objectMode: true});
@@ -155,6 +180,7 @@ function ClientReadableStream(call, deserialize) {
* parameter indicates that the call should end with that status. status
* defaults to OK if not provided.
* @param {Object!} status The status that the call should end with
+ * @access private
*/
function _readsDone(status) {
/* jshint validthis: true */
@@ -173,6 +199,7 @@ ClientReadableStream.prototype._readsDone = _readsDone;
/**
* Called to indicate that we have received a status from the server.
+ * @access private
*/
function _receiveStatus(status) {
/* jshint validthis: true */
@@ -185,6 +212,7 @@ ClientReadableStream.prototype._receiveStatus = _receiveStatus;
/**
* If we have both processed all incoming messages and received the status from
* the server, emit the status. Otherwise, do nothing.
+ * @access private
*/
function _emitStatusIfDone() {
/* jshint validthis: true */
@@ -270,10 +298,16 @@ util.inherits(ClientDuplexStream, Duplex);
* A stream that the client can read from or write to. Used for calls with
* duplex streaming.
* @constructor
+ * @extends external:Duplex
+ * @borrows module:src/client~ClientUnaryCall#cancel as
+ * module:src/client~ClientDuplexStream#cancel
+ * @borrows module:src/client~ClientUnaryCall#getPeer as
+ * module:src/client~ClientDuplexStream#getPeer
* @param {grpc.Call} call Call object to proxy
- * @param {function(*):Buffer=} serialize Serialization function for requests
- * @param {function(Buffer):*=} deserialize Deserialization function for
- * responses
+ * @param {module:src/common~serialize=} [serialize=identity] Serialization
+ * function for requests
+ * @param {module:src/common~deserialize=} [deserialize=identity]
+ * Deserialization function for responses
*/
function ClientDuplexStream(call, serialize, deserialize) {
Duplex.call(this, {objectMode: true});
@@ -300,12 +334,14 @@ ClientDuplexStream.prototype._write = _write;
/**
* Cancel the ongoing call
+ * @alias module:src/client~ClientUnaryCall#cancel
*/
function cancel() {
/* jshint validthis: true */
this.call.cancel();
}
+ClientUnaryCall.prototype.cancel = cancel;
ClientReadableStream.prototype.cancel = cancel;
ClientWritableStream.prototype.cancel = cancel;
ClientDuplexStream.prototype.cancel = cancel;
@@ -313,21 +349,49 @@ ClientDuplexStream.prototype.cancel = cancel;
/**
* Get the endpoint this call/stream is connected to.
* @return {string} The URI of the endpoint
+ * @alias module:src/client~ClientUnaryCall#getPeer
*/
function getPeer() {
/* jshint validthis: true */
return this.call.getPeer();
}
+ClientUnaryCall.prototype.getPeer = getPeer;
ClientReadableStream.prototype.getPeer = getPeer;
ClientWritableStream.prototype.getPeer = getPeer;
ClientDuplexStream.prototype.getPeer = getPeer;
/**
+ * Any client call type
+ * @typedef {(ClientUnaryCall|ClientReadableStream|
+ * ClientWritableStream|ClientDuplexStream)}
+ * module:src/client~Call
+ */
+
+/**
+ * Options that can be set on a call.
+ * @typedef {Object} module:src/client~CallOptions
+ * @property {(date|number)} deadline The deadline for the entire call to
+ * complete. A value of Infinity indicates that no deadline should be set.
+ * @property {(string)} host Server hostname to set on the call. Only meaningful
+ * if different from the server address used to construct the client.
+ * @property {module:src/client~Call} parent Parent call. Used in servers when
+ * making a call as part of the process of handling a call. Used to
+ * propagate some information automatically, as specified by
+ * propagate_flags.
+ * @property {number} propagate_flags Indicates which properties of a parent
+ * call should propagate to this call. Bitwise combination of flags in
+ * [grpc.propagate]{@link module:index.propagate}.
+ * @property {module:src/credentials~CallCredentials} credentials The
+ * credentials that should be used to make this particular call.
+ */
+
+/**
* Get a call object built with the provided options. Keys for options are
* 'deadline', which takes a date or number, and 'host', which takes a string
* and overrides the hostname to connect to.
- * @param {Object} options Options map.
+ * @access private
+ * @param {module:src/client~CallOptions=} options Options object.
*/
function getCall(channel, method, options) {
var deadline;
@@ -354,315 +418,380 @@ function getCall(channel, method, options) {
}
/**
- * Get a function that can make unary requests to the specified method.
- * @param {string} method The name of the method to request
- * @param {function(*):Buffer} serialize The serialization function for inputs
- * @param {function(Buffer)} deserialize The deserialization function for
- * outputs
- * @return {Function} makeUnaryRequest
+ * A generic gRPC client. Primarily useful as a base class for generated clients
+ * @alias module:src/client.Client
+ * @constructor
+ * @param {string} address Server address to connect to
+ * @param {module:src/credentials~ChannelCredentials} credentials Credentials to
+ * use to connect to the server
+ * @param {Object} options Options to apply to channel creation
*/
-function makeUnaryRequestFunction(method, serialize, deserialize) {
- /**
- * Make a unary request with this method on the given channel with the given
- * argument, callback, etc.
- * @this {Client} Client object. Must have a channel member.
- * @param {*} argument The argument to the call. Should be serializable with
- * serialize
- * @param {Metadata=} metadata Metadata to add to the call
- * @param {Object=} options Options map
- * @param {function(?Error, value=)} callback The callback to for when the
- * response is received
- * @return {EventEmitter} An event emitter for stream related events
+var Client = exports.Client = function Client(address, credentials, options) {
+ if (!options) {
+ options = {};
+ }
+ /* Append the grpc-node user agent string after the application user agent
+ * string, and put the combination at the beginning of the user agent string
*/
- function makeUnaryRequest(argument, metadata, options, callback) {
- /* jshint validthis: true */
- /* While the arguments are listed in the function signature, those variables
- * are not used directly. Instead, ArgueJS processes the arguments
- * object. This allows for simple handling of optional arguments in the
- * middle of the argument list, and also provides type checking. */
- var args = arguejs({argument: null, metadata: [Metadata, new Metadata()],
- options: [Object], callback: Function}, arguments);
- var emitter = new EventEmitter();
- var call = getCall(this.$channel, method, args.options);
- metadata = args.metadata.clone();
- emitter.cancel = function cancel() {
- call.cancel();
- };
- emitter.getPeer = function getPeer() {
- return call.getPeer();
- };
- var client_batch = {};
- var message = serialize(args.argument);
- if (args.options) {
- message.grpcWriteFlags = args.options.flags;
- }
-
- client_batch[grpc.opType.SEND_INITIAL_METADATA] =
- metadata._getCoreRepresentation();
- client_batch[grpc.opType.SEND_MESSAGE] = message;
- client_batch[grpc.opType.SEND_CLOSE_FROM_CLIENT] = true;
- client_batch[grpc.opType.RECV_INITIAL_METADATA] = true;
- client_batch[grpc.opType.RECV_MESSAGE] = true;
- client_batch[grpc.opType.RECV_STATUS_ON_CLIENT] = true;
- call.startBatch(client_batch, function(err, response) {
- response.status.metadata = Metadata._fromCoreRepresentation(
- response.status.metadata);
- var status = response.status;
- var error;
- var deserialized;
- emitter.emit('metadata', Metadata._fromCoreRepresentation(
- response.metadata));
- if (status.code === grpc.status.OK) {
- if (err) {
- // Got a batch error, but OK status. Something went wrong
- args.callback(err);
- return;
- } else {
- try {
- deserialized = deserialize(response.read);
- } catch (e) {
- /* Change status to indicate bad server response. This will result
- * in passing an error to the callback */
- status = {
- code: grpc.status.INTERNAL,
- details: 'Failed to parse server response'
- };
- }
- }
- }
- if (status.code !== grpc.status.OK) {
- error = new Error(status.details);
- error.code = status.code;
- error.metadata = status.metadata;
- args.callback(error);
- } else {
- args.callback(null, deserialized);
- }
- emitter.emit('status', status);
- });
- return emitter;
+ if (options['grpc.primary_user_agent']) {
+ options['grpc.primary_user_agent'] += ' ';
+ } else {
+ options['grpc.primary_user_agent'] = '';
}
- return makeUnaryRequest;
-}
+ options['grpc.primary_user_agent'] += 'grpc-node/' + version;
+ /* Private fields use $ as a prefix instead of _ because it is an invalid
+ * prefix of a method name */
+ this.$channel = new grpc.Channel(address, credentials, options);
+};
/**
- * Get a function that can make client stream requests to the specified method.
+ * @typedef {Error} module:src/client.Client~ServiceError
+ * @property {number} code The error code, a key of
+ * [grpc.status]{@link module:src/client.status}
+ * @property {module:metadata.Metadata} metadata Metadata sent with the status
+ * by the server, if any
+ */
+
+/**
+ * @callback module:src/client.Client~requestCallback
+ * @param {?module:src/client.Client~ServiceError} error The error, if the call
+ * failed
+ * @param {*} value The response value, if the call succeeded
+ */
+
+/**
+ * Make a unary request to the given method, using the given serialize
+ * and deserialize functions, with the given argument.
* @param {string} method The name of the method to request
- * @param {function(*):Buffer} serialize The serialization function for inputs
- * @param {function(Buffer)} deserialize The deserialization function for
- * outputs
- * @return {Function} makeClientStreamRequest
+ * @param {module:src/common~serialize} serialize The serialization function for
+ * inputs
+ * @param {module:src/common~deserialize} deserialize The deserialization
+ * function for outputs
+ * @param {*} argument The argument to the call. Should be serializable with
+ * serialize
+ * @param {module:src/metadata.Metadata=} metadata Metadata to add to the call
+ * @param {module:src/client~CallOptions=} options Options map
+ * @param {module:src/client.Client~requestCallback} callback The callback to
+ * for when the response is received
+ * @return {EventEmitter} An event emitter for stream related events
*/
-function makeClientStreamRequestFunction(method, serialize, deserialize) {
- /**
- * Make a client stream request with this method on the given channel with the
- * given callback, etc.
- * @this {Client} Client object. Must have a channel member.
- * @param {Metadata=} metadata Array of metadata key/value pairs to add to the
- * call
- * @param {Object=} options Options map
- * @param {function(?Error, value=)} callback The callback to for when the
- * response is received
- * @return {EventEmitter} An event emitter for stream related events
- */
- function makeClientStreamRequest(metadata, options, callback) {
- /* jshint validthis: true */
- /* While the arguments are listed in the function signature, those variables
- * are not used directly. Instead, ArgueJS processes the arguments
- * object. This allows for simple handling of optional arguments in the
- * middle of the argument list, and also provides type checking. */
- var args = arguejs({metadata: [Metadata, new Metadata()],
- options: [Object], callback: Function}, arguments);
- var call = getCall(this.$channel, method, args.options);
- metadata = args.metadata.clone();
- var stream = new ClientWritableStream(call, serialize);
- var metadata_batch = {};
- metadata_batch[grpc.opType.SEND_INITIAL_METADATA] =
- metadata._getCoreRepresentation();
- metadata_batch[grpc.opType.RECV_INITIAL_METADATA] = true;
- call.startBatch(metadata_batch, function(err, response) {
+Client.prototype.makeUnaryRequest = function(method, serialize, deserialize,
+ argument, metadata, options,
+ callback) {
+ /* While the arguments are listed in the function signature, those variables
+ * are not used directly. Instead, ArgueJS processes the arguments
+ * object. This allows for simple handling of optional arguments in the
+ * middle of the argument list, and also provides type checking. */
+ var args = arguejs({method: String, serialize: Function,
+ deserialize: Function,
+ argument: null, metadata: [Metadata, new Metadata()],
+ options: [Object], callback: Function}, arguments);
+ var call = getCall(this.$channel, method, args.options);
+ var emitter = new ClientUnaryCall(call);
+ metadata = args.metadata.clone();
+ var client_batch = {};
+ var message = serialize(args.argument);
+ if (args.options) {
+ message.grpcWriteFlags = args.options.flags;
+ }
+
+ client_batch[grpc.opType.SEND_INITIAL_METADATA] =
+ metadata._getCoreRepresentation();
+ client_batch[grpc.opType.SEND_MESSAGE] = message;
+ client_batch[grpc.opType.SEND_CLOSE_FROM_CLIENT] = true;
+ client_batch[grpc.opType.RECV_INITIAL_METADATA] = true;
+ client_batch[grpc.opType.RECV_MESSAGE] = true;
+ client_batch[grpc.opType.RECV_STATUS_ON_CLIENT] = true;
+ call.startBatch(client_batch, function(err, response) {
+ response.status.metadata = Metadata._fromCoreRepresentation(
+ response.status.metadata);
+ var status = response.status;
+ var error;
+ var deserialized;
+ emitter.emit('metadata', Metadata._fromCoreRepresentation(
+ response.metadata));
+ if (status.code === grpc.status.OK) {
if (err) {
- // The call has stopped for some reason. A non-OK status will arrive
- // in the other batch.
+ // Got a batch error, but OK status. Something went wrong
+ args.callback(err);
return;
- }
- stream.emit('metadata', Metadata._fromCoreRepresentation(
- response.metadata));
- });
- var client_batch = {};
- client_batch[grpc.opType.RECV_MESSAGE] = true;
- client_batch[grpc.opType.RECV_STATUS_ON_CLIENT] = true;
- call.startBatch(client_batch, function(err, response) {
- response.status.metadata = Metadata._fromCoreRepresentation(
- response.status.metadata);
- var status = response.status;
- var error;
- var deserialized;
- if (status.code === grpc.status.OK) {
- if (err) {
- // Got a batch error, but OK status. Something went wrong
- args.callback(err);
- return;
- } else {
- try {
- deserialized = deserialize(response.read);
- } catch (e) {
- /* Change status to indicate bad server response. This will result
- * in passing an error to the callback */
- status = {
- code: grpc.status.INTERNAL,
- details: 'Failed to parse server response'
- };
- }
- }
- }
- if (status.code !== grpc.status.OK) {
- error = new Error(response.status.details);
- error.code = status.code;
- error.metadata = status.metadata;
- args.callback(error);
} else {
- args.callback(null, deserialized);
+ try {
+ deserialized = deserialize(response.read);
+ } catch (e) {
+ /* Change status to indicate bad server response. This will result
+ * in passing an error to the callback */
+ status = {
+ code: grpc.status.INTERNAL,
+ details: 'Failed to parse server response'
+ };
+ }
}
- stream.emit('status', status);
- });
- return stream;
- }
- return makeClientStreamRequest;
-}
+ }
+ if (status.code !== grpc.status.OK) {
+ error = new Error(status.details);
+ error.code = status.code;
+ error.metadata = status.metadata;
+ args.callback(error);
+ } else {
+ args.callback(null, deserialized);
+ }
+ emitter.emit('status', status);
+ });
+ return emitter;
+};
/**
- * Get a function that can make server stream requests to the specified method.
+ * Make a client stream request to the given method, using the given serialize
+ * and deserialize functions, with the given argument.
* @param {string} method The name of the method to request
- * @param {function(*):Buffer} serialize The serialization function for inputs
- * @param {function(Buffer)} deserialize The deserialization function for
- * outputs
- * @return {Function} makeServerStreamRequest
+ * @param {module:src/common~serialize} serialize The serialization function for
+ * inputs
+ * @param {module:src/common~deserialize} deserialize The deserialization
+ * function for outputs
+ * @param {module:src/metadata.Metadata=} metadata Array of metadata key/value
+ * pairs to add to the call
+ * @param {module:src/client~CallOptions=} options Options map
+ * @param {Client~requestCallback} callback The callback to for when the
+ * response is received
+ * @return {module:src/client~ClientWritableStream} An event emitter for stream
+ * related events
*/
-function makeServerStreamRequestFunction(method, serialize, deserialize) {
- /**
- * Make a server stream request with this method on the given channel with the
- * given argument, etc.
- * @this {SurfaceClient} Client object. Must have a channel member.
- * @param {*} argument The argument to the call. Should be serializable with
- * serialize
- * @param {Metadata=} metadata Array of metadata key/value pairs to add to the
- * call
- * @param {Object} options Options map
- * @return {EventEmitter} An event emitter for stream related events
- */
- function makeServerStreamRequest(argument, metadata, options) {
- /* jshint validthis: true */
- /* While the arguments are listed in the function signature, those variables
- * are not used directly. Instead, ArgueJS processes the arguments
- * object. */
- var args = arguejs({argument: null, metadata: [Metadata, new Metadata()],
- options: [Object]}, arguments);
- var call = getCall(this.$channel, method, args.options);
- metadata = args.metadata.clone();
- var stream = new ClientReadableStream(call, deserialize);
- var start_batch = {};
- var message = serialize(args.argument);
- if (args.options) {
- message.grpcWriteFlags = args.options.flags;
+Client.prototype.makeClientStreamRequest = function(method, serialize,
+ deserialize, metadata,
+ options, callback) {
+ /* While the arguments are listed in the function signature, those variables
+ * are not used directly. Instead, ArgueJS processes the arguments
+ * object. This allows for simple handling of optional arguments in the
+ * middle of the argument list, and also provides type checking. */
+ var args = arguejs({method:String, serialize: Function,
+ deserialize: Function,
+ metadata: [Metadata, new Metadata()],
+ options: [Object], callback: Function}, arguments);
+ var call = getCall(this.$channel, method, args.options);
+ metadata = args.metadata.clone();
+ var stream = new ClientWritableStream(call, serialize);
+ var metadata_batch = {};
+ metadata_batch[grpc.opType.SEND_INITIAL_METADATA] =
+ metadata._getCoreRepresentation();
+ metadata_batch[grpc.opType.RECV_INITIAL_METADATA] = true;
+ call.startBatch(metadata_batch, function(err, response) {
+ if (err) {
+ // The call has stopped for some reason. A non-OK status will arrive
+ // in the other batch.
+ return;
}
- start_batch[grpc.opType.SEND_INITIAL_METADATA] =
- metadata._getCoreRepresentation();
- start_batch[grpc.opType.RECV_INITIAL_METADATA] = true;
- start_batch[grpc.opType.SEND_MESSAGE] = message;
- start_batch[grpc.opType.SEND_CLOSE_FROM_CLIENT] = true;
- call.startBatch(start_batch, function(err, response) {
- if (err) {
- // The call has stopped for some reason. A non-OK status will arrive
- // in the other batch.
- return;
- }
- stream.emit('metadata', Metadata._fromCoreRepresentation(
- response.metadata));
- });
- var status_batch = {};
- status_batch[grpc.opType.RECV_STATUS_ON_CLIENT] = true;
- call.startBatch(status_batch, function(err, response) {
+ stream.emit('metadata', Metadata._fromCoreRepresentation(
+ response.metadata));
+ });
+ var client_batch = {};
+ client_batch[grpc.opType.RECV_MESSAGE] = true;
+ client_batch[grpc.opType.RECV_STATUS_ON_CLIENT] = true;
+ call.startBatch(client_batch, function(err, response) {
+ response.status.metadata = Metadata._fromCoreRepresentation(
+ response.status.metadata);
+ var status = response.status;
+ var error;
+ var deserialized;
+ if (status.code === grpc.status.OK) {
if (err) {
- stream.emit('error', err);
+ // Got a batch error, but OK status. Something went wrong
+ args.callback(err);
return;
+ } else {
+ try {
+ deserialized = deserialize(response.read);
+ } catch (e) {
+ /* Change status to indicate bad server response. This will result
+ * in passing an error to the callback */
+ status = {
+ code: grpc.status.INTERNAL,
+ details: 'Failed to parse server response'
+ };
+ }
}
- response.status.metadata = Metadata._fromCoreRepresentation(
- response.status.metadata);
- stream._receiveStatus(response.status);
- });
- return stream;
- }
- return makeServerStreamRequest;
-}
+ }
+ if (status.code !== grpc.status.OK) {
+ error = new Error(response.status.details);
+ error.code = status.code;
+ error.metadata = status.metadata;
+ args.callback(error);
+ } else {
+ args.callback(null, deserialized);
+ }
+ stream.emit('status', status);
+ });
+ return stream;
+};
/**
- * Get a function that can make bidirectional stream requests to the specified
- * method.
+ * Make a server stream request to the given method, with the given serialize
+ * and deserialize function, using the given argument
* @param {string} method The name of the method to request
- * @param {function(*):Buffer} serialize The serialization function for inputs
- * @param {function(Buffer)} deserialize The deserialization function for
- * outputs
- * @return {Function} makeBidiStreamRequest
+ * @param {module:src/common~serialize} serialize The serialization function for
+ * inputs
+ * @param {module:src/common~deserialize} deserialize The deserialization
+ * function for outputs
+ * @param {*} argument The argument to the call. Should be serializable with
+ * serialize
+ * @param {module:src/metadata.Metadata=} metadata Array of metadata key/value
+ * pairs to add to the call
+ * @param {module:src/client~CallOptions=} options Options map
+ * @return {module:src/client~ClientReadableStream} An event emitter for stream
+ * related events
*/
-function makeBidiStreamRequestFunction(method, serialize, deserialize) {
- /**
- * Make a bidirectional stream request with this method on the given channel.
- * @this {SurfaceClient} Client object. Must have a channel member.
- * @param {Metadata=} metadata Array of metadata key/value pairs to add to the
- * call
- * @param {Options} options Options map
- * @return {EventEmitter} An event emitter for stream related events
- */
- function makeBidiStreamRequest(metadata, options) {
- /* jshint validthis: true */
- /* While the arguments are listed in the function signature, those variables
- * are not used directly. Instead, ArgueJS processes the arguments
- * object. */
- var args = arguejs({metadata: [Metadata, new Metadata()],
- options: [Object]}, arguments);
- var call = getCall(this.$channel, method, args.options);
- metadata = args.metadata.clone();
- var stream = new ClientDuplexStream(call, serialize, deserialize);
- var start_batch = {};
- start_batch[grpc.opType.SEND_INITIAL_METADATA] =
- metadata._getCoreRepresentation();
- start_batch[grpc.opType.RECV_INITIAL_METADATA] = true;
- call.startBatch(start_batch, function(err, response) {
- if (err) {
- // The call has stopped for some reason. A non-OK status will arrive
- // in the other batch.
- return;
- }
- stream.emit('metadata', Metadata._fromCoreRepresentation(
- response.metadata));
- });
- var status_batch = {};
- status_batch[grpc.opType.RECV_STATUS_ON_CLIENT] = true;
- call.startBatch(status_batch, function(err, response) {
- if (err) {
- stream.emit('error', err);
- return;
- }
- response.status.metadata = Metadata._fromCoreRepresentation(
- response.status.metadata);
- stream._receiveStatus(response.status);
- });
- return stream;
+Client.prototype.makeServerStreamRequest = function(method, serialize,
+ deserialize, argument,
+ metadata, options) {
+ /* While the arguments are listed in the function signature, those variables
+ * are not used directly. Instead, ArgueJS processes the arguments
+ * object. */
+ var args = arguejs({method:String, serialize: Function,
+ deserialize: Function,
+ argument: null, metadata: [Metadata, new Metadata()],
+ options: [Object]}, arguments);
+ var call = getCall(this.$channel, method, args.options);
+ metadata = args.metadata.clone();
+ var stream = new ClientReadableStream(call, deserialize);
+ var start_batch = {};
+ var message = serialize(args.argument);
+ if (args.options) {
+ message.grpcWriteFlags = args.options.flags;
}
- return makeBidiStreamRequest;
-}
+ start_batch[grpc.opType.SEND_INITIAL_METADATA] =
+ metadata._getCoreRepresentation();
+ start_batch[grpc.opType.RECV_INITIAL_METADATA] = true;
+ start_batch[grpc.opType.SEND_MESSAGE] = message;
+ start_batch[grpc.opType.SEND_CLOSE_FROM_CLIENT] = true;
+ call.startBatch(start_batch, function(err, response) {
+ if (err) {
+ // The call has stopped for some reason. A non-OK status will arrive
+ // in the other batch.
+ return;
+ }
+ stream.emit('metadata', Metadata._fromCoreRepresentation(
+ response.metadata));
+ });
+ var status_batch = {};
+ status_batch[grpc.opType.RECV_STATUS_ON_CLIENT] = true;
+ call.startBatch(status_batch, function(err, response) {
+ if (err) {
+ stream.emit('error', err);
+ return;
+ }
+ response.status.metadata = Metadata._fromCoreRepresentation(
+ response.status.metadata);
+ stream._receiveStatus(response.status);
+ });
+ return stream;
+};
+
+
+/**
+ * Make a bidirectional stream request with this method on the given channel.
+ * @param {string} method The name of the method to request
+ * @param {module:src/common~serialize} serialize The serialization function for
+ * inputs
+ * @param {module:src/common~deserialize} deserialize The deserialization
+ * function for outputs
+ * @param {module:src/metadata.Metadata=} metadata Array of metadata key/value
+ * pairs to add to the call
+ * @param {module:src/client~CallOptions=} options Options map
+ * @return {module:src/client~ClientDuplexStream} An event emitter for stream
+ * related events
+ */
+Client.prototype.makeBidiStreamRequest = function(method, serialize,
+ deserialize, metadata,
+ options) {
+ /* While the arguments are listed in the function signature, those variables
+ * are not used directly. Instead, ArgueJS processes the arguments
+ * object. */
+ var args = arguejs({method:String, serialize: Function,
+ deserialize: Function,
+ metadata: [Metadata, new Metadata()],
+ options: [Object]}, arguments);
+ var call = getCall(this.$channel, method, args.options);
+ metadata = args.metadata.clone();
+ var stream = new ClientDuplexStream(call, serialize, deserialize);
+ var start_batch = {};
+ start_batch[grpc.opType.SEND_INITIAL_METADATA] =
+ metadata._getCoreRepresentation();
+ start_batch[grpc.opType.RECV_INITIAL_METADATA] = true;
+ call.startBatch(start_batch, function(err, response) {
+ if (err) {
+ // The call has stopped for some reason. A non-OK status will arrive
+ // in the other batch.
+ return;
+ }
+ stream.emit('metadata', Metadata._fromCoreRepresentation(
+ response.metadata));
+ });
+ var status_batch = {};
+ status_batch[grpc.opType.RECV_STATUS_ON_CLIENT] = true;
+ call.startBatch(status_batch, function(err, response) {
+ if (err) {
+ stream.emit('error', err);
+ return;
+ }
+ response.status.metadata = Metadata._fromCoreRepresentation(
+ response.status.metadata);
+ stream._receiveStatus(response.status);
+ });
+ return stream;
+};
+Client.prototype.close = function() {
+ this.$channel.close();
+};
+
+/**
+ * Return the underlying channel object for the specified client
+ * @return {Channel} The channel
+ */
+Client.prototype.getChannel = function() {
+ return this.$channel;
+};
+
+/**
+ * Wait for the client to be ready. The callback will be called when the
+ * client has successfully connected to the server, and it will be called
+ * with an error if the attempt to connect to the server has unrecoverablly
+ * failed or if the deadline expires. This function will make the channel
+ * start connecting if it has not already done so.
+ * @param {(Date|Number)} deadline When to stop waiting for a connection. Pass
+ * Infinity to wait forever.
+ * @param {function(Error)} callback The callback to call when done attempting
+ * to connect.
+ */
+Client.prototype.waitForReady = function(deadline, callback) {
+ var self = this;
+ var checkState = function(err) {
+ if (err) {
+ callback(new Error('Failed to connect before the deadline'));
+ return;
+ }
+ var new_state = self.$channel.getConnectivityState(true);
+ if (new_state === grpc.connectivityState.READY) {
+ callback();
+ } else if (new_state === grpc.connectivityState.FATAL_FAILURE) {
+ callback(new Error('Failed to connect to server'));
+ } else {
+ self.$channel.watchConnectivityState(new_state, deadline, checkState);
+ }
+ };
+ checkState();
+};
/**
* Map with short names for each of the requester maker functions. Used in
* makeClientConstructor
+ * @access private
*/
-var requester_makers = {
- unary: makeUnaryRequestFunction,
- server_stream: makeServerStreamRequestFunction,
- client_stream: makeClientStreamRequestFunction,
- bidi: makeBidiStreamRequestFunction
+var requester_funcs = {
+ unary: Client.prototype.makeUnaryRequest,
+ server_stream: Client.prototype.makeServerStreamRequest,
+ client_stream: Client.prototype.makeClientStreamRequest,
+ bidi: Client.prototype.makeBidiStreamRequest
};
function getDefaultValues(metadata, options) {
@@ -675,6 +804,7 @@ function getDefaultValues(metadata, options) {
/**
* Map with wrappers for each type of requester function to make it use the old
* argument order with optional arguments after the callback.
+ * @access private
*/
var deprecated_request_wrap = {
unary: function(makeUnaryRequest) {
@@ -700,55 +830,33 @@ var deprecated_request_wrap = {
};
/**
- * Creates a constructor for a client with the given methods. The methods object
- * maps method name to an object with the following keys:
- * path: The path on the server for accessing the method. For example, for
- * protocol buffers, we use "/service_name/method_name"
- * requestStream: bool indicating whether the client sends a stream
- * resonseStream: bool indicating whether the server sends a stream
- * requestSerialize: function to serialize request objects
- * responseDeserialize: function to deserialize response objects
- * @param {Object} methods An object mapping method names to method attributes
+ * Creates a constructor for a client with the given methods, as specified in
+ * the methods argument.
+ * @param {module:src/common~ServiceDefinition} methods An object mapping
+ * method names to method attributes
* @param {string} serviceName The fully qualified name of the service
- * @param {Object} class_options An options object. Currently only uses the key
- * deprecatedArgumentOrder, a boolean that Indicates that the old argument
- * order should be used for methods, with optional arguments at the end
- * instead of the callback at the end. Defaults to false. This option is
- * only a temporary stopgap measure to smooth an API breakage.
+ * @param {Object} class_options An options object.
+ * @param {boolean=} [class_options.deprecatedArgumentOrder=false] Indicates
+ * that the old argument order should be used for methods, with optional
+ * arguments at the end instead of the callback at the end. This option
+ * is only a temporary stopgap measure to smooth an API breakage.
* It is deprecated, and new code should not use it.
- * @return {function(string, Object)} New client constructor
+ * @return {function(string, Object)} New client constructor, which is a
+ * subclass of [grpc.Client]{@link module:src/client.Client}, and has the
+ * same arguments as that constructor.
*/
exports.makeClientConstructor = function(methods, serviceName,
class_options) {
if (!class_options) {
class_options = {};
}
- /**
- * Create a client with the given methods
- * @constructor
- * @param {string} address The address of the server to connect to
- * @param {grpc.Credentials} credentials Credentials to use to connect
- * to the server
- * @param {Object} options Options to pass to the underlying channel
- */
- function Client(address, credentials, options) {
- if (!options) {
- options = {};
- }
- /* Append the grpc-node user agent string after the application user agent
- * string, and put the combination at the beginning of the user agent string
- */
- if (options['grpc.primary_user_agent']) {
- options['grpc.primary_user_agent'] += ' ';
- } else {
- options['grpc.primary_user_agent'] = '';
- }
- options['grpc.primary_user_agent'] += 'grpc-node/' + version;
- /* Private fields use $ as a prefix instead of _ because it is an invalid
- * prefix of a method name */
- this.$channel = new grpc.Channel(address, credentials, options);
+
+ function ServiceClient(address, credentials, options) {
+ Client.call(this, address, credentials, options);
}
+ util.inherits(ServiceClient, Client);
+
_.each(methods, function(attrs, name) {
var method_type;
if (_.startsWith(name, '$')) {
@@ -769,20 +877,20 @@ exports.makeClientConstructor = function(methods, serviceName,
}
var serialize = attrs.requestSerialize;
var deserialize = attrs.responseDeserialize;
- var method_func = requester_makers[method_type](
- attrs.path, serialize, deserialize);
+ var method_func = _.partial(requester_funcs[method_type], attrs.path,
+ serialize, deserialize);
if (class_options.deprecatedArgumentOrder) {
- Client.prototype[name] = deprecated_request_wrap(method_func);
+ ServiceClient.prototype[name] = deprecated_request_wrap(method_func);
} else {
- Client.prototype[name] = method_func;
+ ServiceClient.prototype[name] = method_func;
}
// Associate all provided attributes with the method
- _.assign(Client.prototype[name], attrs);
+ _.assign(ServiceClient.prototype[name], attrs);
});
- Client.service = methods;
+ ServiceClient.service = methods;
- return Client;
+ return ServiceClient;
};
/**
@@ -791,7 +899,7 @@ exports.makeClientConstructor = function(methods, serviceName,
* @return {Channel} The channel
*/
exports.getClientChannel = function(client) {
- return client.$channel;
+ return Client.prototype.getChannel.call(client);
};
/**
@@ -807,21 +915,7 @@ exports.getClientChannel = function(client) {
* to connect.
*/
exports.waitForClientReady = function(client, deadline, callback) {
- var checkState = function(err) {
- if (err) {
- callback(new Error('Failed to connect before the deadline'));
- return;
- }
- var new_state = client.$channel.getConnectivityState(true);
- if (new_state === grpc.connectivityState.READY) {
- callback();
- } else if (new_state === grpc.connectivityState.FATAL_FAILURE) {
- callback(new Error('Failed to connect to server'));
- } else {
- client.$channel.watchConnectivityState(new_state, deadline, checkState);
- }
- };
- checkState();
+ Client.prototype.waitForReady.call(client, deadline, callback);
};
/**
diff --git a/src/node/src/common.js b/src/node/src/common.js
index 757969dbdd..4dad60e630 100644
--- a/src/node/src/common.js
+++ b/src/node/src/common.js
@@ -43,7 +43,8 @@ var _ = require('lodash');
/**
* Wrap a function to pass null-like values through without calling it. If no
- * function is given, just uses the identity;
+ * function is given, just uses the identity.
+ * @private
* @param {?function} func The function to wrap
* @return {function} The wrapped function
*/
@@ -90,3 +91,67 @@ exports.defaultGrpcOptions = {
enumsAsStrings: true,
deprecatedArgumentOrder: false
};
+
+// JSDoc definitions that are used in multiple other modules
+
+/**
+ * The EventEmitter class in the event standard module
+ * @external EventEmitter
+ * @see https://nodejs.org/api/events.html#events_class_eventemitter
+ */
+
+/**
+ * The Readable class in the stream standard module
+ * @external Readable
+ * @see https://nodejs.org/api/stream.html#stream_readable_streams
+ */
+
+/**
+ * The Writable class in the stream standard module
+ * @external Writable
+ * @see https://nodejs.org/api/stream.html#stream_writable_streams
+ */
+
+/**
+ * The Duplex class in the stream standard module
+ * @external Duplex
+ * @see https://nodejs.org/api/stream.html#stream_class_stream_duplex
+ */
+
+/**
+ * A serialization function
+ * @callback module:src/common~serialize
+ * @param {*} value The value to serialize
+ * @return {Buffer} The value serialized as a byte sequence
+ */
+
+/**
+ * A deserialization function
+ * @callback module:src/common~deserialize
+ * @param {Buffer} data The byte sequence to deserialize
+ * @return {*} The data deserialized as a value
+ */
+
+/**
+ * An object that completely defines a service method signature.
+ * @typedef {Object} module:src/common~MethodDefinition
+ * @property {string} path The method's URL path
+ * @property {boolean} requestStream Indicates whether the method accepts
+ * a stream of requests
+ * @property {boolean} responseStream Indicates whether the method returns
+ * a stream of responses
+ * @property {module:src/common~serialize} requestSerialize Serialization
+ * function for request values
+ * @property {module:src/common~serialize} responseSerialize Serialization
+ * function for response values
+ * @property {module:src/common~deserialize} requestDeserialize Deserialization
+ * function for request data
+ * @property {module:src/common~deserialize} responseDeserialize Deserialization
+ * function for repsonse data
+ */
+
+/**
+ * An object that completely defines a service.
+ * @typedef {Object.<string, module:src/common~MethodDefinition>}
+ * module:src/common~ServiceDefinition
+ */
diff --git a/src/node/src/metadata.js b/src/node/src/metadata.js
index 612361b0ea..92cf23998b 100644
--- a/src/node/src/metadata.js
+++ b/src/node/src/metadata.js
@@ -35,12 +35,7 @@
* Metadata module
*
* This module defines the Metadata class, which represents header and trailer
- * metadata for gRPC calls. Here is an example of how to use it:
- *
- * var metadata = new metadata_module.Metadata();
- * metadata.set('key1', 'value1');
- * metadata.add('key1', 'value2');
- * metadata.get('key1') // returns ['value1', 'value2']
+ * metadata for gRPC calls.
*
* @module
*/
@@ -54,6 +49,12 @@ var grpc = require('./grpc_extension');
/**
* Class for storing metadata. Keys are normalized to lowercase ASCII.
* @constructor
+ * @alias module:src/metadata.Metadata
+ * @example
+ * var metadata = new metadata_module.Metadata();
+ * metadata.set('key1', 'value1');
+ * metadata.add('key1', 'value2');
+ * metadata.get('key1') // returns ['value1', 'value2']
*/
function Metadata() {
this._internal_repr = {};
diff --git a/src/php/composer.json b/src/php/composer.json
index 24c17c3b57..a4fba7e4f6 100644
--- a/src/php/composer.json
+++ b/src/php/composer.json
@@ -5,7 +5,7 @@
"version": "1.4.0",
"require": {
"php": ">=5.5.0",
- "google/protobuf": "^v3.1.0"
+ "google/protobuf": "^v3.3.0"
},
"require-dev": {
"google/auth": "v0.9"
diff --git a/src/proto/grpc/testing/control.proto b/src/proto/grpc/testing/control.proto
index acee86678d..02b156d0cd 100644
--- a/src/proto/grpc/testing/control.proto
+++ b/src/proto/grpc/testing/control.proto
@@ -52,6 +52,9 @@ enum ServerType {
enum RpcType {
UNARY = 0;
STREAMING = 1;
+ STREAMING_FROM_CLIENT = 2;
+ STREAMING_FROM_SERVER = 3;
+ STREAMING_BOTH_WAYS = 4;
}
// Parameters of poisson process distribution, which is a good representation
diff --git a/src/proto/grpc/testing/services.proto b/src/proto/grpc/testing/services.proto
index 969782d656..85949abd86 100644
--- a/src/proto/grpc/testing/services.proto
+++ b/src/proto/grpc/testing/services.proto
@@ -42,9 +42,22 @@ service BenchmarkService {
// The server returns the client payload as-is.
rpc UnaryCall(SimpleRequest) returns (SimpleResponse);
- // One request followed by one response.
- // The server returns the client payload as-is.
+ // Repeated sequence of one request followed by one response.
+ // Should be called streaming ping-pong
+ // The server returns the client payload as-is on each response
rpc StreamingCall(stream SimpleRequest) returns (stream SimpleResponse);
+
+ // Single-sided unbounded streaming from client to server
+ // The server returns the client payload as-is once the client does WritesDone
+ rpc StreamingFromClient(stream SimpleRequest) returns (SimpleResponse);
+
+ // Single-sided unbounded streaming from server to client
+ // The server repeatedly returns the client payload as-is
+ rpc StreamingFromServer(SimpleRequest) returns (stream SimpleResponse);
+
+ // Two-sided unbounded streaming between server to client
+ // Both sides send the content of their own choice to the other
+ rpc StreamingBothWays(stream SimpleRequest) returns (stream SimpleResponse);
}
service WorkerService {
diff --git a/src/python/grpcio/commands.py b/src/python/grpcio/commands.py
index 4f072809c4..f4ccb1ab94 100644
--- a/src/python/grpcio/commands.py
+++ b/src/python/grpcio/commands.py
@@ -284,9 +284,9 @@ class BuildExt(build_ext.build_ext):
stderr=subprocess.PIPE)
make_out, make_err = make_process.communicate()
if make_out and make_process.returncode != 0:
- sys.stdout.write(make_out + '\n')
+ sys.stdout.write(str(make_out) + '\n')
if make_err:
- sys.stderr.write(make_err + '\n')
+ sys.stderr.write(str(make_err) + '\n')
if make_process.returncode != 0:
raise Exception("make command failed!")
diff --git a/src/python/grpcio/grpc/__init__.py b/src/python/grpcio/grpc/__init__.py
index 4960df3be9..2952dcd36a 100644
--- a/src/python/grpcio/grpc/__init__.py
+++ b/src/python/grpcio/grpc/__init__.py
@@ -61,13 +61,13 @@ class Future(six.with_metaclass(abc.ABCMeta)):
This method does not block.
Returns:
- True if the computation has not yet begun, will not be allowed to take
- place, and determination of both was possible without blocking. False
- under all other circumstances including but not limited to the
- computation's already having begun, the computation's already having
- finished, and the computation's having been scheduled for execution on a
- remote system for which a determination of whether or not it commenced
- before being cancelled cannot be made without blocking.
+ bool:
+ Returns True if the computation was canceled.
+ Returns False under all other circumstances, for example:
+ 1. computation has begun and could not be canceled.
+ 2. computation has finished
+ 3. computation is scheduled for execution and it is impossible to
+ determine its state without blocking.
"""
raise NotImplementedError()
@@ -78,10 +78,12 @@ class Future(six.with_metaclass(abc.ABCMeta)):
This method does not block.
Returns:
- True if the computation was cancelled any time before its result became
- immediately available. False under all other circumstances including but
- not limited to this object's cancel method not having been called and
- the computation's result having become immediately available.
+ bool:
+ Returns True if the computation was cancelled before its result became
+ available.
+ False under all other circumstances, for example:
+ 1. computation was not cancelled.
+ 2. computation's result is available.
"""
raise NotImplementedError()
@@ -92,9 +94,10 @@ class Future(six.with_metaclass(abc.ABCMeta)):
This method does not block.
Returns:
- True if the computation is scheduled to take place in the future or is
- taking place now, or False if the computation took place in the past or
- was cancelled.
+ bool:
+ Returns True if the computation is scheduled for execution or currently
+ executing.
+ Returns False if the computation already executed or was cancelled.
"""
raise NotImplementedError()
@@ -105,22 +108,24 @@ class Future(six.with_metaclass(abc.ABCMeta)):
This method does not block.
Returns:
- True if the computation is known to have either completed or have been
- unscheduled or interrupted. False if the computation may possibly be
- executing or scheduled to execute later.
+ bool:
+ Returns True if the computation already executed or was cancelled.
+ Returns False if the computation is scheduled for execution or currently
+ executing.
+ This is exactly opposite of the running() method's result.
"""
raise NotImplementedError()
@abc.abstractmethod
def result(self, timeout=None):
- """Accesses the outcome of the computation or raises its exception.
+ """Returns the result of the computation or raises its exception.
This method may return immediately or may block.
Args:
timeout: The length of time in seconds to wait for the computation to
- finish or be cancelled, or None if this method should block until the
- computation has finished or is cancelled no matter how long that takes.
+ finish or be cancelled. If None, the call will block until the
+ computations's termination.
Returns:
The return value of the computation.
@@ -142,12 +147,11 @@ class Future(six.with_metaclass(abc.ABCMeta)):
Args:
timeout: The length of time in seconds to wait for the computation to
- terminate or be cancelled, or None if this method should block until
- the computation is terminated or is cancelled no matter how long that
- takes.
+ terminate or be cancelled. If None, the call will block until the
+ computations's termination.
Returns:
- The exception raised by the computation, or None if the computation did
+ The exception raised by the computation, or None if the computation did
not raise an exception.
Raises:
@@ -165,12 +169,11 @@ class Future(six.with_metaclass(abc.ABCMeta)):
Args:
timeout: The length of time in seconds to wait for the computation to
- terminate or be cancelled, or None if this method should block until
- the computation is terminated or is cancelled no matter how long that
- takes.
+ terminate or be cancelled. If None, the call will block until the
+ computations's termination.
Returns:
- The traceback of the exception raised by the computation, or None if the
+ The traceback of the exception raised by the computation, or None if the
computation did not raise an exception.
Raises:
@@ -260,7 +263,12 @@ class RpcContext(six.with_metaclass(abc.ABCMeta)):
@abc.abstractmethod
def is_active(self):
- """Describes whether the RPC is active or has terminated."""
+ """Describes whether the RPC is active or has terminated.
+
+ Returns:
+ bool:
+ True if RPC is active, False otherwise.
+ """
raise NotImplementedError()
@abc.abstractmethod
@@ -290,8 +298,9 @@ class RpcContext(six.with_metaclass(abc.ABCMeta)):
callback: A no-parameter callable to be called on RPC termination.
Returns:
- True if the callback was added and will be called later; False if the
- callback was not added and will not later be called (because the RPC
+ bool:
+ True if the callback was added and will be called later; False if the
+ callback was not added and will not be called (because the RPC
already terminated or some other reason).
"""
raise NotImplementedError()
@@ -305,7 +314,7 @@ class Call(six.with_metaclass(abc.ABCMeta, RpcContext)):
@abc.abstractmethod
def initial_metadata(self):
- """Accesses the initial metadata from the service-side of the RPC.
+ """Accesses the initial metadata sent by the server.
This method blocks until the value is available.
@@ -316,7 +325,7 @@ class Call(six.with_metaclass(abc.ABCMeta, RpcContext)):
@abc.abstractmethod
def trailing_metadata(self):
- """Accesses the trailing metadata from the service-side of the RPC.
+ """Accesses the trailing metadata sent by the server.
This method blocks until the value is available.
@@ -327,7 +336,7 @@ class Call(six.with_metaclass(abc.ABCMeta, RpcContext)):
@abc.abstractmethod
def code(self):
- """Accesses the status code emitted by the service-side of the RPC.
+ """Accesses the status code sent by the server.
This method blocks until the value is available.
@@ -338,7 +347,7 @@ class Call(six.with_metaclass(abc.ABCMeta, RpcContext)):
@abc.abstractmethod
def details(self):
- """Accesses the details value emitted by the service-side of the RPC.
+ """Accesses the details sent by the server.
This method blocks until the value is available.
@@ -352,10 +361,12 @@ class Call(six.with_metaclass(abc.ABCMeta, RpcContext)):
class ChannelCredentials(object):
- """A value encapsulating the data required to create a secure Channel.
+ """An encapsulation of the data required to create a secure Channel.
This class has no supported interface - it exists to define the type of its
- instances and its instances exist to be passed to other functions.
+ instances and its instances exist to be passed to other functions. For
+ example, ssl_channel_credentials returns an instance, and secure_channel
+ consumes an instance of this class.
"""
def __init__(self, credentials):
@@ -363,7 +374,8 @@ class ChannelCredentials(object):
class CallCredentials(object):
- """A value encapsulating data asserting an identity over a channel.
+ """An encapsulation of the data required to assert an identity over a
+ channel.
A CallCredentials may be composed with ChannelCredentials to always assert
identity for every call over that Channel.
@@ -389,7 +401,8 @@ class AuthMetadataPluginCallback(six.with_metaclass(abc.ABCMeta)):
"""Callback object received by a metadata plugin."""
def __call__(self, metadata, error):
- """Inform the gRPC runtime of the metadata to construct a CallCredentials.
+ """Inform the gRPC runtime of the metadata to construct a
+ CallCredentials.
Args:
metadata: The :term:`metadata` used to construct the CallCredentials.
@@ -416,7 +429,7 @@ class AuthMetadataPlugin(six.with_metaclass(abc.ABCMeta)):
class ServerCredentials(object):
- """A value encapsulating the data required to open a secure port on a Server.
+ """An encapsulation of the data required to open a secure port on a Server.
This class has no supported interface - it exists to define the type of its
instances and its instances exist to be passed to other functions.
@@ -430,7 +443,7 @@ class ServerCredentials(object):
class UnaryUnaryMultiCallable(six.with_metaclass(abc.ABCMeta)):
- """Affords invoking a unary-unary RPC."""
+ """Affords invoking a unary-unary RPC from client-side."""
@abc.abstractmethod
def __call__(self, request, timeout=None, metadata=None, credentials=None):
@@ -486,7 +499,7 @@ class UnaryUnaryMultiCallable(six.with_metaclass(abc.ABCMeta)):
credentials: An optional CallCredentials for the RPC.
Returns:
- An object that is both a Call for the RPC and a Future. In the event of
+ An object that is both a Call for the RPC and a Future. In the event of
RPC completion, the return Call-Future's result value will be the
response message of the RPC. Should the event terminate with non-OK
status, the returned Call-Future's exception value will be an RpcError.
@@ -495,7 +508,7 @@ class UnaryUnaryMultiCallable(six.with_metaclass(abc.ABCMeta)):
class UnaryStreamMultiCallable(six.with_metaclass(abc.ABCMeta)):
- """Affords invoking a unary-stream RPC."""
+ """Affords invoking a unary-stream RPC from client-side."""
@abc.abstractmethod
def __call__(self, request, timeout=None, metadata=None, credentials=None):
@@ -504,12 +517,13 @@ class UnaryStreamMultiCallable(six.with_metaclass(abc.ABCMeta)):
Args:
request: The request value for the RPC.
timeout: An optional duration of time in seconds to allow for the RPC.
+ If None, the timeout is considered infinite.
metadata: An optional :term:`metadata` to be transmitted to the
service-side of the RPC.
credentials: An optional CallCredentials for the RPC.
Returns:
- An object that is both a Call for the RPC and an iterator of response
+ An object that is both a Call for the RPC and an iterator of response
values. Drawing response values from the returned Call-iterator may
raise RpcError indicating termination of the RPC with non-OK status.
"""
@@ -517,7 +531,7 @@ class UnaryStreamMultiCallable(six.with_metaclass(abc.ABCMeta)):
class StreamUnaryMultiCallable(six.with_metaclass(abc.ABCMeta)):
- """Affords invoking a stream-unary RPC in any call style."""
+ """Affords invoking a stream-unary RPC from client-side."""
@abc.abstractmethod
def __call__(self,
@@ -530,6 +544,7 @@ class StreamUnaryMultiCallable(six.with_metaclass(abc.ABCMeta)):
Args:
request_iterator: An iterator that yields request values for the RPC.
timeout: An optional duration of time in seconds to allow for the RPC.
+ If None, the timeout is considered infinite.
metadata: Optional :term:`metadata` to be transmitted to the
service-side of the RPC.
credentials: An optional CallCredentials for the RPC.
@@ -539,8 +554,8 @@ class StreamUnaryMultiCallable(six.with_metaclass(abc.ABCMeta)):
Raises:
RpcError: Indicating that the RPC terminated with non-OK status. The
- raised RpcError will also be a Call for the RPC affording the RPC's
- metadata, status code, and details.
+ raised RpcError will also implement grpc.Call, affording methods
+ such as metadata, code, and details.
"""
raise NotImplementedError()
@@ -550,17 +565,18 @@ class StreamUnaryMultiCallable(six.with_metaclass(abc.ABCMeta)):
timeout=None,
metadata=None,
credentials=None):
- """Synchronously invokes the underlying RPC.
+ """Synchronously invokes the underlying RPC on the client.
Args:
request_iterator: An iterator that yields request values for the RPC.
timeout: An optional duration of time in seconds to allow for the RPC.
+ If None, the timeout is considered infinite.
metadata: Optional :term:`metadata` to be transmitted to the
service-side of the RPC.
credentials: An optional CallCredentials for the RPC.
Returns:
- The response value for the RPC and a Call for the RPC.
+ The response value for the RPC and a Call object for the RPC.
Raises:
RpcError: Indicating that the RPC terminated with non-OK status. The
@@ -575,17 +591,18 @@ class StreamUnaryMultiCallable(six.with_metaclass(abc.ABCMeta)):
timeout=None,
metadata=None,
credentials=None):
- """Asynchronously invokes the underlying RPC.
+ """Asynchronously invokes the underlying RPC on the client.
Args:
request_iterator: An iterator that yields request values for the RPC.
timeout: An optional duration of time in seconds to allow for the RPC.
+ If None, the timeout is considered infinite.
metadata: Optional :term:`metadata` to be transmitted to the
service-side of the RPC.
credentials: An optional CallCredentials for the RPC.
Returns:
- An object that is both a Call for the RPC and a Future. In the event of
+ An object that is both a Call for the RPC and a Future. In the event of
RPC completion, the return Call-Future's result value will be the
response message of the RPC. Should the event terminate with non-OK
status, the returned Call-Future's exception value will be an RpcError.
@@ -594,7 +611,7 @@ class StreamUnaryMultiCallable(six.with_metaclass(abc.ABCMeta)):
class StreamStreamMultiCallable(six.with_metaclass(abc.ABCMeta)):
- """Affords invoking a stream-stream RPC in any call style."""
+ """Affords invoking a stream-stream RPC on client-side."""
@abc.abstractmethod
def __call__(self,
@@ -602,17 +619,18 @@ class StreamStreamMultiCallable(six.with_metaclass(abc.ABCMeta)):
timeout=None,
metadata=None,
credentials=None):
- """Invokes the underlying RPC.
+ """Invokes the underlying RPC on the client.
Args:
request_iterator: An iterator that yields request values for the RPC.
timeout: An optional duration of time in seconds to allow for the RPC.
+ if not specified the timeout is considered infinite.
metadata: Optional :term:`metadata` to be transmitted to the
service-side of the RPC.
credentials: An optional CallCredentials for the RPC.
Returns:
- An object that is both a Call for the RPC and an iterator of response
+ An object that is both a Call for the RPC and an iterator of response
values. Drawing response values from the returned Call-iterator may
raise RpcError indicating termination of the RPC with non-OK status.
"""
@@ -623,27 +641,32 @@ class StreamStreamMultiCallable(six.with_metaclass(abc.ABCMeta)):
class Channel(six.with_metaclass(abc.ABCMeta)):
- """Affords RPC invocation via generic methods."""
+ """Affords RPC invocation via generic methods on client-side."""
@abc.abstractmethod
def subscribe(self, callback, try_to_connect=False):
- """Subscribes to this Channel's connectivity.
+ """Subscribe to this Channel's connectivity state machine.
+
+ A Channel may be in any of the states described by ChannelConnectivity.
+ This method allows application to monitor the state transitions.
+ The typical use case is to debug or gain better visibility into gRPC
+ runtime's state.
Args:
- callback: A callable to be invoked and passed a ChannelConnectivity value
- describing this Channel's connectivity. The callable will be invoked
- immediately upon subscription and again for every change to this
- Channel's connectivity thereafter until it is unsubscribed or this
+ callback: A callable to be invoked with ChannelConnectivity argument.
+ ChannelConnectivity describes current state of the channel.
+ The callable will be invoked immediately upon subscription and again for
+ every change to ChannelConnectivity until it is unsubscribed or this
Channel object goes out of scope.
try_to_connect: A boolean indicating whether or not this Channel should
- attempt to connect if it is not already connected and ready to conduct
- RPCs.
+ attempt to connect immediately. If set to False, gRPC runtime decides
+ when to connect.
"""
raise NotImplementedError()
@abc.abstractmethod
def unsubscribe(self, callback):
- """Unsubscribes a callback from this Channel's connectivity.
+ """Unsubscribes a subscribed callback from this Channel's connectivity.
Args:
callback: A callable previously registered with this Channel from having
@@ -736,7 +759,7 @@ class ServicerContext(six.with_metaclass(abc.ABCMeta, RpcContext)):
@abc.abstractmethod
def invocation_metadata(self):
- """Accesses the metadata from the invocation-side of the RPC.
+ """Accesses the metadata from the sent by the client.
Returns:
The invocation :term:`metadata`.
@@ -749,15 +772,16 @@ class ServicerContext(six.with_metaclass(abc.ABCMeta, RpcContext)):
Returns:
A string identifying the peer that invoked the RPC being serviced.
+ The string format is determined by gRPC runtime.
"""
raise NotImplementedError()
@abc.abstractmethod
def send_initial_metadata(self, initial_metadata):
- """Sends the initial metadata value to the invocation-side of the RPC.
+ """Sends the initial metadata value to the client.
- This method need not be called by method implementations if they have no
- service-side initial metadata to transmit.
+ This method need not be called by implementations if they have no
+ metadata to add to what the gRPC runtime will transmit.
Args:
initial_metadata: The initial :term:`metadata`.
@@ -766,10 +790,10 @@ class ServicerContext(six.with_metaclass(abc.ABCMeta, RpcContext)):
@abc.abstractmethod
def set_trailing_metadata(self, trailing_metadata):
- """Accepts the trailing metadata value of the RPC.
+ """Sends the trailing metadata for the RPC.
- This method need not be called by method implementations if they have no
- service-side trailing metadata to transmit.
+ This method need not be called by implementations if they have no
+ metadata to add to what the gRPC runtime will transmit.
Args:
trailing_metadata: The trailing :term:`metadata`.
@@ -778,27 +802,25 @@ class ServicerContext(six.with_metaclass(abc.ABCMeta, RpcContext)):
@abc.abstractmethod
def set_code(self, code):
- """Accepts the status code of the RPC.
+ """Sets the value to be used as status code upon RPC completion.
This method need not be called by method implementations if they wish the
gRPC runtime to determine the status code of the RPC.
Args:
- code: A StatusCode value to be transmitted to the invocation side of the
- RPC as the status code of the RPC.
+ code: A StatusCode object to be sent to the client.
"""
raise NotImplementedError()
@abc.abstractmethod
def set_details(self, details):
- """Accepts the service-side details of the RPC.
+ """Sets the value to be used as detail string upon RPC completion.
This method need not be called by method implementations if they have no
details to transmit.
Args:
- details: A string to be transmitted to the invocation side of the RPC as
- the status details of the RPC.
+ details: An arbitrary string to be sent to the client upon completion.
"""
raise NotImplementedError()
@@ -845,7 +867,7 @@ class HandlerCallDetails(six.with_metaclass(abc.ABCMeta)):
"""Describes an RPC that has just arrived for service.
Attributes:
method: The method name of the RPC.
- invocation_metadata: The :term:`metadata` from the invocation side of the RPC.
+ invocation_metadata: The :term:`metadata` sent by the client.
"""
@@ -854,14 +876,14 @@ class GenericRpcHandler(six.with_metaclass(abc.ABCMeta)):
@abc.abstractmethod
def service(self, handler_call_details):
- """Services an RPC (or not).
+ """Returns the handler for servicing the RPC.
Args:
handler_call_details: A HandlerCallDetails describing the RPC.
Returns:
- An RpcMethodHandler with which the RPC may be serviced, or None to
- indicate that this object will not be servicing the RPC.
+ An RpcMethodHandler with which the RPC may be serviced if the
+ implementation chooses to service this RPC, or None otherwise.
"""
raise NotImplementedError()
@@ -870,15 +892,15 @@ class ServiceRpcHandler(six.with_metaclass(abc.ABCMeta, GenericRpcHandler)):
"""An implementation of RPC methods belonging to a service.
A service handles RPC methods with structured names of the form
- '/Service.Name/Service.MethodX', where 'Service.Name' is the value
- returned by service_name(), and 'Service.MethodX' is the service method
- name. A service can have multiple service methods names, but only a single
+ '/Service.Name/Service.Method', where 'Service.Name' is the value
+ returned by service_name(), and 'Service.Method' is the method
+ name. A service can have multiple method names, but only a single
service name.
"""
@abc.abstractmethod
def service_name(self):
- """Returns this services name.
+ """Returns this service's name.
Returns:
The service name.
@@ -900,88 +922,78 @@ class Server(six.with_metaclass(abc.ABCMeta)):
Args:
generic_rpc_handlers: An iterable of GenericRpcHandlers that will be used
- to service RPCs after this Server is started.
+ to service RPCs.
"""
raise NotImplementedError()
@abc.abstractmethod
def add_insecure_port(self, address):
- """Reserves a port for insecure RPC service once this Server becomes active.
+ """Opens an insecure port for accepting RPCs.
- This method may only be called before calling this Server's start method is
- called.
+ This method may only be called before starting the server.
Args:
address: The address for which to open a port.
+ if the port is 0, or not specified in the address, then gRPC runtime
+ will choose a port.
Returns:
- An integer port on which RPCs will be serviced after this link has been
- started. This is typically the same number as the port number contained
- in the passed address, but will likely be different if the port number
- contained in the passed address was zero.
+ integer:
+ An integer port on which server will accept RPC requests.
"""
raise NotImplementedError()
@abc.abstractmethod
def add_secure_port(self, address, server_credentials):
- """Reserves a port for secure RPC service after this Server becomes active.
+ """Opens a secure port for accepting RPCs.
- This method may only be called before calling this Server's start method is
- called.
+ This method may only be called before starting the server.
Args:
address: The address for which to open a port.
- server_credentials: A ServerCredentials.
+ if the port is 0, or not specified in the address, then gRPC runtime
+ will choose a port.
+ server_credentials: A ServerCredentials object.
Returns:
- An integer port on which RPCs will be serviced after this link has been
- started. This is typically the same number as the port number contained
- in the passed address, but will likely be different if the port number
- contained in the passed address was zero.
+ integer:
+ An integer port on which server will accept RPC requests.
"""
raise NotImplementedError()
@abc.abstractmethod
def start(self):
- """Starts this Server's service of RPCs.
+ """Starts this Server.
- This method may only be called while the server is not serving RPCs (i.e. it
- is not idempotent).
+ This method may only be called once. (i.e. it is not idempotent).
"""
raise NotImplementedError()
@abc.abstractmethod
def stop(self, grace):
- """Stops this Server's service of RPCs.
+ """Stops this Server.
- All calls to this method immediately stop service of new RPCs. When existing
- RPCs are aborted is controlled by the grace period parameter passed to this
- method.
+ This method immediately stop service of new RPCs in all cases.
+ If a grace period is specified, this method returns immediately
+ and all RPCs active at the end of the grace period are aborted.
- This method may be called at any time and is idempotent. Passing a smaller
- grace value than has been passed in a previous call will have the effect of
- stopping the Server sooner. Passing a larger grace value than has been
- passed in a previous call will not have the effect of stopping the server
- later.
+ If a grace period is not specified, then all existing RPCs are
+ teriminated immediately and the this method blocks until the last
+ RPC handler terminates.
- This method does not block for any significant length of time. If None is
- passed as the grace value, existing RPCs are immediately aborted and this
- method blocks until this Server is completely stopped.
+ This method is idempotent and may be called at any time. Passing a smaller
+ grace value in subsequentcall will have the effect of stopping the Server
+ sooner. Passing a larger grace value in subsequent call *will not* have the
+ effect of stopping the server later (i.e. the most restrictive grace
+ value is used).
Args:
- grace: A duration of time in seconds or None. If a duration of time in
- seconds, the time to allow existing RPCs to complete before being
- aborted by this Server's stopping. If None, all RPCs will be aborted
- immediately and this method will block until this Server is completely
- stopped.
+ grace: A duration of time in seconds or None.
Returns:
A threading.Event that will be set when this Server has completely
- stopped. The returned event may not be set until after the full grace
- period (if some ongoing RPC continues for the full length of the period)
- of it may be set much sooner (such as if this Server had no RPCs underway
- at the time it was stopped or if all RPCs that it had underway completed
- very early in the grace period).
+ stopped, i.e. when running RPCs either complete or are aborted and
+ all handlers have terminated.
"""
raise NotImplementedError()
@@ -995,14 +1007,13 @@ def unary_unary_rpc_method_handler(behavior,
"""Creates an RpcMethodHandler for a unary-unary RPC method.
Args:
- behavior: The implementation of an RPC method as a callable behavior taking
- a single request value and returning a single response value.
- request_deserializer: An optional request deserialization behavior.
- response_serializer: An optional response serialization behavior.
+ behavior: The implementation of an RPC that accepts one request and returns
+ one response.
+ request_deserializer: An optional behavior for request deserialization.
+ response_serializer: An optional behavior for response serialization.
Returns:
- An RpcMethodHandler for a unary-unary RPC method constructed from the given
- parameters.
+ An RpcMethodHandler object that is typically used by grpc.Server.
"""
from grpc import _utilities # pylint: disable=cyclic-import
return _utilities.RpcMethodHandler(False, False, request_deserializer,
@@ -1016,14 +1027,13 @@ def unary_stream_rpc_method_handler(behavior,
"""Creates an RpcMethodHandler for a unary-stream RPC method.
Args:
- behavior: The implementation of an RPC method as a callable behavior taking
- a single request value and returning an iterator of response values.
- request_deserializer: An optional request deserialization behavior.
- response_serializer: An optional response serialization behavior.
+ behavior: The implementation of an RPC that accepts one request and returns
+ an iterator of response values.
+ request_deserializer: An optional behavior for request deserialization.
+ response_serializer: An optional behavior for response serialization.
Returns:
- An RpcMethodHandler for a unary-stream RPC method constructed from the
- given parameters.
+ An RpcMethodHandler object that is typically used by grpc.Server.
"""
from grpc import _utilities # pylint: disable=cyclic-import
return _utilities.RpcMethodHandler(False, True, request_deserializer,
@@ -1037,14 +1047,13 @@ def stream_unary_rpc_method_handler(behavior,
"""Creates an RpcMethodHandler for a stream-unary RPC method.
Args:
- behavior: The implementation of an RPC method as a callable behavior taking
- an iterator of request values and returning a single response value.
- request_deserializer: An optional request deserialization behavior.
- response_serializer: An optional response serialization behavior.
+ behavior: The implementation of an RPC that accepts an iterator of request
+ values and returns a single response value.
+ request_deserializer: An optional behavior for request deserialization.
+ response_serializer: An optional behavior for response serialization.
Returns:
- An RpcMethodHandler for a stream-unary RPC method constructed from the
- given parameters.
+ An RpcMethodHandler object that is typically used by grpc.Server.
"""
from grpc import _utilities # pylint: disable=cyclic-import
return _utilities.RpcMethodHandler(True, False, request_deserializer,
@@ -1058,15 +1067,13 @@ def stream_stream_rpc_method_handler(behavior,
"""Creates an RpcMethodHandler for a stream-stream RPC method.
Args:
- behavior: The implementation of an RPC method as a callable behavior taking
- an iterator of request values and returning an iterator of response
- values.
- request_deserializer: An optional request deserialization behavior.
- response_serializer: An optional response serialization behavior.
+ behavior: The implementation of an RPC that accepts an iterator of request
+ values and returns an iterator of response values.
+ request_deserializer: An optional behavior for request deserialization.
+ response_serializer: An optional behavior for response serialization.
Returns:
- An RpcMethodHandler for a stream-stream RPC method constructed from the
- given parameters.
+ An RpcMethodHandler object that is typically used by grpc.Server.
"""
from grpc import _utilities # pylint: disable=cyclic-import
return _utilities.RpcMethodHandler(True, True, request_deserializer,
@@ -1075,15 +1082,16 @@ def stream_stream_rpc_method_handler(behavior,
def method_handlers_generic_handler(service, method_handlers):
- """Creates a grpc.GenericRpcHandler from RpcMethodHandlers.
+ """Creates a GenericRpcHandler from RpcMethodHandlers.
Args:
- service: A service name to be used for the given method handlers.
- method_handlers: A dictionary from method name to RpcMethodHandler
- implementing the named method.
+ service: The name of the service that is implemented by the method_handlers.
+ method_handlers: A dictionary that maps method names to corresponding
+ RpcMethodHandler.
Returns:
- A GenericRpcHandler constructed from the given parameters.
+ A GenericRpcHandler. This is typically added to the grpc.Server object
+ with add_generic_rpc_handlers() before starting the server.
"""
from grpc import _utilities # pylint: disable=cyclic-import
return _utilities.DictionaryGenericHandler(service, method_handlers)
@@ -1095,12 +1103,12 @@ def ssl_channel_credentials(root_certificates=None,
"""Creates a ChannelCredentials for use with an SSL-enabled Channel.
Args:
- root_certificates: The PEM-encoded root certificates or unset to ask for
- them to be retrieved from a default location.
- private_key: The PEM-encoded private key to use or unset if no private key
- should be used.
- certificate_chain: The PEM-encoded certificate chain to use or unset if no
- certificate chain should be used.
+ root_certificates: The PEM-encoded root certificates as a byte string,
+ or None to retrieve them from a default location chosen by gRPC runtime.
+ private_key: The PEM-encoded private key as a byte string, or None if no
+ private key should be used.
+ certificate_chain: The PEM-encoded certificate chain as a byte string
+ to use or or None if no certificate chain should be used.
Returns:
A ChannelCredentials for use with an SSL-enabled Channel.
@@ -1117,9 +1125,8 @@ def metadata_call_credentials(metadata_plugin, name=None):
"""Construct CallCredentials from an AuthMetadataPlugin.
Args:
- metadata_plugin: An AuthMetadataPlugin to use as the authentication behavior
- in the created CallCredentials.
- name: A name for the plugin.
+ metadata_plugin: An AuthMetadataPlugin to use for authentication.
+ name: An optional name for the plugin.
Returns:
A CallCredentials.
@@ -1142,7 +1149,8 @@ def access_token_call_credentials(access_token):
Args:
access_token: A string to place directly in the http request
- authorization header, ie "authorization: Bearer <access_token>".
+ authorization header, for example
+ "authorization: Bearer <access_token>".
Returns:
A CallCredentials.
@@ -1173,12 +1181,12 @@ def composite_channel_credentials(channel_credentials, *call_credentials):
"""Compose a ChannelCredentials and one or more CallCredentials objects.
Args:
- channel_credentials: A ChannelCredentials.
+ channel_credentials: A ChannelCredentials object.
*call_credentials: One or more CallCredentials objects.
Returns:
A ChannelCredentials composed of the given ChannelCredentials and
- CallCredentials objects.
+ CallCredentials objects.
"""
from grpc import _credential_composition # pylint: disable=cyclic-import
cygrpc_call_credentials = tuple(
@@ -1195,18 +1203,18 @@ def ssl_server_credentials(private_key_certificate_chain_pairs,
"""Creates a ServerCredentials for use with an SSL-enabled Server.
Args:
- private_key_certificate_chain_pairs: A nonempty sequence each element of
- which is a pair the first element of which is a PEM-encoded private key
- and the second element of which is the corresponding PEM-encoded
- certificate chain.
- root_certificates: PEM-encoded client root certificates to be used for
- verifying authenticated clients. If omitted, require_client_auth must also
- be omitted or be False.
- require_client_auth: A boolean indicating whether or not to require clients
- to be authenticated. May only be True if root_certificates is not None.
+ private_key_certificate_chain_pairs: A list of pairs of the form
+ [PEM-encoded private key, PEM-encoded certificate chain].
+ root_certificates: An optional byte string of PEM-encoded client root
+ certificates that the server will use to verify client authentication.
+ If omitted, require_client_auth must also be False.
+ require_client_auth: A boolean indicating whether or not to require
+ clients to be authenticated. May only be True if root_certificates
+ is not None.
Returns:
- A ServerCredentials for use with an SSL-enabled Server.
+ A ServerCredentials for use with an SSL-enabled Server. Typically, this
+ object is an argument to add_secure_port() method during server setup.
"""
if len(private_key_certificate_chain_pairs) == 0:
raise ValueError(
@@ -1224,18 +1232,17 @@ def ssl_server_credentials(private_key_certificate_chain_pairs,
def channel_ready_future(channel):
- """Creates a Future tracking when a Channel is ready.
+ """Creates a Future that tracks when a Channel is ready.
- Cancelling the returned Future does not tell the given Channel to abandon
- attempts it may have been making to connect; cancelling merely deactivates the
- returned Future's subscription to the given Channel's connectivity.
+ Cancelling the Future does not affect the channel's state machine.
+ It merely decouples the Future from channel state machine.
Args:
- channel: A Channel.
+ channel: A Channel object.
Returns:
- A Future that matures when the given Channel has connectivity
- ChannelConnectivity.READY.
+ A Future object that matures when the channel connectivity is
+ ChannelConnectivity.READY.
"""
from grpc import _utilities # pylint: disable=cyclic-import
return _utilities.channel_ready_future(channel)
@@ -1245,12 +1252,12 @@ def insecure_channel(target, options=None):
"""Creates an insecure Channel to a server.
Args:
- target: The target to which to connect.
- options: A sequence of string-value pairs according to which to configure
- the created channel.
+ target: The server address
+ options: An optional list of key-value pairs (channel args in gRPC runtime)
+ to configure the channel.
Returns:
- A Channel to the target through which RPCs may be conducted.
+ A Channel object.
"""
from grpc import _channel # pylint: disable=cyclic-import
return _channel.Channel(target, () if options is None else options, None)
@@ -1260,13 +1267,13 @@ def secure_channel(target, credentials, options=None):
"""Creates a secure Channel to a server.
Args:
- target: The target to which to connect.
+ target: The server address.
credentials: A ChannelCredentials instance.
- options: A sequence of string-value pairs according to which to configure
- the created channel.
+ options: An optional list of key-value pairs (channel args in gRPC runtime)
+ to configure the channel.
Returns:
- A Channel to the target through which RPCs may be conducted.
+ A Channel object.
"""
from grpc import _channel # pylint: disable=cyclic-import
return _channel.Channel(target, () if options is None else options,
@@ -1280,21 +1287,19 @@ def server(thread_pool,
"""Creates a Server with which RPCs can be serviced.
Args:
- thread_pool: A futures.ThreadPoolExecutor to be used by the returned Server
- to service RPCs.
- handlers: An optional sequence of GenericRpcHandlers to be used to service
- RPCs after the returned Server is started. These handlers need not be the
- only handlers the server will use to service RPCs; other handlers may
- later be added by calling add_generic_rpc_handlers any time before the
- returned Server is started.
- options: A sequence of string-value pairs according to which to configure
- the created server.
+ thread_pool: A futures.ThreadPoolExecutor to be used by the Server
+ to execute RPC handlers.
+ handlers: An optional list of GenericRpcHandlers used for executing RPCs.
+ More handlers may be added by calling add_generic_rpc_handlers any time
+ before the server is started.
+ options: An optional list of key-value pairs (channel args in gRPC runtime)
+ to configure the channel.
maximum_concurrent_rpcs: The maximum number of concurrent RPCs this server
- will service before returning status RESOURCE_EXHAUSTED, or None to
+ will service before returning RESOURCE_EXHAUSTED status, or None to
indicate no limit.
Returns:
- A Server with which RPCs can be serviced.
+ A Server object.
"""
from grpc import _server # pylint: disable=cyclic-import
return _server.Server(thread_pool, () if handlers is None else handlers, ()
diff --git a/src/python/grpcio/grpc/_cython/_cygrpc/completion_queue.pyx.pxi b/src/python/grpcio/grpc/_cython/_cygrpc/completion_queue.pyx.pxi
index 34b2623d34..502b6556b4 100644
--- a/src/python/grpcio/grpc/_cython/_cygrpc/completion_queue.pyx.pxi
+++ b/src/python/grpcio/grpc/_cython/_cygrpc/completion_queue.pyx.pxi
@@ -37,9 +37,16 @@ cdef int _INTERRUPT_CHECK_PERIOD_MS = 200
cdef class CompletionQueue:
- def __cinit__(self):
+ def __cinit__(self, shutdown_cq=False):
+ cdef grpc_completion_queue_attributes c_attrs
grpc_init()
- with nogil:
+ if shutdown_cq:
+ c_attrs.version = 1
+ c_attrs.cq_completion_type = GRPC_CQ_NEXT
+ c_attrs.cq_polling_type = GRPC_CQ_NON_LISTENING
+ self.c_completion_queue = grpc_completion_queue_create(
+ grpc_completion_queue_factory_lookup(&c_attrs), &c_attrs, NULL);
+ else:
self.c_completion_queue = grpc_completion_queue_create_for_next(NULL)
self.is_shutting_down = False
self.is_shutdown = False
diff --git a/src/python/grpcio/grpc/_cython/_cygrpc/grpc.pxi b/src/python/grpcio/grpc/_cython/_cygrpc/grpc.pxi
index f66f6e4122..1db2056d47 100644
--- a/src/python/grpcio/grpc/_cython/_cygrpc/grpc.pxi
+++ b/src/python/grpcio/grpc/_cython/_cygrpc/grpc.pxi
@@ -217,6 +217,20 @@ cdef extern from "grpc/grpc.h":
GRPC_CALL_ERROR_INVALID_FLAGS
GRPC_CALL_ERROR_INVALID_METADATA
+ ctypedef enum grpc_cq_completion_type:
+ GRPC_CQ_NEXT
+ GRPC_CQ_PLUCK
+
+ ctypedef enum grpc_cq_polling_type:
+ GRPC_CQ_DEFAULT_POLLING
+ GRPC_CQ_NON_LISTENING
+ GRPC_CQ_NON_POLLING
+
+ ctypedef struct grpc_completion_queue_attributes:
+ int version
+ grpc_cq_completion_type cq_completion_type
+ grpc_cq_polling_type cq_polling_type
+
ctypedef enum grpc_connectivity_state:
GRPC_CHANNEL_IDLE
GRPC_CHANNEL_CONNECTING
@@ -309,6 +323,14 @@ cdef extern from "grpc/grpc.h":
void grpc_init() nogil
void grpc_shutdown() nogil
+ ctypedef struct grpc_completion_queue_factory:
+ pass
+
+ grpc_completion_queue_factory *grpc_completion_queue_factory_lookup(
+ const grpc_completion_queue_attributes* attributes) nogil
+ grpc_completion_queue *grpc_completion_queue_create(
+ const grpc_completion_queue_factory* factory,
+ const grpc_completion_queue_attributes* attr, void* reserved) nogil
grpc_completion_queue *grpc_completion_queue_create_for_next(void *reserved) nogil
grpc_event grpc_completion_queue_next(grpc_completion_queue *cq,
diff --git a/src/python/grpcio/grpc/_cython/_cygrpc/server.pyx.pxi b/src/python/grpcio/grpc/_cython/_cygrpc/server.pyx.pxi
index 97192efda7..5233edc789 100644
--- a/src/python/grpcio/grpc/_cython/_cygrpc/server.pyx.pxi
+++ b/src/python/grpcio/grpc/_cython/_cygrpc/server.pyx.pxi
@@ -85,7 +85,7 @@ cdef class Server:
def start(self):
if self.is_started:
raise ValueError("the server has already started")
- self.backup_shutdown_queue = CompletionQueue()
+ self.backup_shutdown_queue = CompletionQueue(shutdown_cq=True)
self.register_completion_queue(self.backup_shutdown_queue)
self.is_started = True
with nogil:
diff --git a/src/python/grpcio_health_checking/setup.py b/src/python/grpcio_health_checking/setup.py
index 1806d311b4..f1c09de2eb 100644
--- a/src/python/grpcio_health_checking/setup.py
+++ b/src/python/grpcio_health_checking/setup.py
@@ -46,7 +46,7 @@ PACKAGE_DIRECTORIES = {
SETUP_REQUIRES = (
'grpcio-tools>={version}'.format(version=grpc_version.VERSION),)
-INSTALL_REQUIRES = ('protobuf>=3.2.0',
+INSTALL_REQUIRES = ('protobuf>=3.3.0',
'grpcio>={version}'.format(version=grpc_version.VERSION),)
COMMAND_CLASS = {
diff --git a/src/python/grpcio_reflection/setup.py b/src/python/grpcio_reflection/setup.py
index e6cf54745e..cb49c3d57e 100644
--- a/src/python/grpcio_reflection/setup.py
+++ b/src/python/grpcio_reflection/setup.py
@@ -47,7 +47,7 @@ PACKAGE_DIRECTORIES = {
SETUP_REQUIRES = (
'grpcio-tools>={version}'.format(version=grpc_version.VERSION),)
-INSTALL_REQUIRES = ('protobuf>=3.2.0',
+INSTALL_REQUIRES = ('protobuf>=3.3.0',
'grpcio>={version}'.format(version=grpc_version.VERSION),)
COMMAND_CLASS = {
diff --git a/src/python/grpcio_tests/setup.py b/src/python/grpcio_tests/setup.py
index b9f0264dae..7ee5336a7d 100644
--- a/src/python/grpcio_tests/setup.py
+++ b/src/python/grpcio_tests/setup.py
@@ -56,7 +56,7 @@ INSTALL_REQUIRES = (
'grpcio>={version}'.format(version=grpc_version.VERSION),
'grpcio-tools>={version}'.format(version=grpc_version.VERSION),
'grpcio-health-checking>={version}'.format(version=grpc_version.VERSION),
- 'oauth2client>=1.4.7', 'protobuf>=3.2.0', 'six>=1.10',)
+ 'oauth2client>=1.4.7', 'protobuf>=3.3.0', 'six>=1.10',)
COMMAND_CLASS = {
# Run `preprocess` *before* doing any packaging!