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