Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | a93a25f | 2016-01-28 13:55:49 -0800 | [diff] [blame] | 3 | * Copyright 2015-2016, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | #include "src/core/channel/client_channel.h" |
| 35 | |
| 36 | #include <stdio.h> |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 37 | #include <string.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 38 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 39 | #include <grpc/support/alloc.h> |
| 40 | #include <grpc/support/log.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 41 | #include <grpc/support/sync.h> |
| 42 | #include <grpc/support/useful.h> |
| 43 | |
Craig Tiller | 8910ac6 | 2015-10-08 16:49:15 -0700 | [diff] [blame] | 44 | #include "src/core/channel/channel_args.h" |
| 45 | #include "src/core/channel/connected_channel.h" |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 46 | #include "src/core/channel/subchannel_call_holder.h" |
Craig Tiller | 8910ac6 | 2015-10-08 16:49:15 -0700 | [diff] [blame] | 47 | #include "src/core/iomgr/iomgr.h" |
| 48 | #include "src/core/profiling/timers.h" |
| 49 | #include "src/core/support/string.h" |
| 50 | #include "src/core/surface/channel.h" |
| 51 | #include "src/core/transport/connectivity_state.h" |
| 52 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 53 | /* Client channel implementation */ |
| 54 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 55 | typedef grpc_subchannel_call_holder call_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 56 | |
Craig Tiller | 800dacb | 2015-10-06 09:10:26 -0700 | [diff] [blame] | 57 | typedef struct client_channel_channel_data { |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 58 | /** resolver for this channel */ |
| 59 | grpc_resolver *resolver; |
Craig Tiller | 20a3c35 | 2015-08-05 08:39:50 -0700 | [diff] [blame] | 60 | /** have we started resolving this channel */ |
| 61 | int started_resolving; |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 62 | |
Craig Tiller | 9d94b60 | 2015-07-01 14:23:18 -0700 | [diff] [blame] | 63 | /** mutex protecting client configuration, including all |
| 64 | variables below in this data structure */ |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 65 | gpr_mu mu_config; |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 66 | /** currently active load balancer - guarded by mu_config */ |
| 67 | grpc_lb_policy *lb_policy; |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 68 | /** incoming configuration - set by resolver.next |
| 69 | guarded by mu_config */ |
| 70 | grpc_client_config *incoming_configuration; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 71 | /** a list of closures that are all waiting for config to come in */ |
Craig Tiller | d9ccbbf | 2015-09-22 09:30:00 -0700 | [diff] [blame] | 72 | grpc_closure_list waiting_for_config_closures; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 73 | /** resolver callback */ |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 74 | grpc_closure on_config_changed; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 75 | /** connectivity state being tracked */ |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 76 | grpc_connectivity_state_tracker state_tracker; |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 77 | /** when an lb_policy arrives, should we try to exit idle */ |
| 78 | int exit_idle_when_lb_policy_arrives; |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 79 | /** owning stack */ |
| 80 | grpc_channel_stack *owning_stack; |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame^] | 81 | /** interested parties (owned) */ |
| 82 | grpc_pollset_set *interested_parties; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 83 | } channel_data; |
| 84 | |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 85 | /** We create one watcher for each new lb_policy that is returned from a |
| 86 | resolver, |
| 87 | to watch for state changes from the lb_policy. When a state change is seen, |
| 88 | we |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 89 | update the channel, and create a new watcher */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 90 | typedef struct { |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 91 | channel_data *chand; |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 92 | grpc_closure on_changed; |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 93 | grpc_connectivity_state state; |
| 94 | grpc_lb_policy *lb_policy; |
| 95 | } lb_policy_connectivity_watcher; |
| 96 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 97 | typedef struct { |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 98 | grpc_closure closure; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 99 | grpc_call_element *elem; |
| 100 | } waiting_call; |
| 101 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 102 | static char *cc_get_peer(grpc_exec_ctx *exec_ctx, grpc_call_element *elem) { |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 103 | return grpc_subchannel_call_holder_get_peer(exec_ctx, elem->call_data); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 104 | } |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 105 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 106 | static void cc_start_transport_stream_op(grpc_exec_ctx *exec_ctx, |
| 107 | grpc_call_element *elem, |
| 108 | grpc_transport_stream_op *op) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 109 | GRPC_CALL_LOG_OP(GPR_INFO, elem, op); |
| 110 | grpc_subchannel_call_holder_perform_op(exec_ctx, elem->call_data, op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 113 | static void watch_lb_policy(grpc_exec_ctx *exec_ctx, channel_data *chand, |
| 114 | grpc_lb_policy *lb_policy, |
| 115 | grpc_connectivity_state current_state); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 116 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 117 | static void on_lb_policy_state_changed_locked( |
| 118 | grpc_exec_ctx *exec_ctx, lb_policy_connectivity_watcher *w) { |
Craig Tiller | cb2609f | 2015-11-24 17:19:19 -0800 | [diff] [blame] | 119 | grpc_connectivity_state publish_state = w->state; |
Craig Tiller | 5795da7 | 2015-09-17 15:27:13 -0700 | [diff] [blame] | 120 | /* check if the notification is for a stale policy */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 121 | if (w->lb_policy != w->chand->lb_policy) return; |
Craig Tiller | 5795da7 | 2015-09-17 15:27:13 -0700 | [diff] [blame] | 122 | |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 123 | if (publish_state == GRPC_CHANNEL_FATAL_FAILURE && |
| 124 | w->chand->resolver != NULL) { |
Craig Tiller | cb2609f | 2015-11-24 17:19:19 -0800 | [diff] [blame] | 125 | publish_state = GRPC_CHANNEL_TRANSIENT_FAILURE; |
| 126 | grpc_resolver_channel_saw_error(exec_ctx, w->chand->resolver); |
Craig Tiller | f62c4d5 | 2015-12-04 07:43:07 -0800 | [diff] [blame] | 127 | GRPC_LB_POLICY_UNREF(exec_ctx, w->chand->lb_policy, "channel"); |
| 128 | w->chand->lb_policy = NULL; |
Craig Tiller | cb2609f | 2015-11-24 17:19:19 -0800 | [diff] [blame] | 129 | } |
| 130 | grpc_connectivity_state_set(exec_ctx, &w->chand->state_tracker, publish_state, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 131 | "lb_changed"); |
| 132 | if (w->state != GRPC_CHANNEL_FATAL_FAILURE) { |
| 133 | watch_lb_policy(exec_ctx, w->chand, w->lb_policy, w->state); |
| 134 | } |
Craig Tiller | 5795da7 | 2015-09-17 15:27:13 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 137 | static void on_lb_policy_state_changed(grpc_exec_ctx *exec_ctx, void *arg, |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 138 | bool iomgr_success) { |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 139 | lb_policy_connectivity_watcher *w = arg; |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 140 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 141 | gpr_mu_lock(&w->chand->mu_config); |
| 142 | on_lb_policy_state_changed_locked(exec_ctx, w); |
| 143 | gpr_mu_unlock(&w->chand->mu_config); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 144 | |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 145 | GRPC_CHANNEL_STACK_UNREF(exec_ctx, w->chand->owning_stack, "watch_lb_policy"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 146 | gpr_free(w); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 149 | static void watch_lb_policy(grpc_exec_ctx *exec_ctx, channel_data *chand, |
| 150 | grpc_lb_policy *lb_policy, |
| 151 | grpc_connectivity_state current_state) { |
| 152 | lb_policy_connectivity_watcher *w = gpr_malloc(sizeof(*w)); |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 153 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "watch_lb_policy"); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 154 | |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 155 | w->chand = chand; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 156 | grpc_closure_init(&w->on_changed, on_lb_policy_state_changed, w); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 157 | w->state = current_state; |
| 158 | w->lb_policy = lb_policy; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 159 | grpc_lb_policy_notify_on_state_change(exec_ctx, lb_policy, &w->state, |
| 160 | &w->on_changed); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 163 | static void cc_on_config_changed(grpc_exec_ctx *exec_ctx, void *arg, |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 164 | bool iomgr_success) { |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 165 | channel_data *chand = arg; |
| 166 | grpc_lb_policy *lb_policy = NULL; |
| 167 | grpc_lb_policy *old_lb_policy; |
| 168 | grpc_resolver *old_resolver; |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 169 | grpc_connectivity_state state = GRPC_CHANNEL_TRANSIENT_FAILURE; |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 170 | int exit_idle = 0; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 171 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 172 | if (chand->incoming_configuration != NULL) { |
| 173 | lb_policy = grpc_client_config_get_lb_policy(chand->incoming_configuration); |
| 174 | if (lb_policy != NULL) { |
| 175 | GRPC_LB_POLICY_REF(lb_policy, "channel"); |
| 176 | GRPC_LB_POLICY_REF(lb_policy, "config_change"); |
| 177 | state = grpc_lb_policy_check_connectivity(exec_ctx, lb_policy); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 178 | } |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 179 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 180 | grpc_client_config_unref(exec_ctx, chand->incoming_configuration); |
| 181 | } |
| 182 | |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 183 | chand->incoming_configuration = NULL; |
| 184 | |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 185 | if (lb_policy != NULL) { |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame^] | 186 | grpc_pollset_set_add_pollset_set(exec_ctx, lb_policy->interested_parties, |
| 187 | chand->interested_parties); |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 188 | } |
| 189 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 190 | gpr_mu_lock(&chand->mu_config); |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 191 | old_lb_policy = chand->lb_policy; |
| 192 | chand->lb_policy = lb_policy; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 193 | if (lb_policy != NULL || chand->resolver == NULL /* disconnected */) { |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 194 | grpc_exec_ctx_enqueue_list(exec_ctx, &chand->waiting_for_config_closures, |
| 195 | NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 196 | } |
| 197 | if (lb_policy != NULL && chand->exit_idle_when_lb_policy_arrives) { |
| 198 | GRPC_LB_POLICY_REF(lb_policy, "exit_idle"); |
| 199 | exit_idle = 1; |
| 200 | chand->exit_idle_when_lb_policy_arrives = 0; |
| 201 | } |
Craig Tiller | 9846503 | 2015-06-29 14:36:42 -0700 | [diff] [blame] | 202 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 203 | if (iomgr_success && chand->resolver) { |
| 204 | grpc_resolver *resolver = chand->resolver; |
| 205 | GRPC_RESOLVER_REF(resolver, "channel-next"); |
| 206 | grpc_connectivity_state_set(exec_ctx, &chand->state_tracker, state, |
| 207 | "new_lb+resolver"); |
| 208 | if (lb_policy != NULL) { |
| 209 | watch_lb_policy(exec_ctx, chand, lb_policy, state); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 210 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 211 | gpr_mu_unlock(&chand->mu_config); |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 212 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 213 | grpc_resolver_next(exec_ctx, resolver, &chand->incoming_configuration, |
| 214 | &chand->on_config_changed); |
| 215 | GRPC_RESOLVER_UNREF(exec_ctx, resolver, "channel-next"); |
| 216 | } else { |
| 217 | old_resolver = chand->resolver; |
| 218 | chand->resolver = NULL; |
| 219 | grpc_connectivity_state_set(exec_ctx, &chand->state_tracker, |
| 220 | GRPC_CHANNEL_FATAL_FAILURE, "resolver_gone"); |
| 221 | gpr_mu_unlock(&chand->mu_config); |
| 222 | if (old_resolver != NULL) { |
| 223 | grpc_resolver_shutdown(exec_ctx, old_resolver); |
| 224 | GRPC_RESOLVER_UNREF(exec_ctx, old_resolver, "channel"); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 225 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 226 | } |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 227 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 228 | if (exit_idle) { |
| 229 | grpc_lb_policy_exit_idle(exec_ctx, lb_policy); |
| 230 | GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "exit_idle"); |
| 231 | } |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 232 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 233 | if (old_lb_policy != NULL) { |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame^] | 234 | grpc_pollset_set_del_pollset_set( |
| 235 | exec_ctx, old_lb_policy->interested_parties, chand->interested_parties); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 236 | GRPC_LB_POLICY_UNREF(exec_ctx, old_lb_policy, "channel"); |
| 237 | } |
Craig Tiller | 000cd8f | 2015-09-18 07:20:29 -0700 | [diff] [blame] | 238 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 239 | if (lb_policy != NULL) { |
| 240 | GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "config_change"); |
| 241 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 242 | |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 243 | GRPC_CHANNEL_STACK_UNREF(exec_ctx, chand->owning_stack, "resolver"); |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 246 | static void cc_start_transport_op(grpc_exec_ctx *exec_ctx, |
| 247 | grpc_channel_element *elem, |
| 248 | grpc_transport_op *op) { |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 249 | channel_data *chand = elem->channel_data; |
Craig Tiller | 9846503 | 2015-06-29 14:36:42 -0700 | [diff] [blame] | 250 | grpc_resolver *destroy_resolver = NULL; |
Craig Tiller | 000cd8f | 2015-09-18 07:20:29 -0700 | [diff] [blame] | 251 | |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 252 | grpc_exec_ctx_enqueue(exec_ctx, op->on_consumed, true, NULL); |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 253 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 254 | GPR_ASSERT(op->set_accept_stream == NULL); |
Craig Tiller | 28bf891 | 2015-12-07 16:07:04 -0800 | [diff] [blame] | 255 | if (op->bind_pollset != NULL) { |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame^] | 256 | grpc_pollset_set_add_pollset(exec_ctx, chand->interested_parties, |
Craig Tiller | e2c6237 | 2015-12-07 16:11:03 -0800 | [diff] [blame] | 257 | op->bind_pollset); |
Craig Tiller | 28bf891 | 2015-12-07 16:07:04 -0800 | [diff] [blame] | 258 | } |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 259 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 260 | gpr_mu_lock(&chand->mu_config); |
| 261 | if (op->on_connectivity_state_change != NULL) { |
| 262 | grpc_connectivity_state_notify_on_state_change( |
| 263 | exec_ctx, &chand->state_tracker, op->connectivity_state, |
| 264 | op->on_connectivity_state_change); |
| 265 | op->on_connectivity_state_change = NULL; |
| 266 | op->connectivity_state = NULL; |
| 267 | } |
| 268 | |
Craig Tiller | 26dab31 | 2015-12-07 14:43:47 -0800 | [diff] [blame] | 269 | if (op->send_ping != NULL) { |
Craig Tiller | 87b71e2 | 2015-12-07 15:14:14 -0800 | [diff] [blame] | 270 | if (chand->lb_policy == NULL) { |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 271 | grpc_exec_ctx_enqueue(exec_ctx, op->send_ping, false, NULL); |
Craig Tiller | 26dab31 | 2015-12-07 14:43:47 -0800 | [diff] [blame] | 272 | } else { |
Craig Tiller | 28bf891 | 2015-12-07 16:07:04 -0800 | [diff] [blame] | 273 | grpc_lb_policy_ping_one(exec_ctx, chand->lb_policy, op->send_ping); |
Craig Tiller | 26dab31 | 2015-12-07 14:43:47 -0800 | [diff] [blame] | 274 | op->bind_pollset = NULL; |
| 275 | } |
| 276 | op->send_ping = NULL; |
| 277 | } |
| 278 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 279 | if (op->disconnect && chand->resolver != NULL) { |
| 280 | grpc_connectivity_state_set(exec_ctx, &chand->state_tracker, |
| 281 | GRPC_CHANNEL_FATAL_FAILURE, "disconnect"); |
| 282 | destroy_resolver = chand->resolver; |
| 283 | chand->resolver = NULL; |
| 284 | if (chand->lb_policy != NULL) { |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 285 | grpc_pollset_set_del_pollset_set(exec_ctx, |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame^] | 286 | chand->lb_policy->interested_parties, |
| 287 | chand->interested_parties); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 288 | GRPC_LB_POLICY_UNREF(exec_ctx, chand->lb_policy, "channel"); |
| 289 | chand->lb_policy = NULL; |
Craig Tiller | d2cc459 | 2015-07-01 07:50:47 -0700 | [diff] [blame] | 290 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 291 | } |
| 292 | gpr_mu_unlock(&chand->mu_config); |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 293 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 294 | if (destroy_resolver) { |
| 295 | grpc_resolver_shutdown(exec_ctx, destroy_resolver); |
| 296 | GRPC_RESOLVER_UNREF(exec_ctx, destroy_resolver, "channel"); |
| 297 | } |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 298 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 299 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 300 | typedef struct { |
| 301 | grpc_metadata_batch *initial_metadata; |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 302 | grpc_connected_subchannel **connected_subchannel; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 303 | grpc_closure *on_ready; |
| 304 | grpc_call_element *elem; |
| 305 | grpc_closure closure; |
| 306 | } continue_picking_args; |
| 307 | |
| 308 | static int cc_pick_subchannel(grpc_exec_ctx *exec_ctx, void *arg, |
| 309 | grpc_metadata_batch *initial_metadata, |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 310 | grpc_connected_subchannel **connected_subchannel, |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 311 | grpc_closure *on_ready); |
| 312 | |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 313 | static void continue_picking(grpc_exec_ctx *exec_ctx, void *arg, bool success) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 314 | continue_picking_args *cpa = arg; |
| 315 | if (!success) { |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 316 | grpc_exec_ctx_enqueue(exec_ctx, cpa->on_ready, false, NULL); |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 317 | } else if (cpa->connected_subchannel == NULL) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 318 | /* cancelled, do nothing */ |
| 319 | } else if (cc_pick_subchannel(exec_ctx, cpa->elem, cpa->initial_metadata, |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 320 | cpa->connected_subchannel, cpa->on_ready)) { |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 321 | grpc_exec_ctx_enqueue(exec_ctx, cpa->on_ready, true, NULL); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 322 | } |
| 323 | gpr_free(cpa); |
| 324 | } |
| 325 | |
| 326 | static int cc_pick_subchannel(grpc_exec_ctx *exec_ctx, void *elemp, |
| 327 | grpc_metadata_batch *initial_metadata, |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 328 | grpc_connected_subchannel **connected_subchannel, |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 329 | grpc_closure *on_ready) { |
| 330 | grpc_call_element *elem = elemp; |
| 331 | channel_data *chand = elem->channel_data; |
| 332 | call_data *calld = elem->call_data; |
| 333 | continue_picking_args *cpa; |
| 334 | grpc_closure *closure; |
| 335 | |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 336 | GPR_ASSERT(connected_subchannel); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 337 | |
| 338 | gpr_mu_lock(&chand->mu_config); |
| 339 | if (initial_metadata == NULL) { |
| 340 | if (chand->lb_policy != NULL) { |
Craig Tiller | ab33b48 | 2015-11-21 08:11:04 -0800 | [diff] [blame] | 341 | grpc_lb_policy_cancel_pick(exec_ctx, chand->lb_policy, |
| 342 | connected_subchannel); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 343 | } |
| 344 | for (closure = chand->waiting_for_config_closures.head; closure != NULL; |
| 345 | closure = grpc_closure_next(closure)) { |
| 346 | cpa = closure->cb_arg; |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 347 | if (cpa->connected_subchannel == connected_subchannel) { |
| 348 | cpa->connected_subchannel = NULL; |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 349 | grpc_exec_ctx_enqueue(exec_ctx, cpa->on_ready, false, NULL); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | gpr_mu_unlock(&chand->mu_config); |
| 353 | return 1; |
| 354 | } |
| 355 | if (chand->lb_policy != NULL) { |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 356 | grpc_lb_policy *lb_policy = chand->lb_policy; |
| 357 | int r; |
| 358 | GRPC_LB_POLICY_REF(lb_policy, "cc_pick_subchannel"); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 359 | gpr_mu_unlock(&chand->mu_config); |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 360 | r = grpc_lb_policy_pick(exec_ctx, lb_policy, calld->pollset, |
| 361 | initial_metadata, connected_subchannel, on_ready); |
| 362 | GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "cc_pick_subchannel"); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 363 | return r; |
| 364 | } |
| 365 | if (chand->resolver != NULL && !chand->started_resolving) { |
| 366 | chand->started_resolving = 1; |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 367 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver"); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 368 | grpc_resolver_next(exec_ctx, chand->resolver, |
| 369 | &chand->incoming_configuration, |
| 370 | &chand->on_config_changed); |
| 371 | } |
| 372 | cpa = gpr_malloc(sizeof(*cpa)); |
| 373 | cpa->initial_metadata = initial_metadata; |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 374 | cpa->connected_subchannel = connected_subchannel; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 375 | cpa->on_ready = on_ready; |
| 376 | cpa->elem = elem; |
| 377 | grpc_closure_init(&cpa->closure, continue_picking, cpa); |
| 378 | grpc_closure_list_add(&chand->waiting_for_config_closures, &cpa->closure, 1); |
| 379 | gpr_mu_unlock(&chand->mu_config); |
| 380 | return 0; |
| 381 | } |
| 382 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 383 | /* Constructor for call_data */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 384 | static void init_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 385 | grpc_call_element_args *args) { |
Craig Tiller | 11beb9a | 2015-11-24 10:29:32 -0800 | [diff] [blame] | 386 | grpc_subchannel_call_holder_init(elem->call_data, cc_pick_subchannel, elem, |
| 387 | args->call_stack); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | /* Destructor for call_data */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 391 | static void destroy_call_elem(grpc_exec_ctx *exec_ctx, |
| 392 | grpc_call_element *elem) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 393 | grpc_subchannel_call_holder_destroy(exec_ctx, elem->call_data); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | /* Constructor for channel_data */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 397 | static void init_channel_elem(grpc_exec_ctx *exec_ctx, |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 398 | grpc_channel_element *elem, |
| 399 | grpc_channel_element_args *args) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 400 | channel_data *chand = elem->channel_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 401 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 402 | memset(chand, 0, sizeof(*chand)); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 403 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 404 | GPR_ASSERT(args->is_last); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 405 | GPR_ASSERT(elem->filter == &grpc_client_channel_filter); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 406 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 407 | gpr_mu_init(&chand->mu_config); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 408 | grpc_closure_init(&chand->on_config_changed, cc_on_config_changed, chand); |
Craig Tiller | 11beb9a | 2015-11-24 10:29:32 -0800 | [diff] [blame] | 409 | chand->owning_stack = args->channel_stack; |
Craig Tiller | 9846503 | 2015-06-29 14:36:42 -0700 | [diff] [blame] | 410 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 411 | grpc_connectivity_state_init(&chand->state_tracker, GRPC_CHANNEL_IDLE, |
| 412 | "client_channel"); |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame^] | 413 | chand->interested_parties = grpc_pollset_set_create(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | /* Destructor for channel_data */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 417 | static void destroy_channel_elem(grpc_exec_ctx *exec_ctx, |
| 418 | grpc_channel_element *elem) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 419 | channel_data *chand = elem->channel_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 420 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 421 | if (chand->resolver != NULL) { |
| 422 | grpc_resolver_shutdown(exec_ctx, chand->resolver); |
| 423 | GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel"); |
| 424 | } |
| 425 | if (chand->lb_policy != NULL) { |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 426 | grpc_pollset_set_del_pollset_set(exec_ctx, |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame^] | 427 | chand->lb_policy->interested_parties, |
| 428 | chand->interested_parties); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 429 | GRPC_LB_POLICY_UNREF(exec_ctx, chand->lb_policy, "channel"); |
| 430 | } |
| 431 | grpc_connectivity_state_destroy(exec_ctx, &chand->state_tracker); |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame^] | 432 | grpc_pollset_set_destroy(chand->interested_parties); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 433 | gpr_mu_destroy(&chand->mu_config); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 434 | } |
| 435 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 436 | static void cc_set_pollset(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
| 437 | grpc_pollset *pollset) { |
| 438 | call_data *calld = elem->call_data; |
| 439 | calld->pollset = pollset; |
| 440 | } |
| 441 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 442 | const grpc_channel_filter grpc_client_channel_filter = { |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame^] | 443 | cc_start_transport_stream_op, |
| 444 | cc_start_transport_op, |
| 445 | sizeof(call_data), |
| 446 | init_call_elem, |
| 447 | cc_set_pollset, |
| 448 | destroy_call_elem, |
| 449 | sizeof(channel_data), |
| 450 | init_channel_elem, |
| 451 | destroy_channel_elem, |
| 452 | cc_get_peer, |
| 453 | "client-channel", |
Craig Tiller | 87d5b19 | 2015-04-16 14:37:57 -0700 | [diff] [blame] | 454 | }; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 455 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 456 | void grpc_client_channel_set_resolver(grpc_exec_ctx *exec_ctx, |
| 457 | grpc_channel_stack *channel_stack, |
| 458 | grpc_resolver *resolver) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 459 | /* post construction initialization: set the transport setup pointer */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 460 | grpc_channel_element *elem = grpc_channel_stack_last_element(channel_stack); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 461 | channel_data *chand = elem->channel_data; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 462 | gpr_mu_lock(&chand->mu_config); |
| 463 | GPR_ASSERT(!chand->resolver); |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 464 | chand->resolver = resolver; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 465 | GRPC_RESOLVER_REF(resolver, "channel"); |
| 466 | if (!grpc_closure_list_empty(chand->waiting_for_config_closures) || |
| 467 | chand->exit_idle_when_lb_policy_arrives) { |
| 468 | chand->started_resolving = 1; |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 469 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 470 | grpc_resolver_next(exec_ctx, resolver, &chand->incoming_configuration, |
| 471 | &chand->on_config_changed); |
| 472 | } |
| 473 | gpr_mu_unlock(&chand->mu_config); |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 474 | } |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 475 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 476 | grpc_connectivity_state grpc_client_channel_check_connectivity_state( |
| 477 | grpc_exec_ctx *exec_ctx, grpc_channel_element *elem, int try_to_connect) { |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 478 | channel_data *chand = elem->channel_data; |
| 479 | grpc_connectivity_state out; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 480 | gpr_mu_lock(&chand->mu_config); |
| 481 | out = grpc_connectivity_state_check(&chand->state_tracker); |
| 482 | if (out == GRPC_CHANNEL_IDLE && try_to_connect) { |
| 483 | if (chand->lb_policy != NULL) { |
| 484 | grpc_lb_policy_exit_idle(exec_ctx, chand->lb_policy); |
| 485 | } else { |
| 486 | chand->exit_idle_when_lb_policy_arrives = 1; |
| 487 | if (!chand->started_resolving && chand->resolver != NULL) { |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 488 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 489 | chand->started_resolving = 1; |
| 490 | grpc_resolver_next(exec_ctx, chand->resolver, |
| 491 | &chand->incoming_configuration, |
| 492 | &chand->on_config_changed); |
| 493 | } |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 494 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 495 | } |
| 496 | gpr_mu_unlock(&chand->mu_config); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 497 | return out; |
| 498 | } |
| 499 | |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 500 | typedef struct { |
| 501 | channel_data *chand; |
| 502 | grpc_pollset *pollset; |
| 503 | grpc_closure *on_complete; |
| 504 | grpc_closure my_closure; |
| 505 | } external_connectivity_watcher; |
| 506 | |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 507 | static void on_external_watch_complete(grpc_exec_ctx *exec_ctx, void *arg, |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 508 | bool iomgr_success) { |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 509 | external_connectivity_watcher *w = arg; |
| 510 | grpc_closure *follow_up = w->on_complete; |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame^] | 511 | grpc_pollset_set_del_pollset(exec_ctx, w->chand->interested_parties, |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 512 | w->pollset); |
| 513 | GRPC_CHANNEL_STACK_UNREF(exec_ctx, w->chand->owning_stack, |
| 514 | "external_connectivity_watcher"); |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 515 | gpr_free(w); |
| 516 | follow_up->cb(exec_ctx, follow_up->cb_arg, iomgr_success); |
| 517 | } |
| 518 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 519 | void grpc_client_channel_watch_connectivity_state( |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 520 | grpc_exec_ctx *exec_ctx, grpc_channel_element *elem, grpc_pollset *pollset, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 521 | grpc_connectivity_state *state, grpc_closure *on_complete) { |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 522 | channel_data *chand = elem->channel_data; |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 523 | external_connectivity_watcher *w = gpr_malloc(sizeof(*w)); |
| 524 | w->chand = chand; |
| 525 | w->pollset = pollset; |
| 526 | w->on_complete = on_complete; |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame^] | 527 | grpc_pollset_set_add_pollset(exec_ctx, chand->interested_parties, pollset); |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 528 | grpc_closure_init(&w->my_closure, on_external_watch_complete, w); |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 529 | GRPC_CHANNEL_STACK_REF(w->chand->owning_stack, |
| 530 | "external_connectivity_watcher"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 531 | gpr_mu_lock(&chand->mu_config); |
| 532 | grpc_connectivity_state_notify_on_state_change( |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 533 | exec_ctx, &chand->state_tracker, state, &w->my_closure); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 534 | gpr_mu_unlock(&chand->mu_config); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 535 | } |