blob: a622b5db9d18eb1bdca0d23588fbbf8f73f8b388 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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 Tiller9533d042016-03-25 17:11:06 -070034#include "src/core/lib/surface/server.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080035
Craig Tillerf96dfc32015-09-10 14:43:18 -070036#include <limits.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080037#include <stdlib.h>
38#include <string.h>
39
Craig Tiller6a006ce2015-07-13 16:25:40 -070040#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 Tiller9533d042016-03-25 17:11:06 -070045#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 Nobleb7ebd3b2014-11-26 16:33:03 -080057
Craig Tillera82950e2015-09-22 12:33:20 -070058typedef struct listener {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080059 void *arg;
Craig Tillera82950e2015-09-22 12:33:20 -070060 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 Nobleb7ebd3b2014-11-26 16:33:03 -080064 struct listener *next;
Craig Tillerdfff1b82015-09-21 14:39:57 -070065 grpc_closure destroy_done;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080066} listener;
67
68typedef struct call_data call_data;
69typedef struct channel_data channel_data;
Craig Tiller24be0f72015-02-10 14:04:22 -080070typedef struct registered_method registered_method;
71
Craig Tillera82950e2015-09-22 12:33:20 -070072typedef struct {
Craig Tiller24be0f72015-02-10 14:04:22 -080073 call_data *next;
74 call_data *prev;
75} call_link;
76
Craig Tillera82950e2015-09-22 12:33:20 -070077typedef enum { BATCH_CALL, REGISTERED_CALL } requested_call_type;
Craig Tiller24be0f72015-02-10 14:04:22 -080078
Craig Tillera82950e2015-09-22 12:33:20 -070079typedef struct requested_call {
Craig Tiller24be0f72015-02-10 14:04:22 -080080 requested_call_type type;
81 void *tag;
Craig Tiller6a006ce2015-07-13 16:25:40 -070082 grpc_server *server;
Craig Tillerf9e6adf2015-05-06 11:45:59 -070083 grpc_completion_queue *cq_bound_to_call;
84 grpc_completion_queue *cq_for_notification;
85 grpc_call **call;
Craig Tiller97fc6a32015-07-08 15:31:35 -070086 grpc_cq_completion completion;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -080087 grpc_metadata_array *initial_metadata;
Craig Tillera82950e2015-09-22 12:33:20 -070088 union {
89 struct {
Craig Tiller24be0f72015-02-10 14:04:22 -080090 grpc_call_details *details;
Craig Tiller24be0f72015-02-10 14:04:22 -080091 } batch;
Craig Tillera82950e2015-09-22 12:33:20 -070092 struct {
Craig Tiller24be0f72015-02-10 14:04:22 -080093 registered_method *registered_method;
94 gpr_timespec *deadline;
Craig Tiller24be0f72015-02-10 14:04:22 -080095 grpc_byte_buffer **optional_payload;
96 } registered;
97 } data;
98} requested_call;
99
Craig Tillera82950e2015-09-22 12:33:20 -0700100typedef struct channel_registered_method {
Craig Tiller24be0f72015-02-10 14:04:22 -0800101 registered_method *server_registered_method;
Craig Tillerb2906862016-03-10 06:50:07 -0800102 uint32_t flags;
Craig Tiller24be0f72015-02-10 14:04:22 -0800103 grpc_mdstr *method;
104 grpc_mdstr *host;
105} channel_registered_method;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800106
Craig Tillera82950e2015-09-22 12:33:20 -0700107struct channel_data {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800108 grpc_server *server;
Craig Tillere039f032015-06-25 12:54:23 -0700109 grpc_connectivity_state connectivity_state;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800110 grpc_channel *channel;
111 /* linked list of all channels on a server */
112 channel_data *next;
113 channel_data *prev;
Craig Tiller04cc8be2015-02-10 16:11:22 -0800114 channel_registered_method *registered_methods;
Craig Tiller7536af02015-12-22 13:49:30 -0800115 uint32_t registered_method_slots;
116 uint32_t registered_method_max_probes;
Craig Tiller33825112015-09-18 07:44:19 -0700117 grpc_closure finish_destroy_channel_closure;
118 grpc_closure channel_connectivity_changed;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800119};
120
Craig Tillera82950e2015-09-22 12:33:20 -0700121typedef struct shutdown_tag {
Craig Tillerbce999f2015-05-27 09:55:51 -0700122 void *tag;
123 grpc_completion_queue *cq;
Craig Tiller97fc6a32015-07-08 15:31:35 -0700124 grpc_cq_completion completion;
Craig Tillerbce999f2015-05-27 09:55:51 -0700125} shutdown_tag;
126
Craig Tillera82950e2015-09-22 12:33:20 -0700127typedef enum {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800128 /* 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 Tiller729b35a2015-07-13 12:36:47 -0700138typedef struct request_matcher request_matcher;
139
Craig Tillera82950e2015-09-22 12:33:20 -0700140struct call_data {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800141 grpc_call *call;
142
Craig Tiller76d2c3b2015-07-07 11:46:01 -0700143 /** protects state */
144 gpr_mu mu_state;
145 /** the current state of a call - see call_state */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800146 call_state state;
Craig Tiller76d2c3b2015-07-07 11:46:01 -0700147
Craig Tillercce17ac2015-01-20 09:29:28 -0800148 grpc_mdstr *path;
149 grpc_mdstr *host;
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700150 gpr_timespec deadline;
Craig Tillercce17ac2015-01-20 09:29:28 -0800151
Craig Tiller20bc56d2015-02-12 09:02:56 -0800152 grpc_completion_queue *cq_new;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800153
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800154 grpc_metadata_batch *recv_initial_metadata;
Craig Tiller4d40ba32016-03-09 17:48:40 -0800155 bool recv_idempotent_request;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800156 grpc_metadata_array initial_metadata;
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700157
Craig Tiller88512692016-04-04 09:32:52 -0700158 request_matcher *request_matcher;
159 grpc_byte_buffer *payload;
160
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800161 grpc_closure got_initial_metadata;
162 grpc_closure server_on_recv_initial_metadata;
Craig Tiller33825112015-09-18 07:44:19 -0700163 grpc_closure kill_zombie_closure;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800164 grpc_closure *on_done_recv_initial_metadata;
David Garcia Quintas284488b2015-05-28 16:27:39 -0700165
Craig Tiller88512692016-04-04 09:32:52 -0700166 grpc_closure publish;
167
Craig Tiller729b35a2015-07-13 12:36:47 -0700168 call_data *pending_next;
169};
170
Craig Tillera82950e2015-09-22 12:33:20 -0700171struct request_matcher {
Craig Tiller88512692016-04-04 09:32:52 -0700172 grpc_server *server;
Craig Tiller729b35a2015-07-13 12:36:47 -0700173 call_data *pending_head;
174 call_data *pending_tail;
Craig Tiller6a006ce2015-07-13 16:25:40 -0700175 gpr_stack_lockfree *requests;
Craig Tiller729b35a2015-07-13 12:36:47 -0700176};
177
Craig Tillera82950e2015-09-22 12:33:20 -0700178struct registered_method {
Craig Tiller729b35a2015-07-13 12:36:47 -0700179 char *method;
180 char *host;
Craig Tiller06cb1a92016-04-04 08:10:47 -0700181 grpc_server_register_method_payload_handling payload_handling;
Craig Tillerb2906862016-03-10 06:50:07 -0800182 uint32_t flags;
Craig Tiller729b35a2015-07-13 12:36:47 -0700183 request_matcher request_matcher;
184 registered_method *next;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800185};
186
Craig Tillera82950e2015-09-22 12:33:20 -0700187typedef struct {
Craig Tillerff3ae682015-06-29 17:44:04 -0700188 grpc_channel **channels;
189 size_t num_channels;
190} channel_broadcaster;
191
Craig Tillera82950e2015-09-22 12:33:20 -0700192struct grpc_server {
Craig Tiller729b35a2015-07-13 12:36:47 -0700193 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 Tillera82950e2015-09-22 12:33:20 -0700206 gpr_mu mu_global; /* mutex for server and channel state */
207 gpr_mu mu_call; /* mutex for call-specific state */
Craig Tiller729b35a2015-07-13 12:36:47 -0700208
209 registered_method *registered_methods;
210 request_matcher unregistered_request_matcher;
Craig Tiller6a006ce2015-07-13 16:25:40 -0700211 /** free list of available requested_calls indices */
212 gpr_stack_lockfree *request_freelist;
213 /** requested call backing data */
214 requested_call *requested_calls;
Craig Tiller32ca48c2015-09-10 11:47:15 -0700215 size_t max_requested_calls;
Craig Tiller729b35a2015-07-13 12:36:47 -0700216
Craig Tiller6a006ce2015-07-13 16:25:40 -0700217 gpr_atm shutdown_flag;
Craig Tiller7536af02015-12-22 13:49:30 -0800218 uint8_t shutdown_published;
Craig Tiller729b35a2015-07-13 12:36:47 -0700219 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 Nobleb7ebd3b2014-11-26 16:33:03 -0800232#define SERVER_FROM_CALL_ELEM(elem) \
233 (((channel_data *)(elem)->channel_data)->server)
234
Craig Tillerf51457b2016-05-03 17:06:32 -0700235static void publish_new_rpc(grpc_exec_ctx *exec_ctx, void *calld,
236 grpc_error *error);
Craig Tillera82950e2015-09-22 12:33:20 -0700237static void fail_call(grpc_exec_ctx *exec_ctx, grpc_server *server,
Craig Tillercae4b1b2016-05-10 09:11:09 -0700238 requested_call *rc, grpc_error *error);
Vijay Pai8931cdd2015-06-17 12:42:17 -0700239/* Before calling maybe_finish_shutdown, we must hold mu_global and not
240 hold mu_call */
Craig Tillera82950e2015-09-22 12:33:20 -0700241static void maybe_finish_shutdown(grpc_exec_ctx *exec_ctx, grpc_server *server);
Craig Tiller24be0f72015-02-10 14:04:22 -0800242
Craig Tiller729b35a2015-07-13 12:36:47 -0700243/*
244 * channel broadcaster
245 */
Craig Tillerff3ae682015-06-29 17:44:04 -0700246
247/* assumes server locked */
Craig Tillera82950e2015-09-22 12:33:20 -0700248static void channel_broadcaster_init(grpc_server *s, channel_broadcaster *cb) {
Craig Tillerff3ae682015-06-29 17:44:04 -0700249 channel_data *c;
250 size_t count = 0;
Craig Tillera82950e2015-09-22 12:33:20 -0700251 for (c = s->root_channel_data.next; c != &s->root_channel_data; c = c->next) {
252 count++;
253 }
Craig Tillerff3ae682015-06-29 17:44:04 -0700254 cb->num_channels = count;
Craig Tillera82950e2015-09-22 12:33:20 -0700255 cb->channels = gpr_malloc(sizeof(*cb->channels) * cb->num_channels);
Craig Tillerff3ae682015-06-29 17:44:04 -0700256 count = 0;
Craig Tillera82950e2015-09-22 12:33:20 -0700257 for (c = s->root_channel_data.next; c != &s->root_channel_data; c = c->next) {
258 cb->channels[count++] = c->channel;
259 GRPC_CHANNEL_INTERNAL_REF(c->channel, "broadcast");
260 }
Craig Tillerff3ae682015-06-29 17:44:04 -0700261}
262
Craig Tillera82950e2015-09-22 12:33:20 -0700263struct shutdown_cleanup_args {
Craig Tiller33825112015-09-18 07:44:19 -0700264 grpc_closure closure;
Craig Tillerff3ae682015-06-29 17:44:04 -0700265 gpr_slice slice;
266};
267
Craig Tillera82950e2015-09-22 12:33:20 -0700268static void shutdown_cleanup(grpc_exec_ctx *exec_ctx, void *arg,
Craig Tillerf51457b2016-05-03 17:06:32 -0700269 grpc_error *error) {
Craig Tillerff3ae682015-06-29 17:44:04 -0700270 struct shutdown_cleanup_args *a = arg;
Craig Tillera82950e2015-09-22 12:33:20 -0700271 gpr_slice_unref(a->slice);
272 gpr_free(a);
Craig Tillerff3ae682015-06-29 17:44:04 -0700273}
274
Craig Tillera82950e2015-09-22 12:33:20 -0700275static void send_shutdown(grpc_exec_ctx *exec_ctx, grpc_channel *channel,
Craig Tiller804ff712016-05-05 16:25:40 -0700276 int send_goaway, grpc_error *send_disconnect) {
Craig Tillerff3ae682015-06-29 17:44:04 -0700277 grpc_transport_op op;
278 struct shutdown_cleanup_args *sc;
279 grpc_channel_element *elem;
280
Craig Tillera82950e2015-09-22 12:33:20 -0700281 memset(&op, 0, sizeof(op));
Craig Tillerff3ae682015-06-29 17:44:04 -0700282 op.send_goaway = send_goaway;
Craig Tillera82950e2015-09-22 12:33:20 -0700283 sc = gpr_malloc(sizeof(*sc));
284 sc->slice = gpr_slice_from_copied_string("Server shutdown");
Craig Tillerff3ae682015-06-29 17:44:04 -0700285 op.goaway_message = &sc->slice;
286 op.goaway_status = GRPC_STATUS_OK;
Craig Tiller804ff712016-05-05 16:25:40 -0700287 op.disconnect_with_error = send_disconnect;
Craig Tillera82950e2015-09-22 12:33:20 -0700288 grpc_closure_init(&sc->closure, shutdown_cleanup, sc);
Craig Tillerff3ae682015-06-29 17:44:04 -0700289 op.on_consumed = &sc->closure;
290
Craig Tillera82950e2015-09-22 12:33:20 -0700291 elem = grpc_channel_stack_element(grpc_channel_get_channel_stack(channel), 0);
292 elem->filter->start_transport_op(exec_ctx, elem, &op);
Craig Tillerff3ae682015-06-29 17:44:04 -0700293}
294
Craig Tillera82950e2015-09-22 12:33:20 -0700295static void channel_broadcaster_shutdown(grpc_exec_ctx *exec_ctx,
296 channel_broadcaster *cb,
297 int send_goaway,
Craig Tiller804ff712016-05-05 16:25:40 -0700298 grpc_error *force_disconnect) {
Craig Tillerff3ae682015-06-29 17:44:04 -0700299 size_t i;
300
Craig Tillera82950e2015-09-22 12:33:20 -0700301 for (i = 0; i < cb->num_channels; i++) {
302 send_shutdown(exec_ctx, cb->channels[i], send_goaway, force_disconnect);
303 GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, cb->channels[i], "broadcast");
304 }
305 gpr_free(cb->channels);
Craig Tillerff3ae682015-06-29 17:44:04 -0700306}
307
Craig Tiller729b35a2015-07-13 12:36:47 -0700308/*
309 * request_matcher
310 */
Craig Tillerff3ae682015-06-29 17:44:04 -0700311
Craig Tiller88512692016-04-04 09:32:52 -0700312static void request_matcher_init(request_matcher *rm, size_t entries,
313 grpc_server *server) {
Craig Tillerb9d35962015-09-11 13:31:16 -0700314 memset(rm, 0, sizeof(*rm));
Craig Tiller88512692016-04-04 09:32:52 -0700315 rm->server = server;
Craig Tillerb9d35962015-09-11 13:31:16 -0700316 rm->requests = gpr_stack_lockfree_create(entries);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800317}
318
Craig Tillerb9d35962015-09-11 13:31:16 -0700319static void request_matcher_destroy(request_matcher *rm) {
320 GPR_ASSERT(gpr_stack_lockfree_pop(rm->requests) == -1);
321 gpr_stack_lockfree_destroy(rm->requests);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800322}
323
Craig Tillerf51457b2016-05-03 17:06:32 -0700324static void kill_zombie(grpc_exec_ctx *exec_ctx, void *elem,
325 grpc_error *error) {
Craig Tillera82950e2015-09-22 12:33:20 -0700326 grpc_call_destroy(grpc_call_from_top_element(elem));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800327}
328
Craig Tiller8dc09712015-09-24 13:58:16 -0700329static void request_matcher_zombify_all_pending_calls(grpc_exec_ctx *exec_ctx,
330 request_matcher *rm) {
Craig Tillerb9d35962015-09-11 13:31:16 -0700331 while (rm->pending_head) {
332 call_data *calld = rm->pending_head;
333 rm->pending_head = calld->pending_next;
Craig Tillera82950e2015-09-22 12:33:20 -0700334 gpr_mu_lock(&calld->mu_state);
335 calld->state = ZOMBIED;
336 gpr_mu_unlock(&calld->mu_state);
337 grpc_closure_init(
338 &calld->kill_zombie_closure, kill_zombie,
339 grpc_call_stack_element(grpc_call_get_call_stack(calld->call), 0));
Craig Tillerf51457b2016-05-03 17:06:32 -0700340 grpc_exec_ctx_push(exec_ctx, &calld->kill_zombie_closure, GRPC_ERROR_NONE,
341 NULL);
Craig Tillera82950e2015-09-22 12:33:20 -0700342 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800343}
344
Craig Tillera82950e2015-09-22 12:33:20 -0700345static void request_matcher_kill_requests(grpc_exec_ctx *exec_ctx,
346 grpc_server *server,
Craig Tillercae4b1b2016-05-10 09:11:09 -0700347 request_matcher *rm,
348 grpc_error *error) {
Craig Tiller1191e212015-07-30 14:49:02 -0700349 int request_id;
Craig Tillera82950e2015-09-22 12:33:20 -0700350 while ((request_id = gpr_stack_lockfree_pop(rm->requests)) != -1) {
Craig Tillercae4b1b2016-05-10 09:11:09 -0700351 fail_call(exec_ctx, server, &server->requested_calls[request_id],
352 GRPC_ERROR_REF(error));
Craig Tillera82950e2015-09-22 12:33:20 -0700353 }
Craig Tillercae4b1b2016-05-10 09:11:09 -0700354 GRPC_ERROR_UNREF(error);
Craig Tiller1191e212015-07-30 14:49:02 -0700355}
356
Craig Tiller729b35a2015-07-13 12:36:47 -0700357/*
358 * server proper
359 */
360
Craig Tillera82950e2015-09-22 12:33:20 -0700361static void server_ref(grpc_server *server) {
362 gpr_ref(&server->internal_refcount);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800363}
364
Craig Tillera82950e2015-09-22 12:33:20 -0700365static void server_delete(grpc_exec_ctx *exec_ctx, grpc_server *server) {
Craig Tillerec3257c2015-02-12 15:59:43 -0800366 registered_method *rm;
Craig Tiller89504612015-04-27 11:48:46 -0700367 size_t i;
Craig Tillera82950e2015-09-22 12:33:20 -0700368 grpc_channel_args_destroy(server->channel_args);
369 gpr_mu_destroy(&server->mu_global);
370 gpr_mu_destroy(&server->mu_call);
Craig Tillera82950e2015-09-22 12:33:20 -0700371 while ((rm = server->registered_methods) != NULL) {
372 server->registered_methods = rm->next;
373 request_matcher_destroy(&rm->request_matcher);
374 gpr_free(rm->method);
375 gpr_free(rm->host);
376 gpr_free(rm);
377 }
378 for (i = 0; i < server->cq_count; i++) {
379 GRPC_CQ_INTERNAL_UNREF(server->cqs[i], "server");
380 }
381 request_matcher_destroy(&server->unregistered_request_matcher);
382 gpr_stack_lockfree_destroy(server->request_freelist);
383 gpr_free(server->cqs);
384 gpr_free(server->pollsets);
385 gpr_free(server->shutdown_tags);
386 gpr_free(server->requested_calls);
387 gpr_free(server);
Craig Tilleree945e82015-05-26 16:15:34 -0700388}
389
Craig Tillera82950e2015-09-22 12:33:20 -0700390static void server_unref(grpc_exec_ctx *exec_ctx, grpc_server *server) {
391 if (gpr_unref(&server->internal_refcount)) {
392 server_delete(exec_ctx, server);
393 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800394}
395
Craig Tillera82950e2015-09-22 12:33:20 -0700396static int is_channel_orphaned(channel_data *chand) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800397 return chand->next == chand;
398}
399
Craig Tillera82950e2015-09-22 12:33:20 -0700400static void orphan_channel(channel_data *chand) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800401 chand->next->prev = chand->prev;
402 chand->prev->next = chand->next;
403 chand->next = chand->prev = chand;
404}
405
Craig Tillera82950e2015-09-22 12:33:20 -0700406static void finish_destroy_channel(grpc_exec_ctx *exec_ctx, void *cd,
Craig Tillerf51457b2016-05-03 17:06:32 -0700407 grpc_error *error) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800408 channel_data *chand = cd;
409 grpc_server *server = chand->server;
Craig Tillera82950e2015-09-22 12:33:20 -0700410 GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, chand->channel, "server");
411 server_unref(exec_ctx, server);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800412}
413
Craig Tillera82950e2015-09-22 12:33:20 -0700414static void destroy_channel(grpc_exec_ctx *exec_ctx, channel_data *chand) {
415 if (is_channel_orphaned(chand)) return;
416 GPR_ASSERT(chand->server != NULL);
417 orphan_channel(chand);
418 server_ref(chand->server);
419 maybe_finish_shutdown(exec_ctx, chand->server);
David Garcia Quintas284488b2015-05-28 16:27:39 -0700420 chand->finish_destroy_channel_closure.cb = finish_destroy_channel;
421 chand->finish_destroy_channel_closure.cb_arg = chand;
Craig Tillerd7f12e32016-03-03 10:08:31 -0800422
423 grpc_transport_op op;
424 memset(&op, 0, sizeof(op));
425 op.set_accept_stream = true;
426 op.on_consumed = &chand->finish_destroy_channel_closure;
427 grpc_channel_next_op(exec_ctx,
428 grpc_channel_stack_element(
429 grpc_channel_get_channel_stack(chand->channel), 0),
430 &op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800431}
432
Craig Tiller06cb1a92016-04-04 08:10:47 -0700433static void cpstr(char **dest, size_t *capacity, grpc_mdstr *value) {
434 gpr_slice slice = value->slice;
435 size_t len = GPR_SLICE_LENGTH(slice);
Craig Tiller6a006ce2015-07-13 16:25:40 -0700436
Craig Tiller06cb1a92016-04-04 08:10:47 -0700437 if (len + 1 > *capacity) {
438 *capacity = GPR_MAX(len + 1, *capacity * 2);
439 *dest = gpr_realloc(*dest, *capacity);
440 }
441 memcpy(*dest, grpc_mdstr_as_c_string(value), len + 1);
442}
443
Craig Tiller88512692016-04-04 09:32:52 -0700444static void done_request_event(grpc_exec_ctx *exec_ctx, void *req,
445 grpc_cq_completion *c) {
446 requested_call *rc = req;
447 grpc_server *server = rc->server;
Craig Tiller06cb1a92016-04-04 08:10:47 -0700448
Craig Tiller88512692016-04-04 09:32:52 -0700449 if (rc >= server->requested_calls &&
450 rc < server->requested_calls + server->max_requested_calls) {
451 GPR_ASSERT(rc - server->requested_calls <= INT_MAX);
452 gpr_stack_lockfree_push(server->request_freelist,
453 (int)(rc - server->requested_calls));
454 } else {
455 gpr_free(req);
456 }
Craig Tiller06cb1a92016-04-04 08:10:47 -0700457
Craig Tiller88512692016-04-04 09:32:52 -0700458 server_unref(exec_ctx, server);
459}
Craig Tiller06cb1a92016-04-04 08:10:47 -0700460
Craig Tiller88512692016-04-04 09:32:52 -0700461static void publish_call(grpc_exec_ctx *exec_ctx, grpc_server *server,
462 call_data *calld, requested_call *rc) {
Craig Tiller06cb1a92016-04-04 08:10:47 -0700463 grpc_call_set_completion_queue(exec_ctx, calld->call, rc->cq_bound_to_call);
Craig Tiller88512692016-04-04 09:32:52 -0700464 grpc_call *call = calld->call;
465 *rc->call = call;
Craig Tiller06cb1a92016-04-04 08:10:47 -0700466 calld->cq_new = rc->cq_for_notification;
467 GPR_SWAP(grpc_metadata_array, *rc->initial_metadata, calld->initial_metadata);
468 switch (rc->type) {
469 case BATCH_CALL:
470 GPR_ASSERT(calld->host != NULL);
471 GPR_ASSERT(calld->path != NULL);
472 cpstr(&rc->data.batch.details->host,
473 &rc->data.batch.details->host_capacity, calld->host);
474 cpstr(&rc->data.batch.details->method,
475 &rc->data.batch.details->method_capacity, calld->path);
476 rc->data.batch.details->deadline = calld->deadline;
477 rc->data.batch.details->flags =
478 0 | (calld->recv_idempotent_request
479 ? GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST
480 : 0);
481 break;
482 case REGISTERED_CALL:
483 *rc->data.registered.deadline = calld->deadline;
484 if (rc->data.registered.optional_payload) {
Craig Tiller88512692016-04-04 09:32:52 -0700485 *rc->data.registered.optional_payload = calld->payload;
Craig Tiller06cb1a92016-04-04 08:10:47 -0700486 }
487 break;
488 default:
489 GPR_UNREACHABLE_CODE(return );
490 }
491
Craig Tiller88512692016-04-04 09:32:52 -0700492 grpc_call_element *elem =
493 grpc_call_stack_element(grpc_call_get_call_stack(call), 0);
494 channel_data *chand = elem->channel_data;
495 server_ref(chand->server);
Craig Tillerf51457b2016-05-03 17:06:32 -0700496 grpc_cq_end_op(exec_ctx, calld->cq_new, rc->tag, GRPC_ERROR_NONE,
497 done_request_event, rc, &rc->completion);
Craig Tiller06cb1a92016-04-04 08:10:47 -0700498}
499
Craig Tillerf51457b2016-05-03 17:06:32 -0700500static void publish_new_rpc(grpc_exec_ctx *exec_ctx, void *arg,
501 grpc_error *error) {
Craig Tiller88512692016-04-04 09:32:52 -0700502 call_data *calld = arg;
503 request_matcher *rm = calld->request_matcher;
504 grpc_server *server = rm->server;
Craig Tiller6a006ce2015-07-13 16:25:40 -0700505
Craig Tillerf51457b2016-05-03 17:06:32 -0700506 if (error != GRPC_ERROR_NONE || gpr_atm_acq_load(&server->shutdown_flag)) {
Craig Tillera82950e2015-09-22 12:33:20 -0700507 gpr_mu_lock(&calld->mu_state);
508 calld->state = ZOMBIED;
509 gpr_mu_unlock(&calld->mu_state);
Craig Tiller88512692016-04-04 09:32:52 -0700510 grpc_closure_init(
511 &calld->kill_zombie_closure, kill_zombie,
512 grpc_call_stack_element(grpc_call_get_call_stack(calld->call), 0));
Craig Tillerf51457b2016-05-03 17:06:32 -0700513 grpc_exec_ctx_push(exec_ctx, &calld->kill_zombie_closure, error, NULL);
Craig Tillera82950e2015-09-22 12:33:20 -0700514 return;
515 }
Craig Tiller45724b32015-09-22 10:42:19 -0700516
Craig Tiller88512692016-04-04 09:32:52 -0700517 int request_id = gpr_stack_lockfree_pop(rm->requests);
Craig Tillera82950e2015-09-22 12:33:20 -0700518 if (request_id == -1) {
519 gpr_mu_lock(&server->mu_call);
520 gpr_mu_lock(&calld->mu_state);
521 calld->state = PENDING;
522 gpr_mu_unlock(&calld->mu_state);
Craig Tillerb9d35962015-09-11 13:31:16 -0700523 if (rm->pending_head == NULL) {
524 rm->pending_tail = rm->pending_head = calld;
Craig Tillera82950e2015-09-22 12:33:20 -0700525 } else {
Craig Tillerb9d35962015-09-11 13:31:16 -0700526 rm->pending_tail->pending_next = calld;
527 rm->pending_tail = calld;
Craig Tiller45724b32015-09-22 10:42:19 -0700528 }
Craig Tillera82950e2015-09-22 12:33:20 -0700529 calld->pending_next = NULL;
530 gpr_mu_unlock(&server->mu_call);
531 } else {
532 gpr_mu_lock(&calld->mu_state);
533 calld->state = ACTIVATED;
534 gpr_mu_unlock(&calld->mu_state);
Craig Tiller88512692016-04-04 09:32:52 -0700535 publish_call(exec_ctx, server, calld, &server->requested_calls[request_id]);
536 }
537}
538
539static void finish_start_new_rpc(
540 grpc_exec_ctx *exec_ctx, grpc_server *server, grpc_call_element *elem,
541 request_matcher *rm,
542 grpc_server_register_method_payload_handling payload_handling) {
543 call_data *calld = elem->call_data;
544
545 if (gpr_atm_acq_load(&server->shutdown_flag)) {
546 gpr_mu_lock(&calld->mu_state);
547 calld->state = ZOMBIED;
548 gpr_mu_unlock(&calld->mu_state);
549 grpc_closure_init(&calld->kill_zombie_closure, kill_zombie, elem);
Craig Tillerf51457b2016-05-03 17:06:32 -0700550 grpc_exec_ctx_push(exec_ctx, &calld->kill_zombie_closure, GRPC_ERROR_NONE,
551 NULL);
Craig Tiller88512692016-04-04 09:32:52 -0700552 return;
553 }
554
555 calld->request_matcher = rm;
556
557 switch (payload_handling) {
558 case GRPC_SRM_PAYLOAD_NONE:
Craig Tillerf51457b2016-05-03 17:06:32 -0700559 publish_new_rpc(exec_ctx, calld, GRPC_ERROR_NONE);
Craig Tiller88512692016-04-04 09:32:52 -0700560 break;
561 case GRPC_SRM_PAYLOAD_READ_INITIAL_BYTE_BUFFER: {
562 grpc_op op;
563 memset(&op, 0, sizeof(op));
564 op.op = GRPC_OP_RECV_MESSAGE;
565 op.data.recv_message = &calld->payload;
566 grpc_closure_init(&calld->publish, publish_new_rpc, calld);
567 grpc_call_start_batch_and_execute(exec_ctx, calld->call, &op, 1,
568 &calld->publish);
569 break;
570 }
Craig Tillera82950e2015-09-22 12:33:20 -0700571 }
Craig Tiller04cc8be2015-02-10 16:11:22 -0800572}
573
Craig Tillera82950e2015-09-22 12:33:20 -0700574static void start_new_rpc(grpc_exec_ctx *exec_ctx, grpc_call_element *elem) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800575 channel_data *chand = elem->channel_data;
576 call_data *calld = elem->call_data;
577 grpc_server *server = chand->server;
Craig Tiller7536af02015-12-22 13:49:30 -0800578 uint32_t i;
579 uint32_t hash;
Craig Tiller04cc8be2015-02-10 16:11:22 -0800580 channel_registered_method *rm;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800581
Craig Tillera82950e2015-09-22 12:33:20 -0700582 if (chand->registered_methods && calld->path && calld->host) {
583 /* TODO(ctiller): unify these two searches */
584 /* check for an exact match with host */
585 hash = GRPC_MDSTR_KV_HASH(calld->host->hash, calld->path->hash);
586 for (i = 0; i <= chand->registered_method_max_probes; i++) {
587 rm = &chand->registered_methods[(hash + i) %
588 chand->registered_method_slots];
589 if (!rm) break;
590 if (rm->host != calld->host) continue;
591 if (rm->method != calld->path) continue;
Craig Tillerb2906862016-03-10 06:50:07 -0800592 if ((rm->flags & GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST) &&
593 !calld->recv_idempotent_request)
594 continue;
Craig Tillera82950e2015-09-22 12:33:20 -0700595 finish_start_new_rpc(exec_ctx, server, elem,
Craig Tiller88512692016-04-04 09:32:52 -0700596 &rm->server_registered_method->request_matcher,
597 rm->server_registered_method->payload_handling);
Craig Tillera82950e2015-09-22 12:33:20 -0700598 return;
Craig Tiller04cc8be2015-02-10 16:11:22 -0800599 }
Craig Tillera82950e2015-09-22 12:33:20 -0700600 /* check for a wildcard method definition (no host set) */
601 hash = GRPC_MDSTR_KV_HASH(0, calld->path->hash);
602 for (i = 0; i <= chand->registered_method_max_probes; i++) {
603 rm = &chand->registered_methods[(hash + i) %
604 chand->registered_method_slots];
605 if (!rm) break;
606 if (rm->host != NULL) continue;
607 if (rm->method != calld->path) continue;
Craig Tillerb2906862016-03-10 06:50:07 -0800608 if ((rm->flags & GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST) &&
609 !calld->recv_idempotent_request)
610 continue;
Craig Tillera82950e2015-09-22 12:33:20 -0700611 finish_start_new_rpc(exec_ctx, server, elem,
Craig Tiller88512692016-04-04 09:32:52 -0700612 &rm->server_registered_method->request_matcher,
613 rm->server_registered_method->payload_handling);
Craig Tillera82950e2015-09-22 12:33:20 -0700614 return;
615 }
616 }
617 finish_start_new_rpc(exec_ctx, server, elem,
Craig Tiller88512692016-04-04 09:32:52 -0700618 &server->unregistered_request_matcher,
619 GRPC_SRM_PAYLOAD_NONE);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800620}
621
Craig Tillera82950e2015-09-22 12:33:20 -0700622static int num_listeners(grpc_server *server) {
Craig Tilleree945e82015-05-26 16:15:34 -0700623 listener *l;
624 int n = 0;
Craig Tillera82950e2015-09-22 12:33:20 -0700625 for (l = server->listeners; l; l = l->next) {
626 n++;
627 }
Craig Tilleree945e82015-05-26 16:15:34 -0700628 return n;
629}
630
Craig Tillera82950e2015-09-22 12:33:20 -0700631static void done_shutdown_event(grpc_exec_ctx *exec_ctx, void *server,
632 grpc_cq_completion *completion) {
633 server_unref(exec_ctx, server);
Craig Tiller97fc6a32015-07-08 15:31:35 -0700634}
635
Craig Tillera82950e2015-09-22 12:33:20 -0700636static int num_channels(grpc_server *server) {
Craig Tillerab54f792015-07-08 08:34:20 -0700637 channel_data *chand;
638 int n = 0;
Craig Tillera82950e2015-09-22 12:33:20 -0700639 for (chand = server->root_channel_data.next;
640 chand != &server->root_channel_data; chand = chand->next) {
641 n++;
642 }
Craig Tillerab54f792015-07-08 08:34:20 -0700643 return n;
644}
645
Craig Tillera82950e2015-09-22 12:33:20 -0700646static void kill_pending_work_locked(grpc_exec_ctx *exec_ctx,
Craig Tillercae4b1b2016-05-10 09:11:09 -0700647 grpc_server *server, grpc_error *error) {
Craig Tiller1191e212015-07-30 14:49:02 -0700648 registered_method *rm;
Craig Tillera82950e2015-09-22 12:33:20 -0700649 request_matcher_kill_requests(exec_ctx, server,
Craig Tillercae4b1b2016-05-10 09:11:09 -0700650 &server->unregistered_request_matcher,
651 GRPC_ERROR_REF(error));
Craig Tillera82950e2015-09-22 12:33:20 -0700652 request_matcher_zombify_all_pending_calls(
653 exec_ctx, &server->unregistered_request_matcher);
654 for (rm = server->registered_methods; rm; rm = rm->next) {
Craig Tillercae4b1b2016-05-10 09:11:09 -0700655 request_matcher_kill_requests(exec_ctx, server, &rm->request_matcher,
656 GRPC_ERROR_REF(error));
Craig Tillera82950e2015-09-22 12:33:20 -0700657 request_matcher_zombify_all_pending_calls(exec_ctx, &rm->request_matcher);
658 }
Craig Tillercae4b1b2016-05-10 09:11:09 -0700659 GRPC_ERROR_UNREF(error);
Craig Tillerdc627722015-05-26 15:27:02 -0700660}
661
Craig Tillera82950e2015-09-22 12:33:20 -0700662static void maybe_finish_shutdown(grpc_exec_ctx *exec_ctx,
663 grpc_server *server) {
Craig Tiller45724b32015-09-22 10:42:19 -0700664 size_t i;
Craig Tillera82950e2015-09-22 12:33:20 -0700665 if (!gpr_atm_acq_load(&server->shutdown_flag) || server->shutdown_published) {
666 return;
667 }
Craig Tiller45724b32015-09-22 10:42:19 -0700668
Craig Tillercae4b1b2016-05-10 09:11:09 -0700669 kill_pending_work_locked(exec_ctx, server,
670 GRPC_ERROR_CREATE("Server Shutdown"));
Craig Tiller45724b32015-09-22 10:42:19 -0700671
Craig Tillera82950e2015-09-22 12:33:20 -0700672 if (server->root_channel_data.next != &server->root_channel_data ||
673 server->listeners_destroyed < num_listeners(server)) {
674 if (gpr_time_cmp(gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME),
675 server->last_shutdown_message_time),
676 gpr_time_from_seconds(1, GPR_TIMESPAN)) >= 0) {
677 server->last_shutdown_message_time = gpr_now(GPR_CLOCK_REALTIME);
678 gpr_log(GPR_DEBUG,
679 "Waiting for %d channels and %d/%d listeners to be destroyed"
680 " before shutting down server",
681 num_channels(server),
682 num_listeners(server) - server->listeners_destroyed,
683 num_listeners(server));
Craig Tiller45724b32015-09-22 10:42:19 -0700684 }
Craig Tillera82950e2015-09-22 12:33:20 -0700685 return;
686 }
Craig Tiller45724b32015-09-22 10:42:19 -0700687 server->shutdown_published = 1;
Craig Tillera82950e2015-09-22 12:33:20 -0700688 for (i = 0; i < server->num_shutdown_tags; i++) {
689 server_ref(server);
690 grpc_cq_end_op(exec_ctx, server->shutdown_tags[i].cq,
Craig Tillerf51457b2016-05-03 17:06:32 -0700691 server->shutdown_tags[i].tag, GRPC_ERROR_NONE,
692 done_shutdown_event, server,
Craig Tillera82950e2015-09-22 12:33:20 -0700693 &server->shutdown_tags[i].completion);
694 }
Craig Tiller45724b32015-09-22 10:42:19 -0700695}
696
Craig Tillera82950e2015-09-22 12:33:20 -0700697static grpc_mdelem *server_filter(void *user_data, grpc_mdelem *md) {
Craig Tiller6902ad22015-04-16 08:01:49 -0700698 grpc_call_element *elem = user_data;
Craig Tillercce17ac2015-01-20 09:29:28 -0800699 call_data *calld = elem->call_data;
Craig Tillerebdef9d2015-11-19 17:09:49 -0800700 if (md->key == GRPC_MDSTR_PATH) {
Craig Tiller9cac2a12016-04-04 20:56:56 -0700701 if (calld->path == NULL) {
702 calld->path = GRPC_MDSTR_REF(md->value);
703 }
Craig Tillera82950e2015-09-22 12:33:20 -0700704 return NULL;
Craig Tillerebdef9d2015-11-19 17:09:49 -0800705 } else if (md->key == GRPC_MDSTR_AUTHORITY) {
Craig Tiller9cac2a12016-04-04 20:56:56 -0700706 if (calld->host == NULL) {
707 calld->host = GRPC_MDSTR_REF(md->value);
708 }
Craig Tillera82950e2015-09-22 12:33:20 -0700709 return NULL;
710 }
Craig Tiller6902ad22015-04-16 08:01:49 -0700711 return md;
712}
713
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800714static void server_on_recv_initial_metadata(grpc_exec_ctx *exec_ctx, void *ptr,
Craig Tillerf51457b2016-05-03 17:06:32 -0700715 grpc_error *error) {
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700716 grpc_call_element *elem = ptr;
Craig Tiller6902ad22015-04-16 08:01:49 -0700717 call_data *calld = elem->call_data;
Craig Tiller94329d02015-07-23 09:52:11 -0700718 gpr_timespec op_deadline;
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700719
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800720 grpc_metadata_batch_filter(calld->recv_initial_metadata, server_filter, elem);
721 op_deadline = calld->recv_initial_metadata->deadline;
722 if (0 != gpr_time_cmp(op_deadline, gpr_inf_future(op_deadline.clock_type))) {
723 calld->deadline = op_deadline;
724 }
725 if (calld->host && calld->path) {
726 /* do nothing */
727 } else {
Craig Tillerf51457b2016-05-03 17:06:32 -0700728 error =
729 GRPC_ERROR_CREATE_REFERENCING("Missing :authority or :path", &error, 1);
Craig Tillera82950e2015-09-22 12:33:20 -0700730 }
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700731
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800732 calld->on_done_recv_initial_metadata->cb(
Craig Tillerf51457b2016-05-03 17:06:32 -0700733 exec_ctx, calld->on_done_recv_initial_metadata->cb_arg, error);
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700734}
735
Craig Tillera82950e2015-09-22 12:33:20 -0700736static void server_mutate_op(grpc_call_element *elem,
737 grpc_transport_stream_op *op) {
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700738 call_data *calld = elem->call_data;
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700739
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800740 if (op->recv_initial_metadata != NULL) {
Craig Tiller4d40ba32016-03-09 17:48:40 -0800741 GPR_ASSERT(op->recv_idempotent_request == NULL);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800742 calld->recv_initial_metadata = op->recv_initial_metadata;
Craig Tillera44cbfc2016-02-03 16:02:49 -0800743 calld->on_done_recv_initial_metadata = op->recv_initial_metadata_ready;
744 op->recv_initial_metadata_ready = &calld->server_on_recv_initial_metadata;
Craig Tiller4d40ba32016-03-09 17:48:40 -0800745 op->recv_idempotent_request = &calld->recv_idempotent_request;
Craig Tillera82950e2015-09-22 12:33:20 -0700746 }
Craig Tiller50d9db52015-04-23 10:52:14 -0700747}
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700748
Craig Tillera82950e2015-09-22 12:33:20 -0700749static void server_start_transport_stream_op(grpc_exec_ctx *exec_ctx,
750 grpc_call_element *elem,
751 grpc_transport_stream_op *op) {
752 GRPC_CALL_LOG_OP(GPR_INFO, elem, op);
753 server_mutate_op(elem, op);
754 grpc_call_next_op(exec_ctx, elem, op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800755}
756
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800757static void got_initial_metadata(grpc_exec_ctx *exec_ctx, void *ptr,
Craig Tillerf51457b2016-05-03 17:06:32 -0700758 grpc_error *error) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800759 grpc_call_element *elem = ptr;
760 call_data *calld = elem->call_data;
Craig Tillerf51457b2016-05-03 17:06:32 -0700761 if (error == GRPC_ERROR_NONE) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800762 start_new_rpc(exec_ctx, elem);
763 } else {
764 gpr_mu_lock(&calld->mu_state);
765 if (calld->state == NOT_STARTED) {
766 calld->state = ZOMBIED;
767 gpr_mu_unlock(&calld->mu_state);
768 grpc_closure_init(&calld->kill_zombie_closure, kill_zombie, elem);
Craig Tillerf51457b2016-05-03 17:06:32 -0700769 grpc_exec_ctx_push(exec_ctx, &calld->kill_zombie_closure, GRPC_ERROR_NONE,
770 NULL);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800771 } else if (calld->state == PENDING) {
772 calld->state = ZOMBIED;
773 gpr_mu_unlock(&calld->mu_state);
774 /* zombied call will be destroyed when it's removed from the pending
775 queue... later */
776 } else {
777 gpr_mu_unlock(&calld->mu_state);
778 }
779 }
780}
781
782static void accept_stream(grpc_exec_ctx *exec_ctx, void *cd,
783 grpc_transport *transport,
Craig Tillera82950e2015-09-22 12:33:20 -0700784 const void *transport_server_data) {
Craig Tillere039f032015-06-25 12:54:23 -0700785 channel_data *chand = cd;
786 /* create a call */
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800787 grpc_call *call =
788 grpc_call_create(chand->channel, NULL, 0, NULL, transport_server_data,
789 NULL, 0, gpr_inf_future(GPR_CLOCK_MONOTONIC));
790 grpc_call_element *elem =
791 grpc_call_stack_element(grpc_call_get_call_stack(call), 0);
792 call_data *calld = elem->call_data;
793 grpc_op op;
794 memset(&op, 0, sizeof(op));
795 op.op = GRPC_OP_RECV_INITIAL_METADATA;
796 op.data.recv_initial_metadata = &calld->initial_metadata;
797 grpc_closure_init(&calld->got_initial_metadata, got_initial_metadata, elem);
798 grpc_call_start_batch_and_execute(exec_ctx, call, &op, 1,
799 &calld->got_initial_metadata);
Craig Tillere039f032015-06-25 12:54:23 -0700800}
801
Craig Tillera82950e2015-09-22 12:33:20 -0700802static void channel_connectivity_changed(grpc_exec_ctx *exec_ctx, void *cd,
Craig Tillerf51457b2016-05-03 17:06:32 -0700803 grpc_error *error) {
Craig Tillere039f032015-06-25 12:54:23 -0700804 channel_data *chand = cd;
805 grpc_server *server = chand->server;
Craig Tillera82950e2015-09-22 12:33:20 -0700806 if (chand->connectivity_state != GRPC_CHANNEL_FATAL_FAILURE) {
807 grpc_transport_op op;
808 memset(&op, 0, sizeof(op));
809 op.on_connectivity_state_change = &chand->channel_connectivity_changed,
810 op.connectivity_state = &chand->connectivity_state;
811 grpc_channel_next_op(exec_ctx,
812 grpc_channel_stack_element(
813 grpc_channel_get_channel_stack(chand->channel), 0),
814 &op);
815 } else {
816 gpr_mu_lock(&server->mu_global);
817 destroy_channel(exec_ctx, chand);
818 gpr_mu_unlock(&server->mu_global);
819 GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, chand->channel, "connectivity");
820 }
Craig Tillere039f032015-06-25 12:54:23 -0700821}
822
Craig Tillera82950e2015-09-22 12:33:20 -0700823static void init_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800824 grpc_call_element_args *args) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800825 call_data *calld = elem->call_data;
826 channel_data *chand = elem->channel_data;
Craig Tillera82950e2015-09-22 12:33:20 -0700827 memset(calld, 0, sizeof(call_data));
828 calld->deadline = gpr_inf_future(GPR_CLOCK_REALTIME);
829 calld->call = grpc_call_from_top_element(elem);
830 gpr_mu_init(&calld->mu_state);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800831
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800832 grpc_closure_init(&calld->server_on_recv_initial_metadata,
833 server_on_recv_initial_metadata, elem);
Craig Tiller1e6facb2015-06-11 22:47:11 -0700834
Craig Tillera82950e2015-09-22 12:33:20 -0700835 server_ref(chand->server);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800836}
837
Craig Tiller2c8063c2016-03-22 22:12:15 -0700838static void destroy_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
839 void *ignored) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800840 channel_data *chand = elem->channel_data;
Craig Tillerdb7db992015-01-29 11:19:01 -0800841 call_data *calld = elem->call_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800842
Craig Tillera82950e2015-09-22 12:33:20 -0700843 GPR_ASSERT(calld->state != PENDING);
Craig Tiller092d8d12015-07-04 22:35:00 -0700844
Craig Tillera82950e2015-09-22 12:33:20 -0700845 if (calld->host) {
846 GRPC_MDSTR_UNREF(calld->host);
847 }
848 if (calld->path) {
849 GRPC_MDSTR_UNREF(calld->path);
850 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800851 grpc_metadata_array_destroy(&calld->initial_metadata);
Craig Tiller4df31a62015-01-30 09:44:31 -0800852
Craig Tillera82950e2015-09-22 12:33:20 -0700853 gpr_mu_destroy(&calld->mu_state);
Craig Tiller76d2c3b2015-07-07 11:46:01 -0700854
Craig Tillera82950e2015-09-22 12:33:20 -0700855 server_unref(exec_ctx, chand->server);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800856}
857
Craig Tillera82950e2015-09-22 12:33:20 -0700858static void init_channel_elem(grpc_exec_ctx *exec_ctx,
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800859 grpc_channel_element *elem,
860 grpc_channel_element_args *args) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800861 channel_data *chand = elem->channel_data;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800862 GPR_ASSERT(args->is_first);
863 GPR_ASSERT(!args->is_last);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800864 chand->server = NULL;
865 chand->channel = NULL;
866 chand->next = chand->prev = chand;
Craig Tiller04cc8be2015-02-10 16:11:22 -0800867 chand->registered_methods = NULL;
Craig Tillere039f032015-06-25 12:54:23 -0700868 chand->connectivity_state = GRPC_CHANNEL_IDLE;
Craig Tillera82950e2015-09-22 12:33:20 -0700869 grpc_closure_init(&chand->channel_connectivity_changed,
870 channel_connectivity_changed, chand);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800871}
872
Craig Tillera82950e2015-09-22 12:33:20 -0700873static void destroy_channel_elem(grpc_exec_ctx *exec_ctx,
874 grpc_channel_element *elem) {
Craig Tillerec3257c2015-02-12 15:59:43 -0800875 size_t i;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800876 channel_data *chand = elem->channel_data;
Craig Tillera82950e2015-09-22 12:33:20 -0700877 if (chand->registered_methods) {
878 for (i = 0; i < chand->registered_method_slots; i++) {
879 if (chand->registered_methods[i].method) {
880 GRPC_MDSTR_UNREF(chand->registered_methods[i].method);
881 }
882 if (chand->registered_methods[i].host) {
883 GRPC_MDSTR_UNREF(chand->registered_methods[i].host);
884 }
Craig Tillerec3257c2015-02-12 15:59:43 -0800885 }
Craig Tillera82950e2015-09-22 12:33:20 -0700886 gpr_free(chand->registered_methods);
887 }
888 if (chand->server) {
889 gpr_mu_lock(&chand->server->mu_global);
890 chand->next->prev = chand->prev;
891 chand->prev->next = chand->next;
892 chand->next = chand->prev = chand;
893 maybe_finish_shutdown(exec_ctx, chand->server);
894 gpr_mu_unlock(&chand->server->mu_global);
Craig Tillera82950e2015-09-22 12:33:20 -0700895 server_unref(exec_ctx, chand->server);
896 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800897}
898
Craig Tiller178edfa2016-02-17 20:54:46 -0800899const grpc_channel_filter grpc_server_top_filter = {
Craig Tillerf40df232016-03-25 13:38:14 -0700900 server_start_transport_stream_op,
901 grpc_channel_next_op,
902 sizeof(call_data),
903 init_call_elem,
904 grpc_call_stack_ignore_set_pollset,
905 destroy_call_elem,
906 sizeof(channel_data),
907 init_channel_elem,
908 destroy_channel_elem,
909 grpc_call_next_get_peer,
910 "server",
Craig Tiller9f28ac22015-01-27 17:01:29 -0800911};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800912
Craig Tillera82950e2015-09-22 12:33:20 -0700913void grpc_server_register_completion_queue(grpc_server *server,
914 grpc_completion_queue *cq,
915 void *reserved) {
Craig Tiller20bc56d2015-02-12 09:02:56 -0800916 size_t i, n;
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700917 GRPC_API_TRACE(
918 "grpc_server_register_completion_queue(server=%p, cq=%p, reserved=%p)", 3,
919 (server, cq, reserved));
Craig Tillera82950e2015-09-22 12:33:20 -0700920 GPR_ASSERT(!reserved);
921 for (i = 0; i < server->cq_count; i++) {
922 if (server->cqs[i] == cq) return;
923 }
924 GRPC_CQ_INTERNAL_REF(cq, "server");
925 grpc_cq_mark_server_cq(cq);
Craig Tiller20bc56d2015-02-12 09:02:56 -0800926 n = server->cq_count++;
Craig Tillera82950e2015-09-22 12:33:20 -0700927 server->cqs = gpr_realloc(server->cqs,
928 server->cq_count * sizeof(grpc_completion_queue *));
Craig Tiller20bc56d2015-02-12 09:02:56 -0800929 server->cqs[n] = cq;
930}
931
Craig Tiller178edfa2016-02-17 20:54:46 -0800932grpc_server *grpc_server_create(const grpc_channel_args *args, void *reserved) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800933 size_t i;
Craig Tiller178edfa2016-02-17 20:54:46 -0800934
935 GRPC_API_TRACE("grpc_server_create(%p, %p)", 2, (args, reserved));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800936
Craig Tillera82950e2015-09-22 12:33:20 -0700937 grpc_server *server = gpr_malloc(sizeof(grpc_server));
Craig Tiller60fd3612015-03-05 16:24:22 -0800938
Craig Tillera82950e2015-09-22 12:33:20 -0700939 GPR_ASSERT(grpc_is_initialized() && "call grpc_init()");
Craig Tiller60fd3612015-03-05 16:24:22 -0800940
Craig Tillera82950e2015-09-22 12:33:20 -0700941 memset(server, 0, sizeof(grpc_server));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800942
Craig Tillera82950e2015-09-22 12:33:20 -0700943 gpr_mu_init(&server->mu_global);
944 gpr_mu_init(&server->mu_call);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800945
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800946 /* decremented by grpc_server_destroy */
Craig Tillera82950e2015-09-22 12:33:20 -0700947 gpr_ref_init(&server->internal_refcount, 1);
948 server->root_channel_data.next = server->root_channel_data.prev =
949 &server->root_channel_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800950
Craig Tiller6a006ce2015-07-13 16:25:40 -0700951 /* TODO(ctiller): expose a channel_arg for this */
952 server->max_requested_calls = 32768;
Craig Tillera82950e2015-09-22 12:33:20 -0700953 server->request_freelist =
954 gpr_stack_lockfree_create(server->max_requested_calls);
955 for (i = 0; i < (size_t)server->max_requested_calls; i++) {
956 gpr_stack_lockfree_push(server->request_freelist, (int)i);
957 }
958 request_matcher_init(&server->unregistered_request_matcher,
Craig Tiller88512692016-04-04 09:32:52 -0700959 server->max_requested_calls, server);
Craig Tillera82950e2015-09-22 12:33:20 -0700960 server->requested_calls = gpr_malloc(server->max_requested_calls *
961 sizeof(*server->requested_calls));
Craig Tiller729b35a2015-07-13 12:36:47 -0700962
Craig Tillera82950e2015-09-22 12:33:20 -0700963 server->channel_args = grpc_channel_args_copy(args);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800964
965 return server;
966}
967
Craig Tillera82950e2015-09-22 12:33:20 -0700968static int streq(const char *a, const char *b) {
969 if (a == NULL && b == NULL) return 1;
970 if (a == NULL) return 0;
971 if (b == NULL) return 0;
972 return 0 == strcmp(a, b);
Craig Tiller24be0f72015-02-10 14:04:22 -0800973}
974
Craig Tiller06cb1a92016-04-04 08:10:47 -0700975void *grpc_server_register_method(
976 grpc_server *server, const char *method, const char *host,
977 grpc_server_register_method_payload_handling payload_handling,
978 uint32_t flags) {
Craig Tiller24be0f72015-02-10 14:04:22 -0800979 registered_method *m;
Craig Tillerb2906862016-03-10 06:50:07 -0800980 GRPC_API_TRACE(
981 "grpc_server_register_method(server=%p, method=%s, host=%s, "
982 "flags=0x%08x)",
983 4, (server, method, host, flags));
Craig Tillera82950e2015-09-22 12:33:20 -0700984 if (!method) {
985 gpr_log(GPR_ERROR,
986 "grpc_server_register_method method string cannot be NULL");
987 return NULL;
988 }
989 for (m = server->registered_methods; m; m = m->next) {
990 if (streq(m->method, method) && streq(m->host, host)) {
991 gpr_log(GPR_ERROR, "duplicate registration for %s@%s", method,
992 host ? host : "*");
Craig Tiller24be0f72015-02-10 14:04:22 -0800993 return NULL;
994 }
Craig Tillera82950e2015-09-22 12:33:20 -0700995 }
Craig Tillerb2906862016-03-10 06:50:07 -0800996 if ((flags & ~GRPC_INITIAL_METADATA_USED_MASK) != 0) {
997 gpr_log(GPR_ERROR, "grpc_server_register_method invalid flags 0x%08x",
998 flags);
999 return NULL;
1000 }
Craig Tillera82950e2015-09-22 12:33:20 -07001001 m = gpr_malloc(sizeof(registered_method));
1002 memset(m, 0, sizeof(*m));
Craig Tiller88512692016-04-04 09:32:52 -07001003 request_matcher_init(&m->request_matcher, server->max_requested_calls,
1004 server);
Craig Tillera82950e2015-09-22 12:33:20 -07001005 m->method = gpr_strdup(method);
1006 m->host = gpr_strdup(host);
Craig Tiller24be0f72015-02-10 14:04:22 -08001007 m->next = server->registered_methods;
Craig Tiller5ada3d22016-04-04 08:30:59 -07001008 m->payload_handling = payload_handling;
Craig Tillerb2906862016-03-10 06:50:07 -08001009 m->flags = flags;
Craig Tiller24be0f72015-02-10 14:04:22 -08001010 server->registered_methods = m;
1011 return m;
1012}
1013
Craig Tillera82950e2015-09-22 12:33:20 -07001014void grpc_server_start(grpc_server *server) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001015 listener *l;
Craig Tiller20bc56d2015-02-12 09:02:56 -08001016 size_t i;
Craig Tillerf5768a62015-09-22 10:54:34 -07001017 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tiller20bc56d2015-02-12 09:02:56 -08001018
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001019 GRPC_API_TRACE("grpc_server_start(server=%p)", 1, (server));
1020
Craig Tillera82950e2015-09-22 12:33:20 -07001021 server->pollsets = gpr_malloc(sizeof(grpc_pollset *) * server->cq_count);
1022 for (i = 0; i < server->cq_count; i++) {
1023 server->pollsets[i] = grpc_cq_pollset(server->cqs[i]);
1024 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001025
Craig Tillera82950e2015-09-22 12:33:20 -07001026 for (l = server->listeners; l; l = l->next) {
1027 l->start(&exec_ctx, server, l->arg, server->pollsets, server->cq_count);
1028 }
Craig Tillerdfff1b82015-09-21 14:39:57 -07001029
Craig Tillera82950e2015-09-22 12:33:20 -07001030 grpc_exec_ctx_finish(&exec_ctx);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001031}
1032
Craig Tillera82950e2015-09-22 12:33:20 -07001033void grpc_server_setup_transport(grpc_exec_ctx *exec_ctx, grpc_server *s,
1034 grpc_transport *transport,
Craig Tillera82950e2015-09-22 12:33:20 -07001035 const grpc_channel_args *args) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001036 size_t i;
Craig Tiller04cc8be2015-02-10 16:11:22 -08001037 size_t num_registered_methods;
1038 size_t alloc;
1039 registered_method *rm;
1040 channel_registered_method *crm;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001041 grpc_channel *channel;
1042 channel_data *chand;
Craig Tiller04cc8be2015-02-10 16:11:22 -08001043 grpc_mdstr *host;
1044 grpc_mdstr *method;
Craig Tiller7536af02015-12-22 13:49:30 -08001045 uint32_t hash;
Craig Tillerf96dfc32015-09-10 14:43:18 -07001046 size_t slots;
Craig Tiller7536af02015-12-22 13:49:30 -08001047 uint32_t probes;
1048 uint32_t max_probes = 0;
Craig Tillere039f032015-06-25 12:54:23 -07001049 grpc_transport_op op;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001050
Craig Tillera82950e2015-09-22 12:33:20 -07001051 for (i = 0; i < s->cq_count; i++) {
1052 memset(&op, 0, sizeof(op));
1053 op.bind_pollset = grpc_cq_pollset(s->cqs[i]);
1054 grpc_transport_perform_op(exec_ctx, transport, &op);
1055 }
ctillerd79b4862014-12-17 16:36:59 -08001056
Craig Tiller178edfa2016-02-17 20:54:46 -08001057 channel =
1058 grpc_channel_create(exec_ctx, NULL, args, GRPC_SERVER_CHANNEL, transport);
Craig Tillera82950e2015-09-22 12:33:20 -07001059 chand = (channel_data *)grpc_channel_stack_element(
Craig Tillerf40df232016-03-25 13:38:14 -07001060 grpc_channel_get_channel_stack(channel), 0)
1061 ->channel_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001062 chand->server = s;
Craig Tillera82950e2015-09-22 12:33:20 -07001063 server_ref(s);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001064 chand->channel = channel;
1065
Craig Tiller04cc8be2015-02-10 16:11:22 -08001066 num_registered_methods = 0;
Craig Tillera82950e2015-09-22 12:33:20 -07001067 for (rm = s->registered_methods; rm; rm = rm->next) {
1068 num_registered_methods++;
1069 }
Craig Tiller04cc8be2015-02-10 16:11:22 -08001070 /* build a lookup table phrased in terms of mdstr's in this channels context
1071 to quickly find registered methods */
Craig Tillera82950e2015-09-22 12:33:20 -07001072 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 Tillerb2b42612015-11-20 12:02:17 -08001078 host = rm->host ? grpc_mdstr_from_string(rm->host) : NULL;
1079 method = grpc_mdstr_from_string(rm->method);
Craig Tillera82950e2015-09-22 12:33:20 -07001080 hash = GRPC_MDSTR_KV_HASH(host ? host->hash : 0, method->hash);
1081 for (probes = 0; chand->registered_methods[(hash + probes) % slots]
Craig Tillerf40df232016-03-25 13:38:14 -07001082 .server_registered_method != NULL;
Craig Tillera82950e2015-09-22 12:33:20 -07001083 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 Tillerb2906862016-03-10 06:50:07 -08001088 crm->flags = rm->flags;
Craig Tillera82950e2015-09-22 12:33:20 -07001089 crm->host = host;
1090 crm->method = method;
Craig Tiller04cc8be2015-02-10 16:11:22 -08001091 }
Craig Tiller7536af02015-12-22 13:49:30 -08001092 GPR_ASSERT(slots <= UINT32_MAX);
1093 chand->registered_method_slots = (uint32_t)slots;
Craig Tillera82950e2015-09-22 12:33:20 -07001094 chand->registered_method_max_probes = max_probes;
1095 }
Craig Tiller04cc8be2015-02-10 16:11:22 -08001096
Craig Tillera82950e2015-09-22 12:33:20 -07001097 gpr_mu_lock(&s->mu_global);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001098 chand->next = &s->root_channel_data;
1099 chand->prev = chand->next->prev;
1100 chand->next->prev = chand->prev->next = chand;
Craig Tillera82950e2015-09-22 12:33:20 -07001101 gpr_mu_unlock(&s->mu_global);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001102
Craig Tillera82950e2015-09-22 12:33:20 -07001103 GRPC_CHANNEL_INTERNAL_REF(channel, "connectivity");
1104 memset(&op, 0, sizeof(op));
Craig Tillerd7f12e32016-03-03 10:08:31 -08001105 op.set_accept_stream = true;
1106 op.set_accept_stream_fn = accept_stream;
Craig Tiller4b804102015-06-26 16:16:12 -07001107 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 Tiller804ff712016-05-05 16:25:40 -07001110 if (gpr_atm_acq_load(&s->shutdown_flag) != 0) {
1111 op.disconnect_with_error = GRPC_ERROR_CREATE("Server shutdown");
1112 }
Craig Tillera82950e2015-09-22 12:33:20 -07001113 grpc_transport_perform_op(exec_ctx, transport, &op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001114}
1115
Craig Tillera82950e2015-09-22 12:33:20 -07001116void done_published_shutdown(grpc_exec_ctx *exec_ctx, void *done_arg,
1117 grpc_cq_completion *storage) {
1118 (void)done_arg;
1119 gpr_free(storage);
murgatroid9900a3dab2015-08-19 11:15:38 -07001120}
1121
Craig Tillera82950e2015-09-22 12:33:20 -07001122static void listener_destroy_done(grpc_exec_ctx *exec_ctx, void *s,
Craig Tillerf51457b2016-05-03 17:06:32 -07001123 grpc_error *error) {
Craig Tillerdfff1b82015-09-21 14:39:57 -07001124 grpc_server *server = s;
Craig Tillera82950e2015-09-22 12:33:20 -07001125 gpr_mu_lock(&server->mu_global);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001126 server->listeners_destroyed++;
Craig Tillera82950e2015-09-22 12:33:20 -07001127 maybe_finish_shutdown(exec_ctx, server);
1128 gpr_mu_unlock(&server->mu_global);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001129}
1130
Craig Tillera82950e2015-09-22 12:33:20 -07001131void grpc_server_shutdown_and_notify(grpc_server *server,
1132 grpc_completion_queue *cq, void *tag) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001133 listener *l;
Craig Tillerbce999f2015-05-27 09:55:51 -07001134 shutdown_tag *sdt;
Craig Tillerff3ae682015-06-29 17:44:04 -07001135 channel_broadcaster broadcaster;
Craig Tillerf5768a62015-09-22 10:54:34 -07001136 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001137
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001138 GRPC_API_TRACE("grpc_server_shutdown_and_notify(server=%p, cq=%p, tag=%p)", 3,
1139 (server, cq, tag));
1140
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001141 /* lock, and gather up some stuff to do */
Craig Tillera82950e2015-09-22 12:33:20 -07001142 gpr_mu_lock(&server->mu_global);
Craig Tiller4bf29282015-12-14 11:25:48 -08001143 grpc_cq_begin_op(cq, tag);
Craig Tillera82950e2015-09-22 12:33:20 -07001144 if (server->shutdown_published) {
Craig Tillerf51457b2016-05-03 17:06:32 -07001145 grpc_cq_end_op(&exec_ctx, cq, tag, GRPC_ERROR_NONE, done_published_shutdown,
1146 NULL, gpr_malloc(sizeof(grpc_cq_completion)));
Craig Tillera82950e2015-09-22 12:33:20 -07001147 gpr_mu_unlock(&server->mu_global);
1148 goto done;
1149 }
1150 server->shutdown_tags =
1151 gpr_realloc(server->shutdown_tags,
1152 sizeof(shutdown_tag) * (server->num_shutdown_tags + 1));
Craig Tillerbce999f2015-05-27 09:55:51 -07001153 sdt = &server->shutdown_tags[server->num_shutdown_tags++];
1154 sdt->tag = tag;
1155 sdt->cq = cq;
Craig Tillera82950e2015-09-22 12:33:20 -07001156 if (gpr_atm_acq_load(&server->shutdown_flag)) {
1157 gpr_mu_unlock(&server->mu_global);
1158 goto done;
1159 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001160
Craig Tillera82950e2015-09-22 12:33:20 -07001161 server->last_shutdown_message_time = gpr_now(GPR_CLOCK_REALTIME);
Craig Tillerab54f792015-07-08 08:34:20 -07001162
Craig Tillera82950e2015-09-22 12:33:20 -07001163 channel_broadcaster_init(server, &broadcaster);
nnoble0c475f02014-12-05 15:37:39 -08001164
Craig Tillerfc193e12015-09-24 15:29:03 -07001165 gpr_atm_rel_store(&server->shutdown_flag, 1);
1166
Craig Tillerbd217572015-02-11 18:10:56 -08001167 /* collect all unregistered then registered calls */
Craig Tillera82950e2015-09-22 12:33:20 -07001168 gpr_mu_lock(&server->mu_call);
Craig Tillercae4b1b2016-05-10 09:11:09 -07001169 kill_pending_work_locked(&exec_ctx, server,
1170 GRPC_ERROR_CREATE("Server Shutdown"));
Craig Tillera82950e2015-09-22 12:33:20 -07001171 gpr_mu_unlock(&server->mu_call);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001172
Craig Tillera82950e2015-09-22 12:33:20 -07001173 maybe_finish_shutdown(&exec_ctx, server);
1174 gpr_mu_unlock(&server->mu_global);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001175
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001176 /* Shutdown listeners */
Craig Tillera82950e2015-09-22 12:33:20 -07001177 for (l = server->listeners; l; l = l->next) {
1178 grpc_closure_init(&l->destroy_done, listener_destroy_done, server);
1179 l->destroy(&exec_ctx, server, l->arg, &l->destroy_done);
1180 }
Craig Tillerff3ae682015-06-29 17:44:04 -07001181
Craig Tillera82950e2015-09-22 12:33:20 -07001182 channel_broadcaster_shutdown(&exec_ctx, &broadcaster, 1, 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001183
Craig Tillerdfff1b82015-09-21 14:39:57 -07001184done:
Craig Tillera82950e2015-09-22 12:33:20 -07001185 grpc_exec_ctx_finish(&exec_ctx);
Craig Tilleraec96aa2015-04-07 14:32:15 -07001186}
1187
Craig Tillera82950e2015-09-22 12:33:20 -07001188void grpc_server_cancel_all_calls(grpc_server *server) {
Craig Tiller092d8d12015-07-04 22:35:00 -07001189 channel_broadcaster broadcaster;
Craig Tillerf5768a62015-09-22 10:54:34 -07001190 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillerafa2d632015-05-26 16:39:13 -07001191
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001192 GRPC_API_TRACE("grpc_server_cancel_all_calls(server=%p)", 1, (server));
1193
Craig Tillera82950e2015-09-22 12:33:20 -07001194 gpr_mu_lock(&server->mu_global);
1195 channel_broadcaster_init(server, &broadcaster);
1196 gpr_mu_unlock(&server->mu_global);
Craig Tillerafa2d632015-05-26 16:39:13 -07001197
Craig Tiller804ff712016-05-05 16:25:40 -07001198 channel_broadcaster_shutdown(&exec_ctx, &broadcaster, 0,
1199 GRPC_ERROR_CREATE("Cancelling all calls"));
Craig Tillera82950e2015-09-22 12:33:20 -07001200 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillerafa2d632015-05-26 16:39:13 -07001201}
1202
Craig Tillera82950e2015-09-22 12:33:20 -07001203void grpc_server_destroy(grpc_server *server) {
Craig Tilleraec96aa2015-04-07 14:32:15 -07001204 listener *l;
Craig Tillerf5768a62015-09-22 10:54:34 -07001205 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tiller872af022015-04-24 15:57:52 -07001206
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001207 GRPC_API_TRACE("grpc_server_destroy(server=%p)", 1, (server));
1208
Craig Tillera82950e2015-09-22 12:33:20 -07001209 gpr_mu_lock(&server->mu_global);
1210 GPR_ASSERT(gpr_atm_acq_load(&server->shutdown_flag) || !server->listeners);
1211 GPR_ASSERT(server->listeners_destroyed == num_listeners(server));
Craig Tilleraec96aa2015-04-07 14:32:15 -07001212
Craig Tillera82950e2015-09-22 12:33:20 -07001213 while (server->listeners) {
1214 l = server->listeners;
1215 server->listeners = l->next;
1216 gpr_free(l);
1217 }
Craig Tilleraec96aa2015-04-07 14:32:15 -07001218
Craig Tillera82950e2015-09-22 12:33:20 -07001219 gpr_mu_unlock(&server->mu_global);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001220
Craig Tillera82950e2015-09-22 12:33:20 -07001221 server_unref(&exec_ctx, server);
1222 grpc_exec_ctx_finish(&exec_ctx);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001223}
1224
Craig Tillera82950e2015-09-22 12:33:20 -07001225void grpc_server_add_listener(
1226 grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg,
1227 void (*start)(grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg,
1228 grpc_pollset **pollsets, size_t pollset_count),
1229 void (*destroy)(grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg,
1230 grpc_closure *on_done)) {
1231 listener *l = gpr_malloc(sizeof(listener));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001232 l->arg = arg;
1233 l->start = start;
1234 l->destroy = destroy;
1235 l->next = server->listeners;
1236 server->listeners = l;
1237}
1238
Craig Tillera82950e2015-09-22 12:33:20 -07001239static grpc_call_error queue_call_request(grpc_exec_ctx *exec_ctx,
1240 grpc_server *server,
1241 requested_call *rc) {
Yang Gaoeb8e7cd2015-02-11 11:43:40 -08001242 call_data *calld = NULL;
Craig Tillerb9d35962015-09-11 13:31:16 -07001243 request_matcher *rm = NULL;
Craig Tiller6a006ce2015-07-13 16:25:40 -07001244 int request_id;
Craig Tillera82950e2015-09-22 12:33:20 -07001245 if (gpr_atm_acq_load(&server->shutdown_flag)) {
Craig Tillercae4b1b2016-05-10 09:11:09 -07001246 fail_call(exec_ctx, server, rc, GRPC_ERROR_CREATE("Server Shutdown"));
Craig Tillera82950e2015-09-22 12:33:20 -07001247 return GRPC_CALL_OK;
1248 }
1249 request_id = gpr_stack_lockfree_pop(server->request_freelist);
1250 if (request_id == -1) {
1251 /* out of request ids: just fail this one */
Craig Tillercae4b1b2016-05-10 09:11:09 -07001252 fail_call(exec_ctx, server, rc, GRPC_ERROR_CREATE("Server Shutdown"));
Craig Tillera82950e2015-09-22 12:33:20 -07001253 return GRPC_CALL_OK;
1254 }
1255 switch (rc->type) {
Craig Tiller04cc8be2015-02-10 16:11:22 -08001256 case BATCH_CALL:
Craig Tillerb9d35962015-09-11 13:31:16 -07001257 rm = &server->unregistered_request_matcher;
Craig Tiller04cc8be2015-02-10 16:11:22 -08001258 break;
1259 case REGISTERED_CALL:
Craig Tillerb9d35962015-09-11 13:31:16 -07001260 rm = &rc->data.registered.registered_method->request_matcher;
Craig Tiller04cc8be2015-02-10 16:11:22 -08001261 break;
Craig Tillera82950e2015-09-22 12:33:20 -07001262 }
Craig Tiller45724b32015-09-22 10:42:19 -07001263 server->requested_calls[request_id] = *rc;
Craig Tillera82950e2015-09-22 12:33:20 -07001264 gpr_free(rc);
Craig Tillerb9d35962015-09-11 13:31:16 -07001265 if (gpr_stack_lockfree_push(rm->requests, request_id)) {
Craig Tillera82950e2015-09-22 12:33:20 -07001266 /* this was the first queued request: we need to lock and start
1267 matching calls */
1268 gpr_mu_lock(&server->mu_call);
Craig Tillerb9d35962015-09-11 13:31:16 -07001269 while ((calld = rm->pending_head) != NULL) {
1270 request_id = gpr_stack_lockfree_pop(rm->requests);
Craig Tillera82950e2015-09-22 12:33:20 -07001271 if (request_id == -1) break;
Craig Tillerb9d35962015-09-11 13:31:16 -07001272 rm->pending_head = calld->pending_next;
Craig Tillera82950e2015-09-22 12:33:20 -07001273 gpr_mu_unlock(&server->mu_call);
1274 gpr_mu_lock(&calld->mu_state);
1275 if (calld->state == ZOMBIED) {
1276 gpr_mu_unlock(&calld->mu_state);
1277 grpc_closure_init(
1278 &calld->kill_zombie_closure, kill_zombie,
1279 grpc_call_stack_element(grpc_call_get_call_stack(calld->call), 0));
Craig Tillerf51457b2016-05-03 17:06:32 -07001280 grpc_exec_ctx_push(exec_ctx, &calld->kill_zombie_closure,
1281 GRPC_ERROR_NONE, NULL);
Craig Tillera82950e2015-09-22 12:33:20 -07001282 } else {
1283 GPR_ASSERT(calld->state == PENDING);
1284 calld->state = ACTIVATED;
1285 gpr_mu_unlock(&calld->mu_state);
Craig Tiller88512692016-04-04 09:32:52 -07001286 publish_call(exec_ctx, server, calld,
1287 &server->requested_calls[request_id]);
Craig Tillera82950e2015-09-22 12:33:20 -07001288 }
1289 gpr_mu_lock(&server->mu_call);
Craig Tiller45724b32015-09-22 10:42:19 -07001290 }
Craig Tillera82950e2015-09-22 12:33:20 -07001291 gpr_mu_unlock(&server->mu_call);
1292 }
Craig Tiller6a006ce2015-07-13 16:25:40 -07001293 return GRPC_CALL_OK;
Craig Tillercce17ac2015-01-20 09:29:28 -08001294}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001295
Craig Tillera82950e2015-09-22 12:33:20 -07001296grpc_call_error grpc_server_request_call(
1297 grpc_server *server, grpc_call **call, grpc_call_details *details,
1298 grpc_metadata_array *initial_metadata,
1299 grpc_completion_queue *cq_bound_to_call,
1300 grpc_completion_queue *cq_for_notification, void *tag) {
Craig Tillerdfff1b82015-09-21 14:39:57 -07001301 grpc_call_error error;
Craig Tillerf5768a62015-09-22 10:54:34 -07001302 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillera82950e2015-09-22 12:33:20 -07001303 requested_call *rc = gpr_malloc(sizeof(*rc));
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001304 GRPC_API_TRACE(
1305 "grpc_server_request_call("
Craig Tiller4de3e4f2015-10-05 08:55:50 -07001306 "server=%p, call=%p, details=%p, initial_metadata=%p, "
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001307 "cq_bound_to_call=%p, cq_for_notification=%p, tag=%p)",
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001308 7, (server, call, details, initial_metadata, cq_bound_to_call,
1309 cq_for_notification, tag));
Craig Tillera82950e2015-09-22 12:33:20 -07001310 if (!grpc_cq_is_server_cq(cq_for_notification)) {
1311 gpr_free(rc);
1312 error = GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE;
1313 goto done;
1314 }
Craig Tiller4bf29282015-12-14 11:25:48 -08001315 grpc_cq_begin_op(cq_for_notification, tag);
Craig Tiller9928d392015-08-18 09:40:24 -07001316 details->reserved = NULL;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001317 rc->type = BATCH_CALL;
Craig Tiller6a006ce2015-07-13 16:25:40 -07001318 rc->server = server;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001319 rc->tag = tag;
1320 rc->cq_bound_to_call = cq_bound_to_call;
1321 rc->cq_for_notification = cq_for_notification;
1322 rc->call = call;
1323 rc->data.batch.details = details;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001324 rc->initial_metadata = initial_metadata;
Craig Tillera82950e2015-09-22 12:33:20 -07001325 error = queue_call_request(&exec_ctx, server, rc);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001326done:
Craig Tillera82950e2015-09-22 12:33:20 -07001327 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001328 return error;
Craig Tiller24be0f72015-02-10 14:04:22 -08001329}
1330
Craig Tillera82950e2015-09-22 12:33:20 -07001331grpc_call_error grpc_server_request_registered_call(
Craig Tillerb9d35962015-09-11 13:31:16 -07001332 grpc_server *server, void *rmp, grpc_call **call, gpr_timespec *deadline,
Craig Tillera82950e2015-09-22 12:33:20 -07001333 grpc_metadata_array *initial_metadata, grpc_byte_buffer **optional_payload,
1334 grpc_completion_queue *cq_bound_to_call,
1335 grpc_completion_queue *cq_for_notification, void *tag) {
Craig Tillerdfff1b82015-09-21 14:39:57 -07001336 grpc_call_error error;
Craig Tillerf5768a62015-09-22 10:54:34 -07001337 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillera82950e2015-09-22 12:33:20 -07001338 requested_call *rc = gpr_malloc(sizeof(*rc));
Craig Tillerb9d35962015-09-11 13:31:16 -07001339 registered_method *rm = rmp;
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001340 GRPC_API_TRACE(
1341 "grpc_server_request_registered_call("
Craig Tiller4de3e4f2015-10-05 08:55:50 -07001342 "server=%p, rmp=%p, call=%p, deadline=%p, initial_metadata=%p, "
1343 "optional_payload=%p, cq_bound_to_call=%p, cq_for_notification=%p, "
1344 "tag=%p)",
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001345 9, (server, rmp, call, deadline, initial_metadata, optional_payload,
1346 cq_bound_to_call, cq_for_notification, tag));
Craig Tillera82950e2015-09-22 12:33:20 -07001347 if (!grpc_cq_is_server_cq(cq_for_notification)) {
1348 gpr_free(rc);
1349 error = GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE;
1350 goto done;
1351 }
Craig Tiller06cb1a92016-04-04 08:10:47 -07001352 if ((optional_payload == NULL) !=
1353 (rm->payload_handling == GRPC_SRM_PAYLOAD_NONE)) {
1354 gpr_free(rc);
1355 error = GRPC_CALL_ERROR_PAYLOAD_TYPE_MISMATCH;
1356 goto done;
1357 }
Craig Tiller4bf29282015-12-14 11:25:48 -08001358 grpc_cq_begin_op(cq_for_notification, tag);
Craig Tiller97fc6a32015-07-08 15:31:35 -07001359 rc->type = REGISTERED_CALL;
Craig Tiller6a006ce2015-07-13 16:25:40 -07001360 rc->server = server;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001361 rc->tag = tag;
1362 rc->cq_bound_to_call = cq_bound_to_call;
1363 rc->cq_for_notification = cq_for_notification;
1364 rc->call = call;
Craig Tillerb9d35962015-09-11 13:31:16 -07001365 rc->data.registered.registered_method = rm;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001366 rc->data.registered.deadline = deadline;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001367 rc->initial_metadata = initial_metadata;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001368 rc->data.registered.optional_payload = optional_payload;
Craig Tillera82950e2015-09-22 12:33:20 -07001369 error = queue_call_request(&exec_ctx, server, rc);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001370done:
Craig Tillera82950e2015-09-22 12:33:20 -07001371 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001372 return error;
Craig Tiller24be0f72015-02-10 14:04:22 -08001373}
1374
Craig Tillera82950e2015-09-22 12:33:20 -07001375static void fail_call(grpc_exec_ctx *exec_ctx, grpc_server *server,
Craig Tillercae4b1b2016-05-10 09:11:09 -07001376 requested_call *rc, grpc_error *error) {
Craig Tillerf9e6adf2015-05-06 11:45:59 -07001377 *rc->call = NULL;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001378 rc->initial_metadata->count = 0;
Craig Tillercae4b1b2016-05-10 09:11:09 -07001379 GPR_ASSERT(error != GRPC_ERROR_NONE);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001380
Craig Tillera82950e2015-09-22 12:33:20 -07001381 server_ref(server);
Craig Tillercae4b1b2016-05-10 09:11:09 -07001382 grpc_cq_end_op(exec_ctx, rc->cq_for_notification, rc->tag, error,
Craig Tillera82950e2015-09-22 12:33:20 -07001383 done_request_event, rc, &rc->completion);
Craig Tiller24be0f72015-02-10 14:04:22 -08001384}
1385
Craig Tillera82950e2015-09-22 12:33:20 -07001386const grpc_channel_args *grpc_server_get_channel_args(grpc_server *server) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001387 return server->channel_args;
Craig Tiller190d3602015-02-18 09:23:38 -08001388}
Craig Tillerba3c3cd2015-05-26 06:28:10 -07001389
Craig Tillera82950e2015-09-22 12:33:20 -07001390int grpc_server_has_open_connections(grpc_server *server) {
Craig Tillerba3c3cd2015-05-26 06:28:10 -07001391 int r;
Craig Tillera82950e2015-09-22 12:33:20 -07001392 gpr_mu_lock(&server->mu_global);
Craig Tillerba3c3cd2015-05-26 06:28:10 -07001393 r = server->root_channel_data.next != &server->root_channel_data;
Craig Tillera82950e2015-09-22 12:33:20 -07001394 gpr_mu_unlock(&server->mu_global);
Craig Tillerba3c3cd2015-05-26 06:28:10 -07001395 return r;
1396}