Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, 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 | |
Craig Tiller | d4c9833 | 2016-03-31 13:45:47 -0700 | [diff] [blame] | 34 | #include "src/core/ext/client_config/client_channel.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 35 | |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 36 | #include <stdbool.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 37 | #include <stdio.h> |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 38 | #include <string.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 39 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 40 | #include <grpc/support/alloc.h> |
| 41 | #include <grpc/support/log.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 42 | #include <grpc/support/sync.h> |
| 43 | #include <grpc/support/useful.h> |
| 44 | |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 45 | #include "src/core/ext/client_config/subchannel.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 46 | #include "src/core/lib/channel/channel_args.h" |
| 47 | #include "src/core/lib/channel/connected_channel.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 48 | #include "src/core/lib/iomgr/iomgr.h" |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 49 | #include "src/core/lib/iomgr/polling_entity.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 50 | #include "src/core/lib/profiling/timers.h" |
| 51 | #include "src/core/lib/support/string.h" |
| 52 | #include "src/core/lib/surface/channel.h" |
| 53 | #include "src/core/lib/transport/connectivity_state.h" |
Craig Tiller | 8910ac6 | 2015-10-08 16:49:15 -0700 | [diff] [blame] | 54 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 55 | /* Client channel implementation */ |
| 56 | |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame^] | 57 | /************************************************************************* |
| 58 | * CHANNEL-WIDE FUNCTIONS |
| 59 | */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 60 | |
Craig Tiller | 800dacb | 2015-10-06 09:10:26 -0700 | [diff] [blame] | 61 | typedef struct client_channel_channel_data { |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 62 | /** resolver for this channel */ |
| 63 | grpc_resolver *resolver; |
Craig Tiller | 20a3c35 | 2015-08-05 08:39:50 -0700 | [diff] [blame] | 64 | /** have we started resolving this channel */ |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 65 | bool started_resolving; |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 66 | |
Craig Tiller | 9d94b60 | 2015-07-01 14:23:18 -0700 | [diff] [blame] | 67 | /** mutex protecting client configuration, including all |
| 68 | variables below in this data structure */ |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 69 | gpr_mu mu; |
| 70 | /** currently active load balancer - guarded by mu */ |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 71 | grpc_lb_policy *lb_policy; |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 72 | /** incoming resolver result - set by resolver.next(), guarded by mu */ |
| 73 | grpc_resolver_result *resolver_result; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 74 | /** 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] | 75 | grpc_closure_list waiting_for_config_closures; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 76 | /** resolver callback */ |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 77 | grpc_closure on_resolver_result_changed; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 78 | /** connectivity state being tracked */ |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 79 | grpc_connectivity_state_tracker state_tracker; |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 80 | /** when an lb_policy arrives, should we try to exit idle */ |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 81 | bool exit_idle_when_lb_policy_arrives; |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 82 | /** owning stack */ |
| 83 | grpc_channel_stack *owning_stack; |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 84 | /** interested parties (owned) */ |
| 85 | grpc_pollset_set *interested_parties; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 86 | } channel_data; |
| 87 | |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 88 | /** We create one watcher for each new lb_policy that is returned from a |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 89 | resolver, to watch for state changes from the lb_policy. When a state |
| 90 | change is seen, we update the channel, and create a new watcher. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 91 | typedef struct { |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 92 | channel_data *chand; |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 93 | grpc_closure on_changed; |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 94 | grpc_connectivity_state state; |
| 95 | grpc_lb_policy *lb_policy; |
| 96 | } lb_policy_connectivity_watcher; |
| 97 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 98 | static void watch_lb_policy(grpc_exec_ctx *exec_ctx, channel_data *chand, |
| 99 | grpc_lb_policy *lb_policy, |
| 100 | grpc_connectivity_state current_state); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 101 | |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 102 | static void set_channel_connectivity_state_locked(grpc_exec_ctx *exec_ctx, |
| 103 | channel_data *chand, |
| 104 | grpc_connectivity_state state, |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 105 | grpc_error *error, |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 106 | const char *reason) { |
| 107 | if ((state == GRPC_CHANNEL_TRANSIENT_FAILURE || |
Craig Tiller | 48ed92e | 2016-06-02 11:07:12 -0700 | [diff] [blame] | 108 | state == GRPC_CHANNEL_SHUTDOWN) && |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 109 | chand->lb_policy != NULL) { |
| 110 | /* cancel fail-fast picks */ |
| 111 | grpc_lb_policy_cancel_picks( |
| 112 | exec_ctx, chand->lb_policy, |
| 113 | /* mask= */ GRPC_INITIAL_METADATA_IGNORE_CONNECTIVITY, |
| 114 | /* check= */ 0); |
| 115 | } |
Craig Tiller | 9ccf5f1 | 2016-05-07 21:41:01 -0700 | [diff] [blame] | 116 | grpc_connectivity_state_set(exec_ctx, &chand->state_tracker, state, error, |
| 117 | reason); |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 120 | static void on_lb_policy_state_changed_locked(grpc_exec_ctx *exec_ctx, |
| 121 | lb_policy_connectivity_watcher *w, |
| 122 | grpc_error *error) { |
Craig Tiller | cb2609f | 2015-11-24 17:19:19 -0800 | [diff] [blame] | 123 | grpc_connectivity_state publish_state = w->state; |
Craig Tiller | 5795da7 | 2015-09-17 15:27:13 -0700 | [diff] [blame] | 124 | /* check if the notification is for a stale policy */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 125 | if (w->lb_policy != w->chand->lb_policy) return; |
Craig Tiller | 5795da7 | 2015-09-17 15:27:13 -0700 | [diff] [blame] | 126 | |
Craig Tiller | 48ed92e | 2016-06-02 11:07:12 -0700 | [diff] [blame] | 127 | if (publish_state == GRPC_CHANNEL_SHUTDOWN && w->chand->resolver != NULL) { |
Craig Tiller | cb2609f | 2015-11-24 17:19:19 -0800 | [diff] [blame] | 128 | publish_state = GRPC_CHANNEL_TRANSIENT_FAILURE; |
| 129 | grpc_resolver_channel_saw_error(exec_ctx, w->chand->resolver); |
Craig Tiller | f62c4d5 | 2015-12-04 07:43:07 -0800 | [diff] [blame] | 130 | GRPC_LB_POLICY_UNREF(exec_ctx, w->chand->lb_policy, "channel"); |
| 131 | w->chand->lb_policy = NULL; |
Craig Tiller | cb2609f | 2015-11-24 17:19:19 -0800 | [diff] [blame] | 132 | } |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 133 | set_channel_connectivity_state_locked(exec_ctx, w->chand, publish_state, |
Craig Tiller | fc353d6 | 2016-05-10 12:58:03 -0700 | [diff] [blame] | 134 | GRPC_ERROR_REF(error), "lb_changed"); |
Craig Tiller | 48ed92e | 2016-06-02 11:07:12 -0700 | [diff] [blame] | 135 | if (w->state != GRPC_CHANNEL_SHUTDOWN) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 136 | watch_lb_policy(exec_ctx, w->chand, w->lb_policy, w->state); |
| 137 | } |
Craig Tiller | 5795da7 | 2015-09-17 15:27:13 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 140 | static void on_lb_policy_state_changed(grpc_exec_ctx *exec_ctx, void *arg, |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 141 | grpc_error *error) { |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 142 | lb_policy_connectivity_watcher *w = arg; |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 143 | |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 144 | gpr_mu_lock(&w->chand->mu); |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 145 | on_lb_policy_state_changed_locked(exec_ctx, w, error); |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 146 | gpr_mu_unlock(&w->chand->mu); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 147 | |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 148 | 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] | 149 | gpr_free(w); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 152 | static void watch_lb_policy(grpc_exec_ctx *exec_ctx, channel_data *chand, |
| 153 | grpc_lb_policy *lb_policy, |
| 154 | grpc_connectivity_state current_state) { |
| 155 | lb_policy_connectivity_watcher *w = gpr_malloc(sizeof(*w)); |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 156 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "watch_lb_policy"); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 157 | |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 158 | w->chand = chand; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 159 | grpc_closure_init(&w->on_changed, on_lb_policy_state_changed, w); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 160 | w->state = current_state; |
| 161 | w->lb_policy = lb_policy; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 162 | grpc_lb_policy_notify_on_state_change(exec_ctx, lb_policy, &w->state, |
| 163 | &w->on_changed); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame^] | 166 | static void on_resolver_result_changed(grpc_exec_ctx *exec_ctx, void *arg, |
| 167 | grpc_error *error) { |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 168 | channel_data *chand = arg; |
| 169 | grpc_lb_policy *lb_policy = NULL; |
| 170 | grpc_lb_policy *old_lb_policy; |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 171 | grpc_connectivity_state state = GRPC_CHANNEL_TRANSIENT_FAILURE; |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 172 | bool exit_idle = false; |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 173 | grpc_error *state_error = GRPC_ERROR_CREATE("No load balancing policy"); |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 174 | |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 175 | if (chand->resolver_result != NULL) { |
Mark D. Roth | a275aea | 2016-08-23 12:30:45 -0700 | [diff] [blame] | 176 | lb_policy = grpc_resolver_result_get_lb_policy(chand->resolver_result); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 177 | if (lb_policy != NULL) { |
| 178 | GRPC_LB_POLICY_REF(lb_policy, "channel"); |
| 179 | GRPC_LB_POLICY_REF(lb_policy, "config_change"); |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 180 | GRPC_ERROR_UNREF(state_error); |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 181 | state = |
| 182 | grpc_lb_policy_check_connectivity(exec_ctx, lb_policy, &state_error); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 183 | } |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 184 | |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 185 | grpc_resolver_result_unref(exec_ctx, chand->resolver_result); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 188 | chand->resolver_result = NULL; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 189 | |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 190 | if (lb_policy != NULL) { |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 191 | grpc_pollset_set_add_pollset_set(exec_ctx, lb_policy->interested_parties, |
| 192 | chand->interested_parties); |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 193 | } |
| 194 | |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 195 | gpr_mu_lock(&chand->mu); |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 196 | old_lb_policy = chand->lb_policy; |
| 197 | chand->lb_policy = lb_policy; |
Craig Tiller | 0ede545 | 2016-04-23 12:21:45 -0700 | [diff] [blame] | 198 | if (lb_policy != NULL) { |
| 199 | grpc_exec_ctx_enqueue_list(exec_ctx, &chand->waiting_for_config_closures, |
| 200 | NULL); |
| 201 | } else if (chand->resolver == NULL /* disconnected */) { |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 202 | grpc_closure_list_fail_all( |
| 203 | &chand->waiting_for_config_closures, |
| 204 | GRPC_ERROR_CREATE_REFERENCING("Channel disconnected", &error, 1)); |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 205 | grpc_exec_ctx_enqueue_list(exec_ctx, &chand->waiting_for_config_closures, |
| 206 | NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 207 | } |
| 208 | if (lb_policy != NULL && chand->exit_idle_when_lb_policy_arrives) { |
| 209 | GRPC_LB_POLICY_REF(lb_policy, "exit_idle"); |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 210 | exit_idle = true; |
| 211 | chand->exit_idle_when_lb_policy_arrives = false; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 212 | } |
Craig Tiller | 9846503 | 2015-06-29 14:36:42 -0700 | [diff] [blame] | 213 | |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 214 | if (error == GRPC_ERROR_NONE && chand->resolver) { |
Craig Tiller | 9ccf5f1 | 2016-05-07 21:41:01 -0700 | [diff] [blame] | 215 | set_channel_connectivity_state_locked( |
| 216 | exec_ctx, chand, state, GRPC_ERROR_REF(state_error), "new_lb+resolver"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 217 | if (lb_policy != NULL) { |
| 218 | watch_lb_policy(exec_ctx, chand, lb_policy, state); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 219 | } |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 220 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver"); |
Mark D. Roth | a275aea | 2016-08-23 12:30:45 -0700 | [diff] [blame] | 221 | grpc_resolver_next(exec_ctx, chand->resolver, &chand->resolver_result, |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 222 | &chand->on_resolver_result_changed); |
| 223 | gpr_mu_unlock(&chand->mu); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 224 | } else { |
Craig Tiller | 76a5c0e | 2016-03-09 09:05:30 -0800 | [diff] [blame] | 225 | if (chand->resolver != NULL) { |
| 226 | grpc_resolver_shutdown(exec_ctx, chand->resolver); |
| 227 | GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel"); |
| 228 | chand->resolver = NULL; |
| 229 | } |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 230 | grpc_error *refs[] = {error, state_error}; |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 231 | set_channel_connectivity_state_locked( |
Craig Tiller | d925c93 | 2016-06-06 08:38:50 -0700 | [diff] [blame] | 232 | exec_ctx, chand, GRPC_CHANNEL_SHUTDOWN, |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 233 | GRPC_ERROR_CREATE_REFERENCING("Got config after disconnection", refs, |
| 234 | GPR_ARRAY_SIZE(refs)), |
| 235 | "resolver_gone"); |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 236 | gpr_mu_unlock(&chand->mu); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 237 | } |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 238 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 239 | if (exit_idle) { |
| 240 | grpc_lb_policy_exit_idle(exec_ctx, lb_policy); |
| 241 | GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "exit_idle"); |
| 242 | } |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 243 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 244 | if (old_lb_policy != NULL) { |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 245 | grpc_pollset_set_del_pollset_set( |
| 246 | exec_ctx, old_lb_policy->interested_parties, chand->interested_parties); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 247 | GRPC_LB_POLICY_UNREF(exec_ctx, old_lb_policy, "channel"); |
| 248 | } |
Craig Tiller | 000cd8f | 2015-09-18 07:20:29 -0700 | [diff] [blame] | 249 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 250 | if (lb_policy != NULL) { |
| 251 | GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "config_change"); |
| 252 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 253 | |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 254 | GRPC_CHANNEL_STACK_UNREF(exec_ctx, chand->owning_stack, "resolver"); |
Craig Tiller | 9ccf5f1 | 2016-05-07 21:41:01 -0700 | [diff] [blame] | 255 | GRPC_ERROR_UNREF(state_error); |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 258 | static void cc_start_transport_op(grpc_exec_ctx *exec_ctx, |
| 259 | grpc_channel_element *elem, |
| 260 | grpc_transport_op *op) { |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 261 | channel_data *chand = elem->channel_data; |
Craig Tiller | 000cd8f | 2015-09-18 07:20:29 -0700 | [diff] [blame] | 262 | |
Craig Tiller | 332f1b3 | 2016-05-24 13:21:21 -0700 | [diff] [blame] | 263 | grpc_exec_ctx_sched(exec_ctx, op->on_consumed, GRPC_ERROR_NONE, NULL); |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 264 | |
Craig Tiller | d7f12e3 | 2016-03-03 10:08:31 -0800 | [diff] [blame] | 265 | GPR_ASSERT(op->set_accept_stream == false); |
Craig Tiller | 28bf891 | 2015-12-07 16:07:04 -0800 | [diff] [blame] | 266 | if (op->bind_pollset != NULL) { |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 267 | grpc_pollset_set_add_pollset(exec_ctx, chand->interested_parties, |
Craig Tiller | e2c6237 | 2015-12-07 16:11:03 -0800 | [diff] [blame] | 268 | op->bind_pollset); |
Craig Tiller | 28bf891 | 2015-12-07 16:07:04 -0800 | [diff] [blame] | 269 | } |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 270 | |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 271 | gpr_mu_lock(&chand->mu); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 272 | if (op->on_connectivity_state_change != NULL) { |
| 273 | grpc_connectivity_state_notify_on_state_change( |
| 274 | exec_ctx, &chand->state_tracker, op->connectivity_state, |
| 275 | op->on_connectivity_state_change); |
| 276 | op->on_connectivity_state_change = NULL; |
| 277 | op->connectivity_state = NULL; |
| 278 | } |
| 279 | |
Craig Tiller | 26dab31 | 2015-12-07 14:43:47 -0800 | [diff] [blame] | 280 | if (op->send_ping != NULL) { |
Craig Tiller | 87b71e2 | 2015-12-07 15:14:14 -0800 | [diff] [blame] | 281 | if (chand->lb_policy == NULL) { |
Craig Tiller | 332f1b3 | 2016-05-24 13:21:21 -0700 | [diff] [blame] | 282 | grpc_exec_ctx_sched(exec_ctx, op->send_ping, |
| 283 | GRPC_ERROR_CREATE("Ping with no load balancing"), |
| 284 | NULL); |
Craig Tiller | 26dab31 | 2015-12-07 14:43:47 -0800 | [diff] [blame] | 285 | } else { |
Craig Tiller | 28bf891 | 2015-12-07 16:07:04 -0800 | [diff] [blame] | 286 | 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] | 287 | op->bind_pollset = NULL; |
| 288 | } |
| 289 | op->send_ping = NULL; |
| 290 | } |
| 291 | |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame] | 292 | if (op->disconnect_with_error != GRPC_ERROR_NONE) { |
| 293 | if (chand->resolver != NULL) { |
| 294 | set_channel_connectivity_state_locked( |
Craig Tiller | d925c93 | 2016-06-06 08:38:50 -0700 | [diff] [blame] | 295 | exec_ctx, chand, GRPC_CHANNEL_SHUTDOWN, |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame] | 296 | GRPC_ERROR_REF(op->disconnect_with_error), "disconnect"); |
| 297 | grpc_resolver_shutdown(exec_ctx, chand->resolver); |
| 298 | GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel"); |
| 299 | chand->resolver = NULL; |
| 300 | if (!chand->started_resolving) { |
| 301 | grpc_closure_list_fail_all(&chand->waiting_for_config_closures, |
| 302 | GRPC_ERROR_REF(op->disconnect_with_error)); |
Craig Tiller | 9ccf5f1 | 2016-05-07 21:41:01 -0700 | [diff] [blame] | 303 | grpc_exec_ctx_enqueue_list(exec_ctx, |
| 304 | &chand->waiting_for_config_closures, NULL); |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame] | 305 | } |
| 306 | if (chand->lb_policy != NULL) { |
| 307 | grpc_pollset_set_del_pollset_set(exec_ctx, |
| 308 | chand->lb_policy->interested_parties, |
| 309 | chand->interested_parties); |
| 310 | GRPC_LB_POLICY_UNREF(exec_ctx, chand->lb_policy, "channel"); |
| 311 | chand->lb_policy = NULL; |
| 312 | } |
Craig Tiller | b12d22a | 2016-04-23 12:50:21 -0700 | [diff] [blame] | 313 | } |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame] | 314 | GRPC_ERROR_UNREF(op->disconnect_with_error); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 315 | } |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 316 | gpr_mu_unlock(&chand->mu); |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 317 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 318 | |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame^] | 319 | /* Constructor for channel_data */ |
| 320 | static void cc_init_channel_elem(grpc_exec_ctx *exec_ctx, |
| 321 | grpc_channel_element *elem, |
| 322 | grpc_channel_element_args *args) { |
| 323 | channel_data *chand = elem->channel_data; |
| 324 | |
| 325 | memset(chand, 0, sizeof(*chand)); |
| 326 | |
| 327 | GPR_ASSERT(args->is_last); |
| 328 | GPR_ASSERT(elem->filter == &grpc_client_channel_filter); |
| 329 | |
| 330 | gpr_mu_init(&chand->mu); |
| 331 | grpc_closure_init(&chand->on_resolver_result_changed, |
| 332 | on_resolver_result_changed, chand); |
| 333 | chand->owning_stack = args->channel_stack; |
| 334 | |
| 335 | grpc_connectivity_state_init(&chand->state_tracker, GRPC_CHANNEL_IDLE, |
| 336 | "client_channel"); |
| 337 | chand->interested_parties = grpc_pollset_set_create(); |
| 338 | } |
| 339 | |
| 340 | /* Destructor for channel_data */ |
| 341 | static void cc_destroy_channel_elem(grpc_exec_ctx *exec_ctx, |
| 342 | grpc_channel_element *elem) { |
| 343 | channel_data *chand = elem->channel_data; |
| 344 | |
| 345 | if (chand->resolver != NULL) { |
| 346 | grpc_resolver_shutdown(exec_ctx, chand->resolver); |
| 347 | GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel"); |
| 348 | } |
| 349 | if (chand->lb_policy != NULL) { |
| 350 | grpc_pollset_set_del_pollset_set(exec_ctx, |
| 351 | chand->lb_policy->interested_parties, |
| 352 | chand->interested_parties); |
| 353 | GRPC_LB_POLICY_UNREF(exec_ctx, chand->lb_policy, "channel"); |
| 354 | } |
| 355 | grpc_connectivity_state_destroy(exec_ctx, &chand->state_tracker); |
| 356 | grpc_pollset_set_destroy(chand->interested_parties); |
| 357 | gpr_mu_destroy(&chand->mu); |
| 358 | } |
| 359 | |
| 360 | /************************************************************************* |
| 361 | * PER-CALL FUNCTIONS |
| 362 | */ |
| 363 | |
| 364 | #define GET_CALL(call_data) \ |
| 365 | ((grpc_subchannel_call *)(gpr_atm_acq_load(&(call_data)->subchannel_call))) |
| 366 | |
| 367 | #define CANCELLED_CALL ((grpc_subchannel_call *)1) |
| 368 | |
| 369 | typedef enum { |
| 370 | GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING, |
| 371 | GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL |
| 372 | } subchannel_creation_phase; |
| 373 | |
| 374 | /** Call data. Holds a pointer to grpc_subchannel_call and the |
| 375 | associated machinery to create such a pointer. |
| 376 | Handles queueing of stream ops until a call object is ready, waiting |
| 377 | for initial metadata before trying to create a call object, |
| 378 | and handling cancellation gracefully. */ |
| 379 | typedef struct client_channel_call_data { |
| 380 | /** either 0 for no call, 1 for cancelled, or a pointer to a |
| 381 | grpc_subchannel_call */ |
| 382 | gpr_atm subchannel_call; |
| 383 | |
| 384 | gpr_mu mu; |
| 385 | |
| 386 | subchannel_creation_phase creation_phase; |
| 387 | grpc_connected_subchannel *connected_subchannel; |
| 388 | grpc_polling_entity *pollent; |
| 389 | |
| 390 | grpc_transport_stream_op *waiting_ops; |
| 391 | size_t waiting_ops_count; |
| 392 | size_t waiting_ops_capacity; |
| 393 | |
| 394 | grpc_closure next_step; |
| 395 | |
| 396 | grpc_call_stack *owning_call; |
| 397 | } call_data; |
| 398 | |
| 399 | static void add_waiting_locked(call_data *calld, grpc_transport_stream_op *op) { |
| 400 | GPR_TIMER_BEGIN("add_waiting_locked", 0); |
| 401 | if (calld->waiting_ops_count == calld->waiting_ops_capacity) { |
| 402 | calld->waiting_ops_capacity = GPR_MAX(3, 2 * calld->waiting_ops_capacity); |
| 403 | calld->waiting_ops = |
| 404 | gpr_realloc(calld->waiting_ops, |
| 405 | calld->waiting_ops_capacity * sizeof(*calld->waiting_ops)); |
| 406 | } |
| 407 | calld->waiting_ops[calld->waiting_ops_count++] = *op; |
| 408 | GPR_TIMER_END("add_waiting_locked", 0); |
| 409 | } |
| 410 | |
| 411 | static void fail_locked(grpc_exec_ctx *exec_ctx, call_data *calld, |
| 412 | grpc_error *error) { |
| 413 | size_t i; |
| 414 | for (i = 0; i < calld->waiting_ops_count; i++) { |
| 415 | grpc_transport_stream_op_finish_with_failure( |
| 416 | exec_ctx, &calld->waiting_ops[i], GRPC_ERROR_REF(error)); |
| 417 | } |
| 418 | calld->waiting_ops_count = 0; |
| 419 | GRPC_ERROR_UNREF(error); |
| 420 | } |
| 421 | |
| 422 | typedef struct { |
| 423 | grpc_transport_stream_op *ops; |
| 424 | size_t nops; |
| 425 | grpc_subchannel_call *call; |
| 426 | } retry_ops_args; |
| 427 | |
| 428 | static void retry_ops(grpc_exec_ctx *exec_ctx, void *args, grpc_error *error) { |
| 429 | retry_ops_args *a = args; |
| 430 | size_t i; |
| 431 | for (i = 0; i < a->nops; i++) { |
| 432 | grpc_subchannel_call_process_op(exec_ctx, a->call, &a->ops[i]); |
| 433 | } |
| 434 | GRPC_SUBCHANNEL_CALL_UNREF(exec_ctx, a->call, "retry_ops"); |
| 435 | gpr_free(a->ops); |
| 436 | gpr_free(a); |
| 437 | } |
| 438 | |
| 439 | static void retry_waiting_locked(grpc_exec_ctx *exec_ctx, call_data *calld) { |
| 440 | retry_ops_args *a = gpr_malloc(sizeof(*a)); |
| 441 | a->ops = calld->waiting_ops; |
| 442 | a->nops = calld->waiting_ops_count; |
| 443 | a->call = GET_CALL(calld); |
| 444 | if (a->call == CANCELLED_CALL) { |
| 445 | gpr_free(a); |
| 446 | fail_locked(exec_ctx, calld, GRPC_ERROR_CANCELLED); |
| 447 | return; |
| 448 | } |
| 449 | calld->waiting_ops = NULL; |
| 450 | calld->waiting_ops_count = 0; |
| 451 | calld->waiting_ops_capacity = 0; |
| 452 | GRPC_SUBCHANNEL_CALL_REF(a->call, "retry_ops"); |
| 453 | grpc_exec_ctx_sched(exec_ctx, grpc_closure_create(retry_ops, a), |
| 454 | GRPC_ERROR_NONE, NULL); |
| 455 | } |
| 456 | |
| 457 | static void subchannel_ready(grpc_exec_ctx *exec_ctx, void *arg, |
| 458 | grpc_error *error) { |
| 459 | call_data *calld = arg; |
| 460 | gpr_mu_lock(&calld->mu); |
| 461 | GPR_ASSERT(calld->creation_phase == |
| 462 | GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL); |
| 463 | calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING; |
| 464 | if (calld->connected_subchannel == NULL) { |
| 465 | gpr_atm_no_barrier_store(&calld->subchannel_call, 1); |
| 466 | fail_locked(exec_ctx, calld, GRPC_ERROR_CREATE_REFERENCING( |
| 467 | "Failed to create subchannel", &error, 1)); |
| 468 | } else if (1 == gpr_atm_acq_load(&calld->subchannel_call)) { |
| 469 | /* already cancelled before subchannel became ready */ |
| 470 | fail_locked(exec_ctx, calld, |
| 471 | GRPC_ERROR_CREATE_REFERENCING( |
| 472 | "Cancelled before creating subchannel", &error, 1)); |
| 473 | } else { |
| 474 | grpc_subchannel_call *subchannel_call = NULL; |
| 475 | grpc_error *new_error = grpc_connected_subchannel_create_call( |
| 476 | exec_ctx, calld->connected_subchannel, calld->pollent, |
| 477 | &subchannel_call); |
| 478 | if (new_error != GRPC_ERROR_NONE) { |
| 479 | new_error = grpc_error_add_child(new_error, error); |
| 480 | subchannel_call = CANCELLED_CALL; |
| 481 | fail_locked(exec_ctx, calld, new_error); |
| 482 | } |
| 483 | gpr_atm_rel_store(&calld->subchannel_call, |
| 484 | (gpr_atm)(uintptr_t)subchannel_call); |
| 485 | retry_waiting_locked(exec_ctx, calld); |
| 486 | } |
| 487 | gpr_mu_unlock(&calld->mu); |
| 488 | GRPC_CALL_STACK_UNREF(exec_ctx, calld->owning_call, "pick_subchannel"); |
| 489 | } |
| 490 | |
| 491 | static char *cc_get_peer(grpc_exec_ctx *exec_ctx, grpc_call_element *elem) { |
| 492 | call_data *calld = elem->call_data; |
| 493 | grpc_subchannel_call *subchannel_call = GET_CALL(calld); |
| 494 | if (subchannel_call == NULL || subchannel_call == CANCELLED_CALL) { |
| 495 | return NULL; |
| 496 | } else { |
| 497 | return grpc_subchannel_call_get_peer(exec_ctx, subchannel_call); |
| 498 | } |
| 499 | } |
| 500 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 501 | typedef struct { |
| 502 | grpc_metadata_batch *initial_metadata; |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 503 | uint32_t initial_metadata_flags; |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 504 | grpc_connected_subchannel **connected_subchannel; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 505 | grpc_closure *on_ready; |
| 506 | grpc_call_element *elem; |
| 507 | grpc_closure closure; |
| 508 | } continue_picking_args; |
| 509 | |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame^] | 510 | static bool pick_subchannel(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
| 511 | grpc_metadata_batch *initial_metadata, |
| 512 | uint32_t initial_metadata_flags, |
| 513 | grpc_connected_subchannel **connected_subchannel, |
| 514 | grpc_closure *on_ready); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 515 | |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 516 | static void continue_picking(grpc_exec_ctx *exec_ctx, void *arg, |
| 517 | grpc_error *error) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 518 | continue_picking_args *cpa = arg; |
Craig Tiller | 0ede545 | 2016-04-23 12:21:45 -0700 | [diff] [blame] | 519 | if (cpa->connected_subchannel == NULL) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 520 | /* cancelled, do nothing */ |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 521 | } else if (error != GRPC_ERROR_NONE) { |
Craig Tiller | 332f1b3 | 2016-05-24 13:21:21 -0700 | [diff] [blame] | 522 | grpc_exec_ctx_sched(exec_ctx, cpa->on_ready, GRPC_ERROR_REF(error), NULL); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame^] | 523 | } else if (pick_subchannel(exec_ctx, cpa->elem, cpa->initial_metadata, |
| 524 | cpa->initial_metadata_flags, |
| 525 | cpa->connected_subchannel, cpa->on_ready)) { |
Craig Tiller | 332f1b3 | 2016-05-24 13:21:21 -0700 | [diff] [blame] | 526 | grpc_exec_ctx_sched(exec_ctx, cpa->on_ready, GRPC_ERROR_NONE, NULL); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 527 | } |
| 528 | gpr_free(cpa); |
| 529 | } |
| 530 | |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame^] | 531 | static bool pick_subchannel(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
| 532 | grpc_metadata_batch *initial_metadata, |
| 533 | uint32_t initial_metadata_flags, |
| 534 | grpc_connected_subchannel **connected_subchannel, |
| 535 | grpc_closure *on_ready) { |
| 536 | GPR_TIMER_BEGIN("pick_subchannel", 0); |
Craig Tiller | bfc9adc | 2016-06-27 13:16:22 -0700 | [diff] [blame] | 537 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 538 | channel_data *chand = elem->channel_data; |
| 539 | call_data *calld = elem->call_data; |
| 540 | continue_picking_args *cpa; |
| 541 | grpc_closure *closure; |
| 542 | |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 543 | GPR_ASSERT(connected_subchannel); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 544 | |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 545 | gpr_mu_lock(&chand->mu); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 546 | if (initial_metadata == NULL) { |
| 547 | if (chand->lb_policy != NULL) { |
Craig Tiller | ab33b48 | 2015-11-21 08:11:04 -0800 | [diff] [blame] | 548 | grpc_lb_policy_cancel_pick(exec_ctx, chand->lb_policy, |
| 549 | connected_subchannel); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 550 | } |
| 551 | for (closure = chand->waiting_for_config_closures.head; closure != NULL; |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 552 | closure = closure->next_data.next) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 553 | cpa = closure->cb_arg; |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 554 | if (cpa->connected_subchannel == connected_subchannel) { |
| 555 | cpa->connected_subchannel = NULL; |
Craig Tiller | 332f1b3 | 2016-05-24 13:21:21 -0700 | [diff] [blame] | 556 | grpc_exec_ctx_sched(exec_ctx, cpa->on_ready, |
| 557 | GRPC_ERROR_CREATE("Pick cancelled"), NULL); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 558 | } |
| 559 | } |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 560 | gpr_mu_unlock(&chand->mu); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame^] | 561 | GPR_TIMER_END("pick_subchannel", 0); |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 562 | return true; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 563 | } |
| 564 | if (chand->lb_policy != NULL) { |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 565 | grpc_lb_policy *lb_policy = chand->lb_policy; |
| 566 | int r; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame^] | 567 | GRPC_LB_POLICY_REF(lb_policy, "pick_subchannel"); |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 568 | gpr_mu_unlock(&chand->mu); |
David Garcia Quintas | 2a50dfe | 2016-05-31 15:09:12 -0700 | [diff] [blame] | 569 | r = grpc_lb_policy_pick(exec_ctx, lb_policy, calld->pollent, |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 570 | initial_metadata, initial_metadata_flags, |
| 571 | connected_subchannel, on_ready); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame^] | 572 | GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "pick_subchannel"); |
| 573 | GPR_TIMER_END("pick_subchannel", 0); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 574 | return r; |
| 575 | } |
| 576 | if (chand->resolver != NULL && !chand->started_resolving) { |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 577 | chand->started_resolving = true; |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 578 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver"); |
Mark D. Roth | a275aea | 2016-08-23 12:30:45 -0700 | [diff] [blame] | 579 | grpc_resolver_next(exec_ctx, chand->resolver, &chand->resolver_result, |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 580 | &chand->on_resolver_result_changed); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 581 | } |
Craig Tiller | 0eab697 | 2016-04-23 12:59:57 -0700 | [diff] [blame] | 582 | if (chand->resolver != NULL) { |
| 583 | cpa = gpr_malloc(sizeof(*cpa)); |
| 584 | cpa->initial_metadata = initial_metadata; |
| 585 | cpa->initial_metadata_flags = initial_metadata_flags; |
| 586 | cpa->connected_subchannel = connected_subchannel; |
| 587 | cpa->on_ready = on_ready; |
| 588 | cpa->elem = elem; |
| 589 | grpc_closure_init(&cpa->closure, continue_picking, cpa); |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 590 | grpc_closure_list_append(&chand->waiting_for_config_closures, &cpa->closure, |
| 591 | GRPC_ERROR_NONE); |
Craig Tiller | 0eab697 | 2016-04-23 12:59:57 -0700 | [diff] [blame] | 592 | } else { |
Craig Tiller | 332f1b3 | 2016-05-24 13:21:21 -0700 | [diff] [blame] | 593 | grpc_exec_ctx_sched(exec_ctx, on_ready, GRPC_ERROR_CREATE("Disconnected"), |
| 594 | NULL); |
Craig Tiller | 0eab697 | 2016-04-23 12:59:57 -0700 | [diff] [blame] | 595 | } |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 596 | gpr_mu_unlock(&chand->mu); |
Craig Tiller | bfc9adc | 2016-06-27 13:16:22 -0700 | [diff] [blame] | 597 | |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame^] | 598 | GPR_TIMER_END("pick_subchannel", 0); |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 599 | return false; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 600 | } |
| 601 | |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame^] | 602 | // The logic here is fairly complicated, due to (a) the fact that we |
| 603 | // need to handle the case where we receive the send op before the |
| 604 | // initial metadata op, and (b) the need for efficiency, especially in |
| 605 | // the streaming case. |
| 606 | // TODO(ctiller): Explain this more thoroughly. |
| 607 | static void cc_start_transport_stream_op(grpc_exec_ctx *exec_ctx, |
| 608 | grpc_call_element *elem, |
| 609 | grpc_transport_stream_op *op) { |
| 610 | call_data *calld = elem->call_data; |
| 611 | GRPC_CALL_LOG_OP(GPR_INFO, elem, op); |
| 612 | /* try to (atomically) get the call */ |
| 613 | grpc_subchannel_call *call = GET_CALL(calld); |
| 614 | GPR_TIMER_BEGIN("cc_start_transport_stream_op", 0); |
| 615 | if (call == CANCELLED_CALL) { |
| 616 | grpc_transport_stream_op_finish_with_failure(exec_ctx, op, |
| 617 | GRPC_ERROR_CANCELLED); |
| 618 | GPR_TIMER_END("cc_start_transport_stream_op", 0); |
| 619 | return; |
| 620 | } |
| 621 | if (call != NULL) { |
| 622 | grpc_subchannel_call_process_op(exec_ctx, call, op); |
| 623 | GPR_TIMER_END("cc_start_transport_stream_op", 0); |
| 624 | return; |
| 625 | } |
| 626 | /* we failed; lock and figure out what to do */ |
| 627 | gpr_mu_lock(&calld->mu); |
| 628 | retry: |
| 629 | /* need to recheck that another thread hasn't set the call */ |
| 630 | call = GET_CALL(calld); |
| 631 | if (call == CANCELLED_CALL) { |
| 632 | gpr_mu_unlock(&calld->mu); |
| 633 | grpc_transport_stream_op_finish_with_failure(exec_ctx, op, |
| 634 | GRPC_ERROR_CANCELLED); |
| 635 | GPR_TIMER_END("cc_start_transport_stream_op", 0); |
| 636 | return; |
| 637 | } |
| 638 | if (call != NULL) { |
| 639 | gpr_mu_unlock(&calld->mu); |
| 640 | grpc_subchannel_call_process_op(exec_ctx, call, op); |
| 641 | GPR_TIMER_END("cc_start_transport_stream_op", 0); |
| 642 | return; |
| 643 | } |
| 644 | /* if this is a cancellation, then we can raise our cancelled flag */ |
| 645 | if (op->cancel_error != GRPC_ERROR_NONE) { |
| 646 | if (!gpr_atm_rel_cas(&calld->subchannel_call, 0, |
| 647 | (gpr_atm)(uintptr_t)CANCELLED_CALL)) { |
| 648 | goto retry; |
| 649 | } else { |
| 650 | switch (calld->creation_phase) { |
| 651 | case GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING: |
| 652 | fail_locked(exec_ctx, calld, GRPC_ERROR_REF(op->cancel_error)); |
| 653 | break; |
| 654 | case GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL: |
| 655 | pick_subchannel(exec_ctx, elem, NULL, 0, |
| 656 | &calld->connected_subchannel, NULL); |
| 657 | break; |
| 658 | } |
| 659 | gpr_mu_unlock(&calld->mu); |
| 660 | grpc_transport_stream_op_finish_with_failure(exec_ctx, op, |
| 661 | GRPC_ERROR_CANCELLED); |
| 662 | GPR_TIMER_END("cc_start_transport_stream_op", 0); |
| 663 | return; |
| 664 | } |
| 665 | } |
| 666 | /* if we don't have a subchannel, try to get one */ |
| 667 | if (calld->creation_phase == GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING && |
| 668 | calld->connected_subchannel == NULL && |
| 669 | op->send_initial_metadata != NULL) { |
| 670 | calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL; |
| 671 | grpc_closure_init(&calld->next_step, subchannel_ready, calld); |
| 672 | GRPC_CALL_STACK_REF(calld->owning_call, "pick_subchannel"); |
| 673 | if (pick_subchannel( |
| 674 | exec_ctx, elem, op->send_initial_metadata, |
| 675 | op->send_initial_metadata_flags, &calld->connected_subchannel, |
| 676 | &calld->next_step)) { |
| 677 | calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING; |
| 678 | GRPC_CALL_STACK_UNREF(exec_ctx, calld->owning_call, "pick_subchannel"); |
| 679 | } |
| 680 | } |
| 681 | /* if we've got a subchannel, then let's ask it to create a call */ |
| 682 | if (calld->creation_phase == GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING && |
| 683 | calld->connected_subchannel != NULL) { |
| 684 | grpc_subchannel_call *subchannel_call = NULL; |
| 685 | grpc_error *error = grpc_connected_subchannel_create_call( |
| 686 | exec_ctx, calld->connected_subchannel, calld->pollent, |
| 687 | &subchannel_call); |
| 688 | if (error != GRPC_ERROR_NONE) { |
| 689 | subchannel_call = CANCELLED_CALL; |
| 690 | fail_locked(exec_ctx, calld, GRPC_ERROR_REF(error)); |
| 691 | grpc_transport_stream_op_finish_with_failure(exec_ctx, op, error); |
| 692 | } |
| 693 | gpr_atm_rel_store(&calld->subchannel_call, |
| 694 | (gpr_atm)(uintptr_t)subchannel_call); |
| 695 | retry_waiting_locked(exec_ctx, calld); |
| 696 | goto retry; |
| 697 | } |
| 698 | /* nothing to be done but wait */ |
| 699 | add_waiting_locked(calld, op); |
| 700 | gpr_mu_unlock(&calld->mu); |
| 701 | GPR_TIMER_END("cc_start_transport_stream_op", 0); |
| 702 | } |
| 703 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 704 | /* Constructor for call_data */ |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame^] | 705 | static grpc_error *cc_init_call_elem(grpc_exec_ctx *exec_ctx, |
| 706 | grpc_call_element *elem, |
| 707 | grpc_call_element_args *args) { |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 708 | call_data *calld = elem->call_data; |
| 709 | gpr_atm_rel_store(&calld->subchannel_call, 0); |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 710 | gpr_mu_init(&calld->mu); |
| 711 | calld->connected_subchannel = NULL; |
| 712 | calld->waiting_ops = NULL; |
| 713 | calld->waiting_ops_count = 0; |
| 714 | calld->waiting_ops_capacity = 0; |
| 715 | calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING; |
| 716 | calld->owning_call = args->call_stack; |
| 717 | calld->pollent = NULL; |
Mark D. Roth | 0badbe8 | 2016-06-23 10:15:12 -0700 | [diff] [blame] | 718 | return GRPC_ERROR_NONE; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | /* Destructor for call_data */ |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame^] | 722 | static void cc_destroy_call_elem(grpc_exec_ctx *exec_ctx, |
| 723 | grpc_call_element *elem, |
| 724 | const grpc_call_final_info *final_info, |
| 725 | void *and_free_memory) { |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 726 | call_data *calld = elem->call_data; |
| 727 | grpc_subchannel_call *call = GET_CALL(calld); |
| 728 | if (call != NULL && call != CANCELLED_CALL) { |
| 729 | GRPC_SUBCHANNEL_CALL_UNREF(exec_ctx, call, "client_channel_destroy_call"); |
| 730 | } |
| 731 | GPR_ASSERT(calld->creation_phase == GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING); |
| 732 | gpr_mu_destroy(&calld->mu); |
| 733 | GPR_ASSERT(calld->waiting_ops_count == 0); |
| 734 | gpr_free(calld->waiting_ops); |
Craig Tiller | 2c8063c | 2016-03-22 22:12:15 -0700 | [diff] [blame] | 735 | gpr_free(and_free_memory); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 736 | } |
| 737 | |
David Garcia Quintas | f72eb97 | 2016-05-03 18:28:09 -0700 | [diff] [blame] | 738 | static void cc_set_pollset_or_pollset_set(grpc_exec_ctx *exec_ctx, |
| 739 | grpc_call_element *elem, |
David Garcia Quintas | 2a50dfe | 2016-05-31 15:09:12 -0700 | [diff] [blame] | 740 | grpc_polling_entity *pollent) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 741 | call_data *calld = elem->call_data; |
David Garcia Quintas | 2a50dfe | 2016-05-31 15:09:12 -0700 | [diff] [blame] | 742 | calld->pollent = pollent; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 743 | } |
| 744 | |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame^] | 745 | /************************************************************************* |
| 746 | * EXPORTED SYMBOLS |
| 747 | */ |
| 748 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 749 | const grpc_channel_filter grpc_client_channel_filter = { |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 750 | cc_start_transport_stream_op, |
| 751 | cc_start_transport_op, |
| 752 | sizeof(call_data), |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame^] | 753 | cc_init_call_elem, |
David Garcia Quintas | 4afce7e | 2016-04-18 16:25:17 -0700 | [diff] [blame] | 754 | cc_set_pollset_or_pollset_set, |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame^] | 755 | cc_destroy_call_elem, |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 756 | sizeof(channel_data), |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame^] | 757 | cc_init_channel_elem, |
| 758 | cc_destroy_channel_elem, |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 759 | cc_get_peer, |
| 760 | "client-channel", |
Craig Tiller | 87d5b19 | 2015-04-16 14:37:57 -0700 | [diff] [blame] | 761 | }; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 762 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 763 | void grpc_client_channel_set_resolver(grpc_exec_ctx *exec_ctx, |
| 764 | grpc_channel_stack *channel_stack, |
| 765 | grpc_resolver *resolver) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 766 | /* post construction initialization: set the transport setup pointer */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 767 | grpc_channel_element *elem = grpc_channel_stack_last_element(channel_stack); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 768 | channel_data *chand = elem->channel_data; |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 769 | gpr_mu_lock(&chand->mu); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 770 | GPR_ASSERT(!chand->resolver); |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 771 | chand->resolver = resolver; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 772 | GRPC_RESOLVER_REF(resolver, "channel"); |
| 773 | if (!grpc_closure_list_empty(chand->waiting_for_config_closures) || |
| 774 | chand->exit_idle_when_lb_policy_arrives) { |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 775 | chand->started_resolving = true; |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 776 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver"); |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 777 | grpc_resolver_next(exec_ctx, resolver, &chand->resolver_result, |
| 778 | &chand->on_resolver_result_changed); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 779 | } |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 780 | gpr_mu_unlock(&chand->mu); |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 781 | } |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 782 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 783 | grpc_connectivity_state grpc_client_channel_check_connectivity_state( |
| 784 | 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] | 785 | channel_data *chand = elem->channel_data; |
| 786 | grpc_connectivity_state out; |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 787 | gpr_mu_lock(&chand->mu); |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 788 | out = grpc_connectivity_state_check(&chand->state_tracker, NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 789 | if (out == GRPC_CHANNEL_IDLE && try_to_connect) { |
| 790 | if (chand->lb_policy != NULL) { |
| 791 | grpc_lb_policy_exit_idle(exec_ctx, chand->lb_policy); |
| 792 | } else { |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 793 | chand->exit_idle_when_lb_policy_arrives = true; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 794 | if (!chand->started_resolving && chand->resolver != NULL) { |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 795 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver"); |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 796 | chand->started_resolving = true; |
Mark D. Roth | a275aea | 2016-08-23 12:30:45 -0700 | [diff] [blame] | 797 | grpc_resolver_next(exec_ctx, chand->resolver, &chand->resolver_result, |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 798 | &chand->on_resolver_result_changed); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 799 | } |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 800 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 801 | } |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 802 | gpr_mu_unlock(&chand->mu); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 803 | return out; |
| 804 | } |
| 805 | |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 806 | typedef struct { |
| 807 | channel_data *chand; |
| 808 | grpc_pollset *pollset; |
| 809 | grpc_closure *on_complete; |
| 810 | grpc_closure my_closure; |
| 811 | } external_connectivity_watcher; |
| 812 | |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 813 | static void on_external_watch_complete(grpc_exec_ctx *exec_ctx, void *arg, |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 814 | grpc_error *error) { |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 815 | external_connectivity_watcher *w = arg; |
| 816 | grpc_closure *follow_up = w->on_complete; |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 817 | grpc_pollset_set_del_pollset(exec_ctx, w->chand->interested_parties, |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 818 | w->pollset); |
| 819 | GRPC_CHANNEL_STACK_UNREF(exec_ctx, w->chand->owning_stack, |
| 820 | "external_connectivity_watcher"); |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 821 | gpr_free(w); |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 822 | follow_up->cb(exec_ctx, follow_up->cb_arg, error); |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 823 | } |
| 824 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 825 | void grpc_client_channel_watch_connectivity_state( |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 826 | grpc_exec_ctx *exec_ctx, grpc_channel_element *elem, grpc_pollset *pollset, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 827 | grpc_connectivity_state *state, grpc_closure *on_complete) { |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 828 | channel_data *chand = elem->channel_data; |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 829 | external_connectivity_watcher *w = gpr_malloc(sizeof(*w)); |
| 830 | w->chand = chand; |
| 831 | w->pollset = pollset; |
| 832 | w->on_complete = on_complete; |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 833 | grpc_pollset_set_add_pollset(exec_ctx, chand->interested_parties, pollset); |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 834 | grpc_closure_init(&w->my_closure, on_external_watch_complete, w); |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 835 | GRPC_CHANNEL_STACK_REF(w->chand->owning_stack, |
| 836 | "external_connectivity_watcher"); |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 837 | gpr_mu_lock(&chand->mu); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 838 | grpc_connectivity_state_notify_on_state_change( |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 839 | exec_ctx, &chand->state_tracker, state, &w->my_closure); |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 840 | gpr_mu_unlock(&chand->mu); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 841 | } |