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