grpc 1.28.0 → 1.30.0

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 (497) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +7694 -11190
  3. data/include/grpc/grpc.h +2 -2
  4. data/include/grpc/grpc_security.h +22 -9
  5. data/include/grpc/grpc_security_constants.h +1 -0
  6. data/include/grpc/impl/codegen/grpc_types.h +19 -21
  7. data/include/grpc/impl/codegen/port_platform.h +6 -2
  8. data/include/grpc/module.modulemap +24 -39
  9. data/src/core/ext/filters/client_channel/backend_metric.cc +7 -4
  10. data/src/core/ext/filters/client_channel/client_channel.cc +203 -236
  11. data/src/core/ext/filters/client_channel/client_channel_plugin.cc +3 -2
  12. data/src/core/ext/filters/client_channel/health/health_check_client.cc +7 -22
  13. data/src/core/ext/filters/client_channel/health/health_check_client.h +3 -3
  14. data/src/core/ext/filters/client_channel/http_proxy.cc +17 -10
  15. data/src/core/ext/filters/client_channel/lb_policy.cc +19 -18
  16. data/src/core/ext/filters/client_channel/lb_policy.h +42 -33
  17. data/src/core/ext/filters/client_channel/lb_policy/address_filtering.cc +83 -0
  18. data/src/core/ext/filters/client_channel/lb_policy/address_filtering.h +99 -0
  19. data/src/core/ext/filters/client_channel/lb_policy/child_policy_handler.cc +10 -4
  20. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.cc +240 -301
  21. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_balancer_addresses.cc +89 -0
  22. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_balancer_addresses.h +40 -0
  23. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_channel_secure.cc +11 -9
  24. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_client_stats.h +3 -2
  25. data/src/core/ext/filters/client_channel/lb_policy/priority/priority.cc +871 -0
  26. data/src/core/ext/filters/client_channel/lb_policy/round_robin/round_robin.cc +1 -1
  27. data/src/core/ext/filters/client_channel/lb_policy/subchannel_list.h +5 -11
  28. data/src/core/ext/filters/client_channel/lb_policy/weighted_target/weighted_target.cc +734 -0
  29. data/src/core/ext/filters/client_channel/lb_policy/xds/cds.cc +84 -37
  30. data/src/core/ext/filters/client_channel/lb_policy/xds/eds.cc +938 -0
  31. data/src/core/ext/filters/client_channel/lb_policy/xds/lrs.cc +528 -0
  32. data/src/core/ext/filters/client_channel/lb_policy/xds/xds.h +1 -2
  33. data/src/core/ext/filters/client_channel/lb_policy/xds/xds_routing.cc +834 -0
  34. data/src/core/ext/filters/client_channel/lb_policy_registry.cc +6 -2
  35. data/src/core/ext/filters/client_channel/local_subchannel_pool.h +2 -1
  36. data/src/core/ext/filters/client_channel/parse_address.cc +22 -21
  37. data/src/core/ext/filters/client_channel/resolver.cc +5 -8
  38. data/src/core/ext/filters/client_channel/resolver.h +12 -14
  39. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/dns_resolver_ares.cc +73 -59
  40. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver.cc +35 -35
  41. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver.h +8 -7
  42. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver_libuv.cc +16 -20
  43. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver_posix.cc +4 -4
  44. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver_windows.cc +72 -117
  45. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.cc +184 -133
  46. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.h +5 -3
  47. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper_fallback.cc +7 -4
  48. data/src/core/ext/filters/client_channel/resolver/dns/native/dns_resolver.cc +40 -43
  49. data/src/core/ext/filters/client_channel/resolver/fake/fake_resolver.cc +93 -102
  50. data/src/core/ext/filters/client_channel/resolver/fake/fake_resolver.h +0 -4
  51. data/src/core/ext/filters/client_channel/resolver/sockaddr/sockaddr_resolver.cc +2 -2
  52. data/src/core/ext/filters/client_channel/resolver/xds/xds_resolver.cc +32 -5
  53. data/src/core/ext/filters/client_channel/resolver_factory.h +2 -2
  54. data/src/core/ext/filters/client_channel/resolver_registry.cc +6 -3
  55. data/src/core/ext/filters/client_channel/resolver_registry.h +8 -8
  56. data/src/core/ext/filters/client_channel/resolver_result_parsing.cc +16 -16
  57. data/src/core/ext/filters/client_channel/resolver_result_parsing.h +19 -16
  58. data/src/core/ext/filters/client_channel/resolving_lb_policy.cc +20 -31
  59. data/src/core/ext/filters/client_channel/resolving_lb_policy.h +4 -3
  60. data/src/core/ext/filters/client_channel/server_address.cc +6 -9
  61. data/src/core/ext/filters/client_channel/server_address.h +6 -12
  62. data/src/core/ext/filters/client_channel/service_config.cc +104 -144
  63. data/src/core/ext/filters/client_channel/service_config.h +28 -98
  64. data/src/core/ext/filters/client_channel/service_config_call_data.h +68 -0
  65. data/src/core/ext/filters/client_channel/service_config_parser.cc +87 -0
  66. data/src/core/ext/filters/client_channel/service_config_parser.h +89 -0
  67. data/src/core/ext/filters/client_channel/subchannel.cc +54 -24
  68. data/src/core/ext/filters/client_channel/subchannel.h +35 -11
  69. data/src/core/ext/filters/client_channel/xds/xds_api.cc +348 -221
  70. data/src/core/ext/filters/client_channel/xds/xds_api.h +37 -37
  71. data/src/core/ext/filters/client_channel/xds/xds_bootstrap.cc +44 -49
  72. data/src/core/ext/filters/client_channel/xds/xds_bootstrap.h +4 -3
  73. data/src/core/ext/filters/client_channel/xds/xds_channel_secure.cc +4 -2
  74. data/src/core/ext/filters/client_channel/xds/xds_client.cc +532 -339
  75. data/src/core/ext/filters/client_channel/xds/xds_client.h +57 -22
  76. data/src/core/ext/filters/client_channel/xds/xds_client_stats.cc +11 -12
  77. data/src/core/ext/filters/client_channel/xds/xds_client_stats.h +31 -19
  78. data/src/core/ext/filters/http/client/http_client_filter.cc +23 -28
  79. data/src/core/ext/filters/http/client_authority_filter.cc +4 -4
  80. data/src/core/ext/filters/http/http_filters_plugin.cc +27 -12
  81. data/src/core/ext/filters/http/message_compress/message_compress_filter.cc +258 -221
  82. data/src/core/ext/filters/http/message_compress/message_decompress_filter.cc +358 -0
  83. data/src/core/ext/filters/http/message_compress/message_decompress_filter.h +29 -0
  84. data/src/core/ext/filters/message_size/message_size_filter.cc +7 -10
  85. data/src/core/ext/filters/message_size/message_size_filter.h +4 -4
  86. data/src/core/ext/transport/chttp2/server/chttp2_server.cc +4 -4
  87. data/src/core/ext/transport/chttp2/transport/bin_encoder.cc +4 -6
  88. data/src/core/ext/transport/chttp2/transport/chttp2_transport.cc +23 -22
  89. data/src/core/ext/transport/chttp2/transport/chttp2_transport.h +1 -0
  90. data/src/core/ext/transport/chttp2/transport/flow_control.cc +3 -3
  91. data/src/core/ext/transport/chttp2/transport/frame_goaway.h +2 -3
  92. data/src/core/ext/transport/chttp2/transport/frame_ping.h +2 -3
  93. data/src/core/ext/transport/chttp2/transport/frame_rst_stream.h +2 -3
  94. data/src/core/ext/transport/chttp2/transport/frame_settings.h +2 -3
  95. data/src/core/ext/transport/chttp2/transport/frame_window_update.h +2 -3
  96. data/src/core/ext/transport/chttp2/transport/hpack_encoder.cc +29 -16
  97. data/src/core/ext/transport/chttp2/transport/hpack_encoder.h +2 -3
  98. data/src/core/ext/transport/chttp2/transport/hpack_parser.h +2 -3
  99. data/src/core/ext/transport/chttp2/transport/hpack_table.h +2 -2
  100. data/src/core/ext/transport/chttp2/transport/http2_settings.h +4 -5
  101. data/src/core/ext/transport/chttp2/transport/huffsyms.h +2 -3
  102. data/src/core/ext/transport/chttp2/transport/internal.h +14 -21
  103. data/src/core/ext/transport/chttp2/transport/stream_map.h +2 -3
  104. data/src/core/ext/transport/chttp2/transport/writing.cc +15 -8
  105. data/src/core/ext/transport/inproc/inproc_transport.cc +19 -0
  106. data/src/core/ext/upb-generated/envoy/api/v2/auth/cert.upb.c +4 -229
  107. data/src/core/ext/upb-generated/envoy/api/v2/auth/cert.upb.h +5 -875
  108. data/src/core/ext/upb-generated/envoy/api/v2/auth/common.upb.c +114 -0
  109. data/src/core/ext/upb-generated/envoy/api/v2/auth/common.upb.h +418 -0
  110. data/src/core/ext/upb-generated/envoy/api/v2/auth/secret.upb.c +72 -0
  111. data/src/core/ext/upb-generated/envoy/api/v2/auth/secret.upb.h +197 -0
  112. data/src/core/ext/upb-generated/envoy/api/v2/auth/tls.upb.c +105 -0
  113. data/src/core/ext/upb-generated/envoy/api/v2/auth/tls.upb.h +378 -0
  114. data/src/core/ext/upb-generated/envoy/api/v2/cds.upb.c +1 -0
  115. data/src/core/ext/upb-generated/envoy/api/v2/cluster.upb.c +21 -8
  116. data/src/core/ext/upb-generated/envoy/api/v2/cluster.upb.h +43 -7
  117. data/src/core/ext/upb-generated/envoy/api/v2/cluster/circuit_breaker.upb.c +1 -0
  118. data/src/core/ext/upb-generated/envoy/api/v2/cluster/filter.upb.c +1 -0
  119. data/src/core/ext/upb-generated/envoy/api/v2/cluster/outlier_detection.upb.c +1 -0
  120. data/src/core/ext/upb-generated/envoy/api/v2/core/address.upb.c +2 -1
  121. data/src/core/ext/upb-generated/envoy/api/v2/core/backoff.upb.c +35 -0
  122. data/src/core/ext/upb-generated/envoy/api/v2/core/backoff.upb.h +78 -0
  123. data/src/core/ext/upb-generated/envoy/api/v2/core/base.upb.c +47 -26
  124. data/src/core/ext/upb-generated/envoy/api/v2/core/base.upb.h +115 -65
  125. data/src/core/ext/upb-generated/envoy/api/v2/core/config_source.upb.c +1 -0
  126. data/src/core/ext/upb-generated/envoy/api/v2/core/event_service_config.upb.c +34 -0
  127. data/src/core/ext/upb-generated/envoy/api/v2/core/event_service_config.upb.h +72 -0
  128. data/src/core/ext/upb-generated/envoy/api/v2/core/grpc_service.upb.c +2 -1
  129. data/src/core/ext/upb-generated/envoy/api/v2/core/health_check.upb.c +24 -20
  130. data/src/core/ext/upb-generated/envoy/api/v2/core/health_check.upb.h +28 -13
  131. data/src/core/ext/upb-generated/envoy/api/v2/core/http_uri.upb.c +1 -0
  132. data/src/core/ext/upb-generated/envoy/api/v2/core/protocol.upb.c +38 -18
  133. data/src/core/ext/upb-generated/envoy/api/v2/core/protocol.upb.h +88 -6
  134. data/src/core/ext/upb-generated/envoy/api/v2/core/socket_option.upb.c +34 -0
  135. data/src/core/ext/upb-generated/envoy/api/v2/core/socket_option.upb.h +89 -0
  136. data/src/core/ext/upb-generated/envoy/api/v2/discovery.upb.c +1 -0
  137. data/src/core/ext/upb-generated/envoy/api/v2/eds.upb.c +1 -0
  138. data/src/core/ext/upb-generated/envoy/api/v2/endpoint.upb.c +1 -0
  139. data/src/core/ext/upb-generated/envoy/api/v2/endpoint/endpoint.upb.c +1 -0
  140. data/src/core/ext/upb-generated/envoy/api/v2/endpoint/endpoint_components.upb.c +9 -6
  141. data/src/core/ext/upb-generated/envoy/api/v2/endpoint/endpoint_components.upb.h +12 -4
  142. data/src/core/ext/upb-generated/envoy/api/v2/endpoint/load_report.upb.c +1 -0
  143. data/src/core/ext/upb-generated/envoy/api/v2/lds.upb.c +1 -0
  144. data/src/core/ext/upb-generated/envoy/api/v2/listener.upb.c +15 -10
  145. data/src/core/ext/upb-generated/envoy/api/v2/listener.upb.h +16 -0
  146. data/src/core/ext/upb-generated/envoy/api/v2/listener/listener.upb.c +1 -0
  147. data/src/core/ext/upb-generated/envoy/api/v2/listener/listener_components.upb.c +2 -1
  148. data/src/core/ext/upb-generated/envoy/api/v2/listener/udp_listener_config.upb.c +1 -0
  149. data/src/core/ext/upb-generated/envoy/api/v2/rds.upb.c +1 -0
  150. data/src/core/ext/upb-generated/envoy/api/v2/route.upb.c +1 -0
  151. data/src/core/ext/upb-generated/envoy/api/v2/route/route.upb.c +1 -0
  152. data/src/core/ext/upb-generated/envoy/api/v2/route/route_components.upb.c +63 -41
  153. data/src/core/ext/upb-generated/envoy/api/v2/route/route_components.upb.h +173 -77
  154. data/src/core/ext/upb-generated/envoy/api/v2/scoped_route.upb.c +1 -0
  155. data/src/core/ext/upb-generated/envoy/api/v2/srds.upb.c +1 -0
  156. data/src/core/ext/upb-generated/envoy/config/filter/accesslog/v2/accesslog.upb.c +1 -0
  157. data/src/core/ext/upb-generated/envoy/config/filter/network/http_connection_manager/v2/http_connection_manager.upb.c +48 -28
  158. data/src/core/ext/upb-generated/envoy/config/filter/network/http_connection_manager/v2/http_connection_manager.upb.h +90 -30
  159. data/src/core/ext/upb-generated/envoy/config/listener/v2/api_listener.upb.c +1 -0
  160. data/src/core/ext/upb-generated/envoy/config/trace/v2/http_tracer.upb.c +51 -0
  161. data/src/core/ext/upb-generated/envoy/config/trace/v2/http_tracer.upb.h +125 -0
  162. data/src/core/ext/upb-generated/envoy/service/discovery/v2/ads.upb.c +1 -0
  163. data/src/core/ext/upb-generated/envoy/service/load_stats/v2/lrs.upb.c +4 -2
  164. data/src/core/ext/upb-generated/envoy/service/load_stats/v2/lrs.upb.h +4 -0
  165. data/src/core/ext/upb-generated/envoy/type/http.upb.c +1 -0
  166. data/src/core/ext/upb-generated/envoy/type/matcher/regex.upb.c +16 -0
  167. data/src/core/ext/upb-generated/envoy/type/matcher/regex.upb.h +36 -0
  168. data/src/core/ext/upb-generated/envoy/type/matcher/string.upb.c +1 -0
  169. data/src/core/ext/upb-generated/envoy/type/metadata/v2/metadata.upb.c +1 -0
  170. data/src/core/ext/upb-generated/envoy/type/percent.upb.c +1 -0
  171. data/src/core/ext/upb-generated/envoy/type/range.upb.c +1 -0
  172. data/src/core/ext/upb-generated/envoy/type/semantic_version.upb.c +1 -0
  173. data/src/core/ext/upb-generated/envoy/type/tracing/v2/custom_tag.upb.c +1 -0
  174. data/src/core/ext/upb-generated/google/protobuf/descriptor.upb.c +9 -8
  175. data/src/core/ext/upb-generated/google/protobuf/descriptor.upb.h +30 -24
  176. data/src/core/ext/upb-generated/udpa/annotations/status.upb.c +28 -0
  177. data/src/core/ext/upb-generated/udpa/annotations/status.upb.h +65 -0
  178. data/src/core/ext/upb-generated/validate/validate.upb.c +21 -20
  179. data/src/core/ext/upb-generated/validate/validate.upb.h +69 -63
  180. data/src/core/lib/channel/channel_args.cc +15 -14
  181. data/src/core/lib/channel/channel_args.h +3 -1
  182. data/src/core/lib/channel/channel_stack.h +20 -13
  183. data/src/core/lib/channel/channelz.cc +5 -6
  184. data/src/core/lib/channel/channelz.h +3 -2
  185. data/src/core/lib/channel/channelz_registry.cc +5 -3
  186. data/src/core/lib/channel/connected_channel.cc +7 -5
  187. data/src/core/lib/channel/context.h +1 -1
  188. data/src/core/lib/channel/handshaker.cc +11 -13
  189. data/src/core/lib/channel/handshaker.h +4 -2
  190. data/src/core/lib/channel/handshaker_registry.cc +5 -17
  191. data/src/core/lib/channel/status_util.cc +2 -3
  192. data/src/core/lib/compression/message_compress.cc +5 -1
  193. data/src/core/lib/debug/stats.cc +21 -27
  194. data/src/core/lib/debug/stats.h +3 -1
  195. data/src/core/lib/gpr/spinlock.h +2 -3
  196. data/src/core/lib/gpr/string.cc +2 -26
  197. data/src/core/lib/gpr/string.h +0 -16
  198. data/src/core/lib/gpr/sync_abseil.cc +2 -0
  199. data/src/core/lib/gpr/time.cc +4 -0
  200. data/src/core/lib/gpr/time_posix.cc +1 -1
  201. data/src/core/lib/gprpp/atomic.h +6 -6
  202. data/src/core/lib/gprpp/fork.cc +1 -1
  203. data/src/core/lib/gprpp/host_port.cc +29 -35
  204. data/src/core/lib/gprpp/host_port.h +14 -17
  205. data/src/core/lib/gprpp/map.h +5 -11
  206. data/src/core/lib/gprpp/ref_counted_ptr.h +5 -0
  207. data/src/core/lib/http/format_request.cc +46 -65
  208. data/src/core/lib/http/httpcli.cc +2 -3
  209. data/src/core/lib/http/httpcli.h +2 -3
  210. data/src/core/lib/http/httpcli_security_connector.cc +5 -5
  211. data/src/core/lib/http/parser.h +2 -3
  212. data/src/core/lib/iomgr/buffer_list.h +22 -21
  213. data/src/core/lib/iomgr/call_combiner.h +3 -2
  214. data/src/core/lib/iomgr/cfstream_handle.cc +3 -2
  215. data/src/core/lib/iomgr/closure.h +2 -3
  216. data/src/core/lib/iomgr/dualstack_socket_posix.cc +47 -0
  217. data/src/core/lib/iomgr/endpoint_cfstream.cc +2 -3
  218. data/src/core/lib/iomgr/endpoint_pair.h +2 -3
  219. data/src/core/lib/iomgr/error.cc +6 -9
  220. data/src/core/lib/iomgr/error.h +0 -1
  221. data/src/core/lib/iomgr/ev_apple.cc +356 -0
  222. data/src/core/lib/iomgr/ev_apple.h +43 -0
  223. data/src/core/lib/iomgr/ev_epoll1_linux.cc +20 -23
  224. data/src/core/lib/iomgr/ev_epollex_linux.cc +2 -3
  225. data/src/core/lib/iomgr/ev_poll_posix.cc +3 -3
  226. data/src/core/lib/iomgr/ev_posix.cc +2 -3
  227. data/src/core/lib/iomgr/exec_ctx.h +14 -2
  228. data/src/core/lib/iomgr/iomgr_posix_cfstream.cc +84 -20
  229. data/src/core/lib/iomgr/pollset_set_custom.cc +10 -10
  230. data/src/core/lib/{gprpp/optional.h → iomgr/pollset_uv.h} +11 -12
  231. data/src/core/lib/iomgr/port.h +1 -0
  232. data/src/core/lib/iomgr/python_util.h +46 -0
  233. data/src/core/lib/iomgr/resolve_address.h +4 -6
  234. data/src/core/lib/iomgr/resolve_address_custom.cc +29 -39
  235. data/src/core/lib/iomgr/resolve_address_custom.h +4 -2
  236. data/src/core/lib/iomgr/resolve_address_posix.cc +10 -11
  237. data/src/core/lib/iomgr/resolve_address_windows.cc +8 -17
  238. data/src/core/lib/iomgr/resource_quota.cc +4 -6
  239. data/src/core/lib/iomgr/sockaddr_utils.cc +23 -29
  240. data/src/core/lib/iomgr/sockaddr_utils.h +9 -14
  241. data/src/core/lib/iomgr/socket_factory_posix.h +2 -3
  242. data/src/core/lib/iomgr/socket_mutator.h +2 -3
  243. data/src/core/lib/iomgr/socket_utils_common_posix.cc +7 -26
  244. data/src/core/lib/iomgr/socket_utils_posix.h +3 -0
  245. data/src/core/lib/iomgr/tcp_client_cfstream.cc +5 -7
  246. data/src/core/lib/iomgr/tcp_client_posix.cc +8 -5
  247. data/src/core/lib/iomgr/tcp_client_windows.cc +2 -3
  248. data/src/core/lib/iomgr/tcp_custom.cc +2 -3
  249. data/src/core/lib/iomgr/tcp_server_custom.cc +5 -9
  250. data/src/core/lib/iomgr/tcp_server_posix.cc +5 -4
  251. data/src/core/lib/iomgr/tcp_server_utils_posix_common.cc +5 -4
  252. data/src/core/lib/iomgr/tcp_server_utils_posix_ifaddrs.cc +8 -11
  253. data/src/core/lib/iomgr/tcp_uv.cc +3 -2
  254. data/src/core/lib/iomgr/time_averaged_stats.h +2 -3
  255. data/src/core/lib/iomgr/timer_generic.cc +2 -3
  256. data/src/core/lib/{gprpp/inlined_vector.h → iomgr/timer_generic.h} +19 -17
  257. data/src/core/lib/iomgr/timer_heap.h +2 -3
  258. data/src/core/lib/iomgr/udp_server.cc +9 -14
  259. data/src/core/lib/json/json.h +3 -2
  260. data/src/core/lib/json/json_reader.cc +5 -5
  261. data/src/core/lib/json/json_writer.cc +13 -12
  262. data/src/core/lib/security/credentials/composite/composite_credentials.cc +12 -0
  263. data/src/core/lib/security/credentials/composite/composite_credentials.h +6 -3
  264. data/src/core/lib/security/credentials/credentials.cc +0 -84
  265. data/src/core/lib/security/credentials/credentials.h +8 -59
  266. data/src/core/lib/security/credentials/fake/fake_credentials.h +4 -0
  267. data/src/core/lib/security/credentials/google_default/google_default_credentials.cc +3 -8
  268. data/src/core/lib/security/credentials/iam/iam_credentials.cc +8 -6
  269. data/src/core/lib/security/credentials/iam/iam_credentials.h +4 -0
  270. data/src/core/lib/security/credentials/jwt/json_token.cc +1 -1
  271. data/src/core/lib/security/credentials/jwt/json_token.h +2 -5
  272. data/src/core/lib/security/credentials/jwt/jwt_credentials.h +12 -0
  273. data/src/core/lib/security/credentials/jwt/jwt_verifier.cc +8 -15
  274. data/src/core/lib/security/credentials/jwt/jwt_verifier.h +2 -3
  275. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.cc +55 -27
  276. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.h +9 -3
  277. data/src/core/lib/security/credentials/plugin/plugin_credentials.cc +13 -0
  278. data/src/core/lib/security/credentials/plugin/plugin_credentials.h +2 -0
  279. data/src/core/lib/security/credentials/tls/grpc_tls_credentials_options.cc +23 -13
  280. data/src/core/lib/security/credentials/tls/grpc_tls_credentials_options.h +38 -11
  281. data/src/core/lib/security/security_connector/alts/alts_security_connector.cc +21 -6
  282. data/src/core/lib/security/security_connector/fake/fake_security_connector.cc +7 -7
  283. data/src/core/lib/security/security_connector/load_system_roots_linux.cc +3 -2
  284. data/src/core/lib/security/security_connector/local/local_security_connector.cc +1 -1
  285. data/src/core/lib/security/security_connector/security_connector.h +1 -1
  286. data/src/core/lib/security/security_connector/ssl/ssl_security_connector.cc +20 -25
  287. data/src/core/lib/security/security_connector/ssl/ssl_security_connector.h +4 -6
  288. data/src/core/lib/security/security_connector/ssl_utils.cc +59 -12
  289. data/src/core/lib/security/security_connector/ssl_utils.h +12 -10
  290. data/src/core/lib/security/security_connector/tls/tls_security_connector.cc +77 -51
  291. data/src/core/lib/security/security_connector/tls/tls_security_connector.h +27 -5
  292. data/src/core/lib/security/transport/client_auth_filter.cc +1 -2
  293. data/src/core/lib/slice/slice_intern.cc +2 -3
  294. data/src/core/lib/slice/slice_internal.h +14 -0
  295. data/src/core/lib/slice/slice_utils.h +9 -0
  296. data/src/core/lib/surface/byte_buffer_reader.cc +2 -47
  297. data/src/core/lib/surface/call.cc +2 -3
  298. data/src/core/lib/surface/call_log_batch.cc +50 -58
  299. data/src/core/lib/surface/channel.cc +53 -31
  300. data/src/core/lib/surface/channel.h +35 -4
  301. data/src/core/lib/surface/channel_ping.cc +2 -3
  302. data/src/core/lib/surface/completion_queue.cc +33 -33
  303. data/src/core/lib/surface/event_string.cc +18 -25
  304. data/src/core/lib/surface/event_string.h +3 -1
  305. data/src/core/lib/surface/init_secure.cc +1 -4
  306. data/src/core/lib/surface/server.cc +570 -369
  307. data/src/core/lib/surface/server.h +32 -0
  308. data/src/core/lib/surface/version.cc +2 -2
  309. data/src/core/lib/transport/byte_stream.h +7 -2
  310. data/src/core/lib/transport/connectivity_state.cc +7 -6
  311. data/src/core/lib/transport/connectivity_state.h +5 -3
  312. data/src/core/lib/transport/metadata.cc +3 -3
  313. data/src/core/lib/transport/metadata_batch.h +2 -3
  314. data/src/core/lib/transport/static_metadata.h +1 -1
  315. data/src/core/lib/transport/status_conversion.cc +6 -14
  316. data/src/core/lib/transport/transport.cc +2 -3
  317. data/src/core/lib/transport/transport.h +3 -2
  318. data/src/core/lib/transport/transport_op_string.cc +61 -102
  319. data/src/core/lib/uri/uri_parser.h +2 -3
  320. data/src/core/plugin_registry/grpc_plugin_registry.cc +20 -4
  321. data/src/core/tsi/alts/crypt/aes_gcm.cc +0 -2
  322. data/src/core/tsi/alts/handshaker/alts_handshaker_client.cc +8 -1
  323. data/src/core/tsi/alts/handshaker/alts_handshaker_client.h +8 -4
  324. data/src/core/tsi/alts/handshaker/alts_tsi_handshaker.cc +32 -2
  325. data/src/core/tsi/alts/handshaker/alts_tsi_handshaker.h +9 -1
  326. data/src/core/tsi/alts/zero_copy_frame_protector/alts_grpc_record_protocol_common.h +2 -3
  327. data/src/core/tsi/fake_transport_security.cc +10 -15
  328. data/src/core/tsi/ssl/session_cache/ssl_session.h +0 -2
  329. data/src/core/tsi/ssl/session_cache/ssl_session_cache.h +0 -2
  330. data/src/core/tsi/ssl_transport_security.cc +52 -39
  331. data/src/core/tsi/ssl_transport_security.h +8 -8
  332. data/src/core/tsi/ssl_types.h +0 -2
  333. data/src/core/tsi/transport_security.h +6 -9
  334. data/src/core/tsi/transport_security_grpc.h +2 -3
  335. data/src/core/tsi/transport_security_interface.h +3 -3
  336. data/src/ruby/ext/grpc/rb_call.c +9 -1
  337. data/src/ruby/lib/grpc/errors.rb +103 -42
  338. data/src/ruby/lib/grpc/generic/active_call.rb +2 -3
  339. data/src/ruby/lib/grpc/generic/interceptors.rb +4 -4
  340. data/src/ruby/lib/grpc/generic/rpc_server.rb +9 -10
  341. data/src/ruby/lib/grpc/generic/service.rb +5 -4
  342. data/src/ruby/lib/grpc/structs.rb +1 -1
  343. data/src/ruby/lib/grpc/version.rb +1 -1
  344. data/src/ruby/pb/generate_proto_ruby.sh +5 -3
  345. data/src/ruby/pb/src/proto/grpc/testing/messages_pb.rb +11 -0
  346. data/src/ruby/pb/src/proto/grpc/testing/test_services_pb.rb +16 -0
  347. data/src/ruby/spec/debug_message_spec.rb +134 -0
  348. data/src/ruby/spec/generic/service_spec.rb +2 -0
  349. data/src/ruby/spec/pb/codegen/grpc/testing/package_options_ruby_style.proto +5 -0
  350. data/src/ruby/spec/pb/codegen/package_option_spec.rb +2 -0
  351. data/src/ruby/spec/testdata/ca.pem +18 -13
  352. data/src/ruby/spec/testdata/client.key +26 -14
  353. data/src/ruby/spec/testdata/client.pem +18 -12
  354. data/src/ruby/spec/testdata/server1.key +26 -14
  355. data/src/ruby/spec/testdata/server1.pem +20 -14
  356. data/third_party/abseil-cpp/absl/time/civil_time.cc +175 -0
  357. data/third_party/abseil-cpp/absl/time/civil_time.h +538 -0
  358. data/third_party/abseil-cpp/absl/time/clock.cc +569 -0
  359. data/third_party/abseil-cpp/absl/time/clock.h +74 -0
  360. data/third_party/abseil-cpp/absl/time/duration.cc +922 -0
  361. data/third_party/abseil-cpp/absl/time/format.cc +153 -0
  362. data/third_party/abseil-cpp/absl/time/internal/cctz/include/cctz/civil_time.h +332 -0
  363. data/third_party/abseil-cpp/absl/time/internal/cctz/include/cctz/civil_time_detail.h +622 -0
  364. data/third_party/abseil-cpp/absl/time/internal/cctz/include/cctz/time_zone.h +384 -0
  365. data/third_party/abseil-cpp/absl/time/internal/cctz/include/cctz/zone_info_source.h +102 -0
  366. data/third_party/abseil-cpp/absl/time/internal/cctz/src/civil_time_detail.cc +94 -0
  367. data/third_party/abseil-cpp/absl/time/internal/cctz/src/time_zone_fixed.cc +140 -0
  368. data/third_party/abseil-cpp/absl/time/internal/cctz/src/time_zone_fixed.h +52 -0
  369. data/third_party/abseil-cpp/absl/time/internal/cctz/src/time_zone_format.cc +922 -0
  370. data/third_party/abseil-cpp/absl/time/internal/cctz/src/time_zone_if.cc +45 -0
  371. data/third_party/abseil-cpp/absl/time/internal/cctz/src/time_zone_if.h +76 -0
  372. data/third_party/abseil-cpp/absl/time/internal/cctz/src/time_zone_impl.cc +121 -0
  373. data/third_party/abseil-cpp/absl/time/internal/cctz/src/time_zone_impl.h +93 -0
  374. data/third_party/abseil-cpp/absl/time/internal/cctz/src/time_zone_info.cc +958 -0
  375. data/third_party/abseil-cpp/absl/time/internal/cctz/src/time_zone_info.h +138 -0
  376. data/third_party/abseil-cpp/absl/time/internal/cctz/src/time_zone_libc.cc +308 -0
  377. data/third_party/abseil-cpp/absl/time/internal/cctz/src/time_zone_libc.h +55 -0
  378. data/third_party/abseil-cpp/absl/time/internal/cctz/src/time_zone_lookup.cc +187 -0
  379. data/third_party/abseil-cpp/absl/time/internal/cctz/src/time_zone_posix.cc +159 -0
  380. data/third_party/abseil-cpp/absl/time/internal/cctz/src/time_zone_posix.h +132 -0
  381. data/third_party/abseil-cpp/absl/time/internal/cctz/src/tzfile.h +122 -0
  382. data/third_party/abseil-cpp/absl/time/internal/cctz/src/zone_info_source.cc +115 -0
  383. data/third_party/abseil-cpp/absl/time/internal/get_current_time_chrono.inc +31 -0
  384. data/third_party/abseil-cpp/absl/time/internal/get_current_time_posix.inc +24 -0
  385. data/third_party/abseil-cpp/absl/time/time.cc +499 -0
  386. data/third_party/abseil-cpp/absl/time/time.h +1584 -0
  387. data/third_party/boringssl-with-bazel/err_data.c +329 -297
  388. data/third_party/boringssl-with-bazel/src/crypto/cipher_extra/derive_key.c +1 -1
  389. data/third_party/boringssl-with-bazel/src/crypto/cpu-arm-linux.c +7 -5
  390. data/third_party/boringssl-with-bazel/src/crypto/cpu-intel.c +13 -4
  391. data/third_party/boringssl-with-bazel/src/crypto/crypto.c +11 -0
  392. data/third_party/boringssl-with-bazel/src/{third_party/fiat → crypto/curve25519}/curve25519.c +18 -26
  393. data/third_party/boringssl-with-bazel/src/{third_party/fiat → crypto/curve25519}/curve25519_tables.h +13 -21
  394. data/third_party/boringssl-with-bazel/src/{third_party/fiat → crypto/curve25519}/internal.h +14 -22
  395. data/third_party/boringssl-with-bazel/src/crypto/curve25519/spake25519.c +1 -1
  396. data/third_party/boringssl-with-bazel/src/crypto/dh/dh.c +15 -0
  397. data/third_party/boringssl-with-bazel/src/crypto/dsa/dsa.c +10 -0
  398. data/third_party/boringssl-with-bazel/src/crypto/ec_extra/hash_to_curve.c +425 -0
  399. data/third_party/boringssl-with-bazel/src/crypto/ec_extra/internal.h +78 -0
  400. data/third_party/boringssl-with-bazel/src/crypto/ecdh_extra/ecdh_extra.c +2 -2
  401. data/third_party/boringssl-with-bazel/src/crypto/err/err.c +33 -32
  402. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/aes/aes_nohw.c +1 -1
  403. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/bcm.c +2 -1
  404. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/bn/exponentiation.c +3 -3
  405. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/bn/internal.h +14 -11
  406. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/bn/montgomery.c +8 -8
  407. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/bn/mul.c +30 -154
  408. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/digest/digests.c +16 -0
  409. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/ec.c +289 -117
  410. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/ec_key.c +13 -27
  411. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/ec_montgomery.c +96 -55
  412. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/felem.c +25 -7
  413. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/internal.h +432 -160
  414. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/oct.c +63 -71
  415. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/p224-64.c +5 -14
  416. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/p256-x86_64-table.h +9481 -9485
  417. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/p256-x86_64.c +80 -99
  418. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/p256.c +736 -0
  419. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/p256_table.h +297 -0
  420. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/scalar.c +90 -11
  421. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/simple.c +125 -148
  422. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/simple_mul.c +189 -3
  423. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/wnaf.c +61 -18
  424. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ecdh/ecdh.c +2 -2
  425. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ecdsa/ecdsa.c +20 -5
  426. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/rand/fork_detect.c +137 -0
  427. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/rand/fork_detect.h +49 -0
  428. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/rand/getrandom_fillin.h +64 -0
  429. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/rand/internal.h +41 -5
  430. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/rand/rand.c +32 -17
  431. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/rand/urandom.c +24 -114
  432. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/rsa/blinding.c +4 -0
  433. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/rsa/internal.h +1 -0
  434. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/rsa/rsa.c +51 -38
  435. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/rsa/rsa_impl.c +15 -1
  436. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/sha/sha512.c +44 -35
  437. data/third_party/boringssl-with-bazel/src/crypto/mem.c +29 -12
  438. data/third_party/boringssl-with-bazel/src/crypto/obj/obj_dat.h +15 -1
  439. data/third_party/boringssl-with-bazel/src/crypto/rand_extra/deterministic.c +6 -10
  440. data/third_party/boringssl-with-bazel/src/crypto/rand_extra/windows.c +16 -0
  441. data/third_party/boringssl-with-bazel/src/crypto/thread_pthread.c +4 -0
  442. data/third_party/boringssl-with-bazel/src/crypto/thread_win.c +4 -0
  443. data/third_party/boringssl-with-bazel/src/crypto/trust_token/internal.h +278 -0
  444. data/third_party/boringssl-with-bazel/src/crypto/trust_token/pmbtoken.c +1474 -0
  445. data/third_party/boringssl-with-bazel/src/crypto/trust_token/trust_token.c +720 -0
  446. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_set.c +16 -0
  447. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_vfy.c +5 -0
  448. data/third_party/boringssl-with-bazel/src/crypto/x509/x509name.c +4 -3
  449. data/third_party/boringssl-with-bazel/src/include/openssl/base.h +5 -1
  450. data/third_party/boringssl-with-bazel/src/include/openssl/crypto.h +9 -4
  451. data/third_party/boringssl-with-bazel/src/include/openssl/dh.h +20 -0
  452. data/third_party/boringssl-with-bazel/src/include/openssl/digest.h +1 -0
  453. data/third_party/boringssl-with-bazel/src/include/openssl/dsa.h +16 -0
  454. data/third_party/boringssl-with-bazel/src/include/openssl/ecdsa.h +6 -0
  455. data/third_party/boringssl-with-bazel/src/include/openssl/err.h +2 -0
  456. data/third_party/boringssl-with-bazel/src/include/openssl/nid.h +5 -0
  457. data/third_party/boringssl-with-bazel/src/include/openssl/rand.h +3 -17
  458. data/third_party/boringssl-with-bazel/src/include/openssl/rsa.h +31 -0
  459. data/third_party/boringssl-with-bazel/src/include/openssl/sha.h +26 -0
  460. data/third_party/boringssl-with-bazel/src/include/openssl/ssl.h +172 -77
  461. data/third_party/boringssl-with-bazel/src/include/openssl/tls1.h +1 -0
  462. data/third_party/boringssl-with-bazel/src/include/openssl/trust_token.h +291 -0
  463. data/third_party/boringssl-with-bazel/src/include/openssl/x509.h +5 -3
  464. data/third_party/boringssl-with-bazel/src/include/openssl/x509_vfy.h +1 -0
  465. data/third_party/boringssl-with-bazel/src/ssl/d1_both.cc +0 -4
  466. data/third_party/boringssl-with-bazel/src/ssl/d1_lib.cc +3 -3
  467. data/third_party/boringssl-with-bazel/src/ssl/dtls_method.cc +13 -4
  468. data/third_party/boringssl-with-bazel/src/ssl/handoff.cc +146 -57
  469. data/third_party/boringssl-with-bazel/src/ssl/handshake.cc +14 -3
  470. data/third_party/boringssl-with-bazel/src/ssl/handshake_client.cc +28 -20
  471. data/third_party/boringssl-with-bazel/src/ssl/handshake_server.cc +12 -4
  472. data/third_party/boringssl-with-bazel/src/ssl/internal.h +64 -47
  473. data/third_party/boringssl-with-bazel/src/ssl/s3_both.cc +10 -10
  474. data/third_party/boringssl-with-bazel/src/ssl/s3_lib.cc +2 -2
  475. data/third_party/boringssl-with-bazel/src/ssl/s3_pkt.cc +21 -21
  476. data/third_party/boringssl-with-bazel/src/ssl/ssl_asn1.cc +29 -0
  477. data/third_party/boringssl-with-bazel/src/ssl/ssl_cert.cc +4 -0
  478. data/third_party/boringssl-with-bazel/src/ssl/ssl_lib.cc +6 -1
  479. data/third_party/boringssl-with-bazel/src/ssl/ssl_privkey.cc +13 -2
  480. data/third_party/boringssl-with-bazel/src/ssl/ssl_session.cc +64 -5
  481. data/third_party/boringssl-with-bazel/src/ssl/ssl_stat.cc +6 -0
  482. data/third_party/boringssl-with-bazel/src/ssl/t1_enc.cc +6 -2
  483. data/third_party/boringssl-with-bazel/src/ssl/t1_lib.cc +47 -53
  484. data/third_party/boringssl-with-bazel/src/ssl/tls13_both.cc +1 -1
  485. data/third_party/boringssl-with-bazel/src/ssl/tls13_client.cc +98 -27
  486. data/third_party/boringssl-with-bazel/src/ssl/tls13_enc.cc +23 -75
  487. data/third_party/boringssl-with-bazel/src/ssl/tls13_server.cc +50 -20
  488. data/third_party/boringssl-with-bazel/src/ssl/tls_method.cc +63 -25
  489. data/third_party/boringssl-with-bazel/src/third_party/fiat/curve25519_32.h +245 -175
  490. data/third_party/boringssl-with-bazel/src/third_party/fiat/curve25519_64.h +135 -75
  491. data/third_party/boringssl-with-bazel/src/third_party/fiat/p256_32.h +1593 -1672
  492. data/third_party/boringssl-with-bazel/src/third_party/fiat/p256_64.h +512 -503
  493. metadata +111 -37
  494. data/src/core/ext/filters/client_channel/lb_policy/xds/xds.cc +0 -1754
  495. data/src/core/lib/gprpp/string_view.h +0 -60
  496. data/src/core/tsi/grpc_shadow_boringssl.h +0 -3311
  497. data/third_party/boringssl-with-bazel/src/third_party/fiat/p256.c +0 -1063
