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