|
|
|
@ -39,6 +39,7 @@ |
|
|
|
|
|
|
|
|
|
#include <grpc/support/alloc.h> |
|
|
|
|
#include <grpc/support/log.h> |
|
|
|
|
#include <grpc/support/string_util.h> |
|
|
|
|
#include <grpc/support/sync.h> |
|
|
|
|
#include <grpc/support/useful.h> |
|
|
|
|
|
|
|
|
@ -123,6 +124,7 @@ typedef struct client_channel_channel_data { |
|
|
|
|
/** mutex protecting all variables below in this data structure */ |
|
|
|
|
gpr_mu mu; |
|
|
|
|
/** currently active load balancer */ |
|
|
|
|
char* lb_policy_name; |
|
|
|
|
grpc_lb_policy *lb_policy; |
|
|
|
|
/** maps method names to method_parameters structs */ |
|
|
|
|
grpc_mdstr_hash_table *method_params_table; |
|
|
|
@ -223,6 +225,7 @@ static void watch_lb_policy(grpc_exec_ctx *exec_ctx, channel_data *chand, |
|
|
|
|
static void on_resolver_result_changed(grpc_exec_ctx *exec_ctx, void *arg, |
|
|
|
|
grpc_error *error) { |
|
|
|
|
channel_data *chand = arg; |
|
|
|
|
char *lb_policy_name = NULL; |
|
|
|
|
grpc_lb_policy *lb_policy = NULL; |
|
|
|
|
grpc_lb_policy *old_lb_policy; |
|
|
|
|
grpc_mdstr_hash_table *method_params_table = NULL; |
|
|
|
@ -236,12 +239,11 @@ static void on_resolver_result_changed(grpc_exec_ctx *exec_ctx, void *arg, |
|
|
|
|
lb_policy_args.client_channel_factory = chand->client_channel_factory; |
|
|
|
|
|
|
|
|
|
// Find LB policy name.
|
|
|
|
|
const char *lb_policy_name = NULL; |
|
|
|
|
const grpc_arg *channel_arg = |
|
|
|
|
grpc_channel_args_find(lb_policy_args.args, GRPC_ARG_LB_POLICY_NAME); |
|
|
|
|
if (channel_arg != NULL) { |
|
|
|
|
GPR_ASSERT(channel_arg->type == GRPC_ARG_STRING); |
|
|
|
|
lb_policy_name = channel_arg->value.string; |
|
|
|
|
lb_policy_name = gpr_strdup(channel_arg->value.string); |
|
|
|
|
} |
|
|
|
|
// Special case: If all of the addresses are balancer addresses,
|
|
|
|
|
// assume that we should use the grpclb policy, regardless of what the
|
|
|
|
@ -265,13 +267,14 @@ static void on_resolver_result_changed(grpc_exec_ctx *exec_ctx, void *arg, |
|
|
|
|
"addresses, no backend addresses -- forcing use of grpclb LB " |
|
|
|
|
"policy", |
|
|
|
|
lb_policy_name); |
|
|
|
|
gpr_free(lb_policy_name); |
|
|
|
|
} |
|
|
|
|
lb_policy_name = "grpclb"; |
|
|
|
|
lb_policy_name = gpr_strdup("grpclb"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
// Use pick_first if nothing was specified and we didn't select grpclb
|
|
|
|
|
// above.
|
|
|
|
|
if (lb_policy_name == NULL) lb_policy_name = "pick_first"; |
|
|
|
|
if (lb_policy_name == NULL) lb_policy_name = gpr_strdup("pick_first"); |
|
|
|
|
|
|
|
|
|
lb_policy = |
|
|
|
|
grpc_lb_policy_create(exec_ctx, lb_policy_name, &lb_policy_args); |
|
|
|
@ -299,6 +302,10 @@ static void on_resolver_result_changed(grpc_exec_ctx *exec_ctx, void *arg, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
gpr_mu_lock(&chand->mu); |
|
|
|
|
if (lb_policy_name != NULL) { |
|
|
|
|
gpr_free(chand->lb_policy_name); |
|
|
|
|
chand->lb_policy_name = lb_policy_name; |
|
|
|
|
} |
|
|
|
|
old_lb_policy = chand->lb_policy; |
|
|
|
|
chand->lb_policy = lb_policy; |
|
|
|
|
if (chand->method_params_table != NULL) { |
|
|
|
@ -426,6 +433,18 @@ static void cc_start_transport_op(grpc_exec_ctx *exec_ctx, |
|
|
|
|
gpr_mu_unlock(&chand->mu); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void cc_get_channel_info(grpc_exec_ctx *exec_ctx, |
|
|
|
|
grpc_channel_element *elem, |
|
|
|
|
grpc_channel_info *info) { |
|
|
|
|
channel_data *chand = elem->channel_data; |
|
|
|
|
gpr_mu_lock(&chand->mu); |
|
|
|
|
if (info->lb_policy_name != NULL) { |
|
|
|
|
*info->lb_policy_name = chand->lb_policy_name == NULL |
|
|
|
|
? NULL : gpr_strdup(chand->lb_policy_name); |
|
|
|
|
} |
|
|
|
|
gpr_mu_unlock(&chand->mu); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* Constructor for channel_data */ |
|
|
|
|
static void cc_init_channel_elem(grpc_exec_ctx *exec_ctx, |
|
|
|
|
grpc_channel_element *elem, |
|
|
|
@ -465,6 +484,7 @@ static void cc_destroy_channel_elem(grpc_exec_ctx *exec_ctx, |
|
|
|
|
chand->interested_parties); |
|
|
|
|
GRPC_LB_POLICY_UNREF(exec_ctx, chand->lb_policy, "channel"); |
|
|
|
|
} |
|
|
|
|
gpr_free(chand->lb_policy_name); |
|
|
|
|
if (chand->method_params_table != NULL) { |
|
|
|
|
grpc_mdstr_hash_table_unref(chand->method_params_table); |
|
|
|
|
} |
|
|
|
@ -1048,6 +1068,7 @@ const grpc_channel_filter grpc_client_channel_filter = { |
|
|
|
|
cc_init_channel_elem, |
|
|
|
|
cc_destroy_channel_elem, |
|
|
|
|
cc_get_peer, |
|
|
|
|
cc_get_channel_info, |
|
|
|
|
"client-channel", |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|