Use ManualConstructor

reviewable/pr13494/r2
David Garcia Quintas 7 years ago
parent dde6afc0c0
commit d27e2422cc
  1. 8
      src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.cc
  2. 7
      src/core/ext/filters/client_channel/resolver/dns/c_ares/dns_resolver_ares.cc
  3. 7
      src/core/ext/filters/client_channel/resolver/dns/native/dns_resolver.cc
  4. 55
      src/core/ext/filters/client_channel/subchannel.cc
  5. 30
      src/core/lib/support/alloc_new.h

@ -113,7 +113,7 @@
#include "src/core/lib/slice/slice_hash_table.h"
#include "src/core/lib/slice/slice_internal.h"
#include "src/core/lib/slice/slice_string_helpers.h"
#include "src/core/lib/support/alloc_new.h"
#include "src/core/lib/support/manual_constructor.h"
#include "src/core/lib/support/string.h"
#include "src/core/lib/surface/call.h"
#include "src/core/lib/surface/channel.h"
@ -398,7 +398,7 @@ typedef struct glb_lb_policy {
grpc_slice lb_call_status_details;
/** LB call retry backoff state */
grpc_core::Backoff* lb_call_backoff;
grpc_core::ManualConstructor<grpc_core::Backoff> lb_call_backoff;
/** LB call retry timer */
grpc_timer lb_call_retry_timer;
@ -987,7 +987,6 @@ static void glb_destroy(grpc_exec_ctx* exec_ctx, grpc_lb_policy* pol) {
}
grpc_fake_resolver_response_generator_unref(glb_policy->response_generator);
grpc_subchannel_index_unref();
gpr_free(glb_policy->lb_call_backoff);
gpr_free(glb_policy);
}
@ -1469,7 +1468,8 @@ static void lb_call_init_locked(grpc_exec_ctx* exec_ctx,
.set_jitter(GRPC_GRPCLB_RECONNECT_JITTER)
.set_min_connect_timeout(GRPC_GRPCLB_MIN_CONNECT_TIMEOUT_SECONDS * 1000)
.set_max_backoff(GRPC_GRPCLB_RECONNECT_MAX_BACKOFF_SECONDS * 1000);
glb_policy->lb_call_backoff = GPR_NEW(grpc_core::Backoff(backoff_options));
glb_policy->lb_call_backoff.Init(backoff_options);
glb_policy->seen_initial_response = false;
glb_policy->last_client_load_report_counters_were_zero = false;

@ -39,8 +39,8 @@
#include "src/core/lib/iomgr/resolve_address.h"
#include "src/core/lib/iomgr/timer.h"
#include "src/core/lib/json/json.h"
#include "src/core/lib/support/alloc_new.h"
#include "src/core/lib/support/env.h"
#include "src/core/lib/support/manual_constructor.h"
#include "src/core/lib/support/string.h"
#include "src/core/lib/transport/service_config.h"
@ -90,7 +90,7 @@ typedef struct {
bool have_retry_timer;
grpc_timer retry_timer;
/** retry backoff state */
grpc_core::Backoff* backoff;
grpc_core::ManualConstructor<grpc_core::Backoff> backoff;
/** currently resolving addresses */
grpc_lb_addresses* lb_addresses;
@ -353,7 +353,6 @@ static void dns_ares_destroy(grpc_exec_ctx* exec_ctx, grpc_resolver* gr) {
gpr_free(r->name_to_resolve);
gpr_free(r->default_port);
grpc_channel_args_destroy(exec_ctx, r->channel_args);
gpr_free(r->backoff);
gpr_free(r);
}
@ -389,7 +388,7 @@ static grpc_resolver* dns_ares_create(grpc_exec_ctx* exec_ctx,
.set_jitter(GRPC_DNS_RECONNECT_JITTER)
.set_min_connect_timeout(GRPC_DNS_MIN_CONNECT_TIMEOUT_SECONDS * 1000)
.set_max_backoff(GRPC_DNS_RECONNECT_MAX_BACKOFF_SECONDS * 1000);
r->backoff = GPR_NEW(grpc_core::Backoff(backoff_options));
r->backoff.Init(grpc_core::Backoff(backoff_options));
GRPC_CLOSURE_INIT(&r->dns_ares_on_retry_timer_locked,
dns_ares_on_retry_timer_locked, r,
grpc_combiner_scheduler(r->base.combiner));

@ -32,8 +32,8 @@
#include "src/core/lib/iomgr/combiner.h"
#include "src/core/lib/iomgr/resolve_address.h"
#include "src/core/lib/iomgr/timer.h"
#include "src/core/lib/support/alloc_new.h"
#include "src/core/lib/support/env.h"
#include "src/core/lib/support/manual_constructor.h"
#include "src/core/lib/support/string.h"
#define GRPC_DNS_MIN_CONNECT_TIMEOUT_SECONDS 1
@ -71,7 +71,7 @@ typedef struct {
grpc_timer retry_timer;
grpc_closure on_retry;
/** retry backoff state */
grpc_core::Backoff* backoff;
grpc_core::ManualConstructor<grpc_core::Backoff> backoff;
/** currently resolving addresses */
grpc_resolved_addresses* addresses;
@ -233,7 +233,6 @@ static void dns_destroy(grpc_exec_ctx* exec_ctx, grpc_resolver* gr) {
gpr_free(r->name_to_resolve);
gpr_free(r->default_port);
grpc_channel_args_destroy(exec_ctx, r->channel_args);
gpr_free(r->backoff);
gpr_free(r);
}
@ -265,7 +264,7 @@ static grpc_resolver* dns_create(grpc_exec_ctx* exec_ctx,
.set_jitter(GRPC_DNS_RECONNECT_JITTER)
.set_min_connect_timeout(GRPC_DNS_MIN_CONNECT_TIMEOUT_SECONDS * 1000)
.set_max_backoff(GRPC_DNS_RECONNECT_MAX_BACKOFF_SECONDS * 1000);
r->backoff = GPR_NEW(grpc_core::Backoff(backoff_options));
r->backoff.Init(grpc_core::Backoff(backoff_options));
return &r->base;
}

@ -39,7 +39,7 @@
#include "src/core/lib/iomgr/timer.h"
#include "src/core/lib/profiling/timers.h"
#include "src/core/lib/slice/slice_internal.h"
#include "src/core/lib/support/alloc_new.h"
#include "src/core/lib/support/manual_constructor.h"
#include "src/core/lib/surface/channel.h"
#include "src/core/lib/surface/channel_init.h"
#include "src/core/lib/transport/connectivity_state.h"
@ -73,9 +73,6 @@ typedef struct external_state_watcher {
} external_state_watcher;
struct grpc_subchannel {
grpc_subchannel(const grpc_core::Backoff::Options& backoff_options)
: backoff(backoff_options) {}
grpc_connector* connector;
/** refcount
@ -122,7 +119,7 @@ struct grpc_subchannel {
external_state_watcher root_external_state_watcher;
/** backoff state */
grpc_core::Backoff backoff;
grpc_core::ManualConstructor<grpc_core::Backoff> backoff;
grpc_core::Backoff::Result backoff_result;
/** do we have an active alarm? */
@ -344,7 +341,7 @@ grpc_subchannel* grpc_subchannel_create(grpc_exec_ctx* exec_ctx,
}
GRPC_STATS_INC_CLIENT_SUBCHANNELS_CREATED(exec_ctx);
c = GPR_NEW(grpc_subchannel(extract_backoff_options(args->args)));
c = (grpc_subchannel*)gpr_zalloc(sizeof(*c));
c->key = key;
gpr_atm_no_barrier_store(&c->ref_pair, 1 << INTERNAL_REF_BITS);
c->connector = connector;
@ -384,47 +381,7 @@ grpc_subchannel* grpc_subchannel_create(grpc_exec_ctx* exec_ctx,
grpc_schedule_on_exec_ctx);
grpc_connectivity_state_init(&c->state_tracker, GRPC_CHANNEL_IDLE,
"subchannel");
int initial_backoff_ms =
GRPC_SUBCHANNEL_INITIAL_CONNECT_BACKOFF_SECONDS * 1000;
int min_connect_timeout_ms =
GRPC_SUBCHANNEL_RECONNECT_MIN_BACKOFF_SECONDS * 1000;
int max_backoff_ms = GRPC_SUBCHANNEL_RECONNECT_MAX_BACKOFF_SECONDS * 1000;
bool fixed_reconnect_backoff = false;
if (c->args) {
for (size_t i = 0; i < c->args->num_args; i++) {
if (0 == strcmp(c->args->args[i].key,
"grpc.testing.fixed_reconnect_backoff_ms")) {
fixed_reconnect_backoff = true;
initial_backoff_ms = min_connect_timeout_ms = max_backoff_ms =
grpc_channel_arg_get_integer(&c->args->args[i],
{initial_backoff_ms, 100, INT_MAX});
} else if (0 == strcmp(c->args->args[i].key,
GRPC_ARG_MIN_RECONNECT_BACKOFF_MS)) {
fixed_reconnect_backoff = false;
min_connect_timeout_ms = grpc_channel_arg_get_integer(
&c->args->args[i], {min_connect_timeout_ms, 100, INT_MAX});
} else if (0 == strcmp(c->args->args[i].key,
GRPC_ARG_MAX_RECONNECT_BACKOFF_MS)) {
fixed_reconnect_backoff = false;
max_backoff_ms = grpc_channel_arg_get_integer(
&c->args->args[i], {max_backoff_ms, 100, INT_MAX});
} else if (0 == strcmp(c->args->args[i].key,
GRPC_ARG_INITIAL_RECONNECT_BACKOFF_MS)) {
fixed_reconnect_backoff = false;
initial_backoff_ms = grpc_channel_arg_get_integer(
&c->args->args[i], {initial_backoff_ms, 100, INT_MAX});
}
}
}
grpc_core::Backoff::Options backoff_options;
backoff_options.set_initial_backoff(initial_backoff_ms)
.set_multiplier(fixed_reconnect_backoff
? 1.0
: GRPC_SUBCHANNEL_RECONNECT_BACKOFF_MULTIPLIER)
.set_jitter(fixed_reconnect_backoff ? 0.0
: GRPC_SUBCHANNEL_RECONNECT_JITTER)
.set_min_connect_timeout(min_connect_timeout_ms)
.set_max_backoff(max_backoff_ms);
c->backoff.Init(extract_backoff_options(args->args));
gpr_mu_init(&c->mu);
return grpc_subchannel_index_register(exec_ctx, key, c);
@ -483,7 +440,7 @@ static void on_alarm(grpc_exec_ctx* exec_ctx, void* arg, grpc_error* error) {
}
if (error == GRPC_ERROR_NONE) {
gpr_log(GPR_INFO, "Failed to connect to channel, retrying");
c->backoff_result = c->backoff.Step(exec_ctx);
c->backoff_result = c->backoff->Step(exec_ctx);
continue_connect_locked(exec_ctx, c);
gpr_mu_unlock(&c->mu);
} else {
@ -520,7 +477,7 @@ static void maybe_start_connecting_locked(grpc_exec_ctx* exec_ctx,
if (!c->backoff_begun) {
c->backoff_begun = true;
c->backoff_result = c->backoff.Begin(exec_ctx);
c->backoff_result = c->backoff->Begin(exec_ctx);
continue_connect_locked(exec_ctx, c);
} else {
GPR_ASSERT(!c->have_alarm);

@ -1,30 +0,0 @@
/*
*
* Copyright 2017 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifndef GRPC_SUPPORT_ALLOC_NEW_H
#define GRPC_SUPPORT_ALLOC_NEW_H
#include <grpc/support/alloc.h>
#define GPR_NEW(expr) new (gpr_zalloc) expr
inline void* operator new(size_t sz, void* (*alloc_fn)(size_t)) {
return alloc_fn(sz);
}
#endif /* GRPC_SUPPORT_ALLOC_NEW_H */
Loading…
Cancel
Save