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