From c7705c7c4701fc1cdc512fb0867b0fd699d69b2e Mon Sep 17 00:00:00 2001 From: David Garcia Quintas Date: Wed, 9 Sep 2015 17:21:11 -0700 Subject: [PATCH] Introduced grpc_lb_policy_args --- src/core/client_config/lb_policies/pick_first.c | 13 ++++++------- src/core/client_config/lb_policy_factory.c | 6 ++---- src/core/client_config/lb_policy_factory.h | 11 +++++++---- src/core/client_config/lb_policy_registry.c | 5 ++--- src/core/client_config/lb_policy_registry.h | 3 +-- src/core/client_config/resolvers/dns_resolver.c | 6 ++++-- .../client_config/resolvers/sockaddr_resolver.c | 5 ++++- 7 files changed, 26 insertions(+), 23 deletions(-) diff --git a/src/core/client_config/lb_policies/pick_first.c b/src/core/client_config/lb_policies/pick_first.c index 1c5d058d97f..c8262e92efa 100644 --- a/src/core/client_config/lb_policies/pick_first.c +++ b/src/core/client_config/lb_policies/pick_first.c @@ -320,18 +320,17 @@ static void pick_first_factory_ref(grpc_lb_policy_factory *factory) {} static void pick_first_factory_unref(grpc_lb_policy_factory *factory) {} static grpc_lb_policy *create_pick_first(grpc_lb_policy_factory *factory, - grpc_subchannel **subchannels, - size_t num_subchannels) { + grpc_lb_policy_args *args) { pick_first_lb_policy *p = gpr_malloc(sizeof(*p)); - GPR_ASSERT(num_subchannels); + GPR_ASSERT(args->num_subchannels > 0); memset(p, 0, sizeof(*p)); grpc_lb_policy_init(&p->base, &pick_first_lb_policy_vtable); - p->subchannels = gpr_malloc(sizeof(grpc_subchannel *) * num_subchannels); - p->num_subchannels = num_subchannels; + p->subchannels = gpr_malloc(sizeof(grpc_subchannel *) * args->num_subchannels); + p->num_subchannels = args->num_subchannels; grpc_connectivity_state_init(&p->state_tracker, GRPC_CHANNEL_IDLE, "pick_first"); - memcpy(p->subchannels, subchannels, - sizeof(grpc_subchannel *) * num_subchannels); + memcpy(p->subchannels, args->subchannels, + sizeof(grpc_subchannel *) * args->num_subchannels); grpc_iomgr_closure_init(&p->connectivity_changed, pf_connectivity_changed, p); gpr_mu_init(&p->mu); return &p->base; diff --git a/src/core/client_config/lb_policy_factory.c b/src/core/client_config/lb_policy_factory.c index 533f973cea0..0c097e05421 100644 --- a/src/core/client_config/lb_policy_factory.c +++ b/src/core/client_config/lb_policy_factory.c @@ -41,9 +41,7 @@ void grpc_lb_policy_factory_unref(grpc_lb_policy_factory *factory) { } grpc_lb_policy *grpc_lb_policy_factory_create_lb_policy( - grpc_lb_policy_factory *factory, grpc_subchannel **subchannels, - size_t num_subchannels) { + grpc_lb_policy_factory *factory, grpc_lb_policy_args *args) { if (factory == NULL) return NULL; - return factory->vtable->create_lb_policy(factory, subchannels, - num_subchannels); + return factory->vtable->create_lb_policy(factory, args); } diff --git a/src/core/client_config/lb_policy_factory.h b/src/core/client_config/lb_policy_factory.h index ba057648f77..04610316ee4 100644 --- a/src/core/client_config/lb_policy_factory.h +++ b/src/core/client_config/lb_policy_factory.h @@ -46,14 +46,18 @@ struct grpc_lb_policy_factory { const grpc_lb_policy_factory_vtable *vtable; }; +typedef struct grpc_lb_policy_args { + grpc_subchannel **subchannels; + size_t num_subchannels; +} grpc_lb_policy_args; + struct grpc_lb_policy_factory_vtable { void (*ref)(grpc_lb_policy_factory *factory); void (*unref)(grpc_lb_policy_factory *factory); /** Implementation of grpc_lb_policy_factory_create_lb_policy */ grpc_lb_policy *(*create_lb_policy)(grpc_lb_policy_factory *factory, - grpc_subchannel **subchannels, - size_t num_subchannels); + grpc_lb_policy_args *args); /** Name for the LB policy this factory implements */ const char *name; @@ -64,7 +68,6 @@ void grpc_lb_policy_factory_unref(grpc_lb_policy_factory *factory); /** Create a lb_policy instance. */ grpc_lb_policy *grpc_lb_policy_factory_create_lb_policy( - grpc_lb_policy_factory *factory, grpc_subchannel **subchannels, - size_t num_subchannels); + grpc_lb_policy_factory *factory, grpc_lb_policy_args *args); #endif /* GRPC_INTERNAL_CORE_CONFIG_LB_POLICY_FACTORY_H */ diff --git a/src/core/client_config/lb_policy_registry.c b/src/core/client_config/lb_policy_registry.c index 0a3ebbe4b3e..ae4a077ef3e 100644 --- a/src/core/client_config/lb_policy_registry.c +++ b/src/core/client_config/lb_policy_registry.c @@ -80,10 +80,9 @@ static grpc_lb_policy_factory *lookup_factory(const char* name) { } grpc_lb_policy *grpc_lb_policy_create(const char *name, - grpc_subchannel **subchannels, - size_t num_subchannels) { + grpc_lb_policy_args *args) { grpc_lb_policy_factory *factory = lookup_factory(name); grpc_lb_policy *lb_policy = grpc_lb_policy_factory_create_lb_policy( - factory, subchannels, num_subchannels); + factory, args); return lb_policy; } diff --git a/src/core/client_config/lb_policy_registry.h b/src/core/client_config/lb_policy_registry.h index 75114d9f02e..96fc2a16285 100644 --- a/src/core/client_config/lb_policy_registry.h +++ b/src/core/client_config/lb_policy_registry.h @@ -49,7 +49,6 @@ void grpc_register_lb_policy(grpc_lb_policy_factory *factory); * If \a name is NULL, the default factory from \a grpc_lb_policy_registry_init * will be returned. */ grpc_lb_policy *grpc_lb_policy_create(const char *name, - grpc_subchannel **subchannels, - size_t num_subchannels); + grpc_lb_policy_args *args); #endif /* GRPC_INTERNAL_CORE_CLIENT_CONFIG_LB_POLICY_REGISTRY_H */ diff --git a/src/core/client_config/resolvers/dns_resolver.c b/src/core/client_config/resolvers/dns_resolver.c index 830f3f52868..c389f46c26b 100644 --- a/src/core/client_config/resolvers/dns_resolver.c +++ b/src/core/client_config/resolvers/dns_resolver.c @@ -134,6 +134,7 @@ static void dns_on_resolved(void *arg, grpc_resolved_addresses *addresses) { grpc_lb_policy *lb_policy; size_t i; if (addresses) { + grpc_lb_policy_args lb_policy_args; config = grpc_client_config_create(); subchannels = gpr_malloc(sizeof(grpc_subchannel *) * addresses->naddrs); for (i = 0; i < addresses->naddrs; i++) { @@ -143,8 +144,9 @@ static void dns_on_resolved(void *arg, grpc_resolved_addresses *addresses) { subchannels[i] = grpc_subchannel_factory_create_subchannel( r->subchannel_factory, &args); } - lb_policy = grpc_lb_policy_create(r->lb_policy_name, subchannels, - addresses->naddrs); + lb_policy_args.subchannels = subchannels; + lb_policy_args.num_subchannels = addresses->naddrs; + lb_policy = grpc_lb_policy_create(r->lb_policy_name, &lb_policy_args); grpc_client_config_set_lb_policy(config, lb_policy); GRPC_LB_POLICY_UNREF(lb_policy, "construction"); grpc_resolved_addresses_destroy(addresses); diff --git a/src/core/client_config/resolvers/sockaddr_resolver.c b/src/core/client_config/resolvers/sockaddr_resolver.c index 1bff1d7abf4..d378ab963e5 100644 --- a/src/core/client_config/resolvers/sockaddr_resolver.c +++ b/src/core/client_config/resolvers/sockaddr_resolver.c @@ -121,6 +121,7 @@ static void sockaddr_next(grpc_resolver *resolver, static void sockaddr_maybe_finish_next_locked(sockaddr_resolver *r) { grpc_client_config *cfg; grpc_lb_policy *lb_policy; + grpc_lb_policy_args lb_policy_args; grpc_subchannel **subchannels; grpc_subchannel_args args; @@ -135,8 +136,10 @@ static void sockaddr_maybe_finish_next_locked(sockaddr_resolver *r) { subchannels[i] = grpc_subchannel_factory_create_subchannel( r->subchannel_factory, &args); } + lb_policy_args.subchannels = subchannels; + lb_policy_args.num_subchannels = r->num_addrs; lb_policy = - grpc_lb_policy_create(r->lb_policy_name, subchannels, r->num_addrs); + grpc_lb_policy_create(r->lb_policy_name, &lb_policy_args); gpr_free(subchannels); grpc_client_config_set_lb_policy(cfg, lb_policy); GRPC_LB_POLICY_UNREF(lb_policy, "unix");