David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | a93a25f | 2016-01-28 13:55:49 -0800 | [diff] [blame] | 3 | * Copyright 2015-2016, Google Inc. |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 34 | #include <assert.h> |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 35 | #include <string.h> |
| 36 | |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 37 | #include <grpc/compression.h> |
David Garcia Quintas | e091af8 | 2015-07-15 21:37:02 -0700 | [diff] [blame] | 38 | #include <grpc/support/alloc.h> |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 39 | #include <grpc/support/log.h> |
| 40 | #include <grpc/support/slice_buffer.h> |
| 41 | |
David Garcia Quintas | 20afd46 | 2015-07-06 23:01:39 -0700 | [diff] [blame] | 42 | #include "src/core/channel/channel_args.h" |
yang-g | d88e1d8 | 2015-12-02 13:23:33 -0800 | [diff] [blame] | 43 | #include "src/core/channel/compress_filter.h" |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 44 | #include "src/core/compression/algorithm_metadata.h" |
David Garcia Quintas | 20afd46 | 2015-07-06 23:01:39 -0700 | [diff] [blame] | 45 | #include "src/core/compression/message_compress.h" |
yang-g | d88e1d8 | 2015-12-02 13:23:33 -0800 | [diff] [blame] | 46 | #include "src/core/profiling/timers.h" |
David Garcia Quintas | e091af8 | 2015-07-15 21:37:02 -0700 | [diff] [blame] | 47 | #include "src/core/support/string.h" |
Craig Tiller | ed43f51 | 2015-11-19 08:53:23 -0800 | [diff] [blame] | 48 | #include "src/core/transport/static_metadata.h" |
David Garcia Quintas | 20afd46 | 2015-07-06 23:01:39 -0700 | [diff] [blame] | 49 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 50 | typedef struct call_data { |
David Garcia Quintas | 7c4fdb5 | 2015-07-17 14:21:15 -0700 | [diff] [blame] | 51 | gpr_slice_buffer slices; /**< Buffers up input slices to be compressed */ |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 52 | grpc_linked_mdelem compression_algorithm_storage; |
David Garcia Quintas | e091af8 | 2015-07-15 21:37:02 -0700 | [diff] [blame] | 53 | grpc_linked_mdelem accept_encoding_storage; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 54 | uint32_t remaining_slice_bytes; |
David Garcia Quintas | 7c4fdb5 | 2015-07-17 14:21:15 -0700 | [diff] [blame] | 55 | /** Compression algorithm we'll try to use. It may be given by incoming |
| 56 | * metadata, or by the channel's default compression settings. */ |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 57 | grpc_compression_algorithm compression_algorithm; |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 58 | /** If true, contents of \a compression_algorithm are authoritative */ |
David Garcia Quintas | 7c4fdb5 | 2015-07-17 14:21:15 -0700 | [diff] [blame] | 59 | int has_compression_algorithm; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 60 | |
| 61 | grpc_transport_stream_op send_op; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 62 | uint32_t send_length; |
| 63 | uint32_t send_flags; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 64 | gpr_slice incoming_slice; |
| 65 | grpc_slice_buffer_stream replacement_stream; |
| 66 | grpc_closure *post_send; |
| 67 | grpc_closure send_done; |
| 68 | grpc_closure got_slice; |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 69 | } call_data; |
| 70 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 71 | typedef struct channel_data { |
David Garcia Quintas | 7c4fdb5 | 2015-07-17 14:21:15 -0700 | [diff] [blame] | 72 | /** The default, channel-level, compression algorithm */ |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 73 | grpc_compression_algorithm default_compression_algorithm; |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame] | 74 | /** Compression options for the channel */ |
| 75 | grpc_compression_options compression_options; |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 76 | /** Supported compression algorithms */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 77 | uint32_t supported_compression_algorithms; |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 78 | } channel_data; |
| 79 | |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 80 | /** For each \a md element from the incoming metadata, filter out the entry for |
David Garcia Quintas | d7d9ce2 | 2015-06-30 23:29:03 -0700 | [diff] [blame] | 81 | * "grpc-encoding", using its value to populate the call data's |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 82 | * compression_algorithm field. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 83 | static grpc_mdelem *compression_md_filter(void *user_data, grpc_mdelem *md) { |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 84 | grpc_call_element *elem = user_data; |
| 85 | call_data *calld = elem->call_data; |
| 86 | channel_data *channeld = elem->channel_data; |
| 87 | |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 88 | if (md->key == GRPC_MDSTR_GRPC_INTERNAL_ENCODING_REQUEST) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 89 | const char *md_c_str = grpc_mdstr_as_c_string(md->value); |
| 90 | if (!grpc_compression_algorithm_parse(md_c_str, strlen(md_c_str), |
| 91 | &calld->compression_algorithm)) { |
| 92 | gpr_log(GPR_ERROR, |
| 93 | "Invalid compression algorithm: '%s' (unknown). Ignoring.", |
| 94 | md_c_str); |
| 95 | calld->compression_algorithm = GRPC_COMPRESS_NONE; |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame] | 96 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 97 | if (grpc_compression_options_is_algorithm_enabled( |
| 98 | &channeld->compression_options, calld->compression_algorithm) == |
| 99 | 0) { |
| 100 | gpr_log(GPR_ERROR, |
| 101 | "Invalid compression algorithm: '%s' (previously disabled). " |
| 102 | "Ignoring.", |
| 103 | md_c_str); |
| 104 | calld->compression_algorithm = GRPC_COMPRESS_NONE; |
| 105 | } |
| 106 | calld->has_compression_algorithm = 1; |
| 107 | return NULL; |
| 108 | } |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 109 | |
| 110 | return md; |
| 111 | } |
| 112 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 113 | static int skip_compression(grpc_call_element *elem) { |
| 114 | call_data *calld = elem->call_data; |
| 115 | channel_data *channeld = elem->channel_data; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 116 | if (calld->has_compression_algorithm) { |
| 117 | if (calld->compression_algorithm == GRPC_COMPRESS_NONE) { |
| 118 | return 1; |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 119 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 120 | return 0; /* we have an actual call-specific algorithm */ |
| 121 | } |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 122 | /* no per-call compression override */ |
| 123 | return channeld->default_compression_algorithm == GRPC_COMPRESS_NONE; |
| 124 | } |
| 125 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 126 | /** Filter initial metadata */ |
| 127 | static void process_send_initial_metadata( |
| 128 | grpc_call_element *elem, grpc_metadata_batch *initial_metadata) { |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 129 | call_data *calld = elem->call_data; |
| 130 | channel_data *channeld = elem->channel_data; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 131 | /* Parse incoming request for compression. If any, it'll be available |
| 132 | * at calld->compression_algorithm */ |
| 133 | grpc_metadata_batch_filter(initial_metadata, compression_md_filter, elem); |
| 134 | if (!calld->has_compression_algorithm) { |
| 135 | /* If no algorithm was found in the metadata and we aren't |
| 136 | * exceptionally skipping compression, fall back to the channel |
| 137 | * default */ |
| 138 | calld->compression_algorithm = channeld->default_compression_algorithm; |
| 139 | calld->has_compression_algorithm = 1; /* GPR_TRUE */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 140 | } |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 141 | /* hint compression algorithm */ |
| 142 | grpc_metadata_batch_add_tail( |
| 143 | initial_metadata, &calld->compression_algorithm_storage, |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 144 | grpc_compression_encoding_mdelem(calld->compression_algorithm)); |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 145 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 146 | /* convey supported compression algorithms */ |
Craig Tiller | b2b4261 | 2015-11-20 12:02:17 -0800 | [diff] [blame] | 147 | grpc_metadata_batch_add_tail(initial_metadata, |
| 148 | &calld->accept_encoding_storage, |
| 149 | GRPC_MDELEM_ACCEPT_ENCODING_FOR_ALGORITHMS( |
| 150 | channeld->supported_compression_algorithms)); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | static void continue_send_message(grpc_exec_ctx *exec_ctx, |
| 154 | grpc_call_element *elem); |
| 155 | |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 156 | static void send_done(grpc_exec_ctx *exec_ctx, void *elemp, bool success) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 157 | grpc_call_element *elem = elemp; |
| 158 | call_data *calld = elem->call_data; |
| 159 | gpr_slice_buffer_reset_and_unref(&calld->slices); |
| 160 | calld->post_send->cb(exec_ctx, calld->post_send->cb_arg, success); |
| 161 | } |
| 162 | |
| 163 | static void finish_send_message(grpc_exec_ctx *exec_ctx, |
| 164 | grpc_call_element *elem) { |
| 165 | call_data *calld = elem->call_data; |
| 166 | int did_compress; |
| 167 | gpr_slice_buffer tmp; |
| 168 | gpr_slice_buffer_init(&tmp); |
| 169 | did_compress = |
| 170 | grpc_msg_compress(calld->compression_algorithm, &calld->slices, &tmp); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 171 | if (did_compress) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 172 | gpr_slice_buffer_swap(&calld->slices, &tmp); |
| 173 | calld->send_flags |= GRPC_WRITE_INTERNAL_COMPRESS; |
| 174 | } |
| 175 | gpr_slice_buffer_destroy(&tmp); |
| 176 | |
| 177 | grpc_slice_buffer_stream_init(&calld->replacement_stream, &calld->slices, |
| 178 | calld->send_flags); |
| 179 | calld->send_op.send_message = &calld->replacement_stream.base; |
| 180 | calld->post_send = calld->send_op.on_complete; |
| 181 | calld->send_op.on_complete = &calld->send_done; |
| 182 | |
| 183 | grpc_call_next_op(exec_ctx, elem, &calld->send_op); |
| 184 | } |
| 185 | |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 186 | static void got_slice(grpc_exec_ctx *exec_ctx, void *elemp, bool success) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 187 | grpc_call_element *elem = elemp; |
| 188 | call_data *calld = elem->call_data; |
| 189 | gpr_slice_buffer_add(&calld->slices, calld->incoming_slice); |
| 190 | if (calld->send_length == calld->slices.length) { |
| 191 | finish_send_message(exec_ctx, elem); |
| 192 | } else { |
| 193 | continue_send_message(exec_ctx, elem); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 194 | } |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 197 | static void continue_send_message(grpc_exec_ctx *exec_ctx, |
| 198 | grpc_call_element *elem) { |
| 199 | call_data *calld = elem->call_data; |
| 200 | while (grpc_byte_stream_next(exec_ctx, calld->send_op.send_message, |
| 201 | &calld->incoming_slice, ~(size_t)0, |
| 202 | &calld->got_slice)) { |
| 203 | gpr_slice_buffer_add(&calld->slices, calld->incoming_slice); |
| 204 | if (calld->send_length == calld->slices.length) { |
| 205 | finish_send_message(exec_ctx, elem); |
| 206 | break; |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 211 | static void compress_start_transport_stream_op(grpc_exec_ctx *exec_ctx, |
| 212 | grpc_call_element *elem, |
| 213 | grpc_transport_stream_op *op) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 214 | call_data *calld = elem->call_data; |
| 215 | |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 216 | GPR_TIMER_BEGIN("compress_start_transport_stream_op", 0); |
Craig Tiller | 1f41b6b | 2015-10-09 15:07:02 -0700 | [diff] [blame] | 217 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 218 | if (op->send_initial_metadata) { |
| 219 | process_send_initial_metadata(elem, op->send_initial_metadata); |
| 220 | } |
| 221 | if (op->send_message != NULL && !skip_compression(elem) && |
| 222 | 0 == (op->send_message->flags & GRPC_WRITE_NO_COMPRESS)) { |
| 223 | calld->send_op = *op; |
| 224 | calld->send_length = op->send_message->length; |
| 225 | calld->send_flags = op->send_message->flags; |
| 226 | continue_send_message(exec_ctx, elem); |
| 227 | } else { |
| 228 | /* pass control down the stack */ |
| 229 | grpc_call_next_op(exec_ctx, elem, op); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 230 | } |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 231 | |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 232 | GPR_TIMER_END("compress_start_transport_stream_op", 0); |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 233 | } |
| 234 | |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 235 | /* Constructor for call_data */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 236 | static void init_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 237 | grpc_call_element_args *args) { |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 238 | /* grab pointers to our data from the call element */ |
| 239 | call_data *calld = elem->call_data; |
| 240 | |
| 241 | /* initialize members */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 242 | gpr_slice_buffer_init(&calld->slices); |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 243 | calld->has_compression_algorithm = 0; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 244 | grpc_closure_init(&calld->got_slice, got_slice, elem); |
| 245 | grpc_closure_init(&calld->send_done, send_done, elem); |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | /* Destructor for call_data */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 249 | static void destroy_call_elem(grpc_exec_ctx *exec_ctx, |
| 250 | grpc_call_element *elem) { |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 251 | /* grab pointers to our data from the call element */ |
| 252 | call_data *calld = elem->call_data; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 253 | gpr_slice_buffer_destroy(&calld->slices); |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /* Constructor for channel_data */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 257 | static void init_channel_elem(grpc_exec_ctx *exec_ctx, |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 258 | grpc_channel_element *elem, |
| 259 | grpc_channel_element_args *args) { |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 260 | channel_data *channeld = elem->channel_data; |
David Garcia Quintas | d16af0e | 2015-06-22 22:39:21 -0700 | [diff] [blame] | 261 | grpc_compression_algorithm algo_idx; |
David Garcia Quintas | f74a49e | 2015-06-18 17:22:45 -0700 | [diff] [blame] | 262 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 263 | grpc_compression_options_init(&channeld->compression_options); |
| 264 | channeld->compression_options.enabled_algorithms_bitset = |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 265 | (uint32_t)grpc_channel_args_compression_algorithm_get_states( |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 266 | args->channel_args); |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame] | 267 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 268 | channeld->default_compression_algorithm = |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 269 | grpc_channel_args_get_compression_algorithm(args->channel_args); |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame] | 270 | /* Make sure the default isn't disabled. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 271 | GPR_ASSERT(grpc_compression_options_is_algorithm_enabled( |
| 272 | &channeld->compression_options, channeld->default_compression_algorithm)); |
| 273 | channeld->compression_options.default_compression_algorithm = |
| 274 | channeld->default_compression_algorithm; |
David Garcia Quintas | f74a49e | 2015-06-18 17:22:45 -0700 | [diff] [blame] | 275 | |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 276 | channeld->supported_compression_algorithms = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 277 | for (algo_idx = 0; algo_idx < GRPC_COMPRESS_ALGORITHMS_COUNT; ++algo_idx) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 278 | /* skip disabled algorithms */ |
| 279 | if (grpc_compression_options_is_algorithm_enabled( |
| 280 | &channeld->compression_options, algo_idx) == 0) { |
| 281 | continue; |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame] | 282 | } |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 283 | channeld->supported_compression_algorithms |= 1u << algo_idx; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 284 | } |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 285 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 286 | GPR_ASSERT(!args->is_last); |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | /* Destructor for channel data */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 290 | static void destroy_channel_elem(grpc_exec_ctx *exec_ctx, |
Craig Tiller | ab33b48 | 2015-11-21 08:11:04 -0800 | [diff] [blame] | 291 | grpc_channel_element *elem) {} |
David Garcia Quintas | 55b4ea1 | 2015-06-16 14:27:32 -0700 | [diff] [blame] | 292 | |
David Garcia Quintas | d317e75 | 2015-07-15 00:09:27 -0700 | [diff] [blame] | 293 | const grpc_channel_filter grpc_compress_filter = { |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 294 | compress_start_transport_stream_op, |
| 295 | grpc_channel_next_op, |
| 296 | sizeof(call_data), |
| 297 | init_call_elem, |
| 298 | grpc_call_stack_ignore_set_pollset, |
| 299 | destroy_call_elem, |
| 300 | sizeof(channel_data), |
| 301 | init_channel_elem, |
| 302 | destroy_channel_elem, |
| 303 | grpc_call_next_get_peer, |
| 304 | "compress"}; |