Craig Tiller | af69180 | 2015-06-23 14:57:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2015, Google Inc. |
| 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/client_config/subchannel.h" |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 35 | |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 36 | #include <string.h> |
| 37 | |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 38 | #include <grpc/support/alloc.h> |
| 39 | |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 40 | #include "src/core/channel/channel_args.h" |
Craig Tiller | ff54c92 | 2015-06-26 16:57:20 -0700 | [diff] [blame] | 41 | #include "src/core/channel/connected_channel.h" |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 42 | #include "src/core/iomgr/alarm.h" |
Craig Tiller | 08a1cf8 | 2015-06-29 09:37:52 -0700 | [diff] [blame] | 43 | #include "src/core/transport/connectivity_state.h" |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 44 | |
| 45 | typedef struct { |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 46 | /* all fields protected by subchannel->mu */ |
| 47 | /** refcount */ |
| 48 | int refs; |
| 49 | /** parent subchannel */ |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 50 | grpc_subchannel *subchannel; |
| 51 | } connection; |
| 52 | |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame] | 53 | typedef struct { |
| 54 | grpc_iomgr_closure closure; |
| 55 | size_t version; |
| 56 | grpc_subchannel *subchannel; |
| 57 | grpc_connectivity_state connectivity_state; |
| 58 | } state_watcher; |
| 59 | |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 60 | typedef struct waiting_for_connect { |
| 61 | struct waiting_for_connect *next; |
| 62 | grpc_iomgr_closure *notify; |
Craig Tiller | abf3638 | 2015-06-29 16:13:27 -0700 | [diff] [blame] | 63 | grpc_pollset *pollset; |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 64 | grpc_subchannel_call **target; |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame] | 65 | grpc_subchannel *subchannel; |
| 66 | grpc_iomgr_closure continuation; |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 67 | } waiting_for_connect; |
| 68 | |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 69 | struct grpc_subchannel { |
Craig Tiller | 9162466 | 2015-06-25 16:31:02 -0700 | [diff] [blame] | 70 | grpc_connector *connector; |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 71 | |
| 72 | /** non-transport related channel filters */ |
| 73 | const grpc_channel_filter **filters; |
Craig Tiller | 5945ee1 | 2015-06-27 10:36:09 -0700 | [diff] [blame] | 74 | size_t num_filters; |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 75 | /** channel arguments */ |
| 76 | grpc_channel_args *args; |
| 77 | /** address to connect to */ |
| 78 | struct sockaddr *addr; |
| 79 | size_t addr_len; |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 80 | /** metadata context */ |
| 81 | grpc_mdctx *mdctx; |
Craig Tiller | 9846503 | 2015-06-29 14:36:42 -0700 | [diff] [blame] | 82 | /** master channel */ |
| 83 | grpc_channel *master; |
Craig Tiller | b6fbf1d | 2015-06-29 15:25:49 -0700 | [diff] [blame] | 84 | /** have we seen a disconnection? */ |
| 85 | int disconnected; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 86 | |
| 87 | /** set during connection */ |
Craig Tiller | 04c5d4b | 2015-06-26 17:21:41 -0700 | [diff] [blame] | 88 | grpc_connect_out_args connecting_result; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 89 | |
| 90 | /** callback for connection finishing */ |
| 91 | grpc_iomgr_closure connected; |
| 92 | |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 93 | /** pollset_set tracking who's interested in a connection |
| 94 | being setup */ |
| 95 | grpc_pollset_set pollset_set; |
| 96 | |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 97 | /** mutex protecting remaining elements */ |
| 98 | gpr_mu mu; |
| 99 | |
| 100 | /** active connection */ |
| 101 | connection *active; |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame] | 102 | /** version number for the active connection */ |
| 103 | size_t active_version; |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 104 | /** refcount */ |
| 105 | int refs; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 106 | /** are we connecting */ |
| 107 | int connecting; |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 108 | /** things waiting for a connection */ |
| 109 | waiting_for_connect *waiting; |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 110 | /** connectivity state tracking */ |
| 111 | grpc_connectivity_state_tracker state_tracker; |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 112 | |
| 113 | /** next connect attempt time */ |
| 114 | gpr_timespec next_attempt; |
| 115 | /** amount to backoff each failure */ |
| 116 | gpr_timespec backoff_delta; |
| 117 | /** do we have an active alarm? */ |
| 118 | int have_alarm; |
| 119 | /** our alarm */ |
| 120 | grpc_alarm alarm; |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | struct grpc_subchannel_call { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 124 | connection *connection; |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 125 | gpr_refcount refs; |
| 126 | }; |
| 127 | |
Craig Tiller | 04c5d4b | 2015-06-26 17:21:41 -0700 | [diff] [blame] | 128 | #define SUBCHANNEL_CALL_TO_CALL_STACK(call) ((grpc_call_stack *)((call) + 1)) |
| 129 | #define CHANNEL_STACK_FROM_CONNECTION(con) ((grpc_channel_stack *)((con) + 1)) |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 130 | |
Craig Tiller | abf3638 | 2015-06-29 16:13:27 -0700 | [diff] [blame] | 131 | static grpc_subchannel_call *create_call(connection *con); |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 132 | static void connectivity_state_changed_locked(grpc_subchannel *c); |
| 133 | static grpc_connectivity_state compute_connectivity_locked(grpc_subchannel *c); |
| 134 | static gpr_timespec compute_connect_deadline(grpc_subchannel *c); |
Craig Tiller | ff54c92 | 2015-06-26 16:57:20 -0700 | [diff] [blame] | 135 | static void subchannel_connected(void *subchannel, int iomgr_success); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 136 | |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 137 | static void subchannel_ref_locked( |
| 138 | grpc_subchannel *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS); |
| 139 | static int subchannel_unref_locked( |
| 140 | grpc_subchannel *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) GRPC_MUST_USE_RESULT; |
Craig Tiller | c396753 | 2015-06-29 14:59:38 -0700 | [diff] [blame] | 141 | static void connection_ref_locked(connection *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS); |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 142 | static grpc_subchannel *connection_unref_locked( |
| 143 | connection *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) GRPC_MUST_USE_RESULT; |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 144 | static void subchannel_destroy(grpc_subchannel *c); |
| 145 | |
Craig Tiller | c396753 | 2015-06-29 14:59:38 -0700 | [diff] [blame] | 146 | #ifdef GRPC_SUBCHANNEL_REFCOUNT_DEBUG |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 147 | #define SUBCHANNEL_REF_LOCKED(p, r) \ |
| 148 | subchannel_ref_locked((p), __FILE__, __LINE__, (r)) |
| 149 | #define SUBCHANNEL_UNREF_LOCKED(p, r) \ |
| 150 | subchannel_unref_locked((p), __FILE__, __LINE__, (r)) |
| 151 | #define CONNECTION_REF_LOCKED(p, r) \ |
| 152 | connection_ref_locked((p), __FILE__, __LINE__, (r)) |
| 153 | #define CONNECTION_UNREF_LOCKED(p, r) \ |
| 154 | connection_unref_locked((p), __FILE__, __LINE__, (r)) |
Craig Tiller | c396753 | 2015-06-29 14:59:38 -0700 | [diff] [blame] | 155 | #define REF_PASS_ARGS , file, line, reason |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 156 | #define REF_LOG(name, p) \ |
| 157 | gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "%s: %p ref %d -> %d %s", \ |
| 158 | (name), (p), (p)->refs, (p)->refs + 1, reason) |
| 159 | #define UNREF_LOG(name, p) \ |
| 160 | gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "%s: %p unref %d -> %d %s", \ |
| 161 | (name), (p), (p)->refs, (p)->refs - 1, reason) |
Craig Tiller | c396753 | 2015-06-29 14:59:38 -0700 | [diff] [blame] | 162 | #else |
| 163 | #define SUBCHANNEL_REF_LOCKED(p, r) subchannel_ref_locked((p)) |
| 164 | #define SUBCHANNEL_UNREF_LOCKED(p, r) subchannel_unref_locked((p)) |
Craig Tiller | 11bf14e | 2015-06-29 16:35:41 -0700 | [diff] [blame] | 165 | #define CONNECTION_REF_LOCKED(p, r) connection_ref_locked((p)) |
| 166 | #define CONNECTION_UNREF_LOCKED(p, r) connection_unref_locked((p)) |
Craig Tiller | c396753 | 2015-06-29 14:59:38 -0700 | [diff] [blame] | 167 | #define REF_PASS_ARGS |
Craig Tiller | 11bf14e | 2015-06-29 16:35:41 -0700 | [diff] [blame] | 168 | #define REF_LOG(name, p) \ |
| 169 | do { \ |
| 170 | } while (0) |
| 171 | #define UNREF_LOG(name, p) \ |
| 172 | do { \ |
| 173 | } while (0) |
Craig Tiller | c396753 | 2015-06-29 14:59:38 -0700 | [diff] [blame] | 174 | #endif |
| 175 | |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 176 | /* |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 177 | * connection implementation |
| 178 | */ |
| 179 | |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 180 | static void connection_destroy(connection *c) { |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 181 | GPR_ASSERT(c->refs == 0); |
Craig Tiller | b6fbf1d | 2015-06-29 15:25:49 -0700 | [diff] [blame] | 182 | gpr_log(GPR_DEBUG, "CONNECTION_DESTROY %p", c); |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 183 | grpc_channel_stack_destroy(CHANNEL_STACK_FROM_CONNECTION(c)); |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 184 | gpr_free(c); |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 187 | static void connection_ref_locked( |
| 188 | connection *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) { |
Craig Tiller | c396753 | 2015-06-29 14:59:38 -0700 | [diff] [blame] | 189 | REF_LOG("CONNECTION", c); |
| 190 | subchannel_ref_locked(c->subchannel REF_PASS_ARGS); |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 191 | ++c->refs; |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 194 | static grpc_subchannel *connection_unref_locked( |
| 195 | connection *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) { |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 196 | grpc_subchannel *destroy = NULL; |
Craig Tiller | c396753 | 2015-06-29 14:59:38 -0700 | [diff] [blame] | 197 | UNREF_LOG("CONNECTION", c); |
| 198 | if (subchannel_unref_locked(c->subchannel REF_PASS_ARGS)) { |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 199 | destroy = c->subchannel; |
| 200 | } |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 201 | if (--c->refs == 0 && c->subchannel->active != c) { |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 202 | connection_destroy(c); |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 203 | } |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 204 | return destroy; |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | /* |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 208 | * grpc_subchannel implementation |
| 209 | */ |
| 210 | |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 211 | static void subchannel_ref_locked( |
| 212 | grpc_subchannel *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) { |
Craig Tiller | c396753 | 2015-06-29 14:59:38 -0700 | [diff] [blame] | 213 | REF_LOG("SUBCHANNEL", c); |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 214 | ++c->refs; |
Craig Tiller | c396753 | 2015-06-29 14:59:38 -0700 | [diff] [blame] | 215 | } |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 216 | |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 217 | static int subchannel_unref_locked( |
| 218 | grpc_subchannel *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) { |
Craig Tiller | c396753 | 2015-06-29 14:59:38 -0700 | [diff] [blame] | 219 | UNREF_LOG("SUBCHANNEL", c); |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 220 | return --c->refs == 0; |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Craig Tiller | c396753 | 2015-06-29 14:59:38 -0700 | [diff] [blame] | 223 | void grpc_subchannel_ref(grpc_subchannel *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) { |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 224 | gpr_mu_lock(&c->mu); |
Craig Tiller | c396753 | 2015-06-29 14:59:38 -0700 | [diff] [blame] | 225 | subchannel_ref_locked(c REF_PASS_ARGS); |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 226 | gpr_mu_unlock(&c->mu); |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Craig Tiller | c396753 | 2015-06-29 14:59:38 -0700 | [diff] [blame] | 229 | void grpc_subchannel_unref(grpc_subchannel *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) { |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 230 | int destroy; |
| 231 | gpr_mu_lock(&c->mu); |
Craig Tiller | c396753 | 2015-06-29 14:59:38 -0700 | [diff] [blame] | 232 | destroy = subchannel_unref_locked(c REF_PASS_ARGS); |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 233 | gpr_mu_unlock(&c->mu); |
| 234 | if (destroy) subchannel_destroy(c); |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | static void subchannel_destroy(grpc_subchannel *c) { |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 238 | if (c->active != NULL) { |
| 239 | connection_destroy(c->active); |
| 240 | } |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 241 | gpr_free(c->filters); |
| 242 | grpc_channel_args_destroy(c->args); |
| 243 | gpr_free(c->addr); |
| 244 | grpc_mdctx_unref(c->mdctx); |
| 245 | grpc_pollset_set_destroy(&c->pollset_set); |
| 246 | grpc_connectivity_state_destroy(&c->state_tracker); |
Craig Tiller | c396753 | 2015-06-29 14:59:38 -0700 | [diff] [blame] | 247 | grpc_connector_unref(c->connector); |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 248 | gpr_free(c); |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 251 | void grpc_subchannel_add_interested_party(grpc_subchannel *c, |
| 252 | grpc_pollset *pollset) { |
| 253 | grpc_pollset_set_add_pollset(&c->pollset_set, pollset); |
| 254 | } |
| 255 | |
| 256 | void grpc_subchannel_del_interested_party(grpc_subchannel *c, |
| 257 | grpc_pollset *pollset) { |
| 258 | grpc_pollset_set_del_pollset(&c->pollset_set, pollset); |
| 259 | } |
| 260 | |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 261 | grpc_subchannel *grpc_subchannel_create(grpc_connector *connector, |
| 262 | grpc_subchannel_args *args) { |
| 263 | grpc_subchannel *c = gpr_malloc(sizeof(*c)); |
| 264 | memset(c, 0, sizeof(*c)); |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 265 | c->refs = 1; |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 266 | c->connector = connector; |
| 267 | grpc_connector_ref(c->connector); |
Craig Tiller | 5945ee1 | 2015-06-27 10:36:09 -0700 | [diff] [blame] | 268 | c->num_filters = args->filter_count; |
| 269 | c->filters = gpr_malloc(sizeof(grpc_channel_filter *) * c->num_filters); |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 270 | memcpy(c->filters, args->filters, |
Craig Tiller | 5945ee1 | 2015-06-27 10:36:09 -0700 | [diff] [blame] | 271 | sizeof(grpc_channel_filter *) * c->num_filters); |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 272 | c->addr = gpr_malloc(args->addr_len); |
| 273 | memcpy(c->addr, args->addr, args->addr_len); |
| 274 | c->addr_len = args->addr_len; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 275 | c->args = grpc_channel_args_copy(args->args); |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 276 | c->mdctx = args->mdctx; |
Craig Tiller | 9846503 | 2015-06-29 14:36:42 -0700 | [diff] [blame] | 277 | c->master = args->master; |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 278 | grpc_mdctx_ref(c->mdctx); |
Craig Tiller | ff54c92 | 2015-06-26 16:57:20 -0700 | [diff] [blame] | 279 | grpc_pollset_set_init(&c->pollset_set); |
| 280 | grpc_iomgr_closure_init(&c->connected, subchannel_connected, c); |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 281 | grpc_connectivity_state_init(&c->state_tracker, GRPC_CHANNEL_IDLE); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 282 | gpr_mu_init(&c->mu); |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 283 | return c; |
| 284 | } |
| 285 | |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 286 | static void continue_connect(grpc_subchannel *c) { |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 287 | grpc_connect_in_args args; |
Craig Tiller | 04c5d4b | 2015-06-26 17:21:41 -0700 | [diff] [blame] | 288 | |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 289 | args.interested_parties = &c->pollset_set; |
| 290 | args.addr = c->addr; |
| 291 | args.addr_len = c->addr_len; |
| 292 | args.deadline = compute_connect_deadline(c); |
| 293 | args.channel_args = c->args; |
| 294 | args.metadata_context = c->mdctx; |
Craig Tiller | 04c5d4b | 2015-06-26 17:21:41 -0700 | [diff] [blame] | 295 | |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 296 | grpc_connector_connect(c->connector, &args, &c->connecting_result, |
| 297 | &c->connected); |
Craig Tiller | 04c5d4b | 2015-06-26 17:21:41 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 300 | static void start_connect(grpc_subchannel *c) { |
| 301 | gpr_timespec now = gpr_now(); |
| 302 | c->next_attempt = now; |
| 303 | c->backoff_delta = gpr_time_from_seconds(1); |
| 304 | |
| 305 | continue_connect(c); |
| 306 | } |
| 307 | |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame] | 308 | static void continue_creating_call(void *arg, int iomgr_success) { |
| 309 | waiting_for_connect *w4c = arg; |
Craig Tiller | abf3638 | 2015-06-29 16:13:27 -0700 | [diff] [blame] | 310 | grpc_subchannel_create_call(w4c->subchannel, w4c->pollset, w4c->target, |
Craig Tiller | f62d6fc | 2015-06-29 10:55:59 -0700 | [diff] [blame] | 311 | w4c->notify); |
Craig Tiller | c396753 | 2015-06-29 14:59:38 -0700 | [diff] [blame] | 312 | GRPC_SUBCHANNEL_UNREF(w4c->subchannel, "waiting_for_connect"); |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame] | 313 | gpr_free(w4c); |
| 314 | } |
| 315 | |
Craig Tiller | abf3638 | 2015-06-29 16:13:27 -0700 | [diff] [blame] | 316 | void grpc_subchannel_create_call(grpc_subchannel *c, grpc_pollset *pollset, |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 317 | grpc_subchannel_call **target, |
| 318 | grpc_iomgr_closure *notify) { |
| 319 | connection *con; |
| 320 | gpr_mu_lock(&c->mu); |
| 321 | if (c->active != NULL) { |
| 322 | con = c->active; |
Craig Tiller | c396753 | 2015-06-29 14:59:38 -0700 | [diff] [blame] | 323 | CONNECTION_REF_LOCKED(con, "call"); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 324 | gpr_mu_unlock(&c->mu); |
| 325 | |
Craig Tiller | abf3638 | 2015-06-29 16:13:27 -0700 | [diff] [blame] | 326 | *target = create_call(con); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 327 | notify->cb(notify->cb_arg, 1); |
| 328 | } else { |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 329 | waiting_for_connect *w4c = gpr_malloc(sizeof(*w4c)); |
| 330 | w4c->next = c->waiting; |
| 331 | w4c->notify = notify; |
Craig Tiller | abf3638 | 2015-06-29 16:13:27 -0700 | [diff] [blame] | 332 | w4c->pollset = pollset; |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 333 | w4c->target = target; |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame] | 334 | w4c->subchannel = c; |
Craig Tiller | 9846503 | 2015-06-29 14:36:42 -0700 | [diff] [blame] | 335 | /* released when clearing w4c */ |
Craig Tiller | c396753 | 2015-06-29 14:59:38 -0700 | [diff] [blame] | 336 | SUBCHANNEL_REF_LOCKED(c, "waiting_for_connect"); |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame] | 337 | grpc_iomgr_closure_init(&w4c->continuation, continue_creating_call, w4c); |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 338 | c->waiting = w4c; |
Craig Tiller | abf3638 | 2015-06-29 16:13:27 -0700 | [diff] [blame] | 339 | grpc_subchannel_add_interested_party(c, pollset); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 340 | if (!c->connecting) { |
| 341 | c->connecting = 1; |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 342 | connectivity_state_changed_locked(c); |
Craig Tiller | 9846503 | 2015-06-29 14:36:42 -0700 | [diff] [blame] | 343 | /* released by connection */ |
Craig Tiller | c396753 | 2015-06-29 14:59:38 -0700 | [diff] [blame] | 344 | SUBCHANNEL_REF_LOCKED(c, "connecting"); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 345 | gpr_mu_unlock(&c->mu); |
| 346 | |
Craig Tiller | 04c5d4b | 2015-06-26 17:21:41 -0700 | [diff] [blame] | 347 | start_connect(c); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 348 | } else { |
| 349 | gpr_mu_unlock(&c->mu); |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 354 | grpc_connectivity_state grpc_subchannel_check_connectivity(grpc_subchannel *c) { |
| 355 | grpc_connectivity_state state; |
| 356 | gpr_mu_lock(&c->mu); |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 357 | state = grpc_connectivity_state_check(&c->state_tracker); |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 358 | gpr_mu_unlock(&c->mu); |
| 359 | return state; |
| 360 | } |
| 361 | |
| 362 | void grpc_subchannel_notify_on_state_change(grpc_subchannel *c, |
| 363 | grpc_connectivity_state *state, |
| 364 | grpc_iomgr_closure *notify) { |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 365 | int do_connect = 0; |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 366 | gpr_mu_lock(&c->mu); |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 367 | if (grpc_connectivity_state_notify_on_state_change(&c->state_tracker, state, |
| 368 | notify)) { |
| 369 | do_connect = 1; |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 370 | c->connecting = 1; |
Craig Tiller | 9846503 | 2015-06-29 14:36:42 -0700 | [diff] [blame] | 371 | /* released by connection */ |
Craig Tiller | c396753 | 2015-06-29 14:59:38 -0700 | [diff] [blame] | 372 | SUBCHANNEL_REF_LOCKED(c, "connecting"); |
Craig Tiller | 11bf14e | 2015-06-29 16:35:41 -0700 | [diff] [blame] | 373 | connectivity_state_changed_locked(c); |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 374 | } |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 375 | gpr_mu_unlock(&c->mu); |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 376 | if (do_connect) { |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 377 | start_connect(c); |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 381 | void grpc_subchannel_process_transport_op(grpc_subchannel *c, |
| 382 | grpc_transport_op *op) { |
Craig Tiller | b6fbf1d | 2015-06-29 15:25:49 -0700 | [diff] [blame] | 383 | connection *con = NULL; |
| 384 | grpc_subchannel *destroy; |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 385 | int cancel_alarm = 0; |
Craig Tiller | b6fbf1d | 2015-06-29 15:25:49 -0700 | [diff] [blame] | 386 | gpr_mu_lock(&c->mu); |
| 387 | if (op->disconnect) { |
| 388 | c->disconnected = 1; |
Craig Tiller | 11bf14e | 2015-06-29 16:35:41 -0700 | [diff] [blame] | 389 | connectivity_state_changed_locked(c); |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 390 | if (c->have_alarm) { |
| 391 | cancel_alarm = 1; |
| 392 | } |
Craig Tiller | b6fbf1d | 2015-06-29 15:25:49 -0700 | [diff] [blame] | 393 | } |
| 394 | if (c->active != NULL) { |
| 395 | con = c->active; |
| 396 | CONNECTION_REF_LOCKED(con, "transport-op"); |
| 397 | } |
| 398 | gpr_mu_unlock(&c->mu); |
| 399 | |
| 400 | if (con != NULL) { |
| 401 | grpc_channel_stack *channel_stack = CHANNEL_STACK_FROM_CONNECTION(con); |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 402 | grpc_channel_element *top_elem = |
| 403 | grpc_channel_stack_element(channel_stack, 0); |
Craig Tiller | b6fbf1d | 2015-06-29 15:25:49 -0700 | [diff] [blame] | 404 | top_elem->filter->start_transport_op(top_elem, op); |
| 405 | |
| 406 | gpr_mu_lock(&c->mu); |
| 407 | destroy = CONNECTION_UNREF_LOCKED(con, "transport-op"); |
| 408 | gpr_mu_unlock(&c->mu); |
| 409 | if (destroy) { |
| 410 | subchannel_destroy(destroy); |
| 411 | } |
| 412 | } |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 413 | |
| 414 | if (cancel_alarm) { |
| 415 | grpc_alarm_cancel(&c->alarm); |
| 416 | } |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | static void on_state_changed(void *p, int iomgr_success) { |
| 420 | state_watcher *sw = p; |
| 421 | grpc_subchannel *c = sw->subchannel; |
| 422 | gpr_mu *mu = &c->mu; |
| 423 | int destroy; |
| 424 | grpc_transport_op op; |
| 425 | grpc_channel_element *elem; |
| 426 | connection *destroy_connection = NULL; |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame] | 427 | |
| 428 | gpr_mu_lock(mu); |
| 429 | |
| 430 | /* if we failed or there is a version number mismatch, just leave |
| 431 | this closure */ |
| 432 | if (!iomgr_success || sw->subchannel->active_version != sw->version) { |
| 433 | goto done; |
| 434 | } |
| 435 | |
Craig Tiller | 11bf14e | 2015-06-29 16:35:41 -0700 | [diff] [blame] | 436 | gpr_log(GPR_DEBUG, "TRANSPORT STATE: %d", sw->connectivity_state); |
| 437 | |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame] | 438 | switch (sw->connectivity_state) { |
| 439 | case GRPC_CHANNEL_CONNECTING: |
| 440 | case GRPC_CHANNEL_READY: |
| 441 | case GRPC_CHANNEL_IDLE: |
| 442 | /* all is still good: keep watching */ |
| 443 | memset(&op, 0, sizeof(op)); |
| 444 | op.connectivity_state = &sw->connectivity_state; |
| 445 | op.on_connectivity_state_change = &sw->closure; |
Craig Tiller | f62d6fc | 2015-06-29 10:55:59 -0700 | [diff] [blame] | 446 | elem = grpc_channel_stack_element( |
| 447 | CHANNEL_STACK_FROM_CONNECTION(c->active), 0); |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame] | 448 | elem->filter->start_transport_op(elem, &op); |
| 449 | /* early out */ |
| 450 | gpr_mu_unlock(mu); |
| 451 | return; |
| 452 | case GRPC_CHANNEL_FATAL_FAILURE: |
Craig Tiller | 49924e0 | 2015-06-29 22:42:33 -0700 | [diff] [blame] | 453 | case GRPC_CHANNEL_TRANSIENT_FAILURE: |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame] | 454 | /* things have gone wrong, deactivate and enter idle */ |
| 455 | if (sw->subchannel->active->refs == 0) { |
| 456 | destroy_connection = sw->subchannel->active; |
| 457 | } |
| 458 | sw->subchannel->active = NULL; |
Craig Tiller | 11bf14e | 2015-06-29 16:35:41 -0700 | [diff] [blame] | 459 | grpc_connectivity_state_set(&c->state_tracker, |
| 460 | GRPC_CHANNEL_TRANSIENT_FAILURE); |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame] | 461 | break; |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | done: |
Craig Tiller | 11bf14e | 2015-06-29 16:35:41 -0700 | [diff] [blame] | 465 | connectivity_state_changed_locked(c); |
Craig Tiller | b6fbf1d | 2015-06-29 15:25:49 -0700 | [diff] [blame] | 466 | destroy = SUBCHANNEL_UNREF_LOCKED(c, "state_watcher"); |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame] | 467 | gpr_free(sw); |
| 468 | gpr_mu_unlock(mu); |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame] | 469 | if (destroy) { |
| 470 | subchannel_destroy(c); |
| 471 | } |
| 472 | if (destroy_connection != NULL) { |
| 473 | connection_destroy(destroy_connection); |
| 474 | } |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 475 | } |
| 476 | |
Craig Tiller | ff54c92 | 2015-06-26 16:57:20 -0700 | [diff] [blame] | 477 | static void publish_transport(grpc_subchannel *c) { |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 478 | size_t channel_stack_size; |
| 479 | connection *con; |
| 480 | grpc_channel_stack *stk; |
| 481 | size_t num_filters; |
| 482 | const grpc_channel_filter **filters; |
| 483 | waiting_for_connect *w4c; |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame] | 484 | grpc_transport_op op; |
| 485 | state_watcher *sw; |
| 486 | connection *destroy_connection = NULL; |
| 487 | grpc_channel_element *elem; |
Craig Tiller | 5945ee1 | 2015-06-27 10:36:09 -0700 | [diff] [blame] | 488 | |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame] | 489 | /* build final filter list */ |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 490 | num_filters = c->num_filters + c->connecting_result.num_filters + 1; |
| 491 | filters = gpr_malloc(sizeof(*filters) * num_filters); |
| 492 | memcpy(filters, c->filters, sizeof(*filters) * c->num_filters); |
| 493 | memcpy(filters + c->num_filters, c->connecting_result.filters, |
| 494 | sizeof(*filters) * c->connecting_result.num_filters); |
| 495 | filters[num_filters - 1] = &grpc_connected_channel_filter; |
Craig Tiller | 5945ee1 | 2015-06-27 10:36:09 -0700 | [diff] [blame] | 496 | |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame] | 497 | /* construct channel stack */ |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 498 | channel_stack_size = grpc_channel_stack_size(filters, num_filters); |
| 499 | con = gpr_malloc(sizeof(connection) + channel_stack_size); |
| 500 | stk = (grpc_channel_stack *)(con + 1); |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 501 | con->refs = 0; |
| 502 | con->subchannel = c; |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 503 | grpc_channel_stack_init(filters, num_filters, c->master, c->args, c->mdctx, |
| 504 | stk); |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 505 | grpc_connected_channel_bind_transport(stk, c->connecting_result.transport); |
Craig Tiller | 9846503 | 2015-06-29 14:36:42 -0700 | [diff] [blame] | 506 | gpr_free(c->connecting_result.filters); |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 507 | memset(&c->connecting_result, 0, sizeof(c->connecting_result)); |
Craig Tiller | ff54c92 | 2015-06-26 16:57:20 -0700 | [diff] [blame] | 508 | |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame] | 509 | /* initialize state watcher */ |
| 510 | sw = gpr_malloc(sizeof(*sw)); |
| 511 | grpc_iomgr_closure_init(&sw->closure, on_state_changed, sw); |
| 512 | sw->subchannel = c; |
| 513 | sw->connectivity_state = GRPC_CHANNEL_READY; |
| 514 | |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 515 | gpr_mu_lock(&c->mu); |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame] | 516 | |
Craig Tiller | b6fbf1d | 2015-06-29 15:25:49 -0700 | [diff] [blame] | 517 | if (c->disconnected) { |
| 518 | gpr_mu_unlock(&c->mu); |
| 519 | gpr_free(sw); |
| 520 | gpr_free(filters); |
| 521 | grpc_channel_stack_destroy(stk); |
| 522 | return; |
| 523 | } |
| 524 | |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame] | 525 | /* publish */ |
| 526 | if (c->active != NULL && c->active->refs == 0) { |
| 527 | destroy_connection = c->active; |
| 528 | } |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 529 | c->active = con; |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame] | 530 | c->active_version++; |
| 531 | sw->version = c->active_version; |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 532 | c->connecting = 0; |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame] | 533 | |
| 534 | /* watch for changes; subchannel ref for connecting is donated |
| 535 | to the state watcher */ |
| 536 | memset(&op, 0, sizeof(op)); |
| 537 | op.connectivity_state = &sw->connectivity_state; |
| 538 | op.on_connectivity_state_change = &sw->closure; |
Craig Tiller | b6fbf1d | 2015-06-29 15:25:49 -0700 | [diff] [blame] | 539 | SUBCHANNEL_REF_LOCKED(c, "state_watcher"); |
| 540 | GPR_ASSERT(!SUBCHANNEL_UNREF_LOCKED(c, "connecting")); |
Craig Tiller | f62d6fc | 2015-06-29 10:55:59 -0700 | [diff] [blame] | 541 | elem = |
| 542 | grpc_channel_stack_element(CHANNEL_STACK_FROM_CONNECTION(c->active), 0); |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame] | 543 | elem->filter->start_transport_op(elem, &op); |
| 544 | |
| 545 | /* signal completion */ |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 546 | connectivity_state_changed_locked(c); |
| 547 | while ((w4c = c->waiting)) { |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame] | 548 | c->waiting = w4c->next; |
| 549 | grpc_iomgr_add_callback(&w4c->continuation); |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 550 | } |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame] | 551 | |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 552 | gpr_mu_unlock(&c->mu); |
Craig Tiller | 5945ee1 | 2015-06-27 10:36:09 -0700 | [diff] [blame] | 553 | |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 554 | gpr_free(filters); |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 555 | |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame] | 556 | if (destroy_connection != NULL) { |
| 557 | connection_destroy(destroy_connection); |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 558 | } |
| 559 | } |
Craig Tiller | ff54c92 | 2015-06-26 16:57:20 -0700 | [diff] [blame] | 560 | |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 561 | static void on_alarm(void *arg, int iomgr_success) { |
| 562 | grpc_subchannel *c = arg; |
| 563 | gpr_mu_lock(&c->mu); |
| 564 | c->have_alarm = 0; |
Craig Tiller | d2cc459 | 2015-07-01 07:50:47 -0700 | [diff] [blame^] | 565 | if (c->disconnected) { |
| 566 | iomgr_success = 0; |
| 567 | } |
Craig Tiller | 87cc084 | 2015-06-30 08:15:55 -0700 | [diff] [blame] | 568 | connectivity_state_changed_locked(c); |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 569 | gpr_mu_unlock(&c->mu); |
| 570 | if (iomgr_success) { |
| 571 | continue_connect(c); |
| 572 | } else { |
| 573 | GRPC_SUBCHANNEL_UNREF(c, "connecting"); |
| 574 | } |
| 575 | } |
| 576 | |
Craig Tiller | ff54c92 | 2015-06-26 16:57:20 -0700 | [diff] [blame] | 577 | static void subchannel_connected(void *arg, int iomgr_success) { |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 578 | grpc_subchannel *c = arg; |
Craig Tiller | 11bf14e | 2015-06-29 16:35:41 -0700 | [diff] [blame] | 579 | if (c->connecting_result.transport != NULL) { |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 580 | publish_transport(c); |
| 581 | } else { |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 582 | gpr_mu_lock(&c->mu); |
Craig Tiller | 87cc084 | 2015-06-30 08:15:55 -0700 | [diff] [blame] | 583 | connectivity_state_changed_locked(c); |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 584 | GPR_ASSERT(!c->have_alarm); |
| 585 | c->have_alarm = 1; |
| 586 | c->next_attempt = gpr_time_add(c->next_attempt, c->backoff_delta); |
| 587 | c->backoff_delta = gpr_time_add(c->backoff_delta, c->backoff_delta); |
| 588 | grpc_alarm_init(&c->alarm, c->next_attempt, on_alarm, c, gpr_now()); |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 589 | gpr_mu_unlock(&c->mu); |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 590 | } |
Craig Tiller | ff54c92 | 2015-06-26 16:57:20 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 593 | static gpr_timespec compute_connect_deadline(grpc_subchannel *c) { |
Craig Tiller | 87cc084 | 2015-06-30 08:15:55 -0700 | [diff] [blame] | 594 | return gpr_time_add(c->next_attempt, c->backoff_delta); |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | static grpc_connectivity_state compute_connectivity_locked(grpc_subchannel *c) { |
Craig Tiller | b6fbf1d | 2015-06-29 15:25:49 -0700 | [diff] [blame] | 598 | if (c->disconnected) { |
| 599 | return GRPC_CHANNEL_FATAL_FAILURE; |
| 600 | } |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 601 | if (c->connecting) { |
Craig Tiller | 87cc084 | 2015-06-30 08:15:55 -0700 | [diff] [blame] | 602 | if (c->have_alarm) { |
| 603 | return GRPC_CHANNEL_TRANSIENT_FAILURE; |
| 604 | } |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 605 | return GRPC_CHANNEL_CONNECTING; |
| 606 | } |
| 607 | if (c->active) { |
| 608 | return GRPC_CHANNEL_READY; |
| 609 | } |
| 610 | return GRPC_CHANNEL_IDLE; |
| 611 | } |
| 612 | |
| 613 | static void connectivity_state_changed_locked(grpc_subchannel *c) { |
| 614 | grpc_connectivity_state current = compute_connectivity_locked(c); |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 615 | grpc_connectivity_state_set(&c->state_tracker, current); |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 616 | } |
| 617 | |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 618 | /* |
| 619 | * grpc_subchannel_call implementation |
| 620 | */ |
| 621 | |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 622 | void grpc_subchannel_call_ref( |
| 623 | grpc_subchannel_call *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) { |
| 624 | gpr_ref(&c->refs); |
Craig Tiller | c396753 | 2015-06-29 14:59:38 -0700 | [diff] [blame] | 625 | } |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 626 | |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 627 | void grpc_subchannel_call_unref( |
| 628 | grpc_subchannel_call *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) { |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 629 | if (gpr_unref(&c->refs)) { |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 630 | gpr_mu *mu = &c->connection->subchannel->mu; |
| 631 | grpc_subchannel *destroy; |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 632 | grpc_call_stack_destroy(SUBCHANNEL_CALL_TO_CALL_STACK(c)); |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 633 | gpr_mu_lock(mu); |
Craig Tiller | c396753 | 2015-06-29 14:59:38 -0700 | [diff] [blame] | 634 | destroy = CONNECTION_UNREF_LOCKED(c->connection, "call"); |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 635 | gpr_mu_unlock(mu); |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 636 | gpr_free(c); |
Craig Tiller | c396753 | 2015-06-29 14:59:38 -0700 | [diff] [blame] | 637 | if (destroy != NULL) { |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 638 | subchannel_destroy(destroy); |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 639 | } |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 640 | } |
| 641 | } |
| 642 | |
| 643 | void grpc_subchannel_call_process_op(grpc_subchannel_call *call, |
| 644 | grpc_transport_stream_op *op) { |
| 645 | grpc_call_stack *call_stack = SUBCHANNEL_CALL_TO_CALL_STACK(call); |
| 646 | grpc_call_element *top_elem = grpc_call_stack_element(call_stack, 0); |
| 647 | top_elem->filter->start_transport_stream_op(top_elem, op); |
| 648 | } |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 649 | |
Craig Tiller | abf3638 | 2015-06-29 16:13:27 -0700 | [diff] [blame] | 650 | grpc_subchannel_call *create_call(connection *con) { |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 651 | grpc_channel_stack *chanstk = CHANNEL_STACK_FROM_CONNECTION(con); |
| 652 | grpc_subchannel_call *call = |
| 653 | gpr_malloc(sizeof(grpc_subchannel_call) + chanstk->call_stack_size); |
Craig Tiller | 04c5d4b | 2015-06-26 17:21:41 -0700 | [diff] [blame] | 654 | grpc_call_stack *callstk = SUBCHANNEL_CALL_TO_CALL_STACK(call); |
| 655 | call->connection = con; |
| 656 | gpr_ref_init(&call->refs, 1); |
Craig Tiller | abf3638 | 2015-06-29 16:13:27 -0700 | [diff] [blame] | 657 | grpc_call_stack_init(chanstk, NULL, NULL, callstk); |
Craig Tiller | 04c5d4b | 2015-06-26 17:21:41 -0700 | [diff] [blame] | 658 | return call; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 659 | } |