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