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