Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 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/surface/server.h" |
| 35 | |
Craig Tiller | f96dfc3 | 2015-09-10 14:43:18 -0700 | [diff] [blame] | 36 | #include <limits.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 37 | #include <stdlib.h> |
| 38 | #include <string.h> |
| 39 | |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 40 | #include <grpc/support/alloc.h> |
| 41 | #include <grpc/support/log.h> |
| 42 | #include <grpc/support/string_util.h> |
| 43 | #include <grpc/support/useful.h> |
| 44 | |
Hongyu Chen | e09dc78 | 2015-08-21 11:28:33 -0700 | [diff] [blame] | 45 | #include "src/core/census/grpc_filter.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 46 | #include "src/core/channel/channel_args.h" |
| 47 | #include "src/core/channel/connected_channel.h" |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 48 | #include "src/core/iomgr/iomgr.h" |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 49 | #include "src/core/support/stack_lockfree.h" |
Craig Tiller | 485d776 | 2015-01-23 12:54:05 -0800 | [diff] [blame] | 50 | #include "src/core/support/string.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 51 | #include "src/core/surface/call.h" |
| 52 | #include "src/core/surface/channel.h" |
| 53 | #include "src/core/surface/completion_queue.h" |
Craig Tiller | 60fd361 | 2015-03-05 16:24:22 -0800 | [diff] [blame] | 54 | #include "src/core/surface/init.h" |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 55 | #include "src/core/transport/metadata.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 56 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 57 | typedef struct listener { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 58 | void *arg; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 59 | void (*start)(grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg, |
| 60 | grpc_pollset **pollsets, size_t pollset_count); |
| 61 | void (*destroy)(grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg, |
| 62 | grpc_closure *closure); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 63 | struct listener *next; |
Craig Tiller | dfff1b8 | 2015-09-21 14:39:57 -0700 | [diff] [blame] | 64 | grpc_closure destroy_done; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 65 | } listener; |
| 66 | |
| 67 | typedef struct call_data call_data; |
| 68 | typedef struct channel_data channel_data; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 69 | typedef struct registered_method registered_method; |
| 70 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 71 | typedef struct { |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 72 | call_data *next; |
| 73 | call_data *prev; |
| 74 | } call_link; |
| 75 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 76 | typedef enum { BATCH_CALL, REGISTERED_CALL } requested_call_type; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 77 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 78 | typedef struct requested_call { |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 79 | requested_call_type type; |
| 80 | void *tag; |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 81 | grpc_server *server; |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 82 | grpc_completion_queue *cq_bound_to_call; |
| 83 | grpc_completion_queue *cq_for_notification; |
| 84 | grpc_call **call; |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 85 | grpc_cq_completion completion; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 86 | union { |
| 87 | struct { |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 88 | grpc_call_details *details; |
| 89 | grpc_metadata_array *initial_metadata; |
| 90 | } batch; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 91 | struct { |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 92 | registered_method *registered_method; |
| 93 | gpr_timespec *deadline; |
| 94 | grpc_metadata_array *initial_metadata; |
| 95 | grpc_byte_buffer **optional_payload; |
| 96 | } registered; |
| 97 | } data; |
| 98 | } requested_call; |
| 99 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 100 | typedef struct channel_registered_method { |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 101 | registered_method *server_registered_method; |
| 102 | grpc_mdstr *method; |
| 103 | grpc_mdstr *host; |
| 104 | } channel_registered_method; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 105 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 106 | struct channel_data { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 107 | grpc_server *server; |
Craig Tiller | e039f03 | 2015-06-25 12:54:23 -0700 | [diff] [blame] | 108 | grpc_connectivity_state connectivity_state; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 109 | grpc_channel *channel; |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 110 | grpc_mdstr *path_key; |
| 111 | grpc_mdstr *authority_key; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 112 | /* linked list of all channels on a server */ |
| 113 | channel_data *next; |
| 114 | channel_data *prev; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 115 | channel_registered_method *registered_methods; |
| 116 | gpr_uint32 registered_method_slots; |
| 117 | gpr_uint32 registered_method_max_probes; |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 118 | grpc_closure finish_destroy_channel_closure; |
| 119 | grpc_closure channel_connectivity_changed; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 120 | }; |
| 121 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 122 | typedef struct shutdown_tag { |
Craig Tiller | bce999f | 2015-05-27 09:55:51 -0700 | [diff] [blame] | 123 | void *tag; |
| 124 | grpc_completion_queue *cq; |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 125 | grpc_cq_completion completion; |
Craig Tiller | bce999f | 2015-05-27 09:55:51 -0700 | [diff] [blame] | 126 | } shutdown_tag; |
| 127 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 128 | typedef enum { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 129 | /* waiting for metadata */ |
| 130 | NOT_STARTED, |
| 131 | /* inital metadata read, not flow controlled in yet */ |
| 132 | PENDING, |
| 133 | /* flow controlled in, on completion queue */ |
| 134 | ACTIVATED, |
| 135 | /* cancelled before being queued */ |
| 136 | ZOMBIED |
| 137 | } call_state; |
| 138 | |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 139 | typedef struct request_matcher request_matcher; |
| 140 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 141 | struct call_data { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 142 | grpc_call *call; |
| 143 | |
Craig Tiller | 76d2c3b | 2015-07-07 11:46:01 -0700 | [diff] [blame] | 144 | /** protects state */ |
| 145 | gpr_mu mu_state; |
| 146 | /** the current state of a call - see call_state */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 147 | call_state state; |
Craig Tiller | 76d2c3b | 2015-07-07 11:46:01 -0700 | [diff] [blame] | 148 | |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 149 | grpc_mdstr *path; |
| 150 | grpc_mdstr *host; |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 151 | gpr_timespec deadline; |
| 152 | int got_initial_metadata; |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 153 | |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 154 | grpc_completion_queue *cq_new; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 155 | |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 156 | grpc_stream_op_buffer *recv_ops; |
| 157 | grpc_stream_state *recv_state; |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 158 | grpc_closure *on_done_recv; |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 159 | |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 160 | grpc_closure server_on_recv; |
| 161 | grpc_closure kill_zombie_closure; |
David Garcia Quintas | 284488b | 2015-05-28 16:27:39 -0700 | [diff] [blame] | 162 | |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 163 | call_data *pending_next; |
| 164 | }; |
| 165 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 166 | struct request_matcher { |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 167 | call_data *pending_head; |
| 168 | call_data *pending_tail; |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 169 | gpr_stack_lockfree *requests; |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 170 | }; |
| 171 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 172 | struct registered_method { |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 173 | char *method; |
| 174 | char *host; |
| 175 | request_matcher request_matcher; |
| 176 | registered_method *next; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 177 | }; |
| 178 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 179 | typedef struct { |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 180 | grpc_channel **channels; |
| 181 | size_t num_channels; |
| 182 | } channel_broadcaster; |
| 183 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 184 | struct grpc_server { |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 185 | size_t channel_filter_count; |
| 186 | const grpc_channel_filter **channel_filters; |
| 187 | grpc_channel_args *channel_args; |
| 188 | |
| 189 | grpc_completion_queue **cqs; |
| 190 | grpc_pollset **pollsets; |
| 191 | size_t cq_count; |
| 192 | |
| 193 | /* The two following mutexes control access to server-state |
| 194 | mu_global controls access to non-call-related state (e.g., channel state) |
| 195 | mu_call controls access to call-related state (e.g., the call lists) |
| 196 | |
| 197 | If they are ever required to be nested, you must lock mu_global |
| 198 | before mu_call. This is currently used in shutdown processing |
| 199 | (grpc_server_shutdown_and_notify and maybe_finish_shutdown) */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 200 | gpr_mu mu_global; /* mutex for server and channel state */ |
| 201 | gpr_mu mu_call; /* mutex for call-specific state */ |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 202 | |
| 203 | registered_method *registered_methods; |
| 204 | request_matcher unregistered_request_matcher; |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 205 | /** free list of available requested_calls indices */ |
| 206 | gpr_stack_lockfree *request_freelist; |
| 207 | /** requested call backing data */ |
| 208 | requested_call *requested_calls; |
Craig Tiller | 32ca48c | 2015-09-10 11:47:15 -0700 | [diff] [blame] | 209 | size_t max_requested_calls; |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 210 | |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 211 | gpr_atm shutdown_flag; |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 212 | gpr_uint8 shutdown_published; |
| 213 | size_t num_shutdown_tags; |
| 214 | shutdown_tag *shutdown_tags; |
| 215 | |
| 216 | channel_data root_channel_data; |
| 217 | |
| 218 | listener *listeners; |
| 219 | int listeners_destroyed; |
| 220 | gpr_refcount internal_refcount; |
| 221 | |
| 222 | /** when did we print the last shutdown progress message */ |
| 223 | gpr_timespec last_shutdown_message_time; |
| 224 | }; |
| 225 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 226 | #define SERVER_FROM_CALL_ELEM(elem) \ |
| 227 | (((channel_data *)(elem)->channel_data)->server) |
| 228 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 229 | static void begin_call(grpc_exec_ctx *exec_ctx, grpc_server *server, |
| 230 | call_data *calld, requested_call *rc); |
| 231 | static void fail_call(grpc_exec_ctx *exec_ctx, grpc_server *server, |
| 232 | requested_call *rc); |
Vijay Pai | 8931cdd | 2015-06-17 12:42:17 -0700 | [diff] [blame] | 233 | /* Before calling maybe_finish_shutdown, we must hold mu_global and not |
| 234 | hold mu_call */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 235 | static void maybe_finish_shutdown(grpc_exec_ctx *exec_ctx, grpc_server *server); |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 236 | |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 237 | /* |
| 238 | * channel broadcaster |
| 239 | */ |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 240 | |
| 241 | /* assumes server locked */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 242 | static void channel_broadcaster_init(grpc_server *s, channel_broadcaster *cb) { |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 243 | channel_data *c; |
| 244 | size_t count = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 245 | for (c = s->root_channel_data.next; c != &s->root_channel_data; c = c->next) { |
| 246 | count++; |
| 247 | } |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 248 | cb->num_channels = count; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 249 | cb->channels = gpr_malloc(sizeof(*cb->channels) * cb->num_channels); |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 250 | count = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 251 | for (c = s->root_channel_data.next; c != &s->root_channel_data; c = c->next) { |
| 252 | cb->channels[count++] = c->channel; |
| 253 | GRPC_CHANNEL_INTERNAL_REF(c->channel, "broadcast"); |
| 254 | } |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 257 | struct shutdown_cleanup_args { |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 258 | grpc_closure closure; |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 259 | gpr_slice slice; |
| 260 | }; |
| 261 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 262 | static void shutdown_cleanup(grpc_exec_ctx *exec_ctx, void *arg, |
| 263 | int iomgr_status_ignored) { |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 264 | struct shutdown_cleanup_args *a = arg; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 265 | gpr_slice_unref(a->slice); |
| 266 | gpr_free(a); |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 269 | static void send_shutdown(grpc_exec_ctx *exec_ctx, grpc_channel *channel, |
| 270 | int send_goaway, int send_disconnect) { |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 271 | grpc_transport_op op; |
| 272 | struct shutdown_cleanup_args *sc; |
| 273 | grpc_channel_element *elem; |
| 274 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 275 | memset(&op, 0, sizeof(op)); |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 276 | op.send_goaway = send_goaway; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 277 | sc = gpr_malloc(sizeof(*sc)); |
| 278 | sc->slice = gpr_slice_from_copied_string("Server shutdown"); |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 279 | op.goaway_message = &sc->slice; |
| 280 | op.goaway_status = GRPC_STATUS_OK; |
| 281 | op.disconnect = send_disconnect; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 282 | grpc_closure_init(&sc->closure, shutdown_cleanup, sc); |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 283 | op.on_consumed = &sc->closure; |
| 284 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 285 | elem = grpc_channel_stack_element(grpc_channel_get_channel_stack(channel), 0); |
| 286 | elem->filter->start_transport_op(exec_ctx, elem, &op); |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 289 | static void channel_broadcaster_shutdown(grpc_exec_ctx *exec_ctx, |
| 290 | channel_broadcaster *cb, |
| 291 | int send_goaway, |
| 292 | int force_disconnect) { |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 293 | size_t i; |
| 294 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 295 | for (i = 0; i < cb->num_channels; i++) { |
| 296 | send_shutdown(exec_ctx, cb->channels[i], send_goaway, force_disconnect); |
| 297 | GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, cb->channels[i], "broadcast"); |
| 298 | } |
| 299 | gpr_free(cb->channels); |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 302 | /* |
| 303 | * request_matcher |
| 304 | */ |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 305 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 306 | static void request_matcher_init(request_matcher *request_matcher, |
| 307 | size_t entries) { |
| 308 | memset(request_matcher, 0, sizeof(*request_matcher)); |
| 309 | request_matcher->requests = gpr_stack_lockfree_create(entries); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 310 | } |
| 311 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 312 | static void request_matcher_destroy(request_matcher *request_matcher) { |
| 313 | GPR_ASSERT(gpr_stack_lockfree_pop(request_matcher->requests) == -1); |
| 314 | gpr_stack_lockfree_destroy(request_matcher->requests); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 315 | } |
| 316 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 317 | static void kill_zombie(grpc_exec_ctx *exec_ctx, void *elem, int success) { |
| 318 | grpc_call_destroy(grpc_call_from_top_element(elem)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 319 | } |
| 320 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 321 | static void request_matcher_zombify_all_pending_calls( |
| 322 | grpc_exec_ctx *exec_ctx, request_matcher *request_matcher) { |
| 323 | while (request_matcher->pending_head) { |
| 324 | call_data *calld = request_matcher->pending_head; |
| 325 | request_matcher->pending_head = calld->pending_next; |
| 326 | gpr_mu_lock(&calld->mu_state); |
| 327 | calld->state = ZOMBIED; |
| 328 | gpr_mu_unlock(&calld->mu_state); |
| 329 | grpc_closure_init( |
| 330 | &calld->kill_zombie_closure, kill_zombie, |
| 331 | grpc_call_stack_element(grpc_call_get_call_stack(calld->call), 0)); |
| 332 | grpc_exec_ctx_enqueue(exec_ctx, &calld->kill_zombie_closure, 1); |
| 333 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 334 | } |
| 335 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 336 | static void request_matcher_kill_requests(grpc_exec_ctx *exec_ctx, |
| 337 | grpc_server *server, |
| 338 | request_matcher *rm) { |
Craig Tiller | 1191e21 | 2015-07-30 14:49:02 -0700 | [diff] [blame] | 339 | int request_id; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 340 | while ((request_id = gpr_stack_lockfree_pop(rm->requests)) != -1) { |
| 341 | fail_call(exec_ctx, server, &server->requested_calls[request_id]); |
| 342 | } |
Craig Tiller | 1191e21 | 2015-07-30 14:49:02 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 345 | /* |
| 346 | * server proper |
| 347 | */ |
| 348 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 349 | static void server_ref(grpc_server *server) { |
| 350 | gpr_ref(&server->internal_refcount); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 351 | } |
| 352 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 353 | static void server_delete(grpc_exec_ctx *exec_ctx, grpc_server *server) { |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame] | 354 | registered_method *rm; |
Craig Tiller | 8950461 | 2015-04-27 11:48:46 -0700 | [diff] [blame] | 355 | size_t i; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 356 | grpc_channel_args_destroy(server->channel_args); |
| 357 | gpr_mu_destroy(&server->mu_global); |
| 358 | gpr_mu_destroy(&server->mu_call); |
| 359 | gpr_free(server->channel_filters); |
| 360 | while ((rm = server->registered_methods) != NULL) { |
| 361 | server->registered_methods = rm->next; |
| 362 | request_matcher_destroy(&rm->request_matcher); |
| 363 | gpr_free(rm->method); |
| 364 | gpr_free(rm->host); |
| 365 | gpr_free(rm); |
| 366 | } |
| 367 | for (i = 0; i < server->cq_count; i++) { |
| 368 | GRPC_CQ_INTERNAL_UNREF(server->cqs[i], "server"); |
| 369 | } |
| 370 | request_matcher_destroy(&server->unregistered_request_matcher); |
| 371 | gpr_stack_lockfree_destroy(server->request_freelist); |
| 372 | gpr_free(server->cqs); |
| 373 | gpr_free(server->pollsets); |
| 374 | gpr_free(server->shutdown_tags); |
| 375 | gpr_free(server->requested_calls); |
| 376 | gpr_free(server); |
Craig Tiller | ee945e8 | 2015-05-26 16:15:34 -0700 | [diff] [blame] | 377 | } |
| 378 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 379 | static void server_unref(grpc_exec_ctx *exec_ctx, grpc_server *server) { |
| 380 | if (gpr_unref(&server->internal_refcount)) { |
| 381 | server_delete(exec_ctx, server); |
| 382 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 383 | } |
| 384 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 385 | static int is_channel_orphaned(channel_data *chand) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 386 | return chand->next == chand; |
| 387 | } |
| 388 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 389 | static void orphan_channel(channel_data *chand) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 390 | chand->next->prev = chand->prev; |
| 391 | chand->prev->next = chand->next; |
| 392 | chand->next = chand->prev = chand; |
| 393 | } |
| 394 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 395 | static void finish_destroy_channel(grpc_exec_ctx *exec_ctx, void *cd, |
| 396 | int success) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 397 | channel_data *chand = cd; |
| 398 | grpc_server *server = chand->server; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 399 | gpr_log(GPR_DEBUG, "finish_destroy_channel: %p", chand->channel); |
| 400 | GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, chand->channel, "server"); |
| 401 | server_unref(exec_ctx, server); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 402 | } |
| 403 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 404 | static void destroy_channel(grpc_exec_ctx *exec_ctx, channel_data *chand) { |
| 405 | if (is_channel_orphaned(chand)) return; |
| 406 | GPR_ASSERT(chand->server != NULL); |
| 407 | orphan_channel(chand); |
| 408 | server_ref(chand->server); |
| 409 | maybe_finish_shutdown(exec_ctx, chand->server); |
David Garcia Quintas | 284488b | 2015-05-28 16:27:39 -0700 | [diff] [blame] | 410 | chand->finish_destroy_channel_closure.cb = finish_destroy_channel; |
| 411 | chand->finish_destroy_channel_closure.cb_arg = chand; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 412 | grpc_exec_ctx_enqueue(exec_ctx, &chand->finish_destroy_channel_closure, 1); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 413 | } |
| 414 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 415 | static void finish_start_new_rpc(grpc_exec_ctx *exec_ctx, grpc_server *server, |
| 416 | grpc_call_element *elem, |
| 417 | request_matcher *request_matcher) { |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 418 | call_data *calld = elem->call_data; |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 419 | int request_id; |
| 420 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 421 | if (gpr_atm_acq_load(&server->shutdown_flag)) { |
| 422 | gpr_mu_lock(&calld->mu_state); |
| 423 | calld->state = ZOMBIED; |
| 424 | gpr_mu_unlock(&calld->mu_state); |
| 425 | grpc_closure_init(&calld->kill_zombie_closure, kill_zombie, elem); |
| 426 | grpc_exec_ctx_enqueue(exec_ctx, &calld->kill_zombie_closure, 1); |
| 427 | return; |
| 428 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 429 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 430 | request_id = gpr_stack_lockfree_pop(request_matcher->requests); |
| 431 | if (request_id == -1) { |
| 432 | gpr_mu_lock(&server->mu_call); |
| 433 | gpr_mu_lock(&calld->mu_state); |
| 434 | calld->state = PENDING; |
| 435 | gpr_mu_unlock(&calld->mu_state); |
| 436 | if (request_matcher->pending_head == NULL) { |
| 437 | request_matcher->pending_tail = request_matcher->pending_head = calld; |
| 438 | } else { |
| 439 | request_matcher->pending_tail->pending_next = calld; |
| 440 | request_matcher->pending_tail = calld; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 441 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 442 | calld->pending_next = NULL; |
| 443 | gpr_mu_unlock(&server->mu_call); |
| 444 | } else { |
| 445 | gpr_mu_lock(&calld->mu_state); |
| 446 | calld->state = ACTIVATED; |
| 447 | gpr_mu_unlock(&calld->mu_state); |
| 448 | begin_call(exec_ctx, server, calld, &server->requested_calls[request_id]); |
| 449 | } |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 450 | } |
| 451 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 452 | static void start_new_rpc(grpc_exec_ctx *exec_ctx, grpc_call_element *elem) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 453 | channel_data *chand = elem->channel_data; |
| 454 | call_data *calld = elem->call_data; |
| 455 | grpc_server *server = chand->server; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 456 | gpr_uint32 i; |
| 457 | gpr_uint32 hash; |
| 458 | channel_registered_method *rm; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 459 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 460 | if (chand->registered_methods && calld->path && calld->host) { |
| 461 | /* TODO(ctiller): unify these two searches */ |
| 462 | /* check for an exact match with host */ |
| 463 | hash = GRPC_MDSTR_KV_HASH(calld->host->hash, calld->path->hash); |
| 464 | for (i = 0; i <= chand->registered_method_max_probes; i++) { |
| 465 | rm = &chand->registered_methods[(hash + i) % |
| 466 | chand->registered_method_slots]; |
| 467 | if (!rm) break; |
| 468 | if (rm->host != calld->host) continue; |
| 469 | if (rm->method != calld->path) continue; |
| 470 | finish_start_new_rpc(exec_ctx, server, elem, |
| 471 | &rm->server_registered_method->request_matcher); |
| 472 | return; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 473 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 474 | /* check for a wildcard method definition (no host set) */ |
| 475 | hash = GRPC_MDSTR_KV_HASH(0, calld->path->hash); |
| 476 | for (i = 0; i <= chand->registered_method_max_probes; i++) { |
| 477 | rm = &chand->registered_methods[(hash + i) % |
| 478 | chand->registered_method_slots]; |
| 479 | if (!rm) break; |
| 480 | if (rm->host != NULL) continue; |
| 481 | if (rm->method != calld->path) continue; |
| 482 | finish_start_new_rpc(exec_ctx, server, elem, |
| 483 | &rm->server_registered_method->request_matcher); |
| 484 | return; |
| 485 | } |
| 486 | } |
| 487 | finish_start_new_rpc(exec_ctx, server, elem, |
| 488 | &server->unregistered_request_matcher); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 489 | } |
| 490 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 491 | static int num_listeners(grpc_server *server) { |
Craig Tiller | ee945e8 | 2015-05-26 16:15:34 -0700 | [diff] [blame] | 492 | listener *l; |
| 493 | int n = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 494 | for (l = server->listeners; l; l = l->next) { |
| 495 | n++; |
| 496 | } |
Craig Tiller | ee945e8 | 2015-05-26 16:15:34 -0700 | [diff] [blame] | 497 | return n; |
| 498 | } |
| 499 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 500 | static void done_shutdown_event(grpc_exec_ctx *exec_ctx, void *server, |
| 501 | grpc_cq_completion *completion) { |
| 502 | server_unref(exec_ctx, server); |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 503 | } |
| 504 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 505 | static int num_channels(grpc_server *server) { |
Craig Tiller | ab54f79 | 2015-07-08 08:34:20 -0700 | [diff] [blame] | 506 | channel_data *chand; |
| 507 | int n = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 508 | for (chand = server->root_channel_data.next; |
| 509 | chand != &server->root_channel_data; chand = chand->next) { |
| 510 | n++; |
| 511 | } |
Craig Tiller | ab54f79 | 2015-07-08 08:34:20 -0700 | [diff] [blame] | 512 | return n; |
| 513 | } |
| 514 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 515 | static void kill_pending_work_locked(grpc_exec_ctx *exec_ctx, |
| 516 | grpc_server *server) { |
Craig Tiller | 1191e21 | 2015-07-30 14:49:02 -0700 | [diff] [blame] | 517 | registered_method *rm; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 518 | request_matcher_kill_requests(exec_ctx, server, |
| 519 | &server->unregistered_request_matcher); |
| 520 | request_matcher_zombify_all_pending_calls( |
| 521 | exec_ctx, &server->unregistered_request_matcher); |
| 522 | for (rm = server->registered_methods; rm; rm = rm->next) { |
| 523 | request_matcher_kill_requests(exec_ctx, server, &rm->request_matcher); |
| 524 | request_matcher_zombify_all_pending_calls(exec_ctx, &rm->request_matcher); |
| 525 | } |
Craig Tiller | dc62772 | 2015-05-26 15:27:02 -0700 | [diff] [blame] | 526 | } |
| 527 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 528 | static void maybe_finish_shutdown(grpc_exec_ctx *exec_ctx, |
| 529 | grpc_server *server) { |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 530 | size_t i; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 531 | if (!gpr_atm_acq_load(&server->shutdown_flag) || server->shutdown_published) { |
| 532 | return; |
| 533 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 534 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 535 | kill_pending_work_locked(exec_ctx, server); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 536 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 537 | if (server->root_channel_data.next != &server->root_channel_data || |
| 538 | server->listeners_destroyed < num_listeners(server)) { |
| 539 | if (gpr_time_cmp(gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME), |
| 540 | server->last_shutdown_message_time), |
| 541 | gpr_time_from_seconds(1, GPR_TIMESPAN)) >= 0) { |
| 542 | server->last_shutdown_message_time = gpr_now(GPR_CLOCK_REALTIME); |
| 543 | gpr_log(GPR_DEBUG, |
| 544 | "Waiting for %d channels and %d/%d listeners to be destroyed" |
| 545 | " before shutting down server", |
| 546 | num_channels(server), |
| 547 | num_listeners(server) - server->listeners_destroyed, |
| 548 | num_listeners(server)); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 549 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 550 | return; |
| 551 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 552 | server->shutdown_published = 1; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 553 | for (i = 0; i < server->num_shutdown_tags; i++) { |
| 554 | server_ref(server); |
| 555 | grpc_cq_end_op(exec_ctx, server->shutdown_tags[i].cq, |
| 556 | server->shutdown_tags[i].tag, 1, done_shutdown_event, server, |
| 557 | &server->shutdown_tags[i].completion); |
| 558 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 559 | } |
| 560 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 561 | static grpc_mdelem *server_filter(void *user_data, grpc_mdelem *md) { |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 562 | grpc_call_element *elem = user_data; |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 563 | channel_data *chand = elem->channel_data; |
| 564 | call_data *calld = elem->call_data; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 565 | if (md->key == chand->path_key) { |
| 566 | calld->path = GRPC_MDSTR_REF(md->value); |
| 567 | return NULL; |
| 568 | } else if (md->key == chand->authority_key) { |
| 569 | calld->host = GRPC_MDSTR_REF(md->value); |
| 570 | return NULL; |
| 571 | } |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 572 | return md; |
| 573 | } |
| 574 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 575 | static void server_on_recv(grpc_exec_ctx *exec_ctx, void *ptr, int success) { |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 576 | grpc_call_element *elem = ptr; |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 577 | call_data *calld = elem->call_data; |
Craig Tiller | 94329d0 | 2015-07-23 09:52:11 -0700 | [diff] [blame] | 578 | gpr_timespec op_deadline; |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 579 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 580 | if (success && !calld->got_initial_metadata) { |
| 581 | size_t i; |
| 582 | size_t nops = calld->recv_ops->nops; |
| 583 | grpc_stream_op *ops = calld->recv_ops->ops; |
| 584 | for (i = 0; i < nops; i++) { |
| 585 | grpc_stream_op *op = &ops[i]; |
| 586 | if (op->type != GRPC_OP_METADATA) continue; |
| 587 | grpc_metadata_batch_filter(&op->data.metadata, server_filter, elem); |
| 588 | op_deadline = op->data.metadata.deadline; |
| 589 | if (0 != |
| 590 | gpr_time_cmp(op_deadline, gpr_inf_future(op_deadline.clock_type))) { |
| 591 | calld->deadline = op->data.metadata.deadline; |
| 592 | } |
| 593 | if (calld->host && calld->path) { |
| 594 | calld->got_initial_metadata = 1; |
| 595 | start_new_rpc(exec_ctx, elem); |
| 596 | } |
| 597 | break; |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 598 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 599 | } |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 600 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 601 | switch (*calld->recv_state) { |
Craig Tiller | 06aeea7 | 2015-04-23 10:54:45 -0700 | [diff] [blame] | 602 | case GRPC_STREAM_OPEN: |
| 603 | break; |
| 604 | case GRPC_STREAM_SEND_CLOSED: |
| 605 | break; |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 606 | case GRPC_STREAM_RECV_CLOSED: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 607 | gpr_mu_lock(&calld->mu_state); |
| 608 | if (calld->state == NOT_STARTED) { |
| 609 | calld->state = ZOMBIED; |
| 610 | gpr_mu_unlock(&calld->mu_state); |
| 611 | grpc_closure_init(&calld->kill_zombie_closure, kill_zombie, elem); |
| 612 | grpc_exec_ctx_enqueue(exec_ctx, &calld->kill_zombie_closure, 1); |
| 613 | } else { |
| 614 | gpr_mu_unlock(&calld->mu_state); |
| 615 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 616 | break; |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 617 | case GRPC_STREAM_CLOSED: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 618 | gpr_mu_lock(&calld->mu_state); |
| 619 | if (calld->state == NOT_STARTED) { |
| 620 | calld->state = ZOMBIED; |
| 621 | gpr_mu_unlock(&calld->mu_state); |
| 622 | grpc_closure_init(&calld->kill_zombie_closure, kill_zombie, elem); |
| 623 | grpc_exec_ctx_enqueue(exec_ctx, &calld->kill_zombie_closure, 1); |
| 624 | } else if (calld->state == PENDING) { |
| 625 | calld->state = ZOMBIED; |
| 626 | gpr_mu_unlock(&calld->mu_state); |
| 627 | /* zombied call will be destroyed when it's removed from the pending |
| 628 | queue... later */ |
| 629 | } else { |
| 630 | gpr_mu_unlock(&calld->mu_state); |
| 631 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 632 | break; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 633 | } |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 634 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 635 | calld->on_done_recv->cb(exec_ctx, calld->on_done_recv->cb_arg, success); |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 636 | } |
| 637 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 638 | static void server_mutate_op(grpc_call_element *elem, |
| 639 | grpc_transport_stream_op *op) { |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 640 | call_data *calld = elem->call_data; |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 641 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 642 | if (op->recv_ops) { |
| 643 | /* substitute our callback for the higher callback */ |
| 644 | calld->recv_ops = op->recv_ops; |
| 645 | calld->recv_state = op->recv_state; |
| 646 | calld->on_done_recv = op->on_done_recv; |
| 647 | op->on_done_recv = &calld->server_on_recv; |
| 648 | } |
Craig Tiller | 50d9db5 | 2015-04-23 10:52:14 -0700 | [diff] [blame] | 649 | } |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 650 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 651 | static void server_start_transport_stream_op(grpc_exec_ctx *exec_ctx, |
| 652 | grpc_call_element *elem, |
| 653 | grpc_transport_stream_op *op) { |
| 654 | GRPC_CALL_LOG_OP(GPR_INFO, elem, op); |
| 655 | server_mutate_op(elem, op); |
| 656 | grpc_call_next_op(exec_ctx, elem, op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 657 | } |
| 658 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 659 | static void accept_stream(void *cd, grpc_transport *transport, |
| 660 | const void *transport_server_data) { |
Craig Tiller | e039f03 | 2015-06-25 12:54:23 -0700 | [diff] [blame] | 661 | channel_data *chand = cd; |
| 662 | /* create a call */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 663 | grpc_call_create(chand->channel, NULL, 0, NULL, transport_server_data, NULL, |
| 664 | 0, gpr_inf_future(GPR_CLOCK_MONOTONIC)); |
Craig Tiller | e039f03 | 2015-06-25 12:54:23 -0700 | [diff] [blame] | 665 | } |
| 666 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 667 | static void channel_connectivity_changed(grpc_exec_ctx *exec_ctx, void *cd, |
| 668 | int iomgr_status_ignored) { |
Craig Tiller | e039f03 | 2015-06-25 12:54:23 -0700 | [diff] [blame] | 669 | channel_data *chand = cd; |
| 670 | grpc_server *server = chand->server; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 671 | if (chand->connectivity_state != GRPC_CHANNEL_FATAL_FAILURE) { |
| 672 | grpc_transport_op op; |
| 673 | memset(&op, 0, sizeof(op)); |
| 674 | op.on_connectivity_state_change = &chand->channel_connectivity_changed, |
| 675 | op.connectivity_state = &chand->connectivity_state; |
| 676 | grpc_channel_next_op(exec_ctx, |
| 677 | grpc_channel_stack_element( |
| 678 | grpc_channel_get_channel_stack(chand->channel), 0), |
| 679 | &op); |
| 680 | } else { |
| 681 | gpr_mu_lock(&server->mu_global); |
| 682 | destroy_channel(exec_ctx, chand); |
| 683 | gpr_mu_unlock(&server->mu_global); |
| 684 | GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, chand->channel, "connectivity"); |
| 685 | } |
Craig Tiller | e039f03 | 2015-06-25 12:54:23 -0700 | [diff] [blame] | 686 | } |
| 687 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 688 | static void init_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
| 689 | const void *server_transport_data, |
| 690 | grpc_transport_stream_op *initial_op) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 691 | call_data *calld = elem->call_data; |
| 692 | channel_data *chand = elem->channel_data; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 693 | memset(calld, 0, sizeof(call_data)); |
| 694 | calld->deadline = gpr_inf_future(GPR_CLOCK_REALTIME); |
| 695 | calld->call = grpc_call_from_top_element(elem); |
| 696 | gpr_mu_init(&calld->mu_state); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 697 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 698 | grpc_closure_init(&calld->server_on_recv, server_on_recv, elem); |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 699 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 700 | server_ref(chand->server); |
Craig Tiller | 50d9db5 | 2015-04-23 10:52:14 -0700 | [diff] [blame] | 701 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 702 | if (initial_op) server_mutate_op(elem, initial_op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 703 | } |
| 704 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 705 | static void destroy_call_elem(grpc_exec_ctx *exec_ctx, |
| 706 | grpc_call_element *elem) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 707 | channel_data *chand = elem->channel_data; |
Craig Tiller | db7db99 | 2015-01-29 11:19:01 -0800 | [diff] [blame] | 708 | call_data *calld = elem->call_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 709 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 710 | GPR_ASSERT(calld->state != PENDING); |
Craig Tiller | 092d8d1 | 2015-07-04 22:35:00 -0700 | [diff] [blame] | 711 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 712 | if (calld->host) { |
| 713 | GRPC_MDSTR_UNREF(calld->host); |
| 714 | } |
| 715 | if (calld->path) { |
| 716 | GRPC_MDSTR_UNREF(calld->path); |
| 717 | } |
Craig Tiller | 4df31a6 | 2015-01-30 09:44:31 -0800 | [diff] [blame] | 718 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 719 | gpr_mu_destroy(&calld->mu_state); |
Craig Tiller | 76d2c3b | 2015-07-07 11:46:01 -0700 | [diff] [blame] | 720 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 721 | server_unref(exec_ctx, chand->server); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 722 | } |
| 723 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 724 | static void init_channel_elem(grpc_exec_ctx *exec_ctx, |
| 725 | grpc_channel_element *elem, grpc_channel *master, |
| 726 | const grpc_channel_args *args, |
| 727 | grpc_mdctx *metadata_context, int is_first, |
| 728 | int is_last) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 729 | channel_data *chand = elem->channel_data; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 730 | GPR_ASSERT(is_first); |
| 731 | GPR_ASSERT(!is_last); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 732 | chand->server = NULL; |
| 733 | chand->channel = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 734 | chand->path_key = grpc_mdstr_from_string(metadata_context, ":path", 0); |
| 735 | chand->authority_key = |
| 736 | grpc_mdstr_from_string(metadata_context, ":authority", 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 737 | chand->next = chand->prev = chand; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 738 | chand->registered_methods = NULL; |
Craig Tiller | e039f03 | 2015-06-25 12:54:23 -0700 | [diff] [blame] | 739 | chand->connectivity_state = GRPC_CHANNEL_IDLE; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 740 | grpc_closure_init(&chand->channel_connectivity_changed, |
| 741 | channel_connectivity_changed, chand); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 742 | } |
| 743 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 744 | static void destroy_channel_elem(grpc_exec_ctx *exec_ctx, |
| 745 | grpc_channel_element *elem) { |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame] | 746 | size_t i; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 747 | channel_data *chand = elem->channel_data; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 748 | if (chand->registered_methods) { |
| 749 | for (i = 0; i < chand->registered_method_slots; i++) { |
| 750 | if (chand->registered_methods[i].method) { |
| 751 | GRPC_MDSTR_UNREF(chand->registered_methods[i].method); |
| 752 | } |
| 753 | if (chand->registered_methods[i].host) { |
| 754 | GRPC_MDSTR_UNREF(chand->registered_methods[i].host); |
| 755 | } |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame] | 756 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 757 | gpr_free(chand->registered_methods); |
| 758 | } |
| 759 | if (chand->server) { |
| 760 | gpr_mu_lock(&chand->server->mu_global); |
| 761 | chand->next->prev = chand->prev; |
| 762 | chand->prev->next = chand->next; |
| 763 | chand->next = chand->prev = chand; |
| 764 | maybe_finish_shutdown(exec_ctx, chand->server); |
| 765 | gpr_mu_unlock(&chand->server->mu_global); |
| 766 | GRPC_MDSTR_UNREF(chand->path_key); |
| 767 | GRPC_MDSTR_UNREF(chand->authority_key); |
| 768 | server_unref(exec_ctx, chand->server); |
| 769 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | static const grpc_channel_filter server_surface_filter = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 773 | server_start_transport_stream_op, |
| 774 | grpc_channel_next_op, |
| 775 | sizeof(call_data), |
| 776 | init_call_elem, |
| 777 | destroy_call_elem, |
| 778 | sizeof(channel_data), |
| 779 | init_channel_elem, |
| 780 | destroy_channel_elem, |
| 781 | grpc_call_next_get_peer, |
| 782 | "server", |
Craig Tiller | 9f28ac2 | 2015-01-27 17:01:29 -0800 | [diff] [blame] | 783 | }; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 784 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 785 | void grpc_server_register_completion_queue(grpc_server *server, |
| 786 | grpc_completion_queue *cq, |
| 787 | void *reserved) { |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 788 | size_t i, n; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 789 | GPR_ASSERT(!reserved); |
| 790 | for (i = 0; i < server->cq_count; i++) { |
| 791 | if (server->cqs[i] == cq) return; |
| 792 | } |
| 793 | GRPC_CQ_INTERNAL_REF(cq, "server"); |
| 794 | grpc_cq_mark_server_cq(cq); |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 795 | n = server->cq_count++; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 796 | server->cqs = gpr_realloc(server->cqs, |
| 797 | server->cq_count * sizeof(grpc_completion_queue *)); |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 798 | server->cqs[n] = cq; |
| 799 | } |
| 800 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 801 | grpc_server *grpc_server_create_from_filters( |
| 802 | const grpc_channel_filter **filters, size_t filter_count, |
| 803 | const grpc_channel_args *args) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 804 | size_t i; |
Alistair Veitch | 9d48ebf | 2015-06-01 10:01:03 -0700 | [diff] [blame] | 805 | /* TODO(census): restore this once we finalize census filter etc. |
| 806 | int census_enabled = grpc_channel_args_is_census_enabled(args); */ |
| 807 | int census_enabled = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 808 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 809 | grpc_server *server = gpr_malloc(sizeof(grpc_server)); |
Craig Tiller | 60fd361 | 2015-03-05 16:24:22 -0800 | [diff] [blame] | 810 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 811 | GPR_ASSERT(grpc_is_initialized() && "call grpc_init()"); |
Craig Tiller | 60fd361 | 2015-03-05 16:24:22 -0800 | [diff] [blame] | 812 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 813 | memset(server, 0, sizeof(grpc_server)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 814 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 815 | gpr_mu_init(&server->mu_global); |
| 816 | gpr_mu_init(&server->mu_call); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 817 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 818 | /* decremented by grpc_server_destroy */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 819 | gpr_ref_init(&server->internal_refcount, 1); |
| 820 | server->root_channel_data.next = server->root_channel_data.prev = |
| 821 | &server->root_channel_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 822 | |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 823 | /* TODO(ctiller): expose a channel_arg for this */ |
| 824 | server->max_requested_calls = 32768; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 825 | server->request_freelist = |
| 826 | gpr_stack_lockfree_create(server->max_requested_calls); |
| 827 | for (i = 0; i < (size_t)server->max_requested_calls; i++) { |
| 828 | gpr_stack_lockfree_push(server->request_freelist, (int)i); |
| 829 | } |
| 830 | request_matcher_init(&server->unregistered_request_matcher, |
| 831 | server->max_requested_calls); |
| 832 | server->requested_calls = gpr_malloc(server->max_requested_calls * |
| 833 | sizeof(*server->requested_calls)); |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 834 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 835 | /* Server filter stack is: |
| 836 | |
| 837 | server_surface_filter - for making surface API calls |
| 838 | grpc_server_census_filter (optional) - for stats collection and tracing |
| 839 | {passed in filter stack} |
| 840 | grpc_connected_channel_filter - for interfacing with transports */ |
Craig Tiller | 32ca48c | 2015-09-10 11:47:15 -0700 | [diff] [blame] | 841 | server->channel_filter_count = filter_count + 1u + (census_enabled ? 1u : 0u); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 842 | server->channel_filters = |
| 843 | gpr_malloc(server->channel_filter_count * sizeof(grpc_channel_filter *)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 844 | server->channel_filters[0] = &server_surface_filter; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 845 | if (census_enabled) { |
| 846 | server->channel_filters[1] = &grpc_server_census_filter; |
| 847 | } |
| 848 | for (i = 0; i < filter_count; i++) { |
| 849 | server->channel_filters[i + 1u + (census_enabled ? 1u : 0u)] = filters[i]; |
| 850 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 851 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 852 | server->channel_args = grpc_channel_args_copy(args); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 853 | |
| 854 | return server; |
| 855 | } |
| 856 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 857 | static int streq(const char *a, const char *b) { |
| 858 | if (a == NULL && b == NULL) return 1; |
| 859 | if (a == NULL) return 0; |
| 860 | if (b == NULL) return 0; |
| 861 | return 0 == strcmp(a, b); |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 862 | } |
| 863 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 864 | void *grpc_server_register_method(grpc_server *server, const char *method, |
| 865 | const char *host) { |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 866 | registered_method *m; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 867 | if (!method) { |
| 868 | gpr_log(GPR_ERROR, |
| 869 | "grpc_server_register_method method string cannot be NULL"); |
| 870 | return NULL; |
| 871 | } |
| 872 | for (m = server->registered_methods; m; m = m->next) { |
| 873 | if (streq(m->method, method) && streq(m->host, host)) { |
| 874 | gpr_log(GPR_ERROR, "duplicate registration for %s@%s", method, |
| 875 | host ? host : "*"); |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 876 | return NULL; |
| 877 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 878 | } |
| 879 | m = gpr_malloc(sizeof(registered_method)); |
| 880 | memset(m, 0, sizeof(*m)); |
| 881 | request_matcher_init(&m->request_matcher, server->max_requested_calls); |
| 882 | m->method = gpr_strdup(method); |
| 883 | m->host = gpr_strdup(host); |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 884 | m->next = server->registered_methods; |
| 885 | server->registered_methods = m; |
| 886 | return m; |
| 887 | } |
| 888 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 889 | void grpc_server_start(grpc_server *server) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 890 | listener *l; |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 891 | size_t i; |
Craig Tiller | f5768a6 | 2015-09-22 10:54:34 -0700 | [diff] [blame] | 892 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 893 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 894 | server->pollsets = gpr_malloc(sizeof(grpc_pollset *) * server->cq_count); |
| 895 | for (i = 0; i < server->cq_count; i++) { |
| 896 | server->pollsets[i] = grpc_cq_pollset(server->cqs[i]); |
| 897 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 898 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 899 | for (l = server->listeners; l; l = l->next) { |
| 900 | l->start(&exec_ctx, server, l->arg, server->pollsets, server->cq_count); |
| 901 | } |
Craig Tiller | dfff1b8 | 2015-09-21 14:39:57 -0700 | [diff] [blame] | 902 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 903 | grpc_exec_ctx_finish(&exec_ctx); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 904 | } |
| 905 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 906 | void grpc_server_setup_transport(grpc_exec_ctx *exec_ctx, grpc_server *s, |
| 907 | grpc_transport *transport, |
| 908 | grpc_channel_filter const **extra_filters, |
| 909 | size_t num_extra_filters, grpc_mdctx *mdctx, |
| 910 | const grpc_channel_args *args) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 911 | size_t num_filters = s->channel_filter_count + num_extra_filters + 1; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 912 | grpc_channel_filter const **filters = |
| 913 | gpr_malloc(sizeof(grpc_channel_filter *) * num_filters); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 914 | size_t i; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 915 | size_t num_registered_methods; |
| 916 | size_t alloc; |
| 917 | registered_method *rm; |
| 918 | channel_registered_method *crm; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 919 | grpc_channel *channel; |
| 920 | channel_data *chand; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 921 | grpc_mdstr *host; |
| 922 | grpc_mdstr *method; |
| 923 | gpr_uint32 hash; |
Craig Tiller | f96dfc3 | 2015-09-10 14:43:18 -0700 | [diff] [blame] | 924 | size_t slots; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 925 | gpr_uint32 probes; |
| 926 | gpr_uint32 max_probes = 0; |
Craig Tiller | e039f03 | 2015-06-25 12:54:23 -0700 | [diff] [blame] | 927 | grpc_transport_op op; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 928 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 929 | for (i = 0; i < s->channel_filter_count; i++) { |
| 930 | filters[i] = s->channel_filters[i]; |
| 931 | } |
| 932 | for (; i < s->channel_filter_count + num_extra_filters; i++) { |
| 933 | filters[i] = extra_filters[i - s->channel_filter_count]; |
| 934 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 935 | filters[i] = &grpc_connected_channel_filter; |
| 936 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 937 | for (i = 0; i < s->cq_count; i++) { |
| 938 | memset(&op, 0, sizeof(op)); |
| 939 | op.bind_pollset = grpc_cq_pollset(s->cqs[i]); |
| 940 | grpc_transport_perform_op(exec_ctx, transport, &op); |
| 941 | } |
ctiller | d79b486 | 2014-12-17 16:36:59 -0800 | [diff] [blame] | 942 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 943 | channel = grpc_channel_create_from_filters(exec_ctx, NULL, filters, |
| 944 | num_filters, args, mdctx, 0); |
| 945 | chand = (channel_data *)grpc_channel_stack_element( |
| 946 | grpc_channel_get_channel_stack(channel), 0) |
| 947 | ->channel_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 948 | chand->server = s; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 949 | server_ref(s); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 950 | chand->channel = channel; |
| 951 | |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 952 | num_registered_methods = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 953 | for (rm = s->registered_methods; rm; rm = rm->next) { |
| 954 | num_registered_methods++; |
| 955 | } |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 956 | /* build a lookup table phrased in terms of mdstr's in this channels context |
| 957 | to quickly find registered methods */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 958 | if (num_registered_methods > 0) { |
| 959 | slots = 2 * num_registered_methods; |
| 960 | alloc = sizeof(channel_registered_method) * slots; |
| 961 | chand->registered_methods = gpr_malloc(alloc); |
| 962 | memset(chand->registered_methods, 0, alloc); |
| 963 | for (rm = s->registered_methods; rm; rm = rm->next) { |
| 964 | host = rm->host ? grpc_mdstr_from_string(mdctx, rm->host, 0) : NULL; |
| 965 | method = grpc_mdstr_from_string(mdctx, rm->method, 0); |
| 966 | hash = GRPC_MDSTR_KV_HASH(host ? host->hash : 0, method->hash); |
| 967 | for (probes = 0; chand->registered_methods[(hash + probes) % slots] |
| 968 | .server_registered_method != NULL; |
| 969 | probes++) |
| 970 | ; |
| 971 | if (probes > max_probes) max_probes = probes; |
| 972 | crm = &chand->registered_methods[(hash + probes) % slots]; |
| 973 | crm->server_registered_method = rm; |
| 974 | crm->host = host; |
| 975 | crm->method = method; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 976 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 977 | GPR_ASSERT(slots <= GPR_UINT32_MAX); |
| 978 | chand->registered_method_slots = (gpr_uint32)slots; |
| 979 | chand->registered_method_max_probes = max_probes; |
| 980 | } |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 981 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 982 | grpc_connected_channel_bind_transport(grpc_channel_get_channel_stack(channel), |
| 983 | transport); |
Craig Tiller | 7bd5ab1 | 2015-02-17 22:29:04 -0800 | [diff] [blame] | 984 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 985 | gpr_mu_lock(&s->mu_global); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 986 | chand->next = &s->root_channel_data; |
| 987 | chand->prev = chand->next->prev; |
| 988 | chand->next->prev = chand->prev->next = chand; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 989 | gpr_mu_unlock(&s->mu_global); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 990 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 991 | gpr_free(filters); |
Craig Tiller | 4b80410 | 2015-06-26 16:16:12 -0700 | [diff] [blame] | 992 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 993 | GRPC_CHANNEL_INTERNAL_REF(channel, "connectivity"); |
| 994 | memset(&op, 0, sizeof(op)); |
Craig Tiller | 4b80410 | 2015-06-26 16:16:12 -0700 | [diff] [blame] | 995 | op.set_accept_stream = accept_stream; |
| 996 | op.set_accept_stream_user_data = chand; |
| 997 | op.on_connectivity_state_change = &chand->channel_connectivity_changed; |
| 998 | op.connectivity_state = &chand->connectivity_state; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 999 | op.disconnect = gpr_atm_acq_load(&s->shutdown_flag) != 0; |
| 1000 | grpc_transport_perform_op(exec_ctx, transport, &op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1001 | } |
| 1002 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1003 | void done_published_shutdown(grpc_exec_ctx *exec_ctx, void *done_arg, |
| 1004 | grpc_cq_completion *storage) { |
| 1005 | (void)done_arg; |
| 1006 | gpr_free(storage); |
murgatroid99 | 00a3dab | 2015-08-19 11:15:38 -0700 | [diff] [blame] | 1007 | } |
| 1008 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1009 | static void listener_destroy_done(grpc_exec_ctx *exec_ctx, void *s, |
| 1010 | int success) { |
Craig Tiller | dfff1b8 | 2015-09-21 14:39:57 -0700 | [diff] [blame] | 1011 | grpc_server *server = s; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1012 | gpr_mu_lock(&server->mu_global); |
Craig Tiller | dfff1b8 | 2015-09-21 14:39:57 -0700 | [diff] [blame] | 1013 | server->listeners_destroyed++; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1014 | maybe_finish_shutdown(exec_ctx, server); |
| 1015 | gpr_mu_unlock(&server->mu_global); |
Craig Tiller | dfff1b8 | 2015-09-21 14:39:57 -0700 | [diff] [blame] | 1016 | } |
| 1017 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1018 | void grpc_server_shutdown_and_notify(grpc_server *server, |
| 1019 | grpc_completion_queue *cq, void *tag) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1020 | listener *l; |
Craig Tiller | bce999f | 2015-05-27 09:55:51 -0700 | [diff] [blame] | 1021 | shutdown_tag *sdt; |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 1022 | channel_broadcaster broadcaster; |
Craig Tiller | f5768a6 | 2015-09-22 10:54:34 -0700 | [diff] [blame] | 1023 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1024 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1025 | GRPC_SERVER_LOG_SHUTDOWN(GPR_INFO, server, cq, tag); |
Craig Tiller | 7156fca | 2015-07-15 13:54:20 -0700 | [diff] [blame] | 1026 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1027 | /* lock, and gather up some stuff to do */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1028 | gpr_mu_lock(&server->mu_global); |
| 1029 | grpc_cq_begin_op(cq); |
| 1030 | if (server->shutdown_published) { |
| 1031 | grpc_cq_end_op(&exec_ctx, cq, tag, 1, done_published_shutdown, NULL, |
| 1032 | gpr_malloc(sizeof(grpc_cq_completion))); |
| 1033 | gpr_mu_unlock(&server->mu_global); |
| 1034 | goto done; |
| 1035 | } |
| 1036 | server->shutdown_tags = |
| 1037 | gpr_realloc(server->shutdown_tags, |
| 1038 | sizeof(shutdown_tag) * (server->num_shutdown_tags + 1)); |
Craig Tiller | bce999f | 2015-05-27 09:55:51 -0700 | [diff] [blame] | 1039 | sdt = &server->shutdown_tags[server->num_shutdown_tags++]; |
| 1040 | sdt->tag = tag; |
| 1041 | sdt->cq = cq; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1042 | if (gpr_atm_acq_load(&server->shutdown_flag)) { |
| 1043 | gpr_mu_unlock(&server->mu_global); |
| 1044 | goto done; |
| 1045 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1046 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1047 | server->last_shutdown_message_time = gpr_now(GPR_CLOCK_REALTIME); |
Craig Tiller | ab54f79 | 2015-07-08 08:34:20 -0700 | [diff] [blame] | 1048 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1049 | channel_broadcaster_init(server, &broadcaster); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 1050 | |
Craig Tiller | bd21757 | 2015-02-11 18:10:56 -0800 | [diff] [blame] | 1051 | /* collect all unregistered then registered calls */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1052 | gpr_mu_lock(&server->mu_call); |
| 1053 | kill_pending_work_locked(&exec_ctx, server); |
| 1054 | gpr_mu_unlock(&server->mu_call); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1055 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1056 | gpr_atm_rel_store(&server->shutdown_flag, 1); |
| 1057 | maybe_finish_shutdown(&exec_ctx, server); |
| 1058 | gpr_mu_unlock(&server->mu_global); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1059 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1060 | /* Shutdown listeners */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1061 | for (l = server->listeners; l; l = l->next) { |
| 1062 | grpc_closure_init(&l->destroy_done, listener_destroy_done, server); |
| 1063 | l->destroy(&exec_ctx, server, l->arg, &l->destroy_done); |
| 1064 | } |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 1065 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1066 | channel_broadcaster_shutdown(&exec_ctx, &broadcaster, 1, 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1067 | |
Craig Tiller | dfff1b8 | 2015-09-21 14:39:57 -0700 | [diff] [blame] | 1068 | done: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1069 | grpc_exec_ctx_finish(&exec_ctx); |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 1070 | } |
| 1071 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1072 | void grpc_server_cancel_all_calls(grpc_server *server) { |
Craig Tiller | 092d8d1 | 2015-07-04 22:35:00 -0700 | [diff] [blame] | 1073 | channel_broadcaster broadcaster; |
Craig Tiller | f5768a6 | 2015-09-22 10:54:34 -0700 | [diff] [blame] | 1074 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
Craig Tiller | afa2d63 | 2015-05-26 16:39:13 -0700 | [diff] [blame] | 1075 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1076 | gpr_mu_lock(&server->mu_global); |
| 1077 | channel_broadcaster_init(server, &broadcaster); |
| 1078 | gpr_mu_unlock(&server->mu_global); |
Craig Tiller | afa2d63 | 2015-05-26 16:39:13 -0700 | [diff] [blame] | 1079 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1080 | channel_broadcaster_shutdown(&exec_ctx, &broadcaster, 0, 1); |
| 1081 | grpc_exec_ctx_finish(&exec_ctx); |
Craig Tiller | afa2d63 | 2015-05-26 16:39:13 -0700 | [diff] [blame] | 1082 | } |
| 1083 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1084 | void grpc_server_destroy(grpc_server *server) { |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 1085 | listener *l; |
Craig Tiller | f5768a6 | 2015-09-22 10:54:34 -0700 | [diff] [blame] | 1086 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
Craig Tiller | 872af02 | 2015-04-24 15:57:52 -0700 | [diff] [blame] | 1087 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1088 | gpr_mu_lock(&server->mu_global); |
| 1089 | GPR_ASSERT(gpr_atm_acq_load(&server->shutdown_flag) || !server->listeners); |
| 1090 | GPR_ASSERT(server->listeners_destroyed == num_listeners(server)); |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 1091 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1092 | while (server->listeners) { |
| 1093 | l = server->listeners; |
| 1094 | server->listeners = l->next; |
| 1095 | gpr_free(l); |
| 1096 | } |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 1097 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1098 | gpr_mu_unlock(&server->mu_global); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1099 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1100 | server_unref(&exec_ctx, server); |
| 1101 | grpc_exec_ctx_finish(&exec_ctx); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1102 | } |
| 1103 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1104 | void grpc_server_add_listener( |
| 1105 | grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg, |
| 1106 | void (*start)(grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg, |
| 1107 | grpc_pollset **pollsets, size_t pollset_count), |
| 1108 | void (*destroy)(grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg, |
| 1109 | grpc_closure *on_done)) { |
| 1110 | listener *l = gpr_malloc(sizeof(listener)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1111 | l->arg = arg; |
| 1112 | l->start = start; |
| 1113 | l->destroy = destroy; |
| 1114 | l->next = server->listeners; |
| 1115 | server->listeners = l; |
| 1116 | } |
| 1117 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1118 | static grpc_call_error queue_call_request(grpc_exec_ctx *exec_ctx, |
| 1119 | grpc_server *server, |
| 1120 | requested_call *rc) { |
Yang Gao | eb8e7cd | 2015-02-11 11:43:40 -0800 | [diff] [blame] | 1121 | call_data *calld = NULL; |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 1122 | request_matcher *request_matcher = NULL; |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 1123 | int request_id; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1124 | if (gpr_atm_acq_load(&server->shutdown_flag)) { |
| 1125 | fail_call(exec_ctx, server, rc); |
| 1126 | return GRPC_CALL_OK; |
| 1127 | } |
| 1128 | request_id = gpr_stack_lockfree_pop(server->request_freelist); |
| 1129 | if (request_id == -1) { |
| 1130 | /* out of request ids: just fail this one */ |
| 1131 | fail_call(exec_ctx, server, rc); |
| 1132 | return GRPC_CALL_OK; |
| 1133 | } |
| 1134 | switch (rc->type) { |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 1135 | case BATCH_CALL: |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 1136 | request_matcher = &server->unregistered_request_matcher; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 1137 | break; |
| 1138 | case REGISTERED_CALL: |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 1139 | request_matcher = &rc->data.registered.registered_method->request_matcher; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 1140 | break; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1141 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 1142 | server->requested_calls[request_id] = *rc; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1143 | gpr_free(rc); |
| 1144 | if (gpr_stack_lockfree_push(request_matcher->requests, request_id)) { |
| 1145 | /* this was the first queued request: we need to lock and start |
| 1146 | matching calls */ |
| 1147 | gpr_mu_lock(&server->mu_call); |
| 1148 | while ((calld = request_matcher->pending_head) != NULL) { |
| 1149 | request_id = gpr_stack_lockfree_pop(request_matcher->requests); |
| 1150 | if (request_id == -1) break; |
| 1151 | request_matcher->pending_head = calld->pending_next; |
| 1152 | gpr_mu_unlock(&server->mu_call); |
| 1153 | gpr_mu_lock(&calld->mu_state); |
| 1154 | if (calld->state == ZOMBIED) { |
| 1155 | gpr_mu_unlock(&calld->mu_state); |
| 1156 | grpc_closure_init( |
| 1157 | &calld->kill_zombie_closure, kill_zombie, |
| 1158 | grpc_call_stack_element(grpc_call_get_call_stack(calld->call), 0)); |
| 1159 | grpc_exec_ctx_enqueue(exec_ctx, &calld->kill_zombie_closure, 1); |
| 1160 | } else { |
| 1161 | GPR_ASSERT(calld->state == PENDING); |
| 1162 | calld->state = ACTIVATED; |
| 1163 | gpr_mu_unlock(&calld->mu_state); |
| 1164 | begin_call(exec_ctx, server, calld, |
| 1165 | &server->requested_calls[request_id]); |
| 1166 | } |
| 1167 | gpr_mu_lock(&server->mu_call); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 1168 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1169 | gpr_mu_unlock(&server->mu_call); |
| 1170 | } |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 1171 | return GRPC_CALL_OK; |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 1172 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1173 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1174 | grpc_call_error grpc_server_request_call( |
| 1175 | grpc_server *server, grpc_call **call, grpc_call_details *details, |
| 1176 | grpc_metadata_array *initial_metadata, |
| 1177 | grpc_completion_queue *cq_bound_to_call, |
| 1178 | grpc_completion_queue *cq_for_notification, void *tag) { |
Craig Tiller | dfff1b8 | 2015-09-21 14:39:57 -0700 | [diff] [blame] | 1179 | grpc_call_error error; |
Craig Tiller | f5768a6 | 2015-09-22 10:54:34 -0700 | [diff] [blame] | 1180 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1181 | requested_call *rc = gpr_malloc(sizeof(*rc)); |
| 1182 | GRPC_SERVER_LOG_REQUEST_CALL(GPR_INFO, server, call, details, |
| 1183 | initial_metadata, cq_bound_to_call, |
| 1184 | cq_for_notification, tag); |
| 1185 | if (!grpc_cq_is_server_cq(cq_for_notification)) { |
| 1186 | gpr_free(rc); |
| 1187 | error = GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE; |
| 1188 | goto done; |
| 1189 | } |
| 1190 | grpc_cq_begin_op(cq_for_notification); |
Craig Tiller | 9928d39 | 2015-08-18 09:40:24 -0700 | [diff] [blame] | 1191 | details->reserved = NULL; |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 1192 | rc->type = BATCH_CALL; |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 1193 | rc->server = server; |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 1194 | rc->tag = tag; |
| 1195 | rc->cq_bound_to_call = cq_bound_to_call; |
| 1196 | rc->cq_for_notification = cq_for_notification; |
| 1197 | rc->call = call; |
| 1198 | rc->data.batch.details = details; |
| 1199 | rc->data.batch.initial_metadata = initial_metadata; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1200 | error = queue_call_request(&exec_ctx, server, rc); |
Craig Tiller | dfff1b8 | 2015-09-21 14:39:57 -0700 | [diff] [blame] | 1201 | done: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1202 | grpc_exec_ctx_finish(&exec_ctx); |
Craig Tiller | dfff1b8 | 2015-09-21 14:39:57 -0700 | [diff] [blame] | 1203 | return error; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1204 | } |
| 1205 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1206 | grpc_call_error grpc_server_request_registered_call( |
| 1207 | grpc_server *server, void *rm, grpc_call **call, gpr_timespec *deadline, |
| 1208 | grpc_metadata_array *initial_metadata, grpc_byte_buffer **optional_payload, |
| 1209 | grpc_completion_queue *cq_bound_to_call, |
| 1210 | grpc_completion_queue *cq_for_notification, void *tag) { |
Craig Tiller | dfff1b8 | 2015-09-21 14:39:57 -0700 | [diff] [blame] | 1211 | grpc_call_error error; |
Craig Tiller | f5768a6 | 2015-09-22 10:54:34 -0700 | [diff] [blame] | 1212 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1213 | requested_call *rc = gpr_malloc(sizeof(*rc)); |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 1214 | registered_method *registered_method = rm; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1215 | if (!grpc_cq_is_server_cq(cq_for_notification)) { |
| 1216 | gpr_free(rc); |
| 1217 | error = GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE; |
| 1218 | goto done; |
| 1219 | } |
| 1220 | grpc_cq_begin_op(cq_for_notification); |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 1221 | rc->type = REGISTERED_CALL; |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 1222 | rc->server = server; |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 1223 | rc->tag = tag; |
| 1224 | rc->cq_bound_to_call = cq_bound_to_call; |
| 1225 | rc->cq_for_notification = cq_for_notification; |
| 1226 | rc->call = call; |
| 1227 | rc->data.registered.registered_method = registered_method; |
| 1228 | rc->data.registered.deadline = deadline; |
| 1229 | rc->data.registered.initial_metadata = initial_metadata; |
| 1230 | rc->data.registered.optional_payload = optional_payload; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1231 | error = queue_call_request(&exec_ctx, server, rc); |
Craig Tiller | dfff1b8 | 2015-09-21 14:39:57 -0700 | [diff] [blame] | 1232 | done: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1233 | grpc_exec_ctx_finish(&exec_ctx); |
Craig Tiller | dfff1b8 | 2015-09-21 14:39:57 -0700 | [diff] [blame] | 1234 | return error; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1235 | } |
| 1236 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1237 | static void publish_registered_or_batch(grpc_exec_ctx *exec_ctx, |
| 1238 | grpc_call *call, int success, |
| 1239 | void *tag); |
| 1240 | static void publish_was_not_set(grpc_exec_ctx *exec_ctx, grpc_call *call, |
| 1241 | int success, void *tag) { |
| 1242 | abort(); |
Yang Gao | eb8e7cd | 2015-02-11 11:43:40 -0800 | [diff] [blame] | 1243 | } |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1244 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1245 | static void cpstr(char **dest, size_t *capacity, grpc_mdstr *value) { |
Craig Tiller | 166e250 | 2015-02-03 20:14:41 -0800 | [diff] [blame] | 1246 | gpr_slice slice = value->slice; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1247 | size_t len = GPR_SLICE_LENGTH(slice); |
Craig Tiller | 166e250 | 2015-02-03 20:14:41 -0800 | [diff] [blame] | 1248 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1249 | if (len + 1 > *capacity) { |
| 1250 | *capacity = GPR_MAX(len + 1, *capacity * 2); |
| 1251 | *dest = gpr_realloc(*dest, *capacity); |
| 1252 | } |
| 1253 | memcpy(*dest, grpc_mdstr_as_c_string(value), len + 1); |
Craig Tiller | 166e250 | 2015-02-03 20:14:41 -0800 | [diff] [blame] | 1254 | } |
| 1255 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1256 | static void begin_call(grpc_exec_ctx *exec_ctx, grpc_server *server, |
| 1257 | call_data *calld, requested_call *rc) { |
Yang Gao | eb8e7cd | 2015-02-11 11:43:40 -0800 | [diff] [blame] | 1258 | grpc_ioreq_completion_func publish = publish_was_not_set; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1259 | grpc_ioreq req[2]; |
| 1260 | grpc_ioreq *r = req; |
| 1261 | |
| 1262 | /* called once initial metadata has been read by the call, but BEFORE |
| 1263 | the ioreq to fetch it out of the call has been executed. |
| 1264 | This means metadata related fields can be relied on in calld, but to |
| 1265 | fill in the metadata array passed by the client, we need to perform |
| 1266 | an ioreq op, that should complete immediately. */ |
| 1267 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1268 | grpc_call_set_completion_queue(exec_ctx, calld->call, rc->cq_bound_to_call); |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 1269 | *rc->call = calld->call; |
| 1270 | calld->cq_new = rc->cq_for_notification; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1271 | switch (rc->type) { |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1272 | case BATCH_CALL: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1273 | GPR_ASSERT(calld->host != NULL); |
| 1274 | GPR_ASSERT(calld->path != NULL); |
| 1275 | cpstr(&rc->data.batch.details->host, |
| 1276 | &rc->data.batch.details->host_capacity, calld->host); |
| 1277 | cpstr(&rc->data.batch.details->method, |
| 1278 | &rc->data.batch.details->method_capacity, calld->path); |
Masood Malekghassemi | bf177c8 | 2015-04-27 12:14:38 -0700 | [diff] [blame] | 1279 | rc->data.batch.details->deadline = calld->deadline; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1280 | r->op = GRPC_IOREQ_RECV_INITIAL_METADATA; |
| 1281 | r->data.recv_metadata = rc->data.batch.initial_metadata; |
David Garcia Quintas | b8f5450 | 2015-06-15 16:19:10 -0700 | [diff] [blame] | 1282 | r->flags = 0; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1283 | r++; |
| 1284 | publish = publish_registered_or_batch; |
| 1285 | break; |
| 1286 | case REGISTERED_CALL: |
| 1287 | *rc->data.registered.deadline = calld->deadline; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1288 | r->op = GRPC_IOREQ_RECV_INITIAL_METADATA; |
| 1289 | r->data.recv_metadata = rc->data.registered.initial_metadata; |
David Garcia Quintas | b8f5450 | 2015-06-15 16:19:10 -0700 | [diff] [blame] | 1290 | r->flags = 0; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1291 | r++; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1292 | if (rc->data.registered.optional_payload) { |
| 1293 | r->op = GRPC_IOREQ_RECV_MESSAGE; |
| 1294 | r->data.recv_message = rc->data.registered.optional_payload; |
| 1295 | r->flags = 0; |
| 1296 | r++; |
| 1297 | } |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1298 | publish = publish_registered_or_batch; |
| 1299 | break; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1300 | } |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1301 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1302 | GRPC_CALL_INTERNAL_REF(calld->call, "server"); |
| 1303 | grpc_call_start_ioreq_and_call_back(exec_ctx, calld->call, req, |
| 1304 | (size_t)(r - req), publish, rc); |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 1305 | } |
| 1306 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1307 | static void done_request_event(grpc_exec_ctx *exec_ctx, void *req, |
| 1308 | grpc_cq_completion *c) { |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 1309 | requested_call *rc = req; |
| 1310 | grpc_server *server = rc->server; |
| 1311 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1312 | if (rc >= server->requested_calls && |
| 1313 | rc < server->requested_calls + server->max_requested_calls) { |
| 1314 | GPR_ASSERT(rc - server->requested_calls <= INT_MAX); |
| 1315 | gpr_stack_lockfree_push(server->request_freelist, |
| 1316 | (int)(rc - server->requested_calls)); |
| 1317 | } else { |
| 1318 | gpr_free(req); |
| 1319 | } |
Craig Tiller | e2b3bfa | 2015-07-30 10:40:22 -0700 | [diff] [blame] | 1320 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1321 | server_unref(exec_ctx, server); |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1322 | } |
| 1323 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1324 | static void fail_call(grpc_exec_ctx *exec_ctx, grpc_server *server, |
| 1325 | requested_call *rc) { |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 1326 | *rc->call = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1327 | switch (rc->type) { |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1328 | case BATCH_CALL: |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1329 | rc->data.batch.initial_metadata->count = 0; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1330 | break; |
| 1331 | case REGISTERED_CALL: |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1332 | rc->data.registered.initial_metadata->count = 0; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1333 | break; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1334 | } |
| 1335 | server_ref(server); |
| 1336 | grpc_cq_end_op(exec_ctx, rc->cq_for_notification, rc->tag, 0, |
| 1337 | done_request_event, rc, &rc->completion); |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1338 | } |
| 1339 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1340 | static void publish_registered_or_batch(grpc_exec_ctx *exec_ctx, |
| 1341 | grpc_call *call, int success, |
| 1342 | void *prc) { |
| 1343 | grpc_call_element *elem = |
| 1344 | grpc_call_stack_element(grpc_call_get_call_stack(call), 0); |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 1345 | requested_call *rc = prc; |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 1346 | call_data *calld = elem->call_data; |
Craig Tiller | e2b3bfa | 2015-07-30 10:40:22 -0700 | [diff] [blame] | 1347 | channel_data *chand = elem->channel_data; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1348 | server_ref(chand->server); |
| 1349 | grpc_cq_end_op(exec_ctx, calld->cq_new, rc->tag, success, done_request_event, |
| 1350 | rc, &rc->completion); |
| 1351 | GRPC_CALL_INTERNAL_UNREF(exec_ctx, call, "server"); |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1352 | } |
| 1353 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1354 | const grpc_channel_args *grpc_server_get_channel_args(grpc_server *server) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1355 | return server->channel_args; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 1356 | } |
Craig Tiller | ba3c3cd | 2015-05-26 06:28:10 -0700 | [diff] [blame] | 1357 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1358 | int grpc_server_has_open_connections(grpc_server *server) { |
Craig Tiller | ba3c3cd | 2015-05-26 06:28:10 -0700 | [diff] [blame] | 1359 | int r; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1360 | gpr_mu_lock(&server->mu_global); |
Craig Tiller | ba3c3cd | 2015-05-26 06:28:10 -0700 | [diff] [blame] | 1361 | r = server->root_channel_data.next != &server->root_channel_data; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 1362 | gpr_mu_unlock(&server->mu_global); |
Craig Tiller | ba3c3cd | 2015-05-26 06:28:10 -0700 | [diff] [blame] | 1363 | return r; |
| 1364 | } |