grpc 1.22.1 → 1.23.0.pre1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of grpc might be problematic. Click here for more details.

Files changed (176) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +487 -649
  3. data/include/grpc/grpc_security.h +25 -0
  4. data/include/grpc/impl/codegen/grpc_types.h +11 -2
  5. data/include/grpc/impl/codegen/port_platform.h +12 -0
  6. data/src/core/ext/filters/client_channel/backup_poller.cc +4 -2
  7. data/src/core/ext/filters/client_channel/client_channel.cc +477 -182
  8. data/src/core/ext/filters/client_channel/client_channel_channelz.cc +25 -16
  9. data/src/core/ext/filters/client_channel/client_channel_channelz.h +11 -6
  10. data/src/core/ext/filters/client_channel/connector.h +10 -2
  11. data/src/core/ext/filters/client_channel/health/health_check_client.cc +3 -3
  12. data/src/core/ext/filters/client_channel/http_proxy.cc +9 -10
  13. data/src/core/ext/filters/client_channel/lb_policy.cc +2 -17
  14. data/src/core/ext/filters/client_channel/lb_policy.h +36 -8
  15. data/src/core/ext/filters/client_channel/lb_policy/grpclb/client_load_reporting_filter.cc +22 -8
  16. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.cc +86 -52
  17. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.h +7 -0
  18. data/src/core/ext/filters/client_channel/lb_policy/pick_first/pick_first.cc +73 -72
  19. data/src/core/ext/filters/client_channel/lb_policy/round_robin/round_robin.cc +8 -12
  20. data/src/core/ext/filters/client_channel/lb_policy/subchannel_list.h +25 -101
  21. data/src/core/ext/filters/client_channel/lb_policy/xds/xds.cc +5 -5
  22. data/src/core/ext/filters/client_channel/parse_address.cc +29 -26
  23. data/src/core/ext/filters/client_channel/resolver.h +3 -11
  24. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/dns_resolver_ares.cc +5 -3
  25. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver_windows.cc +405 -82
  26. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.cc +44 -51
  27. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper_libuv.cc +0 -1
  28. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper_windows.cc +0 -1
  29. data/src/core/ext/filters/client_channel/resolver/dns/native/dns_resolver.cc +11 -6
  30. data/src/core/ext/filters/client_channel/resolver/fake/fake_resolver.cc +130 -65
  31. data/src/core/ext/filters/client_channel/resolver/fake/fake_resolver.h +8 -3
  32. data/src/core/ext/filters/client_channel/resolver/sockaddr/sockaddr_resolver.cc +31 -14
  33. data/src/core/ext/filters/client_channel/resolver_factory.h +4 -0
  34. data/src/core/ext/filters/client_channel/resolver_registry.cc +11 -0
  35. data/src/core/ext/filters/client_channel/resolver_registry.h +3 -0
  36. data/src/core/ext/filters/client_channel/resolving_lb_policy.cc +10 -49
  37. data/src/core/ext/filters/client_channel/resolving_lb_policy.h +1 -14
  38. data/src/core/ext/filters/client_channel/retry_throttle.h +2 -3
  39. data/src/core/ext/filters/client_channel/subchannel.cc +65 -58
  40. data/src/core/ext/filters/client_channel/subchannel.h +65 -45
  41. data/src/core/ext/filters/client_channel/subchannel_interface.h +15 -30
  42. data/src/core/ext/filters/client_idle/client_idle_filter.cc +264 -0
  43. data/src/core/ext/filters/http/client/http_client_filter.cc +4 -5
  44. data/src/core/ext/filters/http/client_authority_filter.cc +2 -2
  45. data/src/core/ext/filters/http/message_compress/message_compress_filter.cc +140 -152
  46. data/src/core/ext/filters/max_age/max_age_filter.cc +3 -3
  47. data/src/core/ext/transport/chttp2/client/chttp2_connector.cc +3 -4
  48. data/src/core/ext/transport/chttp2/server/chttp2_server.cc +7 -6
  49. data/src/core/ext/transport/chttp2/transport/chttp2_transport.cc +41 -60
  50. data/src/core/ext/transport/chttp2/transport/context_list.cc +3 -1
  51. data/src/core/ext/transport/chttp2/transport/frame_data.cc +4 -4
  52. data/src/core/ext/transport/chttp2/transport/frame_goaway.cc +1 -1
  53. data/src/core/ext/transport/chttp2/transport/frame_ping.cc +0 -1
  54. data/src/core/ext/transport/chttp2/transport/frame_rst_stream.cc +0 -8
  55. data/src/core/ext/transport/chttp2/transport/frame_rst_stream.h +0 -7
  56. data/src/core/ext/transport/chttp2/transport/frame_settings.cc +0 -1
  57. data/src/core/ext/transport/chttp2/transport/hpack_encoder.cc +37 -22
  58. data/src/core/ext/transport/chttp2/transport/hpack_parser.cc +137 -80
  59. data/src/core/ext/transport/chttp2/transport/hpack_parser.h +8 -0
  60. data/src/core/ext/transport/chttp2/transport/hpack_table.cc +7 -166
  61. data/src/core/ext/transport/chttp2/transport/hpack_table.h +41 -15
  62. data/src/core/ext/transport/chttp2/transport/internal.h +6 -9
  63. data/src/core/ext/transport/chttp2/transport/parsing.cc +37 -20
  64. data/src/core/ext/transport/chttp2/transport/stream_map.cc +28 -18
  65. data/src/core/ext/transport/chttp2/transport/writing.cc +0 -1
  66. data/src/core/ext/transport/inproc/inproc_transport.cc +1 -1
  67. data/src/core/lib/channel/channelz.cc +80 -33
  68. data/src/core/lib/channel/channelz.h +28 -13
  69. data/src/core/lib/compression/compression.cc +1 -2
  70. data/src/core/lib/compression/compression_args.cc +13 -6
  71. data/src/core/lib/compression/compression_args.h +3 -2
  72. data/src/core/lib/compression/compression_internal.cc +1 -1
  73. data/src/core/lib/gpr/env_linux.cc +10 -21
  74. data/src/core/lib/gpr/env_posix.cc +0 -5
  75. data/src/core/lib/gpr/string.cc +7 -2
  76. data/src/core/lib/gpr/string.h +1 -0
  77. data/src/core/lib/gpr/sync_posix.cc +0 -129
  78. data/src/core/lib/gprpp/debug_location.h +3 -2
  79. data/src/core/lib/gprpp/fork.cc +14 -21
  80. data/src/core/lib/gprpp/fork.h +15 -4
  81. data/src/core/lib/gprpp/host_port.cc +118 -0
  82. data/src/core/lib/{gpr → gprpp}/host_port.h +27 -11
  83. data/src/core/lib/gprpp/map.h +25 -0
  84. data/src/core/lib/gprpp/memory.h +26 -9
  85. data/src/core/lib/gprpp/ref_counted.h +63 -21
  86. data/src/core/lib/gprpp/string_view.h +143 -0
  87. data/src/core/lib/gprpp/thd.h +10 -1
  88. data/src/core/lib/gprpp/thd_posix.cc +25 -0
  89. data/src/core/lib/gprpp/thd_windows.cc +9 -1
  90. data/src/core/lib/http/httpcli_security_connector.cc +3 -1
  91. data/src/core/lib/iomgr/cfstream_handle.cc +6 -1
  92. data/src/core/lib/iomgr/cfstream_handle.h +8 -2
  93. data/src/core/lib/iomgr/combiner.cc +4 -4
  94. data/src/core/lib/iomgr/error.cc +18 -8
  95. data/src/core/lib/iomgr/error.h +2 -0
  96. data/src/core/lib/iomgr/ev_posix.cc +4 -2
  97. data/src/core/lib/iomgr/executor.cc +4 -1
  98. data/src/core/lib/iomgr/executor/mpmcqueue.cc +183 -0
  99. data/src/core/lib/iomgr/executor/mpmcqueue.h +178 -0
  100. data/src/core/lib/iomgr/executor/threadpool.cc +138 -0
  101. data/src/core/lib/iomgr/executor/threadpool.h +153 -0
  102. data/src/core/lib/iomgr/fork_posix.cc +4 -2
  103. data/src/core/lib/iomgr/iocp_windows.cc +2 -2
  104. data/src/core/lib/iomgr/iomgr_posix_cfstream.cc +14 -0
  105. data/src/core/lib/iomgr/iomgr_uv.cc +3 -0
  106. data/src/core/lib/iomgr/lockfree_event.cc +3 -3
  107. data/src/core/lib/iomgr/resolve_address_custom.cc +16 -20
  108. data/src/core/lib/iomgr/resolve_address_posix.cc +8 -10
  109. data/src/core/lib/iomgr/resolve_address_windows.cc +6 -8
  110. data/src/core/lib/iomgr/sockaddr_utils.cc +5 -3
  111. data/src/core/lib/iomgr/socket_utils_common_posix.cc +0 -1
  112. data/src/core/lib/iomgr/socket_windows.h +1 -1
  113. data/src/core/lib/iomgr/tcp_client_cfstream.cc +7 -6
  114. data/src/core/lib/iomgr/tcp_client_custom.cc +1 -0
  115. data/src/core/lib/iomgr/tcp_custom.cc +4 -0
  116. data/src/core/lib/iomgr/tcp_posix.cc +8 -2
  117. data/src/core/lib/iomgr/tcp_server_custom.cc +1 -0
  118. data/src/core/lib/iomgr/tcp_server_windows.cc +1 -1
  119. data/src/core/lib/iomgr/tcp_windows.cc +7 -7
  120. data/src/core/lib/iomgr/timer_custom.cc +1 -0
  121. data/src/core/lib/iomgr/timer_manager.cc +0 -29
  122. data/src/core/lib/security/credentials/credentials.cc +84 -0
  123. data/src/core/lib/security/credentials/credentials.h +58 -2
  124. data/src/core/lib/security/credentials/jwt/json_token.cc +6 -2
  125. data/src/core/lib/security/credentials/jwt/json_token.h +1 -1
  126. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.cc +245 -24
  127. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.h +16 -0
  128. data/src/core/lib/security/security_connector/alts/alts_security_connector.cc +3 -2
  129. data/src/core/lib/security/security_connector/fake/fake_security_connector.cc +21 -25
  130. data/src/core/lib/security/security_connector/local/local_security_connector.cc +3 -2
  131. data/src/core/lib/security/security_connector/security_connector.cc +1 -1
  132. data/src/core/lib/security/security_connector/security_connector.h +1 -1
  133. data/src/core/lib/security/security_connector/ssl/ssl_security_connector.cc +19 -19
  134. data/src/core/lib/security/security_connector/ssl_utils.cc +26 -31
  135. data/src/core/lib/security/security_connector/ssl_utils.h +11 -8
  136. data/src/core/lib/security/security_connector/tls/spiffe_security_connector.cc +16 -20
  137. data/src/core/lib/security/security_connector/tls/spiffe_security_connector.h +4 -3
  138. data/src/core/lib/security/transport/client_auth_filter.cc +1 -2
  139. data/src/core/lib/security/util/json_util.cc +19 -5
  140. data/src/core/lib/security/util/json_util.h +3 -1
  141. data/src/core/lib/slice/slice.cc +69 -50
  142. data/src/core/lib/slice/slice_buffer.cc +6 -5
  143. data/src/core/lib/slice/slice_hash_table.h +3 -7
  144. data/src/core/lib/slice/slice_intern.cc +130 -39
  145. data/src/core/lib/slice/slice_internal.h +8 -0
  146. data/src/core/lib/slice/slice_utils.h +120 -0
  147. data/src/core/lib/slice/slice_weak_hash_table.h +2 -7
  148. data/src/core/lib/surface/call.cc +8 -3
  149. data/src/core/lib/surface/channel.cc +31 -8
  150. data/src/core/lib/surface/completion_queue.cc +17 -7
  151. data/src/core/lib/surface/init_secure.cc +4 -1
  152. data/src/core/lib/surface/lame_client.cc +2 -2
  153. data/src/core/lib/surface/server.cc +34 -35
  154. data/src/core/lib/surface/server.h +8 -17
  155. data/src/core/lib/surface/version.cc +1 -1
  156. data/src/core/lib/transport/byte_stream.cc +3 -5
  157. data/src/core/lib/transport/byte_stream.h +1 -2
  158. data/src/core/lib/transport/error_utils.cc +10 -1
  159. data/src/core/lib/transport/metadata.cc +202 -35
  160. data/src/core/lib/transport/metadata.h +81 -6
  161. data/src/core/lib/transport/static_metadata.cc +1257 -465
  162. data/src/core/lib/transport/static_metadata.h +190 -347
  163. data/src/core/lib/transport/timeout_encoding.cc +7 -0
  164. data/src/core/lib/transport/timeout_encoding.h +3 -2
  165. data/src/core/plugin_registry/grpc_plugin_registry.cc +4 -0
  166. data/src/core/tsi/alts/handshaker/alts_tsi_handshaker.cc +0 -1
  167. data/src/core/tsi/ssl/session_cache/ssl_session_cache.h +2 -7
  168. data/src/core/tsi/ssl_transport_security.cc +35 -43
  169. data/src/core/tsi/ssl_transport_security.h +2 -1
  170. data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +2 -0
  171. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +3 -0
  172. data/src/ruby/lib/grpc/generic/rpc_server.rb +1 -1
  173. data/src/ruby/lib/grpc/grpc.rb +1 -1
  174. data/src/ruby/lib/grpc/version.rb +1 -1
  175. metadata +38 -32
  176. data/src/core/lib/gpr/host_port.cc +0 -98
