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