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