Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(253)

Side by Side Diff: third_party/grpc/src/core/surface/channel.c

Issue 1932353002: Initial checkin of gRPC to third_party/ Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 *
3 * Copyright 2015-2016, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34 #include "src/core/surface/channel.h"
35
36 #include <stdlib.h>
37 #include <string.h>
38
39 #include <grpc/support/alloc.h>
40 #include <grpc/support/log.h>
41 #include <grpc/support/string_util.h>
42
43 #include "src/core/client_config/resolver_registry.h"
44 #include "src/core/iomgr/iomgr.h"
45 #include "src/core/support/string.h"
46 #include "src/core/surface/api_trace.h"
47 #include "src/core/surface/call.h"
48 #include "src/core/surface/init.h"
49 #include "src/core/transport/static_metadata.h"
50
51 /** Cache grpc-status: X mdelems for X = 0..NUM_CACHED_STATUS_ELEMS.
52 * Avoids needing to take a metadata context lock for sending status
53 * if the status code is <= NUM_CACHED_STATUS_ELEMS.
54 * Sized to allow the most commonly used codes to fit in
55 * (OK, Cancelled, Unknown). */
56 #define NUM_CACHED_STATUS_ELEMS 3
57
58 typedef struct registered_call {
59 grpc_mdelem *path;
60 grpc_mdelem *authority;
61 struct registered_call *next;
62 } registered_call;
63
64 struct grpc_channel {
65 int is_client;
66 uint32_t max_message_length;
67 grpc_mdelem *default_authority;
68
69 gpr_mu registered_call_mu;
70 registered_call *registered_calls;
71 char *target;
72 };
73
74 #define CHANNEL_STACK_FROM_CHANNEL(c) ((grpc_channel_stack *)((c) + 1))
75 #define CHANNEL_FROM_CHANNEL_STACK(channel_stack) \
76 (((grpc_channel *)(channel_stack)) - 1)
77 #define CHANNEL_FROM_TOP_ELEM(top_elem) \
78 CHANNEL_FROM_CHANNEL_STACK(grpc_channel_stack_from_top_element(top_elem))
79
80 /* the protobuf library will (by default) start warning at 100megs */
81 #define DEFAULT_MAX_MESSAGE_LENGTH (100 * 1024 * 1024)
82
83 static void destroy_channel(grpc_exec_ctx *exec_ctx, void *arg, bool success);
84
85 grpc_channel *grpc_channel_create_from_filters(
86 grpc_exec_ctx *exec_ctx, const char *target,
87 const grpc_channel_filter **filters, size_t num_filters,
88 const grpc_channel_args *args, int is_client) {
89 size_t i;
90 size_t size =
91 sizeof(grpc_channel) + grpc_channel_stack_size(filters, num_filters);
92 grpc_channel *channel = gpr_malloc(size);
93 memset(channel, 0, sizeof(*channel));
94 channel->target = gpr_strdup(target);
95 GPR_ASSERT(grpc_is_initialized() && "call grpc_init()");
96 channel->is_client = is_client;
97 gpr_mu_init(&channel->registered_call_mu);
98 channel->registered_calls = NULL;
99
100 channel->max_message_length = DEFAULT_MAX_MESSAGE_LENGTH;
101 if (args) {
102 for (i = 0; i < args->num_args; i++) {
103 if (0 == strcmp(args->args[i].key, GRPC_ARG_MAX_MESSAGE_LENGTH)) {
104 if (args->args[i].type != GRPC_ARG_INTEGER) {
105 gpr_log(GPR_ERROR, "%s ignored: it must be an integer",
106 GRPC_ARG_MAX_MESSAGE_LENGTH);
107 } else if (args->args[i].value.integer < 0) {
108 gpr_log(GPR_ERROR, "%s ignored: it must be >= 0",
109 GRPC_ARG_MAX_MESSAGE_LENGTH);
110 } else {
111 channel->max_message_length = (uint32_t)args->args[i].value.integer;
112 }
113 } else if (0 == strcmp(args->args[i].key, GRPC_ARG_DEFAULT_AUTHORITY)) {
114 if (args->args[i].type != GRPC_ARG_STRING) {
115 gpr_log(GPR_ERROR, "%s ignored: it must be a string",
116 GRPC_ARG_DEFAULT_AUTHORITY);
117 } else {
118 if (channel->default_authority) {
119 /* setting this takes precedence over anything else */
120 GRPC_MDELEM_UNREF(channel->default_authority);
121 }
122 channel->default_authority = grpc_mdelem_from_strings(
123 ":authority", args->args[i].value.string);
124 }
125 } else if (0 ==
126 strcmp(args->args[i].key, GRPC_SSL_TARGET_NAME_OVERRIDE_ARG)) {
127 if (args->args[i].type != GRPC_ARG_STRING) {
128 gpr_log(GPR_ERROR, "%s ignored: it must be a string",
129 GRPC_SSL_TARGET_NAME_OVERRIDE_ARG);
130 } else {
131 if (channel->default_authority) {
132 /* other ways of setting this (notably ssl) take precedence */
133 gpr_log(GPR_ERROR,
134 "%s ignored: default host already set some other way",
135 GRPC_SSL_TARGET_NAME_OVERRIDE_ARG);
136 } else {
137 channel->default_authority = grpc_mdelem_from_strings(
138 ":authority", args->args[i].value.string);
139 }
140 }
141 }
142 }
143 }
144
145 if (channel->is_client && channel->default_authority == NULL &&
146 target != NULL) {
147 char *default_authority = grpc_get_default_authority(target);
148 if (default_authority) {
149 channel->default_authority =
150 grpc_mdelem_from_strings(":authority", default_authority);
151 }
152 gpr_free(default_authority);
153 }
154
155 grpc_channel_stack_init(exec_ctx, 1, destroy_channel, channel, filters,
156 num_filters, args,
157 is_client ? "CLIENT_CHANNEL" : "SERVER_CHANNEL",
158 CHANNEL_STACK_FROM_CHANNEL(channel));
159
160 return channel;
161 }
162
163 char *grpc_channel_get_target(grpc_channel *channel) {
164 GRPC_API_TRACE("grpc_channel_get_target(channel=%p)", 1, (channel));
165 return gpr_strdup(channel->target);
166 }
167
168 static grpc_call *grpc_channel_create_call_internal(
169 grpc_channel *channel, grpc_call *parent_call, uint32_t propagation_mask,
170 grpc_completion_queue *cq, grpc_mdelem *path_mdelem,
171 grpc_mdelem *authority_mdelem, gpr_timespec deadline) {
172 grpc_mdelem *send_metadata[2];
173 size_t num_metadata = 0;
174
175 GPR_ASSERT(channel->is_client);
176
177 send_metadata[num_metadata++] = path_mdelem;
178 if (authority_mdelem != NULL) {
179 send_metadata[num_metadata++] = authority_mdelem;
180 } else if (channel->default_authority != NULL) {
181 send_metadata[num_metadata++] = GRPC_MDELEM_REF(channel->default_authority);
182 }
183
184 return grpc_call_create(channel, parent_call, propagation_mask, cq, NULL,
185 send_metadata, num_metadata, deadline);
186 }
187
188 grpc_call *grpc_channel_create_call(grpc_channel *channel,
189 grpc_call *parent_call,
190 uint32_t propagation_mask,
191 grpc_completion_queue *cq,
192 const char *method, const char *host,
193 gpr_timespec deadline, void *reserved) {
194 GRPC_API_TRACE(
195 "grpc_channel_create_call("
196 "channel=%p, parent_call=%p, propagation_mask=%x, cq=%p, method=%s, "
197 "host=%s, "
198 "deadline=gpr_timespec { tv_sec: %lld, tv_nsec: %d, clock_type: %d }, "
199 "reserved=%p)",
200 10, (channel, parent_call, (unsigned)propagation_mask, cq, method, host,
201 (long long)deadline.tv_sec, (int)deadline.tv_nsec,
202 (int)deadline.clock_type, reserved));
203 GPR_ASSERT(!reserved);
204 return grpc_channel_create_call_internal(
205 channel, parent_call, propagation_mask, cq,
206 grpc_mdelem_from_metadata_strings(GRPC_MDSTR_PATH,
207 grpc_mdstr_from_string(method)),
208 host ? grpc_mdelem_from_metadata_strings(GRPC_MDSTR_AUTHORITY,
209 grpc_mdstr_from_string(host))
210 : NULL,
211 deadline);
212 }
213
214 void *grpc_channel_register_call(grpc_channel *channel, const char *method,
215 const char *host, void *reserved) {
216 registered_call *rc = gpr_malloc(sizeof(registered_call));
217 GRPC_API_TRACE(
218 "grpc_channel_register_call(channel=%p, method=%s, host=%s, reserved=%p)",
219 4, (channel, method, host, reserved));
220 GPR_ASSERT(!reserved);
221 rc->path = grpc_mdelem_from_metadata_strings(GRPC_MDSTR_PATH,
222 grpc_mdstr_from_string(method));
223 rc->authority = host ? grpc_mdelem_from_metadata_strings(
224 GRPC_MDSTR_AUTHORITY, grpc_mdstr_from_string(host))
225 : NULL;
226 gpr_mu_lock(&channel->registered_call_mu);
227 rc->next = channel->registered_calls;
228 channel->registered_calls = rc;
229 gpr_mu_unlock(&channel->registered_call_mu);
230 return rc;
231 }
232
233 grpc_call *grpc_channel_create_registered_call(
234 grpc_channel *channel, grpc_call *parent_call, uint32_t propagation_mask,
235 grpc_completion_queue *completion_queue, void *registered_call_handle,
236 gpr_timespec deadline, void *reserved) {
237 registered_call *rc = registered_call_handle;
238 GRPC_API_TRACE(
239 "grpc_channel_create_registered_call("
240 "channel=%p, parent_call=%p, propagation_mask=%x, completion_queue=%p, "
241 "registered_call_handle=%p, "
242 "deadline=gpr_timespec { tv_sec: %lld, tv_nsec: %d, clock_type: %d }, "
243 "reserved=%p)",
244 9, (channel, parent_call, (unsigned)propagation_mask, completion_queue,
245 registered_call_handle, (long long)deadline.tv_sec,
246 (int)deadline.tv_nsec, (int)deadline.clock_type, reserved));
247 GPR_ASSERT(!reserved);
248 return grpc_channel_create_call_internal(
249 channel, parent_call, propagation_mask, completion_queue,
250 GRPC_MDELEM_REF(rc->path),
251 rc->authority ? GRPC_MDELEM_REF(rc->authority) : NULL, deadline);
252 }
253
254 #ifdef GRPC_STREAM_REFCOUNT_DEBUG
255 #define REF_REASON reason
256 #define REF_ARG , const char *reason
257 #else
258 #define REF_REASON ""
259 #define REF_ARG
260 #endif
261 void grpc_channel_internal_ref(grpc_channel *c REF_ARG) {
262 GRPC_CHANNEL_STACK_REF(CHANNEL_STACK_FROM_CHANNEL(c), REF_REASON);
263 }
264
265 void grpc_channel_internal_unref(grpc_exec_ctx *exec_ctx,
266 grpc_channel *c REF_ARG) {
267 GRPC_CHANNEL_STACK_UNREF(exec_ctx, CHANNEL_STACK_FROM_CHANNEL(c), REF_REASON);
268 }
269
270 static void destroy_channel(grpc_exec_ctx *exec_ctx, void *arg,
271 bool iomgr_success) {
272 grpc_channel *channel = arg;
273 grpc_channel_stack_destroy(exec_ctx, CHANNEL_STACK_FROM_CHANNEL(channel));
274 while (channel->registered_calls) {
275 registered_call *rc = channel->registered_calls;
276 channel->registered_calls = rc->next;
277 GRPC_MDELEM_UNREF(rc->path);
278 if (rc->authority) {
279 GRPC_MDELEM_UNREF(rc->authority);
280 }
281 gpr_free(rc);
282 }
283 if (channel->default_authority != NULL) {
284 GRPC_MDELEM_UNREF(channel->default_authority);
285 }
286 gpr_mu_destroy(&channel->registered_call_mu);
287 gpr_free(channel->target);
288 gpr_free(channel);
289 }
290
291 void grpc_channel_destroy(grpc_channel *channel) {
292 grpc_transport_op op;
293 grpc_channel_element *elem;
294 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
295 GRPC_API_TRACE("grpc_channel_destroy(channel=%p)", 1, (channel));
296 memset(&op, 0, sizeof(op));
297 op.disconnect = 1;
298 elem = grpc_channel_stack_element(CHANNEL_STACK_FROM_CHANNEL(channel), 0);
299 elem->filter->start_transport_op(&exec_ctx, elem, &op);
300
301 GRPC_CHANNEL_INTERNAL_UNREF(&exec_ctx, channel, "channel");
302
303 grpc_exec_ctx_finish(&exec_ctx);
304 }
305
306 grpc_channel_stack *grpc_channel_get_channel_stack(grpc_channel *channel) {
307 return CHANNEL_STACK_FROM_CHANNEL(channel);
308 }
309
310 grpc_mdelem *grpc_channel_get_reffed_status_elem(grpc_channel *channel, int i) {
311 char tmp[GPR_LTOA_MIN_BUFSIZE];
312 switch (i) {
313 case 0:
314 return GRPC_MDELEM_GRPC_STATUS_0;
315 case 1:
316 return GRPC_MDELEM_GRPC_STATUS_1;
317 case 2:
318 return GRPC_MDELEM_GRPC_STATUS_2;
319 }
320 gpr_ltoa(i, tmp);
321 return grpc_mdelem_from_metadata_strings(GRPC_MDSTR_GRPC_STATUS,
322 grpc_mdstr_from_string(tmp));
323 }
324
325 uint32_t grpc_channel_get_max_message_length(grpc_channel *channel) {
326 return channel->max_message_length;
327 }
OLDNEW
« no previous file with comments | « third_party/grpc/src/core/surface/channel.h ('k') | third_party/grpc/src/core/surface/channel_connectivity.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698