Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 19 | #include "src/core/lib/surface/channel.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 20 | |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | |
David Garcia Quintas | 73dcbda | 2016-04-23 00:17:05 -0700 | [diff] [blame] | 24 | #include <grpc/compression.h> |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 25 | #include <grpc/support/alloc.h> |
| 26 | #include <grpc/support/log.h> |
| 27 | #include <grpc/support/string_util.h> |
| 28 | |
David Garcia Quintas | 73dcbda | 2016-04-23 00:17:05 -0700 | [diff] [blame] | 29 | #include "src/core/lib/channel/channel_args.h" |
Craig Tiller | f1dc9c3 | 2017-09-13 14:21:27 -0700 | [diff] [blame] | 30 | #include "src/core/lib/debug/stats.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 31 | #include "src/core/lib/iomgr/iomgr.h" |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 32 | #include "src/core/lib/slice/slice_internal.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 33 | #include "src/core/lib/support/string.h" |
| 34 | #include "src/core/lib/surface/api_trace.h" |
| 35 | #include "src/core/lib/surface/call.h" |
| 36 | #include "src/core/lib/surface/channel_init.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 37 | #include "src/core/lib/transport/static_metadata.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 38 | |
Craig Tiller | 5d5bd22 | 2015-06-08 17:01:49 -0700 | [diff] [blame] | 39 | /** Cache grpc-status: X mdelems for X = 0..NUM_CACHED_STATUS_ELEMS. |
| 40 | * Avoids needing to take a metadata context lock for sending status |
| 41 | * if the status code is <= NUM_CACHED_STATUS_ELEMS. |
| 42 | * Sized to allow the most commonly used codes to fit in |
| 43 | * (OK, Cancelled, Unknown). */ |
Craig Tiller | 3fc8e82 | 2015-06-08 16:31:28 -0700 | [diff] [blame] | 44 | #define NUM_CACHED_STATUS_ELEMS 3 |
| 45 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 46 | typedef struct registered_call { |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 47 | grpc_mdelem path; |
| 48 | grpc_mdelem authority; |
Craig Tiller | 0845337 | 2015-04-10 16:05:38 -0700 | [diff] [blame] | 49 | struct registered_call *next; |
| 50 | } registered_call; |
| 51 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 52 | struct grpc_channel { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 53 | int is_client; |
David Garcia Quintas | 73dcbda | 2016-04-23 00:17:05 -0700 | [diff] [blame] | 54 | grpc_compression_options compression_options; |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 55 | grpc_mdelem default_authority; |
Craig Tiller | 0845337 | 2015-04-10 16:05:38 -0700 | [diff] [blame] | 56 | |
Craig Tiller | a6bec8f | 2017-03-14 08:26:04 -0700 | [diff] [blame] | 57 | gpr_atm call_size_estimate; |
| 58 | |
Craig Tiller | 0845337 | 2015-04-10 16:05:38 -0700 | [diff] [blame] | 59 | gpr_mu registered_call_mu; |
| 60 | registered_call *registered_calls; |
David Garcia Quintas | b0dd253 | 2016-05-17 19:14:51 -0700 | [diff] [blame] | 61 | |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 62 | char *target; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 63 | }; |
| 64 | |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 65 | #define CHANNEL_STACK_FROM_CHANNEL(c) ((grpc_channel_stack *)((c) + 1)) |
Craig Tiller | b20111c | 2015-04-10 23:27:11 +0000 | [diff] [blame] | 66 | #define CHANNEL_FROM_CHANNEL_STACK(channel_stack) \ |
| 67 | (((grpc_channel *)(channel_stack)) - 1) |
Craig Tiller | da66937 | 2015-02-05 10:10:15 -0800 | [diff] [blame] | 68 | #define CHANNEL_FROM_TOP_ELEM(top_elem) \ |
| 69 | CHANNEL_FROM_CHANNEL_STACK(grpc_channel_stack_from_top_element(top_elem)) |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 70 | |
Craig Tiller | f51457b | 2016-05-03 17:06:32 -0700 | [diff] [blame] | 71 | static void destroy_channel(grpc_exec_ctx *exec_ctx, void *arg, |
| 72 | grpc_error *error); |
Craig Tiller | 7b43561 | 2015-11-24 08:15:05 -0800 | [diff] [blame] | 73 | |
Craig Tiller | b7f35a6 | 2017-03-16 13:29:05 -0700 | [diff] [blame] | 74 | grpc_channel *grpc_channel_create_with_builder( |
| 75 | grpc_exec_ctx *exec_ctx, grpc_channel_stack_builder *builder, |
| 76 | grpc_channel_stack_type channel_stack_type) { |
| 77 | char *target = gpr_strdup(grpc_channel_stack_builder_get_target(builder)); |
Mark D. Roth | e62605f | 2016-11-29 16:31:36 +0000 | [diff] [blame] | 78 | grpc_channel_args *args = grpc_channel_args_copy( |
Mark D. Roth | 5e2566e | 2016-11-18 10:53:13 -0800 | [diff] [blame] | 79 | grpc_channel_stack_builder_get_channel_arguments(builder)); |
Mark D. Roth | e62605f | 2016-11-29 16:31:36 +0000 | [diff] [blame] | 80 | grpc_channel *channel; |
Craig Tiller | f1dc9c3 | 2017-09-13 14:21:27 -0700 | [diff] [blame] | 81 | if (channel_stack_type == GRPC_SERVER_CHANNEL) { |
| 82 | GRPC_STATS_INC_SERVER_CHANNELS_CREATED(exec_ctx); |
| 83 | } else { |
| 84 | GRPC_STATS_INC_CLIENT_CHANNELS_CREATED(exec_ctx); |
| 85 | } |
Mark D. Roth | c108788 | 2016-11-18 10:54:45 -0800 | [diff] [blame] | 86 | grpc_error *error = grpc_channel_stack_builder_finish( |
Mark D. Roth | 5e2566e | 2016-11-18 10:53:13 -0800 | [diff] [blame] | 87 | exec_ctx, builder, sizeof(grpc_channel), 1, destroy_channel, NULL, |
Mark D. Roth | c108788 | 2016-11-18 10:54:45 -0800 | [diff] [blame] | 88 | (void **)&channel); |
Mark D. Roth | 5e2566e | 2016-11-18 10:53:13 -0800 | [diff] [blame] | 89 | if (error != GRPC_ERROR_NONE) { |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 90 | gpr_log(GPR_ERROR, "channel stack builder failed: %s", |
| 91 | grpc_error_string(error)); |
Mark D. Roth | e62605f | 2016-11-29 16:31:36 +0000 | [diff] [blame] | 92 | GRPC_ERROR_UNREF(error); |
Craig Tiller | b7f35a6 | 2017-03-16 13:29:05 -0700 | [diff] [blame] | 93 | gpr_free(target); |
Mark D. Roth | e62605f | 2016-11-29 16:31:36 +0000 | [diff] [blame] | 94 | goto done; |
Craig Tiller | 839bebe | 2016-04-06 08:07:11 -0700 | [diff] [blame] | 95 | } |
Craig Tiller | 178edfa | 2016-02-17 20:54:46 -0800 | [diff] [blame] | 96 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 97 | memset(channel, 0, sizeof(*channel)); |
Craig Tiller | b7f35a6 | 2017-03-16 13:29:05 -0700 | [diff] [blame] | 98 | channel->target = target; |
Mark D. Roth | e62605f | 2016-11-29 16:31:36 +0000 | [diff] [blame] | 99 | channel->is_client = grpc_channel_stack_type_is_client(channel_stack_type); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 100 | gpr_mu_init(&channel->registered_call_mu); |
Craig Tiller | 0845337 | 2015-04-10 16:05:38 -0700 | [diff] [blame] | 101 | channel->registered_calls = NULL; |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 102 | |
Craig Tiller | a6bec8f | 2017-03-14 08:26:04 -0700 | [diff] [blame] | 103 | gpr_atm_no_barrier_store( |
| 104 | &channel->call_size_estimate, |
Craig Tiller | 2ccd502 | 2017-03-16 09:57:46 -0700 | [diff] [blame] | 105 | (gpr_atm)CHANNEL_STACK_FROM_CHANNEL(channel)->call_stack_size); |
Craig Tiller | a6bec8f | 2017-03-14 08:26:04 -0700 | [diff] [blame] | 106 | |
David Garcia Quintas | 73dcbda | 2016-04-23 00:17:05 -0700 | [diff] [blame] | 107 | grpc_compression_options_init(&channel->compression_options); |
Mark D. Roth | e62605f | 2016-11-29 16:31:36 +0000 | [diff] [blame] | 108 | for (size_t i = 0; i < args->num_args; i++) { |
| 109 | if (0 == strcmp(args->args[i].key, GRPC_ARG_DEFAULT_AUTHORITY)) { |
| 110 | if (args->args[i].type != GRPC_ARG_STRING) { |
| 111 | gpr_log(GPR_ERROR, "%s ignored: it must be a string", |
| 112 | GRPC_ARG_DEFAULT_AUTHORITY); |
| 113 | } else { |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 114 | if (!GRPC_MDISNULL(channel->default_authority)) { |
Mark D. Roth | e62605f | 2016-11-29 16:31:36 +0000 | [diff] [blame] | 115 | /* setting this takes precedence over anything else */ |
Craig Tiller | 4cc1c35 | 2016-12-27 08:48:01 -0800 | [diff] [blame] | 116 | GRPC_MDELEM_UNREF(exec_ctx, channel->default_authority); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 117 | } |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 118 | channel->default_authority = grpc_mdelem_from_slices( |
| 119 | exec_ctx, GRPC_MDSTR_AUTHORITY, |
Craig Tiller | 5f4264f | 2017-02-08 14:53:04 -0800 | [diff] [blame] | 120 | grpc_slice_intern( |
| 121 | grpc_slice_from_static_string(args->args[i].value.string))); |
Mark D. Roth | e62605f | 2016-11-29 16:31:36 +0000 | [diff] [blame] | 122 | } |
| 123 | } else if (0 == |
| 124 | strcmp(args->args[i].key, GRPC_SSL_TARGET_NAME_OVERRIDE_ARG)) { |
| 125 | if (args->args[i].type != GRPC_ARG_STRING) { |
| 126 | gpr_log(GPR_ERROR, "%s ignored: it must be a string", |
| 127 | GRPC_SSL_TARGET_NAME_OVERRIDE_ARG); |
| 128 | } else { |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 129 | if (!GRPC_MDISNULL(channel->default_authority)) { |
Mark D. Roth | e62605f | 2016-11-29 16:31:36 +0000 | [diff] [blame] | 130 | /* other ways of setting this (notably ssl) take precedence */ |
| 131 | gpr_log(GPR_ERROR, |
| 132 | "%s ignored: default host already set some other way", |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 133 | GRPC_SSL_TARGET_NAME_OVERRIDE_ARG); |
| 134 | } else { |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 135 | channel->default_authority = grpc_mdelem_from_slices( |
| 136 | exec_ctx, GRPC_MDSTR_AUTHORITY, |
Craig Tiller | 5f4264f | 2017-02-08 14:53:04 -0800 | [diff] [blame] | 137 | grpc_slice_intern( |
| 138 | grpc_slice_from_static_string(args->args[i].value.string))); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
Mark D. Roth | e62605f | 2016-11-29 16:31:36 +0000 | [diff] [blame] | 141 | } else if (0 == strcmp(args->args[i].key, |
| 142 | GRPC_COMPRESSION_CHANNEL_DEFAULT_LEVEL)) { |
| 143 | channel->compression_options.default_level.is_set = true; |
Mark D. Roth | e62605f | 2016-11-29 16:31:36 +0000 | [diff] [blame] | 144 | channel->compression_options.default_level.level = |
David Garcia Quintas | 228a514 | 2017-03-30 19:43:00 -0700 | [diff] [blame] | 145 | (grpc_compression_level)grpc_channel_arg_get_integer( |
| 146 | &args->args[i], |
| 147 | (grpc_integer_options){GRPC_COMPRESS_LEVEL_NONE, |
| 148 | GRPC_COMPRESS_LEVEL_NONE, |
| 149 | GRPC_COMPRESS_LEVEL_COUNT - 1}); |
Mark D. Roth | e62605f | 2016-11-29 16:31:36 +0000 | [diff] [blame] | 150 | } else if (0 == strcmp(args->args[i].key, |
Muxi Yan | d37411e | 2017-08-17 18:44:12 -0700 | [diff] [blame] | 151 | GRPC_STREAM_COMPRESSION_CHANNEL_DEFAULT_LEVEL)) { |
| 152 | channel->compression_options.default_stream_compression_level.is_set = |
| 153 | true; |
| 154 | channel->compression_options.default_stream_compression_level.level = |
Muxi Yan | da4f087 | 2017-08-18 17:05:27 -0700 | [diff] [blame] | 155 | (grpc_stream_compression_level)grpc_channel_arg_get_integer( |
Muxi Yan | d37411e | 2017-08-17 18:44:12 -0700 | [diff] [blame] | 156 | &args->args[i], |
Muxi Yan | da4f087 | 2017-08-18 17:05:27 -0700 | [diff] [blame] | 157 | (grpc_integer_options){GRPC_STREAM_COMPRESS_LEVEL_NONE, |
| 158 | GRPC_STREAM_COMPRESS_LEVEL_NONE, |
| 159 | GRPC_STREAM_COMPRESS_LEVEL_COUNT - 1}); |
Muxi Yan | d37411e | 2017-08-17 18:44:12 -0700 | [diff] [blame] | 160 | } else if (0 == strcmp(args->args[i].key, |
Mark D. Roth | e62605f | 2016-11-29 16:31:36 +0000 | [diff] [blame] | 161 | GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM)) { |
| 162 | channel->compression_options.default_algorithm.is_set = true; |
Mark D. Roth | e62605f | 2016-11-29 16:31:36 +0000 | [diff] [blame] | 163 | channel->compression_options.default_algorithm.algorithm = |
David Garcia Quintas | 228a514 | 2017-03-30 19:43:00 -0700 | [diff] [blame] | 164 | (grpc_compression_algorithm)grpc_channel_arg_get_integer( |
| 165 | &args->args[i], |
| 166 | (grpc_integer_options){GRPC_COMPRESS_NONE, GRPC_COMPRESS_NONE, |
| 167 | GRPC_COMPRESS_ALGORITHMS_COUNT - 1}); |
Muxi Yan | 68a0fd5 | 2017-07-21 09:26:04 -0700 | [diff] [blame] | 168 | } else if (0 == strcmp(args->args[i].key, |
| 169 | GRPC_STREAM_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM)) { |
| 170 | channel->compression_options.default_stream_compression_algorithm.is_set = |
| 171 | true; |
| 172 | channel->compression_options.default_stream_compression_algorithm |
| 173 | .algorithm = |
| 174 | (grpc_stream_compression_algorithm)grpc_channel_arg_get_integer( |
| 175 | &args->args[i], |
| 176 | (grpc_integer_options){ |
| 177 | GRPC_STREAM_COMPRESS_NONE, GRPC_STREAM_COMPRESS_NONE, |
| 178 | GRPC_STREAM_COMPRESS_ALGORITHMS_COUNT - 1}); |
Mark D. Roth | e62605f | 2016-11-29 16:31:36 +0000 | [diff] [blame] | 179 | } else if (0 == |
| 180 | strcmp(args->args[i].key, |
| 181 | GRPC_COMPRESSION_CHANNEL_ENABLED_ALGORITHMS_BITSET)) { |
| 182 | channel->compression_options.enabled_algorithms_bitset = |
| 183 | (uint32_t)args->args[i].value.integer | |
| 184 | 0x1; /* always support no compression */ |
Muxi Yan | 68a0fd5 | 2017-07-21 09:26:04 -0700 | [diff] [blame] | 185 | } else if (0 == |
| 186 | strcmp( |
| 187 | args->args[i].key, |
| 188 | GRPC_STREAM_COMPRESSION_CHANNEL_ENABLED_ALGORITHMS_BITSET)) { |
| 189 | channel->compression_options |
| 190 | .enabled_stream_compression_algorithms_bitset = |
| 191 | (uint32_t)args->args[i].value.integer | |
| 192 | 0x1; /* always support no compression */ |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 193 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 194 | } |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 195 | |
Mark D. Roth | e62605f | 2016-11-29 16:31:36 +0000 | [diff] [blame] | 196 | done: |
Craig Tiller | 4cc1c35 | 2016-12-27 08:48:01 -0800 | [diff] [blame] | 197 | grpc_channel_args_destroy(exec_ctx, args); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 198 | return channel; |
| 199 | } |
| 200 | |
Craig Tiller | b7f35a6 | 2017-03-16 13:29:05 -0700 | [diff] [blame] | 201 | grpc_channel *grpc_channel_create(grpc_exec_ctx *exec_ctx, const char *target, |
| 202 | const grpc_channel_args *input_args, |
| 203 | grpc_channel_stack_type channel_stack_type, |
| 204 | grpc_transport *optional_transport) { |
| 205 | grpc_channel_stack_builder *builder = grpc_channel_stack_builder_create(); |
| 206 | grpc_channel_stack_builder_set_channel_arguments(exec_ctx, builder, |
| 207 | input_args); |
| 208 | grpc_channel_stack_builder_set_target(builder, target); |
| 209 | grpc_channel_stack_builder_set_transport(builder, optional_transport); |
| 210 | if (!grpc_channel_init_create_stack(exec_ctx, builder, channel_stack_type)) { |
| 211 | grpc_channel_stack_builder_destroy(exec_ctx, builder); |
| 212 | return NULL; |
| 213 | } |
| 214 | return grpc_channel_create_with_builder(exec_ctx, builder, |
| 215 | channel_stack_type); |
| 216 | } |
| 217 | |
Craig Tiller | a6bec8f | 2017-03-14 08:26:04 -0700 | [diff] [blame] | 218 | size_t grpc_channel_get_call_size_estimate(grpc_channel *channel) { |
| 219 | #define ROUND_UP_SIZE 256 |
Craig Tiller | 878a7c2 | 2017-03-23 15:14:48 -0700 | [diff] [blame] | 220 | /* We round up our current estimate to the NEXT value of ROUND_UP_SIZE. |
| 221 | This ensures: |
| 222 | 1. a consistent size allocation when our estimate is drifting slowly |
| 223 | (which is common) - which tends to help most allocators reuse memory |
| 224 | 2. a small amount of allowed growth over the estimate without hitting |
| 225 | the arena size doubling case, reducing overall memory usage */ |
Craig Tiller | b64d652 | 2017-03-23 15:17:18 -0700 | [diff] [blame] | 226 | return ((size_t)gpr_atm_no_barrier_load(&channel->call_size_estimate) + |
| 227 | 2 * ROUND_UP_SIZE) & |
| 228 | ~(size_t)(ROUND_UP_SIZE - 1); |
Craig Tiller | a6bec8f | 2017-03-14 08:26:04 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | void grpc_channel_update_call_size_estimate(grpc_channel *channel, |
| 232 | size_t size) { |
| 233 | size_t cur = (size_t)gpr_atm_no_barrier_load(&channel->call_size_estimate); |
| 234 | if (cur < size) { |
| 235 | /* size grew: update estimate */ |
| 236 | gpr_atm_no_barrier_cas(&channel->call_size_estimate, (gpr_atm)cur, |
| 237 | (gpr_atm)size); |
| 238 | /* if we lose: never mind, something else will likely update soon enough */ |
| 239 | } else if (cur == size) { |
| 240 | /* no change: holding pattern */ |
| 241 | } else if (cur > 0) { |
| 242 | /* size shrank: decrease estimate */ |
| 243 | gpr_atm_no_barrier_cas( |
| 244 | &channel->call_size_estimate, (gpr_atm)cur, |
| 245 | (gpr_atm)(GPR_MIN(cur - 1, (255 * cur + size) / 256))); |
| 246 | /* if we lose: never mind, something else will likely update soon enough */ |
| 247 | } |
| 248 | } |
| 249 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 250 | char *grpc_channel_get_target(grpc_channel *channel) { |
Masood Malekghassemi | 76c3d74 | 2015-08-19 18:22:53 -0700 | [diff] [blame] | 251 | GRPC_API_TRACE("grpc_channel_get_target(channel=%p)", 1, (channel)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 252 | return gpr_strdup(channel->target); |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Mark D. Roth | b2d2488 | 2016-10-27 15:44:07 -0700 | [diff] [blame] | 255 | void grpc_channel_get_info(grpc_channel *channel, |
Mark D. Roth | f79ce7d | 2016-11-04 08:43:36 -0700 | [diff] [blame] | 256 | const grpc_channel_info *channel_info) { |
Mark D. Roth | b2d2488 | 2016-10-27 15:44:07 -0700 | [diff] [blame] | 257 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 258 | grpc_channel_element *elem = |
| 259 | grpc_channel_stack_element(CHANNEL_STACK_FROM_CHANNEL(channel), 0); |
| 260 | elem->filter->get_channel_info(&exec_ctx, elem, channel_info); |
| 261 | grpc_exec_ctx_finish(&exec_ctx); |
| 262 | } |
| 263 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 264 | static grpc_call *grpc_channel_create_call_internal( |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 265 | grpc_exec_ctx *exec_ctx, grpc_channel *channel, grpc_call *parent_call, |
| 266 | uint32_t propagation_mask, grpc_completion_queue *cq, |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 267 | grpc_pollset_set *pollset_set_alternative, grpc_mdelem path_mdelem, |
| 268 | grpc_mdelem authority_mdelem, gpr_timespec deadline) { |
| 269 | grpc_mdelem send_metadata[2]; |
Craig Tiller | 32ca48c | 2015-09-10 11:47:15 -0700 | [diff] [blame] | 270 | size_t num_metadata = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 271 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 272 | GPR_ASSERT(channel->is_client); |
David Garcia Quintas | 879b3b9 | 2016-04-25 11:23:38 -0700 | [diff] [blame] | 273 | GPR_ASSERT(!(cq != NULL && pollset_set_alternative != NULL)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 274 | |
Craig Tiller | c4b56b6 | 2015-07-23 17:44:11 -0700 | [diff] [blame] | 275 | send_metadata[num_metadata++] = path_mdelem; |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 276 | if (!GRPC_MDISNULL(authority_mdelem)) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 277 | send_metadata[num_metadata++] = authority_mdelem; |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 278 | } else if (!GRPC_MDISNULL(channel->default_authority)) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 279 | send_metadata[num_metadata++] = GRPC_MDELEM_REF(channel->default_authority); |
| 280 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 281 | |
Craig Tiller | 8e21465 | 2016-08-19 09:54:31 -0700 | [diff] [blame] | 282 | grpc_call_create_args args; |
| 283 | memset(&args, 0, sizeof(args)); |
| 284 | args.channel = channel; |
Yash Tibrewal | b2a54ac | 2017-09-13 10:18:07 -0700 | [diff] [blame] | 285 | args.parent = parent_call; |
Craig Tiller | 8e21465 | 2016-08-19 09:54:31 -0700 | [diff] [blame] | 286 | args.propagation_mask = propagation_mask; |
| 287 | args.cq = cq; |
| 288 | args.pollset_set_alternative = pollset_set_alternative; |
| 289 | args.server_transport_data = NULL; |
| 290 | args.add_initial_metadata = send_metadata; |
| 291 | args.add_initial_metadata_count = num_metadata; |
| 292 | args.send_deadline = deadline; |
| 293 | |
| 294 | grpc_call *call; |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 295 | GRPC_LOG_IF_ERROR("call_create", grpc_call_create(exec_ctx, &args, &call)); |
Craig Tiller | 8e21465 | 2016-08-19 09:54:31 -0700 | [diff] [blame] | 296 | return call; |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 297 | } |
klempner | c463f74 | 2014-12-19 13:03:35 -0800 | [diff] [blame] | 298 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 299 | grpc_call *grpc_channel_create_call(grpc_channel *channel, |
| 300 | grpc_call *parent_call, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 301 | uint32_t propagation_mask, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 302 | grpc_completion_queue *cq, |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 303 | grpc_slice method, const grpc_slice *host, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 304 | gpr_timespec deadline, void *reserved) { |
| 305 | GPR_ASSERT(!reserved); |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 306 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 307 | grpc_call *call = grpc_channel_create_call_internal( |
| 308 | &exec_ctx, channel, parent_call, propagation_mask, cq, NULL, |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 309 | grpc_mdelem_from_slices(&exec_ctx, GRPC_MDSTR_PATH, |
| 310 | grpc_slice_ref_internal(method)), |
| 311 | host != NULL ? grpc_mdelem_from_slices(&exec_ctx, GRPC_MDSTR_AUTHORITY, |
| 312 | grpc_slice_ref_internal(*host)) |
| 313 | : GRPC_MDNULL, |
David Garcia Quintas | 4afce7e | 2016-04-18 16:25:17 -0700 | [diff] [blame] | 314 | deadline); |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 315 | grpc_exec_ctx_finish(&exec_ctx); |
| 316 | return call; |
David Garcia Quintas | 4afce7e | 2016-04-18 16:25:17 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | grpc_call *grpc_channel_create_pollset_set_call( |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 320 | grpc_exec_ctx *exec_ctx, grpc_channel *channel, grpc_call *parent_call, |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 321 | uint32_t propagation_mask, grpc_pollset_set *pollset_set, grpc_slice method, |
| 322 | const grpc_slice *host, gpr_timespec deadline, void *reserved) { |
David Garcia Quintas | 4afce7e | 2016-04-18 16:25:17 -0700 | [diff] [blame] | 323 | GPR_ASSERT(!reserved); |
| 324 | return grpc_channel_create_call_internal( |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 325 | exec_ctx, channel, parent_call, propagation_mask, NULL, pollset_set, |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 326 | grpc_mdelem_from_slices(exec_ctx, GRPC_MDSTR_PATH, |
| 327 | grpc_slice_ref_internal(method)), |
| 328 | host != NULL ? grpc_mdelem_from_slices(exec_ctx, GRPC_MDSTR_AUTHORITY, |
| 329 | grpc_slice_ref_internal(*host)) |
| 330 | : GRPC_MDNULL, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 331 | deadline); |
Craig Tiller | 0845337 | 2015-04-10 16:05:38 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 334 | void *grpc_channel_register_call(grpc_channel *channel, const char *method, |
| 335 | const char *host, void *reserved) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 336 | registered_call *rc = (registered_call *)gpr_malloc(sizeof(registered_call)); |
Masood Malekghassemi | 76c3d74 | 2015-08-19 18:22:53 -0700 | [diff] [blame] | 337 | GRPC_API_TRACE( |
| 338 | "grpc_channel_register_call(channel=%p, method=%s, host=%s, reserved=%p)", |
| 339 | 4, (channel, method, host, reserved)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 340 | GPR_ASSERT(!reserved); |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 341 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 342 | |
| 343 | rc->path = grpc_mdelem_from_slices( |
| 344 | &exec_ctx, GRPC_MDSTR_PATH, |
| 345 | grpc_slice_intern(grpc_slice_from_static_string(method))); |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 346 | rc->authority = |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 347 | host ? grpc_mdelem_from_slices( |
| 348 | &exec_ctx, GRPC_MDSTR_AUTHORITY, |
| 349 | grpc_slice_intern(grpc_slice_from_static_string(host))) |
| 350 | : GRPC_MDNULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 351 | gpr_mu_lock(&channel->registered_call_mu); |
Craig Tiller | 0845337 | 2015-04-10 16:05:38 -0700 | [diff] [blame] | 352 | rc->next = channel->registered_calls; |
| 353 | channel->registered_calls = rc; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 354 | gpr_mu_unlock(&channel->registered_call_mu); |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 355 | grpc_exec_ctx_finish(&exec_ctx); |
Craig Tiller | 0845337 | 2015-04-10 16:05:38 -0700 | [diff] [blame] | 356 | return rc; |
| 357 | } |
| 358 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 359 | grpc_call *grpc_channel_create_registered_call( |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 360 | grpc_channel *channel, grpc_call *parent_call, uint32_t propagation_mask, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 361 | grpc_completion_queue *completion_queue, void *registered_call_handle, |
| 362 | gpr_timespec deadline, void *reserved) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 363 | registered_call *rc = (registered_call *)registered_call_handle; |
Masood Malekghassemi | 76c3d74 | 2015-08-19 18:22:53 -0700 | [diff] [blame] | 364 | GRPC_API_TRACE( |
| 365 | "grpc_channel_create_registered_call(" |
Craig Tiller | 4de3e4f | 2015-10-05 08:55:50 -0700 | [diff] [blame] | 366 | "channel=%p, parent_call=%p, propagation_mask=%x, completion_queue=%p, " |
| 367 | "registered_call_handle=%p, " |
Ken Payson | 62a6c92 | 2016-06-24 11:53:54 -0700 | [diff] [blame] | 368 | "deadline=gpr_timespec { tv_sec: %" PRId64 |
| 369 | ", tv_nsec: %d, clock_type: %d }, " |
Craig Tiller | 4de3e4f | 2015-10-05 08:55:50 -0700 | [diff] [blame] | 370 | "reserved=%p)", |
Masood Malekghassemi | 76c3d74 | 2015-08-19 18:22:53 -0700 | [diff] [blame] | 371 | 9, (channel, parent_call, (unsigned)propagation_mask, completion_queue, |
Ken Payson | 62a6c92 | 2016-06-24 11:53:54 -0700 | [diff] [blame] | 372 | registered_call_handle, deadline.tv_sec, deadline.tv_nsec, |
| 373 | (int)deadline.clock_type, reserved)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 374 | GPR_ASSERT(!reserved); |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 375 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 376 | grpc_call *call = grpc_channel_create_call_internal( |
| 377 | &exec_ctx, channel, parent_call, propagation_mask, completion_queue, NULL, |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 378 | GRPC_MDELEM_REF(rc->path), GRPC_MDELEM_REF(rc->authority), deadline); |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 379 | grpc_exec_ctx_finish(&exec_ctx); |
| 380 | return call; |
Craig Tiller | 0845337 | 2015-04-10 16:05:38 -0700 | [diff] [blame] | 381 | } |
| 382 | |
ncteisen | 9c43fc0 | 2017-06-08 16:06:23 -0700 | [diff] [blame] | 383 | #ifndef NDEBUG |
Craig Tiller | 7b43561 | 2015-11-24 08:15:05 -0800 | [diff] [blame] | 384 | #define REF_REASON reason |
| 385 | #define REF_ARG , const char *reason |
Craig Tiller | 9ec2a52 | 2015-05-29 22:46:54 -0700 | [diff] [blame] | 386 | #else |
Craig Tiller | 7b43561 | 2015-11-24 08:15:05 -0800 | [diff] [blame] | 387 | #define REF_REASON "" |
| 388 | #define REF_ARG |
Craig Tiller | 9ec2a52 | 2015-05-29 22:46:54 -0700 | [diff] [blame] | 389 | #endif |
Craig Tiller | 7b43561 | 2015-11-24 08:15:05 -0800 | [diff] [blame] | 390 | void grpc_channel_internal_ref(grpc_channel *c REF_ARG) { |
| 391 | GRPC_CHANNEL_STACK_REF(CHANNEL_STACK_FROM_CHANNEL(c), REF_REASON); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 392 | } |
| 393 | |
Craig Tiller | 7b43561 | 2015-11-24 08:15:05 -0800 | [diff] [blame] | 394 | void grpc_channel_internal_unref(grpc_exec_ctx *exec_ctx, |
| 395 | grpc_channel *c REF_ARG) { |
| 396 | GRPC_CHANNEL_STACK_UNREF(exec_ctx, CHANNEL_STACK_FROM_CHANNEL(c), REF_REASON); |
| 397 | } |
| 398 | |
| 399 | static void destroy_channel(grpc_exec_ctx *exec_ctx, void *arg, |
Craig Tiller | f51457b | 2016-05-03 17:06:32 -0700 | [diff] [blame] | 400 | grpc_error *error) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 401 | grpc_channel *channel = (grpc_channel *)arg; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 402 | grpc_channel_stack_destroy(exec_ctx, CHANNEL_STACK_FROM_CHANNEL(channel)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 403 | while (channel->registered_calls) { |
| 404 | registered_call *rc = channel->registered_calls; |
| 405 | channel->registered_calls = rc->next; |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 406 | GRPC_MDELEM_UNREF(exec_ctx, rc->path); |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 407 | GRPC_MDELEM_UNREF(exec_ctx, rc->authority); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 408 | gpr_free(rc); |
| 409 | } |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 410 | GRPC_MDELEM_UNREF(exec_ctx, channel->default_authority); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 411 | gpr_mu_destroy(&channel->registered_call_mu); |
| 412 | gpr_free(channel->target); |
| 413 | gpr_free(channel); |
Craig Tiller | 7bd5ab1 | 2015-02-17 22:29:04 -0800 | [diff] [blame] | 414 | } |
| 415 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 416 | void grpc_channel_destroy(grpc_channel *channel) { |
Craig Tiller | e0221ff | 2016-07-11 15:56:08 -0700 | [diff] [blame] | 417 | grpc_transport_op *op = grpc_make_transport_op(NULL); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 418 | grpc_channel_element *elem; |
Craig Tiller | f5768a6 | 2015-09-22 10:54:34 -0700 | [diff] [blame] | 419 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
Masood Malekghassemi | 76c3d74 | 2015-08-19 18:22:53 -0700 | [diff] [blame] | 420 | GRPC_API_TRACE("grpc_channel_destroy(channel=%p)", 1, (channel)); |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 421 | op->disconnect_with_error = |
| 422 | GRPC_ERROR_CREATE_FROM_STATIC_STRING("Channel Destroyed"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 423 | elem = grpc_channel_stack_element(CHANNEL_STACK_FROM_CHANNEL(channel), 0); |
Craig Tiller | e0221ff | 2016-07-11 15:56:08 -0700 | [diff] [blame] | 424 | elem->filter->start_transport_op(&exec_ctx, elem, op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 425 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 426 | GRPC_CHANNEL_INTERNAL_UNREF(&exec_ctx, channel, "channel"); |
Craig Tiller | dfff1b8 | 2015-09-21 14:39:57 -0700 | [diff] [blame] | 427 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 428 | grpc_exec_ctx_finish(&exec_ctx); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 429 | } |
| 430 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 431 | grpc_channel_stack *grpc_channel_get_channel_stack(grpc_channel *channel) { |
| 432 | return CHANNEL_STACK_FROM_CHANNEL(channel); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 433 | } |
| 434 | |
David Garcia Quintas | ac09447 | 2016-05-18 20:25:57 -0700 | [diff] [blame] | 435 | grpc_compression_options grpc_channel_compression_options( |
David Garcia Quintas | 73dcbda | 2016-04-23 00:17:05 -0700 | [diff] [blame] | 436 | const grpc_channel *channel) { |
| 437 | return channel->compression_options; |
| 438 | } |
| 439 | |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 440 | grpc_mdelem grpc_channel_get_reffed_status_elem(grpc_exec_ctx *exec_ctx, |
| 441 | grpc_channel *channel, int i) { |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 442 | char tmp[GPR_LTOA_MIN_BUFSIZE]; |
| 443 | switch (i) { |
| 444 | case 0: |
| 445 | return GRPC_MDELEM_GRPC_STATUS_0; |
| 446 | case 1: |
| 447 | return GRPC_MDELEM_GRPC_STATUS_1; |
| 448 | case 2: |
| 449 | return GRPC_MDELEM_GRPC_STATUS_2; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 450 | } |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 451 | gpr_ltoa(i, tmp); |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 452 | return grpc_mdelem_from_slices(exec_ctx, GRPC_MDSTR_GRPC_STATUS, |
| 453 | grpc_slice_from_copied_string(tmp)); |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 454 | } |