Skip to content

Commit

Permalink
Change xds args to const
Browse files Browse the repository at this point in the history
  • Loading branch information
AspirinSJL committed Aug 14, 2019
1 parent 58aca9a commit c8dc36b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
19 changes: 9 additions & 10 deletions src/core/ext/filters/client_channel/lb_policy/xds/xds.cc
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ class XdsLb : public LoadBalancingPolicy {
OrphanablePtr<LbChannelState> pending_lb_chand_;

// Timeout in milliseconds for the LB call. 0 means no deadline.
int lb_call_timeout_ms_ = 0;
const grpc_millis lb_call_timeout_ms_;

// Whether the checks for fallback at startup are ALL pending. There are
// several cases where this can be reset:
Expand All @@ -587,7 +587,7 @@ class XdsLb : public LoadBalancingPolicy {
bool fallback_at_startup_checks_pending_ = false;
// Timeout in milliseconds for before using fallback backend addresses.
// 0 means not using fallback.
int lb_fallback_timeout_ms_ = 0;
const grpc_millis lb_fallback_timeout_ms_;
// The backend addresses from the resolver.
ServerAddressList fallback_backend_addresses_;
// Fallback timer.
Expand Down Expand Up @@ -1705,7 +1705,13 @@ grpc_channel_args* BuildBalancerChannelArgs(const grpc_channel_args* args) {
//

XdsLb::XdsLb(Args args)
: LoadBalancingPolicy(std::move(args)), locality_map_(this) {
: LoadBalancingPolicy(std::move(args)),
lb_call_timeout_ms_(grpc_channel_args_find_integer(
args.args, GRPC_ARG_GRPCLB_CALL_TIMEOUT_MS, {0, 0, INT_MAX})),
lb_fallback_timeout_ms_(grpc_channel_args_find_integer(
args.args, GRPC_ARG_XDS_FALLBACK_TIMEOUT_MS,
{GRPC_XDS_DEFAULT_FALLBACK_TIMEOUT_MS, 0, INT_MAX})),
locality_map_(this) {
// Record server name.
const grpc_arg* arg = grpc_channel_args_find(args.args, GRPC_ARG_SERVER_URI);
const char* server_uri = grpc_channel_arg_get_string(arg);
Expand All @@ -1719,13 +1725,6 @@ XdsLb::XdsLb(Args args)
server_name_);
}
grpc_uri_destroy(uri);
// Record LB call timeout.
arg = grpc_channel_args_find(args.args, GRPC_ARG_GRPCLB_CALL_TIMEOUT_MS);
lb_call_timeout_ms_ = grpc_channel_arg_get_integer(arg, {0, 0, INT_MAX});
// Record fallback timeout.
arg = grpc_channel_args_find(args.args, GRPC_ARG_XDS_FALLBACK_TIMEOUT_MS);
lb_fallback_timeout_ms_ = grpc_channel_arg_get_integer(
arg, {GRPC_XDS_DEFAULT_FALLBACK_TIMEOUT_MS, 0, INT_MAX});
}

XdsLb::~XdsLb() {
Expand Down
19 changes: 19 additions & 0 deletions src/core/lib/channel/channel_args.cc
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,13 @@ int grpc_channel_arg_get_integer(const grpc_arg* arg,
return arg->value.integer;
}

int grpc_channel_args_find_integer(const grpc_channel_args* args,
const char* name,
const grpc_integer_options options) {
const grpc_arg* arg = grpc_channel_args_find(args, name);
return grpc_channel_arg_get_integer(arg, options);
}

char* grpc_channel_arg_get_string(const grpc_arg* arg) {
if (arg == nullptr) return nullptr;
if (arg->type != GRPC_ARG_STRING) {
Expand All @@ -266,6 +273,12 @@ char* grpc_channel_arg_get_string(const grpc_arg* arg) {
return arg->value.string;
}

char* grpc_channel_args_find_string(const grpc_channel_args* args,
const char* name) {
const grpc_arg* arg = grpc_channel_args_find(args, name);
return grpc_channel_arg_get_string(arg);
}

bool grpc_channel_arg_get_bool(const grpc_arg* arg, bool default_value) {
if (arg == nullptr) return default_value;
if (arg->type != GRPC_ARG_INTEGER) {
Expand All @@ -284,6 +297,12 @@ bool grpc_channel_arg_get_bool(const grpc_arg* arg, bool default_value) {
}
}

bool grpc_channel_args_find_bool(const grpc_channel_args* args,
const char* name, bool default_value) {
const grpc_arg* arg = grpc_channel_args_find(args, name);
return grpc_channel_arg_get_bool(arg, default_value);
}

bool grpc_channel_args_want_minimal_stack(const grpc_channel_args* args) {
return grpc_channel_arg_get_bool(
grpc_channel_args_find(args, GRPC_ARG_MINIMAL_STACK), false);
Expand Down
18 changes: 16 additions & 2 deletions src/core/lib/channel/channel_args.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,30 @@ typedef struct grpc_integer_options {
int max_value;
} grpc_integer_options;

/** Returns the value of \a arg, subject to the contraints in \a options. */
/** Returns the value of \a arg, subject to the constraints in \a options. */
int grpc_channel_arg_get_integer(const grpc_arg* arg,
const grpc_integer_options options);
/** Similar to the above, but needs to find the arg from \a args by the name
* first. */
int grpc_channel_args_find_integer(const grpc_channel_args* args,
const char* name,
const grpc_integer_options options);

/** Returns the value of \a arg if \a arg is of type GRPC_ARG_STRING.
Otherwise, emits a warning log, and returns nullptr.
If arg is nullptr, returns nullptr, and does not emit a warning. */
char* grpc_channel_arg_get_string(const grpc_arg* arg);

/** Similar to the above, but needs to find the arg from \a args by the name
* first. */
char* grpc_channel_args_find_string(const grpc_channel_args* args,
const char* name);
/** If \a arg is of type GRPC_ARG_INTEGER, returns true if it's non-zero.
* Returns \a default_value if \a arg is of other types. */
bool grpc_channel_arg_get_bool(const grpc_arg* arg, bool default_value);
/** Similar to the above, but needs to find the arg from \a args by the name
* first. */
bool grpc_channel_args_find_bool(const grpc_channel_args* args,
const char* name, bool default_value);

// Helpers for creating channel args.
grpc_arg grpc_channel_arg_string_create(char* name, char* value);
Expand Down

0 comments on commit c8dc36b

Please sign in to comment.