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