@@ -0,0 +1,74 @@
1
+ // Copyright 2017 The Abseil Authors.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // https://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ //
15
+ // -----------------------------------------------------------------------------
16
+ // File: clock.h
17
+ // -----------------------------------------------------------------------------
18
+ //
19
+ // This header file contains utility functions for working with the system-wide
20
+ // realtime clock. For descriptions of the main time abstractions used within
21
+ // this header file, consult the time.h header file.
22
+ #ifndef ABSL_TIME_CLOCK_H_
23
+ #define ABSL_TIME_CLOCK_H_
24
+
25
+ #include "absl/base/macros.h"
26
+ #include "absl/time/time.h"
27
+
28
+ namespace absl {
29
+ ABSL_NAMESPACE_BEGIN
30
+
31
+ // Now()
32
+ //
33
+ // Returns the current time, expressed as an `absl::Time` absolute time value.
34
+ absl::Time Now();
35
+
36
+ // GetCurrentTimeNanos()
37
+ //
38
+ // Returns the current time, expressed as a count of nanoseconds since the Unix
39
+ // Epoch (https://en.wikipedia.org/wiki/Unix_time). Prefer `absl::Now()` instead
40
+ // for all but the most performance-sensitive cases (i.e. when you are calling
41
+ // this function hundreds of thousands of times per second).
42
+ int64_t GetCurrentTimeNanos();
43
+
44
+ // SleepFor()
45
+ //
46
+ // Sleeps for the specified duration, expressed as an `absl::Duration`.
47
+ //
48
+ // Notes:
49
+ // * Signal interruptions will not reduce the sleep duration.
50
+ // * Returns immediately when passed a nonpositive duration.
51
+ void SleepFor(absl::Duration duration);
52
+
53
+ ABSL_NAMESPACE_END
54
+ } // namespace absl
55
+
56
+ // -----------------------------------------------------------------------------
57
+ // Implementation Details
58
+ // -----------------------------------------------------------------------------
59
+
60
+ // In some build configurations we pass --detect-odr-violations to the
61
+ // gold linker. This causes it to flag weak symbol overrides as ODR
62
+ // violations. Because ODR only applies to C++ and not C,
63
+ // --detect-odr-violations ignores symbols not mangled with C++ names.
64
+ // By changing our extension points to be extern "C", we dodge this
65
+ // check.
66
+ extern "C" {
67
+ void AbslInternalSleepFor(absl::Duration duration);
68
+ } // extern "C"
69
+
70
+ inline void absl::SleepFor(absl::Duration duration) {
71
+ AbslInternalSleepFor(duration);
72
+ }
73
+
74
+ #endif // ABSL_TIME_CLOCK_H_
@@ -0,0 +1,922 @@
1
+ // Copyright 2017 The Abseil Authors.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // https://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ // The implementation of the absl::Duration class, which is declared in
16
+ // //absl/time.h. This class behaves like a numeric type; it has no public
17
+ // methods and is used only through the operators defined here.
18
+ //
19
+ // Implementation notes:
20
+ //
21
+ // An absl::Duration is represented as
22
+ //
23
+ // rep_hi_ : (int64_t) Whole seconds
24
+ // rep_lo_ : (uint32_t) Fractions of a second
25
+ //
26
+ // The seconds value (rep_hi_) may be positive or negative as appropriate.
27
+ // The fractional seconds (rep_lo_) is always a positive offset from rep_hi_.
28
+ // The API for Duration guarantees at least nanosecond resolution, which
29
+ // means rep_lo_ could have a max value of 1B - 1 if it stored nanoseconds.
30
+ // However, to utilize more of the available 32 bits of space in rep_lo_,
31
+ // we instead store quarters of a nanosecond in rep_lo_ resulting in a max
32
+ // value of 4B - 1. This allows us to correctly handle calculations like
33
+ // 0.5 nanos + 0.5 nanos = 1 nano. The following example shows the actual
34
+ // Duration rep using quarters of a nanosecond.
35
+ //
36
+ // 2.5 sec = {rep_hi_=2, rep_lo_=2000000000} // lo = 4 * 500000000
37
+ // -2.5 sec = {rep_hi_=-3, rep_lo_=2000000000}
38
+ //
39
+ // Infinite durations are represented as Durations with the rep_lo_ field set
40
+ // to all 1s.
41
+ //
42
+ // +InfiniteDuration:
43
+ // rep_hi_ : kint64max
44
+ // rep_lo_ : ~0U
45
+ //
46
+ // -InfiniteDuration:
47
+ // rep_hi_ : kint64min
48
+ // rep_lo_ : ~0U
49
+ //
50
+ // Arithmetic overflows/underflows to +/- infinity and saturates.
51
+
52
+ #if defined(_MSC_VER)
53
+ #include <winsock2.h> // for timeval
54
+ #endif
55
+
56
+ #include <algorithm>
57
+ #include <cassert>
58
+ #include <cctype>
59
+ #include <cerrno>
60
+ #include <cmath>
61
+ #include <cstdint>
62
+ #include <cstdlib>
63
+ #include <cstring>
64
+ #include <ctime>
65
+ #include <functional>
66
+ #include <limits>
67
+ #include <string>
68
+
69
+ #include "absl/base/casts.h"
70
+ #include "absl/numeric/int128.h"
71
+ #include "absl/time/time.h"
72
+
73
+ namespace absl {
74
+ ABSL_NAMESPACE_BEGIN
75
+
76
+ namespace {
77
+
78
+ using time_internal::kTicksPerNanosecond;
79
+ using time_internal::kTicksPerSecond;
80
+
81
+ constexpr int64_t kint64max = std::numeric_limits<int64_t>::max();
82
+ constexpr int64_t kint64min = std::numeric_limits<int64_t>::min();
83
+
84
+ // Can't use std::isinfinite() because it doesn't exist on windows.
85
+ inline bool IsFinite(double d) {
86
+ if (std::isnan(d)) return false;
87
+ return d != std::numeric_limits<double>::infinity() &&
88
+ d != -std::numeric_limits<double>::infinity();
89
+ }
90
+
91
+ inline bool IsValidDivisor(double d) {
92
+ if (std::isnan(d)) return false;
93
+ return d != 0.0;
94
+ }
95
+
96
+ // Can't use std::round() because it is only available in C++11.
97
+ // Note that we ignore the possibility of floating-point over/underflow.
98
+ template <typename Double>
99
+ inline double Round(Double d) {
100
+ return d < 0 ? std::ceil(d - 0.5) : std::floor(d + 0.5);
101
+ }
102
+
103
+ // *sec may be positive or negative. *ticks must be in the range
104
+ // -kTicksPerSecond < *ticks < kTicksPerSecond. If *ticks is negative it
105
+ // will be normalized to a positive value by adjusting *sec accordingly.
106
+ inline void NormalizeTicks(int64_t* sec, int64_t* ticks) {
107
+ if (*ticks < 0) {
108
+ --*sec;
109
+ *ticks += kTicksPerSecond;
110
+ }
111
+ }
112
+
113
+ // Makes a uint128 from the absolute value of the given scalar.
114
+ inline uint128 MakeU128(int64_t a) {
115
+ uint128 u128 = 0;
116
+ if (a < 0) {
117
+ ++u128;
118
+ ++a; // Makes it safe to negate 'a'
119
+ a = -a;
120
+ }
121
+ u128 += static_cast<uint64_t>(a);
122
+ return u128;
123
+ }
124
+
125
+ // Makes a uint128 count of ticks out of the absolute value of the Duration.
126
+ inline uint128 MakeU128Ticks(Duration d) {
127
+ int64_t rep_hi = time_internal::GetRepHi(d);
128
+ uint32_t rep_lo = time_internal::GetRepLo(d);
129
+ if (rep_hi < 0) {
130
+ ++rep_hi;
131
+ rep_hi = -rep_hi;
132
+ rep_lo = kTicksPerSecond - rep_lo;
133
+ }
134
+ uint128 u128 = static_cast<uint64_t>(rep_hi);
135
+ u128 *= static_cast<uint64_t>(kTicksPerSecond);
136
+ u128 += rep_lo;
137
+ return u128;
138
+ }
139
+
140
+ // Breaks a uint128 of ticks into a Duration.
141
+ inline Duration MakeDurationFromU128(uint128 u128, bool is_neg) {
142
+ int64_t rep_hi;
143
+ uint32_t rep_lo;
144
+ const uint64_t h64 = Uint128High64(u128);
145
+ const uint64_t l64 = Uint128Low64(u128);
146
+ if (h64 == 0) { // fastpath
147
+ const uint64_t hi = l64 / kTicksPerSecond;
148
+ rep_hi = static_cast<int64_t>(hi);
149
+ rep_lo = static_cast<uint32_t>(l64 - hi * kTicksPerSecond);
150
+ } else {
151
+ // kMaxRepHi64 is the high 64 bits of (2^63 * kTicksPerSecond).
152
+ // Any positive tick count whose high 64 bits are >= kMaxRepHi64
153
+ // is not representable as a Duration. A negative tick count can
154
+ // have its high 64 bits == kMaxRepHi64 but only when the low 64
155
+ // bits are all zero, otherwise it is not representable either.
156
+ const uint64_t kMaxRepHi64 = 0x77359400UL;
157
+ if (h64 >= kMaxRepHi64) {
158
+ if (is_neg && h64 == kMaxRepHi64 && l64 == 0) {
159
+ // Avoid trying to represent -kint64min below.
160
+ return time_internal::MakeDuration(kint64min);
161
+ }
162
+ return is_neg ? -InfiniteDuration() : InfiniteDuration();
163
+ }
164
+ const uint128 kTicksPerSecond128 = static_cast<uint64_t>(kTicksPerSecond);
165
+ const uint128 hi = u128 / kTicksPerSecond128;
166
+ rep_hi = static_cast<int64_t>(Uint128Low64(hi));
167
+ rep_lo =
168
+ static_cast<uint32_t>(Uint128Low64(u128 - hi * kTicksPerSecond128));
169
+ }
170
+ if (is_neg) {
171
+ rep_hi = -rep_hi;
172
+ if (rep_lo != 0) {
173
+ --rep_hi;
174
+ rep_lo = kTicksPerSecond - rep_lo;
175
+ }
176
+ }
177
+ return time_internal::MakeDuration(rep_hi, rep_lo);
178
+ }
179
+
180
+ // Convert between int64_t and uint64_t, preserving representation. This
181
+ // allows us to do arithmetic in the unsigned domain, where overflow has
182
+ // well-defined behavior. See operator+=() and operator-=().
183
+ //
184
+ // C99 7.20.1.1.1, as referenced by C++11 18.4.1.2, says, "The typedef
185
+ // name intN_t designates a signed integer type with width N, no padding
186
+ // bits, and a two's complement representation." So, we can convert to
187
+ // and from the corresponding uint64_t value using a bit cast.
188
+ inline uint64_t EncodeTwosComp(int64_t v) {
189
+ return absl::bit_cast<uint64_t>(v);
190
+ }
191
+ inline int64_t DecodeTwosComp(uint64_t v) { return absl::bit_cast<int64_t>(v); }
192
+
193
+ // Note: The overflow detection in this function is done using greater/less *or
194
+ // equal* because kint64max/min is too large to be represented exactly in a
195
+ // double (which only has 53 bits of precision). In order to avoid assigning to
196
+ // rep->hi a double value that is too large for an int64_t (and therefore is
197
+ // undefined), we must consider computations that equal kint64max/min as a
198
+ // double as overflow cases.
199
+ inline bool SafeAddRepHi(double a_hi, double b_hi, Duration* d) {
200
+ double c = a_hi + b_hi;
201
+ if (c >= static_cast<double>(kint64max)) {
202
+ *d = InfiniteDuration();
203
+ return false;
204
+ }
205
+ if (c <= static_cast<double>(kint64min)) {
206
+ *d = -InfiniteDuration();
207
+ return false;
208
+ }
209
+ *d = time_internal::MakeDuration(c, time_internal::GetRepLo(*d));
210
+ return true;
211
+ }
212
+
213
+ // A functor that's similar to std::multiplies<T>, except this returns the max
214
+ // T value instead of overflowing. This is only defined for uint128.
215
+ template <typename Ignored>
216
+ struct SafeMultiply {
217
+ uint128 operator()(uint128 a, uint128 b) const {
218
+ // b hi is always zero because it originated as an int64_t.
219
+ assert(Uint128High64(b) == 0);
220
+ // Fastpath to avoid the expensive overflow check with division.
221
+ if (Uint128High64(a) == 0) {
222
+ return (((Uint128Low64(a) | Uint128Low64(b)) >> 32) == 0)
223
+ ? static_cast<uint128>(Uint128Low64(a) * Uint128Low64(b))
224
+ : a * b;
225
+ }
226
+ return b == 0 ? b : (a > kuint128max / b) ? kuint128max : a * b;
227
+ }
228
+ };
229
+
230
+ // Scales (i.e., multiplies or divides, depending on the Operation template)
231
+ // the Duration d by the int64_t r.
232
+ template <template <typename> class Operation>
233
+ inline Duration ScaleFixed(Duration d, int64_t r) {
234
+ const uint128 a = MakeU128Ticks(d);
235
+ const uint128 b = MakeU128(r);
236
+ const uint128 q = Operation<uint128>()(a, b);
237
+ const bool is_neg = (time_internal::GetRepHi(d) < 0) != (r < 0);
238
+ return MakeDurationFromU128(q, is_neg);
239
+ }
240
+
241
+ // Scales (i.e., multiplies or divides, depending on the Operation template)
242
+ // the Duration d by the double r.
243
+ template <template <typename> class Operation>
244
+ inline Duration ScaleDouble(Duration d, double r) {
245
+ Operation<double> op;
246
+ double hi_doub = op(time_internal::GetRepHi(d), r);
247
+ double lo_doub = op(time_internal::GetRepLo(d), r);
248
+
249
+ double hi_int = 0;
250
+ double hi_frac = std::modf(hi_doub, &hi_int);
251
+
252
+ // Moves hi's fractional bits to lo.
253
+ lo_doub /= kTicksPerSecond;
254
+ lo_doub += hi_frac;
255
+
256
+ double lo_int = 0;
257
+ double lo_frac = std::modf(lo_doub, &lo_int);
258
+
259
+ // Rolls lo into hi if necessary.
260
+ int64_t lo64 = Round(lo_frac * kTicksPerSecond);
261
+
262
+ Duration ans;
263
+ if (!SafeAddRepHi(hi_int, lo_int, &ans)) return ans;
264
+ int64_t hi64 = time_internal::GetRepHi(ans);
265
+ if (!SafeAddRepHi(hi64, lo64 / kTicksPerSecond, &ans)) return ans;
266
+ hi64 = time_internal::GetRepHi(ans);
267
+ lo64 %= kTicksPerSecond;
268
+ NormalizeTicks(&hi64, &lo64);
269
+ return time_internal::MakeDuration(hi64, lo64);
270
+ }
271
+
272
+ // Tries to divide num by den as fast as possible by looking for common, easy
273
+ // cases. If the division was done, the quotient is in *q and the remainder is
274
+ // in *rem and true will be returned.
275
+ inline bool IDivFastPath(const Duration num, const Duration den, int64_t* q,
276
+ Duration* rem) {
277
+ // Bail if num or den is an infinity.
278
+ if (time_internal::IsInfiniteDuration(num) ||
279
+ time_internal::IsInfiniteDuration(den))
280
+ return false;
281
+
282
+ int64_t num_hi = time_internal::GetRepHi(num);
283
+ uint32_t num_lo = time_internal::GetRepLo(num);
284
+ int64_t den_hi = time_internal::GetRepHi(den);
285
+ uint32_t den_lo = time_internal::GetRepLo(den);
286
+
287
+ if (den_hi == 0 && den_lo == kTicksPerNanosecond) {
288
+ // Dividing by 1ns
289
+ if (num_hi >= 0 && num_hi < (kint64max - kTicksPerSecond) / 1000000000) {
290
+ *q = num_hi * 1000000000 + num_lo / kTicksPerNanosecond;
291
+ *rem = time_internal::MakeDuration(0, num_lo % den_lo);
292
+ return true;
293
+ }
294
+ } else if (den_hi == 0 && den_lo == 100 * kTicksPerNanosecond) {
295
+ // Dividing by 100ns (common when converting to Universal time)
296
+ if (num_hi >= 0 && num_hi < (kint64max - kTicksPerSecond) / 10000000) {
297
+ *q = num_hi * 10000000 + num_lo / (100 * kTicksPerNanosecond);
298
+ *rem = time_internal::MakeDuration(0, num_lo % den_lo);
299
+ return true;
300
+ }
301
+ } else if (den_hi == 0 && den_lo == 1000 * kTicksPerNanosecond) {
302
+ // Dividing by 1us
303
+ if (num_hi >= 0 && num_hi < (kint64max - kTicksPerSecond) / 1000000) {
304
+ *q = num_hi * 1000000 + num_lo / (1000 * kTicksPerNanosecond);
305
+ *rem = time_internal::MakeDuration(0, num_lo % den_lo);
306
+ return true;
307
+ }
308
+ } else if (den_hi == 0 && den_lo == 1000000 * kTicksPerNanosecond) {
309
+ // Dividing by 1ms
310
+ if (num_hi >= 0 && num_hi < (kint64max - kTicksPerSecond) / 1000) {
311
+ *q = num_hi * 1000 + num_lo / (1000000 * kTicksPerNanosecond);
312
+ *rem = time_internal::MakeDuration(0, num_lo % den_lo);
313
+ return true;
314
+ }
315
+ } else if (den_hi > 0 && den_lo == 0) {
316
+ // Dividing by positive multiple of 1s
317
+ if (num_hi >= 0) {
318
+ if (den_hi == 1) {
319
+ *q = num_hi;
320
+ *rem = time_internal::MakeDuration(0, num_lo);
321
+ return true;
322
+ }
323
+ *q = num_hi / den_hi;
324
+ *rem = time_internal::MakeDuration(num_hi % den_hi, num_lo);
325
+ return true;
326
+ }
327
+ if (num_lo != 0) {
328
+ num_hi += 1;
329
+ }
330
+ int64_t quotient = num_hi / den_hi;
331
+ int64_t rem_sec = num_hi % den_hi;
332
+ if (rem_sec > 0) {
333
+ rem_sec -= den_hi;
334
+ quotient += 1;
335
+ }
336
+ if (num_lo != 0) {
337
+ rem_sec -= 1;
338
+ }
339
+ *q = quotient;
340
+ *rem = time_internal::MakeDuration(rem_sec, num_lo);
341
+ return true;
342
+ }
343
+
344
+ return false;
345
+ }
346
+
347
+ } // namespace
348
+
349
+ namespace time_internal {
350
+
351
+ // The 'satq' argument indicates whether the quotient should saturate at the
352
+ // bounds of int64_t. If it does saturate, the difference will spill over to
353
+ // the remainder. If it does not saturate, the remainder remain accurate,
354
+ // but the returned quotient will over/underflow int64_t and should not be used.
355
+ int64_t IDivDuration(bool satq, const Duration num, const Duration den,
356
+ Duration* rem) {
357
+ int64_t q = 0;
358
+ if (IDivFastPath(num, den, &q, rem)) {
359
+ return q;
360
+ }
361
+
362
+ const bool num_neg = num < ZeroDuration();
363
+ const bool den_neg = den < ZeroDuration();
364
+ const bool quotient_neg = num_neg != den_neg;
365
+
366
+ if (time_internal::IsInfiniteDuration(num) || den == ZeroDuration()) {
367
+ *rem = num_neg ? -InfiniteDuration() : InfiniteDuration();
368
+ return quotient_neg ? kint64min : kint64max;
369
+ }
370
+ if (time_internal::IsInfiniteDuration(den)) {
371
+ *rem = num;
372
+ return 0;
373
+ }
374
+
375
+ const uint128 a = MakeU128Ticks(num);
376
+ const uint128 b = MakeU128Ticks(den);
377
+ uint128 quotient128 = a / b;
378
+
379
+ if (satq) {
380
+ // Limits the quotient to the range of int64_t.
381
+ if (quotient128 > uint128(static_cast<uint64_t>(kint64max))) {
382
+ quotient128 = quotient_neg ? uint128(static_cast<uint64_t>(kint64min))
383
+ : uint128(static_cast<uint64_t>(kint64max));
384
+ }
385
+ }
386
+
387
+ const uint128 remainder128 = a - quotient128 * b;
388
+ *rem = MakeDurationFromU128(remainder128, num_neg);
389
+
390
+ if (!quotient_neg || quotient128 == 0) {
391
+ return Uint128Low64(quotient128) & kint64max;
392
+ }
393
+ // The quotient needs to be negated, but we need to carefully handle
394
+ // quotient128s with the top bit on.
395
+ return -static_cast<int64_t>(Uint128Low64(quotient128 - 1) & kint64max) - 1;
396
+ }
397
+
398
+ } // namespace time_internal
399
+
400
+ //
401
+ // Additive operators.
402
+ //
403
+
404
+ Duration& Duration::operator+=(Duration rhs) {
405
+ if (time_internal::IsInfiniteDuration(*this)) return *this;
406
+ if (time_internal::IsInfiniteDuration(rhs)) return *this = rhs;
407
+ const int64_t orig_rep_hi = rep_hi_;
408
+ rep_hi_ =
409
+ DecodeTwosComp(EncodeTwosComp(rep_hi_) + EncodeTwosComp(rhs.rep_hi_));
410
+ if (rep_lo_ >= kTicksPerSecond - rhs.rep_lo_) {
411
+ rep_hi_ = DecodeTwosComp(EncodeTwosComp(rep_hi_) + 1);
412
+ rep_lo_ -= kTicksPerSecond;
413
+ }
414
+ rep_lo_ += rhs.rep_lo_;
415
+ if (rhs.rep_hi_ < 0 ? rep_hi_ > orig_rep_hi : rep_hi_ < orig_rep_hi) {
416
+ return *this = rhs.rep_hi_ < 0 ? -InfiniteDuration() : InfiniteDuration();
417
+ }
418
+ return *this;
419
+ }
420
+
421
+ Duration& Duration::operator-=(Duration rhs) {
422
+ if (time_internal::IsInfiniteDuration(*this)) return *this;
423
+ if (time_internal::IsInfiniteDuration(rhs)) {
424
+ return *this = rhs.rep_hi_ >= 0 ? -InfiniteDuration() : InfiniteDuration();
425
+ }
426
+ const int64_t orig_rep_hi = rep_hi_;
427
+ rep_hi_ =
428
+ DecodeTwosComp(EncodeTwosComp(rep_hi_) - EncodeTwosComp(rhs.rep_hi_));
429
+ if (rep_lo_ < rhs.rep_lo_) {
430
+ rep_hi_ = DecodeTwosComp(EncodeTwosComp(rep_hi_) - 1);
431
+ rep_lo_ += kTicksPerSecond;
432
+ }
433
+ rep_lo_ -= rhs.rep_lo_;
434
+ if (rhs.rep_hi_ < 0 ? rep_hi_ < orig_rep_hi : rep_hi_ > orig_rep_hi) {
435
+ return *this = rhs.rep_hi_ >= 0 ? -InfiniteDuration() : InfiniteDuration();
436
+ }
437
+ return *this;
438
+ }
439
+
440
+ //
441
+ // Multiplicative operators.
442
+ //
443
+
444
+ Duration& Duration::operator*=(int64_t r) {
445
+ if (time_internal::IsInfiniteDuration(*this)) {
446
+ const bool is_neg = (r < 0) != (rep_hi_ < 0);
447
+ return *this = is_neg ? -InfiniteDuration() : InfiniteDuration();
448
+ }
449
+ return *this = ScaleFixed<SafeMultiply>(*this, r);
450
+ }
451
+
452
+ Duration& Duration::operator*=(double r) {
453
+ if (time_internal::IsInfiniteDuration(*this) || !IsFinite(r)) {
454
+ const bool is_neg = (std::signbit(r) != 0) != (rep_hi_ < 0);
455
+ return *this = is_neg ? -InfiniteDuration() : InfiniteDuration();
456
+ }
457
+ return *this = ScaleDouble<std::multiplies>(*this, r);
458
+ }
459
+
460
+ Duration& Duration::operator/=(int64_t r) {
461
+ if (time_internal::IsInfiniteDuration(*this) || r == 0) {
462
+ const bool is_neg = (r < 0) != (rep_hi_ < 0);
463
+ return *this = is_neg ? -InfiniteDuration() : InfiniteDuration();
464
+ }
465
+ return *this = ScaleFixed<std::divides>(*this, r);
466
+ }
467
+
468
+ Duration& Duration::operator/=(double r) {
469
+ if (time_internal::IsInfiniteDuration(*this) || !IsValidDivisor(r)) {
470
+ const bool is_neg = (std::signbit(r) != 0) != (rep_hi_ < 0);
471
+ return *this = is_neg ? -InfiniteDuration() : InfiniteDuration();
472
+ }
473
+ return *this = ScaleDouble<std::divides>(*this, r);
474
+ }
475
+
476
+ Duration& Duration::operator%=(Duration rhs) {
477
+ time_internal::IDivDuration(false, *this, rhs, this);
478
+ return *this;
479
+ }
480
+
481
+ double FDivDuration(Duration num, Duration den) {
482
+ // Arithmetic with infinity is sticky.
483
+ if (time_internal::IsInfiniteDuration(num) || den == ZeroDuration()) {
484
+ return (num < ZeroDuration()) == (den < ZeroDuration())
485
+ ? std::numeric_limits<double>::infinity()
486
+ : -std::numeric_limits<double>::infinity();
487
+ }
488
+ if (time_internal::IsInfiniteDuration(den)) return 0.0;
489
+
490
+ double a =
491
+ static_cast<double>(time_internal::GetRepHi(num)) * kTicksPerSecond +
492
+ time_internal::GetRepLo(num);
493
+ double b =
494
+ static_cast<double>(time_internal::GetRepHi(den)) * kTicksPerSecond +
495
+ time_internal::GetRepLo(den);
496
+ return a / b;
497
+ }
498
+
499
+ //
500
+ // Trunc/Floor/Ceil.
501
+ //
502
+
503
+ Duration Trunc(Duration d, Duration unit) {
504
+ return d - (d % unit);
505
+ }
506
+
507
+ Duration Floor(const Duration d, const Duration unit) {
508
+ const absl::Duration td = Trunc(d, unit);
509
+ return td <= d ? td : td - AbsDuration(unit);
510
+ }
511
+
512
+ Duration Ceil(const Duration d, const Duration unit) {
513
+ const absl::Duration td = Trunc(d, unit);
514
+ return td >= d ? td : td + AbsDuration(unit);
515
+ }
516
+
517
+ //
518
+ // Factory functions.
519
+ //
520
+
521
+ Duration DurationFromTimespec(timespec ts) {
522
+ if (static_cast<uint64_t>(ts.tv_nsec) < 1000 * 1000 * 1000) {
523
+ int64_t ticks = ts.tv_nsec * kTicksPerNanosecond;
524
+ return time_internal::MakeDuration(ts.tv_sec, ticks);
525
+ }
526
+ return Seconds(ts.tv_sec) + Nanoseconds(ts.tv_nsec);
527
+ }
528
+
529
+ Duration DurationFromTimeval(timeval tv) {
530
+ if (static_cast<uint64_t>(tv.tv_usec) < 1000 * 1000) {
531
+ int64_t ticks = tv.tv_usec * 1000 * kTicksPerNanosecond;
532
+ return time_internal::MakeDuration(tv.tv_sec, ticks);
533
+ }
534
+ return Seconds(tv.tv_sec) + Microseconds(tv.tv_usec);
535
+ }
536
+
537
+ //
538
+ // Conversion to other duration types.
539
+ //
540
+
541
+ int64_t ToInt64Nanoseconds(Duration d) {
542
+ if (time_internal::GetRepHi(d) >= 0 &&
543
+ time_internal::GetRepHi(d) >> 33 == 0) {
544
+ return (time_internal::GetRepHi(d) * 1000 * 1000 * 1000) +
545
+ (time_internal::GetRepLo(d) / kTicksPerNanosecond);
546
+ }
547
+ return d / Nanoseconds(1);
548
+ }
549
+ int64_t ToInt64Microseconds(Duration d) {
550
+ if (time_internal::GetRepHi(d) >= 0 &&
551
+ time_internal::GetRepHi(d) >> 43 == 0) {
552
+ return (time_internal::GetRepHi(d) * 1000 * 1000) +
553
+ (time_internal::GetRepLo(d) / (kTicksPerNanosecond * 1000));
554
+ }
555
+ return d / Microseconds(1);
556
+ }
557
+ int64_t ToInt64Milliseconds(Duration d) {
558
+ if (time_internal::GetRepHi(d) >= 0 &&
559
+ time_internal::GetRepHi(d) >> 53 == 0) {
560
+ return (time_internal::GetRepHi(d) * 1000) +
561
+ (time_internal::GetRepLo(d) / (kTicksPerNanosecond * 1000 * 1000));
562
+ }
563
+ return d / Milliseconds(1);
564
+ }
565
+ int64_t ToInt64Seconds(Duration d) {
566
+ int64_t hi = time_internal::GetRepHi(d);
567
+ if (time_internal::IsInfiniteDuration(d)) return hi;
568
+ if (hi < 0 && time_internal::GetRepLo(d) != 0) ++hi;
569
+ return hi;
570
+ }
571
+ int64_t ToInt64Minutes(Duration d) {
572
+ int64_t hi = time_internal::GetRepHi(d);
573
+ if (time_internal::IsInfiniteDuration(d)) return hi;
574
+ if (hi < 0 && time_internal::GetRepLo(d) != 0) ++hi;
575
+ return hi / 60;
576
+ }
577
+ int64_t ToInt64Hours(Duration d) {
578
+ int64_t hi = time_internal::GetRepHi(d);
579
+ if (time_internal::IsInfiniteDuration(d)) return hi;
580
+ if (hi < 0 && time_internal::GetRepLo(d) != 0) ++hi;
581
+ return hi / (60 * 60);
582
+ }
583
+
584
+ double ToDoubleNanoseconds(Duration d) {
585
+ return FDivDuration(d, Nanoseconds(1));
586
+ }
587
+ double ToDoubleMicroseconds(Duration d) {
588
+ return FDivDuration(d, Microseconds(1));
589
+ }
590
+ double ToDoubleMilliseconds(Duration d) {
591
+ return FDivDuration(d, Milliseconds(1));
592
+ }
593
+ double ToDoubleSeconds(Duration d) {
594
+ return FDivDuration(d, Seconds(1));
595
+ }
596
+ double ToDoubleMinutes(Duration d) {
597
+ return FDivDuration(d, Minutes(1));
598
+ }
599
+ double ToDoubleHours(Duration d) {
600
+ return FDivDuration(d, Hours(1));
601
+ }
602
+
603
+ timespec ToTimespec(Duration d) {
604
+ timespec ts;
605
+ if (!time_internal::IsInfiniteDuration(d)) {
606
+ int64_t rep_hi = time_internal::GetRepHi(d);
607
+ uint32_t rep_lo = time_internal::GetRepLo(d);
608
+ if (rep_hi < 0) {
609
+ // Tweak the fields so that unsigned division of rep_lo
610
+ // maps to truncation (towards zero) for the timespec.
611
+ rep_lo += kTicksPerNanosecond - 1;
612
+ if (rep_lo >= kTicksPerSecond) {
613
+ rep_hi += 1;
614
+ rep_lo -= kTicksPerSecond;
615
+ }
616
+ }
617
+ ts.tv_sec = rep_hi;
618
+ if (ts.tv_sec == rep_hi) { // no time_t narrowing
619
+ ts.tv_nsec = rep_lo / kTicksPerNanosecond;
620
+ return ts;
621
+ }
622
+ }
623
+ if (d >= ZeroDuration()) {
624
+ ts.tv_sec = std::numeric_limits<time_t>::max();
625
+ ts.tv_nsec = 1000 * 1000 * 1000 - 1;
626
+ } else {
627
+ ts.tv_sec = std::numeric_limits<time_t>::min();
628
+ ts.tv_nsec = 0;
629
+ }
630
+ return ts;
631
+ }
632
+
633
+ timeval ToTimeval(Duration d) {
634
+ timeval tv;
635
+ timespec ts = ToTimespec(d);
636
+ if (ts.tv_sec < 0) {
637
+ // Tweak the fields so that positive division of tv_nsec
638
+ // maps to truncation (towards zero) for the timeval.
639
+ ts.tv_nsec += 1000 - 1;
640
+ if (ts.tv_nsec >= 1000 * 1000 * 1000) {
641
+ ts.tv_sec += 1;
642
+ ts.tv_nsec -= 1000 * 1000 * 1000;
643
+ }
644
+ }
645
+ tv.tv_sec = ts.tv_sec;
646
+ if (tv.tv_sec != ts.tv_sec) { // narrowing
647
+ if (ts.tv_sec < 0) {
648
+ tv.tv_sec = std::numeric_limits<decltype(tv.tv_sec)>::min();
649
+ tv.tv_usec = 0;
650
+ } else {
651
+ tv.tv_sec = std::numeric_limits<decltype(tv.tv_sec)>::max();
652
+ tv.tv_usec = 1000 * 1000 - 1;
653
+ }
654
+ return tv;
655
+ }
656
+ tv.tv_usec = static_cast<int>(ts.tv_nsec / 1000); // suseconds_t
657
+ return tv;
658
+ }
659
+
660
+ std::chrono::nanoseconds ToChronoNanoseconds(Duration d) {
661
+ return time_internal::ToChronoDuration<std::chrono::nanoseconds>(d);
662
+ }
663
+ std::chrono::microseconds ToChronoMicroseconds(Duration d) {
664
+ return time_internal::ToChronoDuration<std::chrono::microseconds>(d);
665
+ }
666
+ std::chrono::milliseconds ToChronoMilliseconds(Duration d) {
667
+ return time_internal::ToChronoDuration<std::chrono::milliseconds>(d);
668
+ }
669
+ std::chrono::seconds ToChronoSeconds(Duration d) {
670
+ return time_internal::ToChronoDuration<std::chrono::seconds>(d);
671
+ }
672
+ std::chrono::minutes ToChronoMinutes(Duration d) {
673
+ return time_internal::ToChronoDuration<std::chrono::minutes>(d);
674
+ }
675
+ std::chrono::hours ToChronoHours(Duration d) {
676
+ return time_internal::ToChronoDuration<std::chrono::hours>(d);
677
+ }
678
+
679
+ //
680
+ // To/From string formatting.
681
+ //
682
+
683
+ namespace {
684
+
685
+ // Formats a positive 64-bit integer in the given field width. Note that
686
+ // it is up to the caller of Format64() to ensure that there is sufficient
687
+ // space before ep to hold the conversion.
688
+ char* Format64(char* ep, int width, int64_t v) {
689
+ do {
690
+ --width;
691
+ *--ep = '0' + (v % 10); // contiguous digits
692
+ } while (v /= 10);
693
+ while (--width >= 0) *--ep = '0'; // zero pad
694
+ return ep;
695
+ }
696
+
697
+ // Helpers for FormatDuration() that format 'n' and append it to 'out'
698
+ // followed by the given 'unit'. If 'n' formats to "0", nothing is
699
+ // appended (not even the unit).
700
+
701
+ // A type that encapsulates how to display a value of a particular unit. For
702
+ // values that are displayed with fractional parts, the precision indicates
703
+ // where to round the value. The precision varies with the display unit because
704
+ // a Duration can hold only quarters of a nanosecond, so displaying information
705
+ // beyond that is just noise.
706
+ //
707
+ // For example, a microsecond value of 42.00025xxxxx should not display beyond 5
708
+ // fractional digits, because it is in the noise of what a Duration can
709
+ // represent.
710
+ struct DisplayUnit {
711
+ const char* abbr;
712
+ int prec;
713
+ double pow10;
714
+ };
715
+ const DisplayUnit kDisplayNano = {"ns", 2, 1e2};
716
+ const DisplayUnit kDisplayMicro = {"us", 5, 1e5};
717
+ const DisplayUnit kDisplayMilli = {"ms", 8, 1e8};
718
+ const DisplayUnit kDisplaySec = {"s", 11, 1e11};
719
+ const DisplayUnit kDisplayMin = {"m", -1, 0.0}; // prec ignored
720
+ const DisplayUnit kDisplayHour = {"h", -1, 0.0}; // prec ignored
721
+
722
+ void AppendNumberUnit(std::string* out, int64_t n, DisplayUnit unit) {
723
+ char buf[sizeof("2562047788015216")]; // hours in max duration
724
+ char* const ep = buf + sizeof(buf);
725
+ char* bp = Format64(ep, 0, n);
726
+ if (*bp != '0' || bp + 1 != ep) {
727
+ out->append(bp, ep - bp);
728
+ out->append(unit.abbr);
729
+ }
730
+ }
731
+
732
+ // Note: unit.prec is limited to double's digits10 value (typically 15) so it
733
+ // always fits in buf[].
734
+ void AppendNumberUnit(std::string* out, double n, DisplayUnit unit) {
735
+ const int buf_size = std::numeric_limits<double>::digits10;
736
+ const int prec = std::min(buf_size, unit.prec);
737
+ char buf[buf_size]; // also large enough to hold integer part
738
+ char* ep = buf + sizeof(buf);
739
+ double d = 0;
740
+ int64_t frac_part = Round(std::modf(n, &d) * unit.pow10);
741
+ int64_t int_part = d;
742
+ if (int_part != 0 || frac_part != 0) {
743
+ char* bp = Format64(ep, 0, int_part); // always < 1000
744
+ out->append(bp, ep - bp);
745
+ if (frac_part != 0) {
746
+ out->push_back('.');
747
+ bp = Format64(ep, prec, frac_part);
748
+ while (ep[-1] == '0') --ep;
749
+ out->append(bp, ep - bp);
750
+ }
751
+ out->append(unit.abbr);
752
+ }
753
+ }
754
+
755
+ } // namespace
756
+
757
+ // From Go's doc at https://golang.org/pkg/time/#Duration.String
758
+ // [FormatDuration] returns a string representing the duration in the
759
+ // form "72h3m0.5s". Leading zero units are omitted. As a special
760
+ // case, durations less than one second format use a smaller unit
761
+ // (milli-, micro-, or nanoseconds) to ensure that the leading digit
762
+ // is non-zero. The zero duration formats as 0, with no unit.
763
+ std::string FormatDuration(Duration d) {
764
+ const Duration min_duration = Seconds(kint64min);
765
+ if (d == min_duration) {
766
+ // Avoid needing to negate kint64min by directly returning what the
767
+ // following code should produce in that case.
768
+ return "-2562047788015215h30m8s";
769
+ }
770
+ std::string s;
771
+ if (d < ZeroDuration()) {
772
+ s.append("-");
773
+ d = -d;
774
+ }
775
+ if (d == InfiniteDuration()) {
776
+ s.append("inf");
777
+ } else if (d < Seconds(1)) {
778
+ // Special case for durations with a magnitude < 1 second. The duration
779
+ // is printed as a fraction of a single unit, e.g., "1.2ms".
780
+ if (d < Microseconds(1)) {
781
+ AppendNumberUnit(&s, FDivDuration(d, Nanoseconds(1)), kDisplayNano);
782
+ } else if (d < Milliseconds(1)) {
783
+ AppendNumberUnit(&s, FDivDuration(d, Microseconds(1)), kDisplayMicro);
784
+ } else {
785
+ AppendNumberUnit(&s, FDivDuration(d, Milliseconds(1)), kDisplayMilli);
786
+ }
787
+ } else {
788
+ AppendNumberUnit(&s, IDivDuration(d, Hours(1), &d), kDisplayHour);
789
+ AppendNumberUnit(&s, IDivDuration(d, Minutes(1), &d), kDisplayMin);
790
+ AppendNumberUnit(&s, FDivDuration(d, Seconds(1)), kDisplaySec);
791
+ }
792
+ if (s.empty() || s == "-") {
793
+ s = "0";
794
+ }
795
+ return s;
796
+ }
797
+
798
+ namespace {
799
+
800
+ // A helper for ParseDuration() that parses a leading number from the given
801
+ // string and stores the result in *int_part/*frac_part/*frac_scale. The
802
+ // given string pointer is modified to point to the first unconsumed char.
803
+ bool ConsumeDurationNumber(const char** dpp, int64_t* int_part,
804
+ int64_t* frac_part, int64_t* frac_scale) {
805
+ *int_part = 0;
806
+ *frac_part = 0;
807
+ *frac_scale = 1; // invariant: *frac_part < *frac_scale
808
+ const char* start = *dpp;
809
+ for (; std::isdigit(**dpp); *dpp += 1) {
810
+ const int d = **dpp - '0'; // contiguous digits
811
+ if (*int_part > kint64max / 10) return false;
812
+ *int_part *= 10;
813
+ if (*int_part > kint64max - d) return false;
814
+ *int_part += d;
815
+ }
816
+ const bool int_part_empty = (*dpp == start);
817
+ if (**dpp != '.') return !int_part_empty;
818
+ for (*dpp += 1; std::isdigit(**dpp); *dpp += 1) {
819
+ const int d = **dpp - '0'; // contiguous digits
820
+ if (*frac_scale <= kint64max / 10) {
821
+ *frac_part *= 10;
822
+ *frac_part += d;
823
+ *frac_scale *= 10;
824
+ }
825
+ }
826
+ return !int_part_empty || *frac_scale != 1;
827
+ }
828
+
829
+ // A helper for ParseDuration() that parses a leading unit designator (e.g.,
830
+ // ns, us, ms, s, m, h) from the given string and stores the resulting unit
831
+ // in "*unit". The given string pointer is modified to point to the first
832
+ // unconsumed char.
833
+ bool ConsumeDurationUnit(const char** start, Duration* unit) {
834
+ const char *s = *start;
835
+ bool ok = true;
836
+ if (strncmp(s, "ns", 2) == 0) {
837
+ s += 2;
838
+ *unit = Nanoseconds(1);
839
+ } else if (strncmp(s, "us", 2) == 0) {
840
+ s += 2;
841
+ *unit = Microseconds(1);
842
+ } else if (strncmp(s, "ms", 2) == 0) {
843
+ s += 2;
844
+ *unit = Milliseconds(1);
845
+ } else if (strncmp(s, "s", 1) == 0) {
846
+ s += 1;
847
+ *unit = Seconds(1);
848
+ } else if (strncmp(s, "m", 1) == 0) {
849
+ s += 1;
850
+ *unit = Minutes(1);
851
+ } else if (strncmp(s, "h", 1) == 0) {
852
+ s += 1;
853
+ *unit = Hours(1);
854
+ } else {
855
+ ok = false;
856
+ }
857
+ *start = s;
858
+ return ok;
859
+ }
860
+
861
+ } // namespace
862
+
863
+ // From Go's doc at https://golang.org/pkg/time/#ParseDuration
864
+ // [ParseDuration] parses a duration string. A duration string is
865
+ // a possibly signed sequence of decimal numbers, each with optional
866
+ // fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m".
867
+ // Valid time units are "ns", "us" "ms", "s", "m", "h".
868
+ bool ParseDuration(const std::string& dur_string, Duration* d) {
869
+ const char* start = dur_string.c_str();
870
+ int sign = 1;
871
+
872
+ if (*start == '-' || *start == '+') {
873
+ sign = *start == '-' ? -1 : 1;
874
+ ++start;
875
+ }
876
+
877
+ // Can't parse a duration from an empty std::string.
878
+ if (*start == '\0') {
879
+ return false;
880
+ }
881
+
882
+ // Special case for a std::string of "0".
883
+ if (*start == '0' && *(start + 1) == '\0') {
884
+ *d = ZeroDuration();
885
+ return true;
886
+ }
887
+
888
+ if (strcmp(start, "inf") == 0) {
889
+ *d = sign * InfiniteDuration();
890
+ return true;
891
+ }
892
+
893
+ Duration dur;
894
+ while (*start != '\0') {
895
+ int64_t int_part;
896
+ int64_t frac_part;
897
+ int64_t frac_scale;
898
+ Duration unit;
899
+ if (!ConsumeDurationNumber(&start, &int_part, &frac_part, &frac_scale) ||
900
+ !ConsumeDurationUnit(&start, &unit)) {
901
+ return false;
902
+ }
903
+ if (int_part != 0) dur += sign * int_part * unit;
904
+ if (frac_part != 0) dur += sign * frac_part * unit / frac_scale;
905
+ }
906
+ *d = dur;
907
+ return true;
908
+ }
909
+
910
+ bool AbslParseFlag(absl::string_view text, Duration* dst, std::string*) {
911
+ return ParseDuration(std::string(text), dst);
912
+ }
913
+
914
+ std::string AbslUnparseFlag(Duration d) { return FormatDuration(d); }
915
+ bool ParseFlag(const std::string& text, Duration* dst, std::string* ) {
916
+ return ParseDuration(text, dst);
917
+ }
918
+
919
+ std::string UnparseFlag(Duration d) { return FormatDuration(d); }
920
+
921
+ ABSL_NAMESPACE_END
922
+ } // namespace absl