@@ -304,7 +304,7 @@ static grpc_error* update_path_for_get(grpc_call_element* elem,
304
304
  estimated_len += grpc_base64_estimate_encoded_size(
305
305
  batch->payload->send_message.send_message->length(), true /* url_safe */,
306
306
  false /* multi_line */);
307
- grpc_slice path_with_query_slice = GRPC_SLICE_MALLOC(estimated_len);
307
+ grpc_core::UnmanagedMemorySlice path_with_query_slice(estimated_len);
308
308
  /* memcopy individual pieces into this slice */
309
309
  char* write_ptr =
310
310
  reinterpret_cast<char*> GRPC_SLICE_START_PTR(path_with_query_slice);
@@ -514,13 +514,12 @@ static size_t max_payload_size_from_args(const grpc_channel_args* args) {
514
514
  return kMaxPayloadSizeForGet;
515
515
  }
516
516
 
517
- static grpc_slice user_agent_from_args(const grpc_channel_args* args,
518
- const char* transport_name) {
517
+ static grpc_core::ManagedMemorySlice user_agent_from_args(
518
+ const grpc_channel_args* args, const char* transport_name) {
519
519
  gpr_strvec v;
520
520
  size_t i;
521
521
  int is_first = 1;
522
522
  char* tmp;
523
- grpc_slice result;
524
523
 
525
524
  gpr_strvec_init(&v);
526
525
 
@@ -558,7 +557,7 @@ static grpc_slice user_agent_from_args(const grpc_channel_args* args,
558
557
 
559
558
  tmp = gpr_strvec_flatten(&v, nullptr);
560
559
  gpr_strvec_destroy(&v);
561
- result = grpc_slice_intern(grpc_slice_from_static_string(tmp));
560
+ grpc_core::ManagedMemorySlice result(tmp);
562
561
  gpr_free(tmp);
563
562
 
564
563
  return result;
@@ -44,7 +44,7 @@ struct call_data {
44
44
  };
45
45
 
46
46
  struct channel_data {
47
- grpc_slice default_authority;
47
+ grpc_core::ManagedMemorySlice default_authority;
48
48
  grpc_mdelem default_authority_mdelem;
49
49
  };
50
50
 
@@ -102,7 +102,7 @@ grpc_error* init_channel_elem(grpc_channel_element* elem,
102
102
  "GRPC_ARG_DEFAULT_AUTHORITY channel arg. must be a string");
103
103
  }
104
104
  chand->default_authority =
105
- grpc_slice_intern(grpc_slice_from_static_string(default_authority_str));
105
+ grpc_core::ManagedMemorySlice(default_authority_str);
106
106
  chand->default_authority_mdelem = grpc_mdelem_create(
107
107
  GRPC_MDSTR_AUTHORITY, chand->default_authority, nullptr);
108
108
  GPR_ASSERT(!args->is_last);
@@ -45,46 +45,58 @@ static void send_message_on_complete(void* arg, grpc_error* error);
45
45
  static void on_send_message_next_done(void* arg, grpc_error* error);
46
46
 
47
47
  namespace {
48
- enum initial_metadata_state {
49
- // Initial metadata not yet seen.
50
- INITIAL_METADATA_UNSEEN = 0,
51
- // Initial metadata seen; compression algorithm set.
52
- HAS_COMPRESSION_ALGORITHM,
53
- // Initial metadata seen; no compression algorithm set.
54
- NO_COMPRESSION_ALGORITHM,
48
+
49
+ struct channel_data {
50
+ /** The default, channel-level, compression algorithm */
51
+ grpc_compression_algorithm default_compression_algorithm;
52
+ /** Bitset of enabled compression algorithms */
53
+ uint32_t enabled_compression_algorithms_bitset;
54
+ /** Bitset of enabled message compression algorithms */
55
+ uint32_t enabled_message_compression_algorithms_bitset;
56
+ /** Bitset of enabled stream compression algorithms */
57
+ uint32_t enabled_stream_compression_algorithms_bitset;
55
58
  };
56
59
 
57
60
  struct call_data {
58
61
  call_data(grpc_call_element* elem, const grpc_call_element_args& args)
59
62
  : call_combiner(args.call_combiner) {
63
+ channel_data* channeld = static_cast<channel_data*>(elem->channel_data);
64
+ // The call's message compression algorithm is set to channel's default
65
+ // setting. It can be overridden later by initial metadata.
66
+ if (GPR_LIKELY(GPR_BITGET(channeld->enabled_compression_algorithms_bitset,
67
+ channeld->default_compression_algorithm))) {
68
+ message_compression_algorithm =
69
+ grpc_compression_algorithm_to_message_compression_algorithm(
70
+ channeld->default_compression_algorithm);
71
+ }
60
72
  GRPC_CLOSURE_INIT(&start_send_message_batch_in_call_combiner,
61
73
  start_send_message_batch, elem,
62
74
  grpc_schedule_on_exec_ctx);
63
- grpc_slice_buffer_init(&slices);
64
- GRPC_CLOSURE_INIT(&send_message_on_complete, ::send_message_on_complete,
65
- elem, grpc_schedule_on_exec_ctx);
66
- GRPC_CLOSURE_INIT(&on_send_message_next_done, ::on_send_message_next_done,
67
- elem, grpc_schedule_on_exec_ctx);
68
75
  }
69
76
 
70
77
  ~call_data() {
71
- grpc_slice_buffer_destroy_internal(&slices);
78
+ if (state_initialized) {
79
+ grpc_slice_buffer_destroy_internal(&slices);
80
+ }
72
81
  GRPC_ERROR_UNREF(cancel_error);
73
82
  }
74
83
 
75
84
  grpc_core::CallCombiner* call_combiner;
76
- grpc_linked_mdelem compression_algorithm_storage;
77
- grpc_linked_mdelem stream_compression_algorithm_storage;
78
- grpc_linked_mdelem accept_encoding_storage;
79
- grpc_linked_mdelem accept_stream_encoding_storage;
80
- /** Compression algorithm we'll try to use. It may be given by incoming
81
- * metadata, or by the channel's default compression settings. */
82
85
  grpc_message_compression_algorithm message_compression_algorithm =
83
86
  GRPC_MESSAGE_COMPRESS_NONE;
84
- initial_metadata_state send_initial_metadata_state = INITIAL_METADATA_UNSEEN;
85
87
  grpc_error* cancel_error = GRPC_ERROR_NONE;
86
- grpc_closure start_send_message_batch_in_call_combiner;
87
88
  grpc_transport_stream_op_batch* send_message_batch = nullptr;
89
+ bool seen_initial_metadata = false;
90
+ /* Set to true, if the fields below are initialized. */
91
+ bool state_initialized = false;
92
+ grpc_closure start_send_message_batch_in_call_combiner;
93
+ /* The fields below are only initialized when we compress the payload.
94
+ * Keep them at the bottom of the struct, so they don't pollute the
95
+ * cache-lines. */
96
+ grpc_linked_mdelem message_compression_algorithm_storage;
97
+ grpc_linked_mdelem stream_compression_algorithm_storage;
98
+ grpc_linked_mdelem accept_encoding_storage;
99
+ grpc_linked_mdelem accept_stream_encoding_storage;
88
100
  grpc_slice_buffer slices; /**< Buffers up input slices to be compressed */
89
101
  grpc_core::ManualConstructor<grpc_core::SliceBufferByteStream>
90
102
  replacement_stream;
@@ -93,130 +105,118 @@ struct call_data {
93
105
  grpc_closure on_send_message_next_done;
94
106
  };
95
107
 
96
- struct channel_data {
97
- /** The default, channel-level, compression algorithm */
98
- grpc_compression_algorithm default_compression_algorithm;
99
- /** Bitset of enabled compression algorithms */
100
- uint32_t enabled_algorithms_bitset;
101
- /** Supported compression algorithms */
102
- uint32_t supported_message_compression_algorithms;
103
- /** Supported stream compression algorithms */
104
- uint32_t supported_stream_compression_algorithms;
105
- };
106
108
  } // namespace
107
109
 
108
- static bool skip_compression(grpc_call_element* elem, uint32_t flags,
109
- bool has_compression_algorithm) {
110
+ // Returns true if we should skip message compression for the current message.
111
+ static bool skip_message_compression(grpc_call_element* elem) {
110
112
  call_data* calld = static_cast<call_data*>(elem->call_data);
111
- channel_data* channeld = static_cast<channel_data*>(elem->channel_data);
112
-
113
+ // If the flags of this message indicate that it shouldn't be compressed, we
114
+ // skip message compression.
115
+ uint32_t flags =
116
+ calld->send_message_batch->payload->send_message.send_message->flags();
113
117
  if (flags & (GRPC_WRITE_NO_COMPRESS | GRPC_WRITE_INTERNAL_COMPRESS)) {
114
118
  return true;
115
119
  }
116
- if (has_compression_algorithm) {
117
- if (calld->message_compression_algorithm == GRPC_MESSAGE_COMPRESS_NONE) {
118
- return true;
119
- }
120
- return false; /* we have an actual call-specific algorithm */
120
+ // If this call doesn't have any message compression algorithm set, skip
121
+ // message compression.
122
+ return calld->message_compression_algorithm == GRPC_MESSAGE_COMPRESS_NONE;
123
+ }
124
+
125
+ // Determines the compression algorithm from the initial metadata and the
126
+ // channel's default setting.
127
+ static grpc_compression_algorithm find_compression_algorithm(
128
+ grpc_metadata_batch* initial_metadata, channel_data* channeld) {
129
+ if (initial_metadata->idx.named.grpc_internal_encoding_request == nullptr) {
130
+ return channeld->default_compression_algorithm;
131
+ }
132
+ grpc_compression_algorithm compression_algorithm;
133
+ // Parse the compression algorithm from the initial metadata.
134
+ grpc_mdelem md =
135
+ initial_metadata->idx.named.grpc_internal_encoding_request->md;
136
+ GPR_ASSERT(grpc_compression_algorithm_parse(GRPC_MDVALUE(md),
137
+ &compression_algorithm));
138
+ // Remove this metadata since it's an internal one (i.e., it won't be
139
+ // transmitted out).
140
+ grpc_metadata_batch_remove(
141
+ initial_metadata,
142
+ initial_metadata->idx.named.grpc_internal_encoding_request);
143
+ // Check if that algorithm is enabled. Note that GRPC_COMPRESS_NONE is always
144
+ // enabled.
145
+ // TODO(juanlishen): Maybe use channel default or abort() if the algorithm
146
+ // from the initial metadata is disabled.
147
+ if (GPR_LIKELY(GPR_BITGET(channeld->enabled_compression_algorithms_bitset,
148
+ compression_algorithm))) {
149
+ return compression_algorithm;
121
150
  }
122
- /* no per-call compression override */
123
- return channeld->default_compression_algorithm == GRPC_COMPRESS_NONE;
151
+ const char* algorithm_name;
152
+ GPR_ASSERT(
153
+ grpc_compression_algorithm_name(compression_algorithm, &algorithm_name));
154
+ gpr_log(GPR_ERROR,
155
+ "Invalid compression algorithm from initial metadata: '%s' "
156
+ "(previously disabled). "
157
+ "Will not compress.",
158
+ algorithm_name);
159
+ return GRPC_COMPRESS_NONE;
160
+ }
161
+
162
+ static void initialize_state(grpc_call_element* elem, call_data* calld) {
163
+ GPR_DEBUG_ASSERT(!calld->state_initialized);
164
+ calld->state_initialized = true;
165
+ grpc_slice_buffer_init(&calld->slices);
166
+ GRPC_CLOSURE_INIT(&calld->send_message_on_complete,
167
+ ::send_message_on_complete, elem,
168
+ grpc_schedule_on_exec_ctx);
169
+ GRPC_CLOSURE_INIT(&calld->on_send_message_next_done,
170
+ ::on_send_message_next_done, elem,
171
+ grpc_schedule_on_exec_ctx);
124
172
  }
125
173
 
126
- /** Filter initial metadata */
127
174
  static grpc_error* process_send_initial_metadata(
128
- grpc_call_element* elem, grpc_metadata_batch* initial_metadata,
129
- bool* has_compression_algorithm) GRPC_MUST_USE_RESULT;
175
+ grpc_call_element* elem,
176
+ grpc_metadata_batch* initial_metadata) GRPC_MUST_USE_RESULT;
130
177
  static grpc_error* process_send_initial_metadata(
131
- grpc_call_element* elem, grpc_metadata_batch* initial_metadata,
132
- bool* has_compression_algorithm) {
178
+ grpc_call_element* elem, grpc_metadata_batch* initial_metadata) {
133
179
  call_data* calld = static_cast<call_data*>(elem->call_data);
134
180
  channel_data* channeld = static_cast<channel_data*>(elem->channel_data);
135
- *has_compression_algorithm = false;
136
- grpc_compression_algorithm compression_algorithm;
181
+ // Find the compression algorithm.
182
+ grpc_compression_algorithm compression_algorithm =
183
+ find_compression_algorithm(initial_metadata, channeld);
184
+ // Note that at most one of the following algorithms can be set.
185
+ calld->message_compression_algorithm =
186
+ grpc_compression_algorithm_to_message_compression_algorithm(
187
+ compression_algorithm);
137
188
  grpc_stream_compression_algorithm stream_compression_algorithm =
138
- GRPC_STREAM_COMPRESS_NONE;
139
- if (initial_metadata->idx.named.grpc_internal_encoding_request != nullptr) {
140
- grpc_mdelem md =
141
- initial_metadata->idx.named.grpc_internal_encoding_request->md;
142
- if (GPR_UNLIKELY(!grpc_compression_algorithm_parse(
143
- GRPC_MDVALUE(md), &compression_algorithm))) {
144
- char* val = grpc_slice_to_c_string(GRPC_MDVALUE(md));
145
- gpr_log(GPR_ERROR,
146
- "Invalid compression algorithm: '%s' (unknown). Ignoring.", val);
147
- gpr_free(val);
148
- calld->message_compression_algorithm = GRPC_MESSAGE_COMPRESS_NONE;
149
- stream_compression_algorithm = GRPC_STREAM_COMPRESS_NONE;
150
- }
151
- if (GPR_UNLIKELY(!GPR_BITGET(channeld->enabled_algorithms_bitset,
152
- compression_algorithm))) {
153
- char* val = grpc_slice_to_c_string(GRPC_MDVALUE(md));
154
- gpr_log(GPR_ERROR,
155
- "Invalid compression algorithm: '%s' (previously disabled). "
156
- "Ignoring.",
157
- val);
158
- gpr_free(val);
159
- calld->message_compression_algorithm = GRPC_MESSAGE_COMPRESS_NONE;
160
- stream_compression_algorithm = GRPC_STREAM_COMPRESS_NONE;
161
- }
162
- *has_compression_algorithm = true;
163
- grpc_metadata_batch_remove(
164
- initial_metadata,
165
- initial_metadata->idx.named.grpc_internal_encoding_request);
166
- calld->message_compression_algorithm =
167
- grpc_compression_algorithm_to_message_compression_algorithm(
168
- compression_algorithm);
169
- stream_compression_algorithm =
170
- grpc_compression_algorithm_to_stream_compression_algorithm(
171
- compression_algorithm);
172
- } else {
173
- /* If no algorithm was found in the metadata and we aren't
174
- * exceptionally skipping compression, fall back to the channel
175
- * default */
176
- if (channeld->default_compression_algorithm != GRPC_COMPRESS_NONE) {
177
- calld->message_compression_algorithm =
178
- grpc_compression_algorithm_to_message_compression_algorithm(
179
- channeld->default_compression_algorithm);
180
- stream_compression_algorithm =
181
- grpc_compression_algorithm_to_stream_compression_algorithm(
182
- channeld->default_compression_algorithm);
183
- }
184
- *has_compression_algorithm = true;
185
- }
186
-
189
+ grpc_compression_algorithm_to_stream_compression_algorithm(
190
+ compression_algorithm);
191
+ // Hint compression algorithm.
187
192
  grpc_error* error = GRPC_ERROR_NONE;
188
- /* hint compression algorithm */
189
- if (stream_compression_algorithm != GRPC_STREAM_COMPRESS_NONE) {
190
- error = grpc_metadata_batch_add_tail(
191
- initial_metadata, &calld->stream_compression_algorithm_storage,
192
- grpc_stream_compression_encoding_mdelem(stream_compression_algorithm));
193
- } else if (calld->message_compression_algorithm !=
194
- GRPC_MESSAGE_COMPRESS_NONE) {
193
+ if (calld->message_compression_algorithm != GRPC_MESSAGE_COMPRESS_NONE) {
194
+ initialize_state(elem, calld);
195
195
  error = grpc_metadata_batch_add_tail(
196
- initial_metadata, &calld->compression_algorithm_storage,
196
+ initial_metadata, &calld->message_compression_algorithm_storage,
197
197
  grpc_message_compression_encoding_mdelem(
198
198
  calld->message_compression_algorithm));
199
+ } else if (stream_compression_algorithm != GRPC_STREAM_COMPRESS_NONE) {
200
+ initialize_state(elem, calld);
201
+ error = grpc_metadata_batch_add_tail(
202
+ initial_metadata, &calld->stream_compression_algorithm_storage,
203
+ grpc_stream_compression_encoding_mdelem(stream_compression_algorithm));
199
204
  }
200
-
201
205
  if (error != GRPC_ERROR_NONE) return error;
202
-
203
- /* convey supported compression algorithms */
206
+ // Convey supported compression algorithms.
204
207
  error = grpc_metadata_batch_add_tail(
205
208
  initial_metadata, &calld->accept_encoding_storage,
206
209
  GRPC_MDELEM_ACCEPT_ENCODING_FOR_ALGORITHMS(
207
- channeld->supported_message_compression_algorithms));
208
-
210
+ channeld->enabled_message_compression_algorithms_bitset));
209
211
  if (error != GRPC_ERROR_NONE) return error;
210
-
211
- /* Do not overwrite accept-encoding header if it already presents (e.g. added
212
- * by some proxy). */
212
+ // Do not overwrite accept-encoding header if it already presents (e.g., added
213
+ // by some proxy).
213
214
  if (!initial_metadata->idx.named.accept_encoding) {
214
215
  error = grpc_metadata_batch_add_tail(
215
216
  initial_metadata, &calld->accept_stream_encoding_storage,
216
217
  GRPC_MDELEM_ACCEPT_STREAM_ENCODING_FOR_ALGORITHMS(
217
- channeld->supported_stream_compression_algorithms));
218
+ channeld->enabled_stream_compression_algorithms_bitset));
218
219
  }
219
-
220
220
  return error;
221
221
  }
222
222
 
@@ -241,6 +241,8 @@ static void send_message_batch_continue(grpc_call_element* elem) {
241
241
 
242
242
  static void finish_send_message(grpc_call_element* elem) {
243
243
  call_data* calld = static_cast<call_data*>(elem->call_data);
244
+ GPR_DEBUG_ASSERT(calld->message_compression_algorithm !=
245
+ GRPC_MESSAGE_COMPRESS_NONE);
244
246
  // Compress the data if appropriate.
245
247
  grpc_slice_buffer tmp;
246
248
  grpc_slice_buffer_init(&tmp);
@@ -358,12 +360,7 @@ static void on_send_message_next_done(void* arg, grpc_error* error) {
358
360
 
359
361
  static void start_send_message_batch(void* arg, grpc_error* unused) {
360
362
  grpc_call_element* elem = static_cast<grpc_call_element*>(arg);
361
- call_data* calld = static_cast<call_data*>(elem->call_data);
362
- if (skip_compression(
363
- elem,
364
- calld->send_message_batch->payload->send_message.send_message
365
- ->flags(),
366
- calld->send_initial_metadata_state == HAS_COMPRESSION_ALGORITHM)) {
363
+ if (skip_message_compression(elem)) {
367
364
  send_message_batch_continue(elem);
368
365
  } else {
369
366
  continue_reading_send_message(elem);
@@ -380,7 +377,7 @@ static void compress_start_transport_stream_op_batch(
380
377
  calld->cancel_error =
381
378
  GRPC_ERROR_REF(batch->payload->cancel_stream.cancel_error);
382
379
  if (calld->send_message_batch != nullptr) {
383
- if (calld->send_initial_metadata_state == INITIAL_METADATA_UNSEEN) {
380
+ if (!calld->seen_initial_metadata) {
384
381
  GRPC_CALL_COMBINER_START(
385
382
  calld->call_combiner,
386
383
  GRPC_CLOSURE_CREATE(fail_send_message_batch_in_call_combiner, calld,
@@ -398,19 +395,15 @@ static void compress_start_transport_stream_op_batch(
398
395
  }
399
396
  // Handle send_initial_metadata.
400
397
  if (batch->send_initial_metadata) {
401
- GPR_ASSERT(calld->send_initial_metadata_state == INITIAL_METADATA_UNSEEN);
402
- bool has_compression_algorithm;
398
+ GPR_ASSERT(!calld->seen_initial_metadata);
403
399
  grpc_error* error = process_send_initial_metadata(
404
- elem, batch->payload->send_initial_metadata.send_initial_metadata,
405
- &has_compression_algorithm);
400
+ elem, batch->payload->send_initial_metadata.send_initial_metadata);
406
401
  if (error != GRPC_ERROR_NONE) {
407
402
  grpc_transport_stream_op_batch_finish_with_failure(batch, error,
408
403
  calld->call_combiner);
409
404
  return;
410
405
  }
411
- calld->send_initial_metadata_state = has_compression_algorithm
412
- ? HAS_COMPRESSION_ALGORITHM
413
- : NO_COMPRESSION_ALGORITHM;
406
+ calld->seen_initial_metadata = true;
414
407
  // If we had previously received a batch containing a send_message op,
415
408
  // handle it now. Note that we need to re-enter the call combiner
416
409
  // for this, since we can't send two batches down while holding the
@@ -431,7 +424,7 @@ static void compress_start_transport_stream_op_batch(
431
424
  // wait. We save the batch in calld and then drop the call
432
425
  // combiner, which we'll have to pick up again later when we get
433
426
  // send_initial_metadata.
434
- if (calld->send_initial_metadata_state == INITIAL_METADATA_UNSEEN) {
427
+ if (!calld->seen_initial_metadata) {
435
428
  GRPC_CALL_COMBINER_STOP(
436
429
  calld->call_combiner,
437
430
  "send_message batch pending send_initial_metadata");
@@ -463,34 +456,29 @@ static void destroy_call_elem(grpc_call_element* elem,
463
456
  static grpc_error* init_channel_elem(grpc_channel_element* elem,
464
457
  grpc_channel_element_args* args) {
465
458
  channel_data* channeld = static_cast<channel_data*>(elem->channel_data);
466
-
467
- channeld->enabled_algorithms_bitset =
459
+ // Get the enabled and the default algorithms from channel args.
460
+ channeld->enabled_compression_algorithms_bitset =
468
461
  grpc_channel_args_compression_algorithm_get_states(args->channel_args);
469
462
  channeld->default_compression_algorithm =
470
- grpc_channel_args_get_compression_algorithm(args->channel_args);
471
-
472
- /* Make sure the default isn't disabled. */
473
- if (!GPR_BITGET(channeld->enabled_algorithms_bitset,
463
+ grpc_channel_args_get_channel_default_compression_algorithm(
464
+ args->channel_args);
465
+ // Make sure the default is enabled.
466
+ if (!GPR_BITGET(channeld->enabled_compression_algorithms_bitset,
474
467
  channeld->default_compression_algorithm)) {
475
- gpr_log(GPR_DEBUG,
476
- "compression algorithm %d not enabled: switching to none",
477
- channeld->default_compression_algorithm);
468
+ const char* name;
469
+ GPR_ASSERT(grpc_compression_algorithm_name(
470
+ channeld->default_compression_algorithm, &name) == 1);
471
+ gpr_log(GPR_ERROR,
472
+ "default compression algorithm %s not enabled: switching to none",
473
+ name);
478
474
  channeld->default_compression_algorithm = GRPC_COMPRESS_NONE;
479
475
  }
480
-
481
- uint32_t supported_compression_algorithms =
482
- (((1u << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1) &
483
- channeld->enabled_algorithms_bitset) |
484
- 1u;
485
-
486
- channeld->supported_message_compression_algorithms =
476
+ channeld->enabled_message_compression_algorithms_bitset =
487
477
  grpc_compression_bitset_to_message_bitset(
488
- supported_compression_algorithms);
489
-
490
- channeld->supported_stream_compression_algorithms =
478
+ channeld->enabled_compression_algorithms_bitset);
479
+ channeld->enabled_stream_compression_algorithms_bitset =
491
480
  grpc_compression_bitset_to_stream_bitset(
492
- supported_compression_algorithms);
493
-
481
+ channeld->enabled_compression_algorithms_bitset);
494
482
  GPR_ASSERT(!args->is_last);
495
483
  return GRPC_ERROR_NONE;
496
484
  }