omg-activesupport 8.0.0.alpha1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (289) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +86 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.rdoc +40 -0
  5. data/lib/active_support/actionable_error.rb +50 -0
  6. data/lib/active_support/all.rb +5 -0
  7. data/lib/active_support/array_inquirer.rb +50 -0
  8. data/lib/active_support/backtrace_cleaner.rb +163 -0
  9. data/lib/active_support/benchmark.rb +21 -0
  10. data/lib/active_support/benchmarkable.rb +53 -0
  11. data/lib/active_support/broadcast_logger.rb +251 -0
  12. data/lib/active_support/builder.rb +8 -0
  13. data/lib/active_support/cache/coder.rb +153 -0
  14. data/lib/active_support/cache/entry.rb +134 -0
  15. data/lib/active_support/cache/file_store.rb +244 -0
  16. data/lib/active_support/cache/mem_cache_store.rb +290 -0
  17. data/lib/active_support/cache/memory_store.rb +262 -0
  18. data/lib/active_support/cache/null_store.rb +62 -0
  19. data/lib/active_support/cache/redis_cache_store.rb +492 -0
  20. data/lib/active_support/cache/serializer_with_fallback.rb +152 -0
  21. data/lib/active_support/cache/strategy/local_cache.rb +201 -0
  22. data/lib/active_support/cache/strategy/local_cache_middleware.rb +45 -0
  23. data/lib/active_support/cache.rb +1104 -0
  24. data/lib/active_support/callbacks.rb +944 -0
  25. data/lib/active_support/class_attribute.rb +26 -0
  26. data/lib/active_support/code_generator.rb +79 -0
  27. data/lib/active_support/concern.rb +217 -0
  28. data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +72 -0
  29. data/lib/active_support/concurrency/null_lock.rb +13 -0
  30. data/lib/active_support/concurrency/share_lock.rb +225 -0
  31. data/lib/active_support/configurable.rb +159 -0
  32. data/lib/active_support/configuration_file.rb +60 -0
  33. data/lib/active_support/core_ext/array/access.rb +100 -0
  34. data/lib/active_support/core_ext/array/conversions.rb +213 -0
  35. data/lib/active_support/core_ext/array/extract.rb +21 -0
  36. data/lib/active_support/core_ext/array/extract_options.rb +31 -0
  37. data/lib/active_support/core_ext/array/grouping.rb +109 -0
  38. data/lib/active_support/core_ext/array/inquiry.rb +19 -0
  39. data/lib/active_support/core_ext/array/wrap.rb +48 -0
  40. data/lib/active_support/core_ext/array.rb +9 -0
  41. data/lib/active_support/core_ext/benchmark.rb +13 -0
  42. data/lib/active_support/core_ext/big_decimal/conversions.rb +14 -0
  43. data/lib/active_support/core_ext/big_decimal.rb +3 -0
  44. data/lib/active_support/core_ext/class/attribute.rb +122 -0
  45. data/lib/active_support/core_ext/class/attribute_accessors.rb +6 -0
  46. data/lib/active_support/core_ext/class/subclasses.rb +24 -0
  47. data/lib/active_support/core_ext/class.rb +4 -0
  48. data/lib/active_support/core_ext/date/acts_like.rb +10 -0
  49. data/lib/active_support/core_ext/date/blank.rb +18 -0
  50. data/lib/active_support/core_ext/date/calculations.rb +161 -0
  51. data/lib/active_support/core_ext/date/conversions.rb +98 -0
  52. data/lib/active_support/core_ext/date/zones.rb +8 -0
  53. data/lib/active_support/core_ext/date.rb +7 -0
  54. data/lib/active_support/core_ext/date_and_time/calculations.rb +374 -0
  55. data/lib/active_support/core_ext/date_and_time/compatibility.rb +58 -0
  56. data/lib/active_support/core_ext/date_and_time/zones.rb +40 -0
  57. data/lib/active_support/core_ext/date_time/acts_like.rb +16 -0
  58. data/lib/active_support/core_ext/date_time/blank.rb +18 -0
  59. data/lib/active_support/core_ext/date_time/calculations.rb +215 -0
  60. data/lib/active_support/core_ext/date_time/compatibility.rb +18 -0
  61. data/lib/active_support/core_ext/date_time/conversions.rb +106 -0
  62. data/lib/active_support/core_ext/date_time.rb +7 -0
  63. data/lib/active_support/core_ext/digest/uuid.rb +76 -0
  64. data/lib/active_support/core_ext/digest.rb +3 -0
  65. data/lib/active_support/core_ext/enumerable.rb +267 -0
  66. data/lib/active_support/core_ext/erb/util.rb +201 -0
  67. data/lib/active_support/core_ext/file/atomic.rb +72 -0
  68. data/lib/active_support/core_ext/file.rb +3 -0
  69. data/lib/active_support/core_ext/hash/conversions.rb +262 -0
  70. data/lib/active_support/core_ext/hash/deep_merge.rb +42 -0
  71. data/lib/active_support/core_ext/hash/deep_transform_values.rb +46 -0
  72. data/lib/active_support/core_ext/hash/except.rb +12 -0
  73. data/lib/active_support/core_ext/hash/indifferent_access.rb +24 -0
  74. data/lib/active_support/core_ext/hash/keys.rb +143 -0
  75. data/lib/active_support/core_ext/hash/reverse_merge.rb +25 -0
  76. data/lib/active_support/core_ext/hash/slice.rb +27 -0
  77. data/lib/active_support/core_ext/hash.rb +10 -0
  78. data/lib/active_support/core_ext/integer/inflections.rb +31 -0
  79. data/lib/active_support/core_ext/integer/multiple.rb +12 -0
  80. data/lib/active_support/core_ext/integer/time.rb +22 -0
  81. data/lib/active_support/core_ext/integer.rb +5 -0
  82. data/lib/active_support/core_ext/kernel/concern.rb +14 -0
  83. data/lib/active_support/core_ext/kernel/reporting.rb +45 -0
  84. data/lib/active_support/core_ext/kernel/singleton_class.rb +8 -0
  85. data/lib/active_support/core_ext/kernel.rb +5 -0
  86. data/lib/active_support/core_ext/load_error.rb +9 -0
  87. data/lib/active_support/core_ext/module/aliasing.rb +31 -0
  88. data/lib/active_support/core_ext/module/anonymous.rb +30 -0
  89. data/lib/active_support/core_ext/module/attr_internal.rb +49 -0
  90. data/lib/active_support/core_ext/module/attribute_accessors.rb +214 -0
  91. data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +175 -0
  92. data/lib/active_support/core_ext/module/concerning.rb +140 -0
  93. data/lib/active_support/core_ext/module/delegation.rb +225 -0
  94. data/lib/active_support/core_ext/module/deprecation.rb +25 -0
  95. data/lib/active_support/core_ext/module/introspection.rb +62 -0
  96. data/lib/active_support/core_ext/module/redefine_method.rb +40 -0
  97. data/lib/active_support/core_ext/module/remove_method.rb +17 -0
  98. data/lib/active_support/core_ext/module.rb +13 -0
  99. data/lib/active_support/core_ext/name_error.rb +59 -0
  100. data/lib/active_support/core_ext/numeric/bytes.rb +75 -0
  101. data/lib/active_support/core_ext/numeric/conversions.rb +145 -0
  102. data/lib/active_support/core_ext/numeric/time.rb +66 -0
  103. data/lib/active_support/core_ext/numeric.rb +5 -0
  104. data/lib/active_support/core_ext/object/acts_like.rb +45 -0
  105. data/lib/active_support/core_ext/object/blank.rb +199 -0
  106. data/lib/active_support/core_ext/object/conversions.rb +6 -0
  107. data/lib/active_support/core_ext/object/deep_dup.rb +71 -0
  108. data/lib/active_support/core_ext/object/duplicable.rb +69 -0
  109. data/lib/active_support/core_ext/object/inclusion.rb +37 -0
  110. data/lib/active_support/core_ext/object/instance_variables.rb +32 -0
  111. data/lib/active_support/core_ext/object/json.rb +260 -0
  112. data/lib/active_support/core_ext/object/to_param.rb +3 -0
  113. data/lib/active_support/core_ext/object/to_query.rb +87 -0
  114. data/lib/active_support/core_ext/object/try.rb +158 -0
  115. data/lib/active_support/core_ext/object/with.rb +46 -0
  116. data/lib/active_support/core_ext/object/with_options.rb +101 -0
  117. data/lib/active_support/core_ext/object.rb +17 -0
  118. data/lib/active_support/core_ext/pathname/blank.rb +20 -0
  119. data/lib/active_support/core_ext/pathname/existence.rb +23 -0
  120. data/lib/active_support/core_ext/pathname.rb +4 -0
  121. data/lib/active_support/core_ext/range/compare_range.rb +57 -0
  122. data/lib/active_support/core_ext/range/conversions.rb +62 -0
  123. data/lib/active_support/core_ext/range/each.rb +24 -0
  124. data/lib/active_support/core_ext/range/overlap.rb +40 -0
  125. data/lib/active_support/core_ext/range.rb +6 -0
  126. data/lib/active_support/core_ext/regexp.rb +14 -0
  127. data/lib/active_support/core_ext/securerandom.rb +41 -0
  128. data/lib/active_support/core_ext/string/access.rb +95 -0
  129. data/lib/active_support/core_ext/string/behavior.rb +8 -0
  130. data/lib/active_support/core_ext/string/conversions.rb +60 -0
  131. data/lib/active_support/core_ext/string/exclude.rb +13 -0
  132. data/lib/active_support/core_ext/string/filters.rb +151 -0
  133. data/lib/active_support/core_ext/string/indent.rb +45 -0
  134. data/lib/active_support/core_ext/string/inflections.rb +300 -0
  135. data/lib/active_support/core_ext/string/inquiry.rb +16 -0
  136. data/lib/active_support/core_ext/string/multibyte.rb +58 -0
  137. data/lib/active_support/core_ext/string/output_safety.rb +228 -0
  138. data/lib/active_support/core_ext/string/starts_ends_with.rb +6 -0
  139. data/lib/active_support/core_ext/string/strip.rb +27 -0
  140. data/lib/active_support/core_ext/string/zones.rb +16 -0
  141. data/lib/active_support/core_ext/string.rb +15 -0
  142. data/lib/active_support/core_ext/symbol/starts_ends_with.rb +6 -0
  143. data/lib/active_support/core_ext/symbol.rb +3 -0
  144. data/lib/active_support/core_ext/thread/backtrace/location.rb +12 -0
  145. data/lib/active_support/core_ext/time/acts_like.rb +10 -0
  146. data/lib/active_support/core_ext/time/calculations.rb +386 -0
  147. data/lib/active_support/core_ext/time/compatibility.rb +32 -0
  148. data/lib/active_support/core_ext/time/conversions.rb +75 -0
  149. data/lib/active_support/core_ext/time/zones.rb +97 -0
  150. data/lib/active_support/core_ext/time.rb +7 -0
  151. data/lib/active_support/core_ext.rb +5 -0
  152. data/lib/active_support/current_attributes/test_helper.rb +13 -0
  153. data/lib/active_support/current_attributes.rb +233 -0
  154. data/lib/active_support/deep_mergeable.rb +53 -0
  155. data/lib/active_support/delegation.rb +202 -0
  156. data/lib/active_support/dependencies/autoload.rb +72 -0
  157. data/lib/active_support/dependencies/interlock.rb +49 -0
  158. data/lib/active_support/dependencies/require_dependency.rb +28 -0
  159. data/lib/active_support/dependencies.rb +98 -0
  160. data/lib/active_support/deprecation/behaviors.rb +148 -0
  161. data/lib/active_support/deprecation/constant_accessor.rb +74 -0
  162. data/lib/active_support/deprecation/deprecators.rb +104 -0
  163. data/lib/active_support/deprecation/disallowed.rb +54 -0
  164. data/lib/active_support/deprecation/method_wrappers.rb +68 -0
  165. data/lib/active_support/deprecation/proxy_wrappers.rb +189 -0
  166. data/lib/active_support/deprecation/reporting.rb +179 -0
  167. data/lib/active_support/deprecation.rb +81 -0
  168. data/lib/active_support/deprecator.rb +7 -0
  169. data/lib/active_support/descendants_tracker.rb +112 -0
  170. data/lib/active_support/digest.rb +22 -0
  171. data/lib/active_support/duration/iso8601_parser.rb +123 -0
  172. data/lib/active_support/duration/iso8601_serializer.rb +64 -0
  173. data/lib/active_support/duration.rb +520 -0
  174. data/lib/active_support/encrypted_configuration.rb +126 -0
  175. data/lib/active_support/encrypted_file.rb +133 -0
  176. data/lib/active_support/environment_inquirer.rb +40 -0
  177. data/lib/active_support/error_reporter/test_helper.rb +15 -0
  178. data/lib/active_support/error_reporter.rb +265 -0
  179. data/lib/active_support/evented_file_update_checker.rb +182 -0
  180. data/lib/active_support/execution_context/test_helper.rb +13 -0
  181. data/lib/active_support/execution_context.rb +53 -0
  182. data/lib/active_support/execution_wrapper.rb +150 -0
  183. data/lib/active_support/executor/test_helper.rb +7 -0
  184. data/lib/active_support/executor.rb +8 -0
  185. data/lib/active_support/file_update_checker.rb +164 -0
  186. data/lib/active_support/fork_tracker.rb +43 -0
  187. data/lib/active_support/gem_version.rb +17 -0
  188. data/lib/active_support/gzip.rb +40 -0
  189. data/lib/active_support/hash_with_indifferent_access.rb +445 -0
  190. data/lib/active_support/html_safe_translation.rb +56 -0
  191. data/lib/active_support/i18n.rb +17 -0
  192. data/lib/active_support/i18n_railtie.rb +138 -0
  193. data/lib/active_support/inflections.rb +72 -0
  194. data/lib/active_support/inflector/inflections.rb +273 -0
  195. data/lib/active_support/inflector/methods.rb +387 -0
  196. data/lib/active_support/inflector/transliterate.rb +149 -0
  197. data/lib/active_support/inflector.rb +9 -0
  198. data/lib/active_support/isolated_execution_state.rb +75 -0
  199. data/lib/active_support/json/decoding.rb +76 -0
  200. data/lib/active_support/json/encoding.rb +120 -0
  201. data/lib/active_support/json.rb +4 -0
  202. data/lib/active_support/key_generator.rb +66 -0
  203. data/lib/active_support/lazy_load_hooks.rb +107 -0
  204. data/lib/active_support/locale/en.rb +33 -0
  205. data/lib/active_support/locale/en.yml +141 -0
  206. data/lib/active_support/log_subscriber/test_helper.rb +106 -0
  207. data/lib/active_support/log_subscriber.rb +192 -0
  208. data/lib/active_support/logger.rb +55 -0
  209. data/lib/active_support/logger_silence.rb +21 -0
  210. data/lib/active_support/logger_thread_safe_level.rb +47 -0
  211. data/lib/active_support/message_encryptor.rb +374 -0
  212. data/lib/active_support/message_encryptors.rb +141 -0
  213. data/lib/active_support/message_pack/cache_serializer.rb +23 -0
  214. data/lib/active_support/message_pack/extensions.rb +305 -0
  215. data/lib/active_support/message_pack/serializer.rb +63 -0
  216. data/lib/active_support/message_pack.rb +50 -0
  217. data/lib/active_support/message_verifier.rb +368 -0
  218. data/lib/active_support/message_verifiers.rb +135 -0
  219. data/lib/active_support/messages/codec.rb +65 -0
  220. data/lib/active_support/messages/metadata.rb +146 -0
  221. data/lib/active_support/messages/rotation_configuration.rb +23 -0
  222. data/lib/active_support/messages/rotation_coordinator.rb +93 -0
  223. data/lib/active_support/messages/rotator.rb +59 -0
  224. data/lib/active_support/messages/serializer_with_fallback.rb +158 -0
  225. data/lib/active_support/multibyte/chars.rb +178 -0
  226. data/lib/active_support/multibyte/unicode.rb +42 -0
  227. data/lib/active_support/multibyte.rb +23 -0
  228. data/lib/active_support/notifications/fanout.rb +446 -0
  229. data/lib/active_support/notifications/instrumenter.rb +240 -0
  230. data/lib/active_support/notifications.rb +281 -0
  231. data/lib/active_support/number_helper/number_converter.rb +190 -0
  232. data/lib/active_support/number_helper/number_to_currency_converter.rb +46 -0
  233. data/lib/active_support/number_helper/number_to_delimited_converter.rb +30 -0
  234. data/lib/active_support/number_helper/number_to_human_converter.rb +69 -0
  235. data/lib/active_support/number_helper/number_to_human_size_converter.rb +60 -0
  236. data/lib/active_support/number_helper/number_to_percentage_converter.rb +16 -0
  237. data/lib/active_support/number_helper/number_to_phone_converter.rb +60 -0
  238. data/lib/active_support/number_helper/number_to_rounded_converter.rb +59 -0
  239. data/lib/active_support/number_helper/rounding_helper.rb +46 -0
  240. data/lib/active_support/number_helper.rb +479 -0
  241. data/lib/active_support/option_merger.rb +38 -0
  242. data/lib/active_support/ordered_hash.rb +50 -0
  243. data/lib/active_support/ordered_options.rb +147 -0
  244. data/lib/active_support/parameter_filter.rb +157 -0
  245. data/lib/active_support/proxy_object.rb +20 -0
  246. data/lib/active_support/rails.rb +26 -0
  247. data/lib/active_support/railtie.rb +161 -0
  248. data/lib/active_support/reloader.rb +138 -0
  249. data/lib/active_support/rescuable.rb +176 -0
  250. data/lib/active_support/secure_compare_rotator.rb +58 -0
  251. data/lib/active_support/security_utils.rb +38 -0
  252. data/lib/active_support/string_inquirer.rb +35 -0
  253. data/lib/active_support/subscriber.rb +146 -0
  254. data/lib/active_support/syntax_error_proxy.rb +60 -0
  255. data/lib/active_support/tagged_logging.rb +152 -0
  256. data/lib/active_support/test_case.rb +304 -0
  257. data/lib/active_support/testing/assertions.rb +332 -0
  258. data/lib/active_support/testing/autorun.rb +5 -0
  259. data/lib/active_support/testing/constant_lookup.rb +51 -0
  260. data/lib/active_support/testing/constant_stubbing.rb +54 -0
  261. data/lib/active_support/testing/declarative.rb +28 -0
  262. data/lib/active_support/testing/deprecation.rb +82 -0
  263. data/lib/active_support/testing/error_reporter_assertions.rb +107 -0
  264. data/lib/active_support/testing/file_fixtures.rb +38 -0
  265. data/lib/active_support/testing/isolation.rb +121 -0
  266. data/lib/active_support/testing/method_call_assertions.rb +69 -0
  267. data/lib/active_support/testing/parallelization/server.rb +85 -0
  268. data/lib/active_support/testing/parallelization/worker.rb +103 -0
  269. data/lib/active_support/testing/parallelization.rb +55 -0
  270. data/lib/active_support/testing/parallelize_executor.rb +81 -0
  271. data/lib/active_support/testing/setup_and_teardown.rb +57 -0
  272. data/lib/active_support/testing/stream.rb +41 -0
  273. data/lib/active_support/testing/strict_warnings.rb +43 -0
  274. data/lib/active_support/testing/tagged_logging.rb +27 -0
  275. data/lib/active_support/testing/tests_without_assertions.rb +19 -0
  276. data/lib/active_support/testing/time_helpers.rb +269 -0
  277. data/lib/active_support/time.rb +20 -0
  278. data/lib/active_support/time_with_zone.rb +609 -0
  279. data/lib/active_support/values/time_zone.rb +614 -0
  280. data/lib/active_support/version.rb +10 -0
  281. data/lib/active_support/xml_mini/jdom.rb +175 -0
  282. data/lib/active_support/xml_mini/libxml.rb +80 -0
  283. data/lib/active_support/xml_mini/libxmlsax.rb +83 -0
  284. data/lib/active_support/xml_mini/nokogiri.rb +83 -0
  285. data/lib/active_support/xml_mini/nokogirisax.rb +86 -0
  286. data/lib/active_support/xml_mini/rexml.rb +137 -0
  287. data/lib/active_support/xml_mini.rb +211 -0
  288. data/lib/active_support.rb +144 -0
  289. metadata +526 -0
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveSupport # :nodoc:
4
+ module Multibyte
5
+ autoload :Chars, "active_support/multibyte/chars"
6
+ autoload :Unicode, "active_support/multibyte/unicode"
7
+
8
+ # The proxy class returned when calling mb_chars. You can use this accessor
9
+ # to configure your own proxy class so you can support other encodings. See
10
+ # the ActiveSupport::Multibyte::Chars implementation for an example how to
11
+ # do this.
12
+ #
13
+ # ActiveSupport::Multibyte.proxy_class = CharsForUTF32
14
+ def self.proxy_class=(klass)
15
+ @proxy_class = klass
16
+ end
17
+
18
+ # Returns the current proxy class.
19
+ def self.proxy_class
20
+ @proxy_class ||= ActiveSupport::Multibyte::Chars
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,446 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "concurrent/map"
4
+ require "set"
5
+ require "active_support/core_ext/object/try"
6
+
7
+ module ActiveSupport
8
+ module Notifications
9
+ class InstrumentationSubscriberError < RuntimeError
10
+ attr_reader :exceptions
11
+
12
+ def initialize(exceptions)
13
+ @exceptions = exceptions
14
+ exception_class_names = exceptions.map { |e| e.class.name }
15
+ super "Exception(s) occurred within instrumentation subscribers: #{exception_class_names.join(', ')}"
16
+ end
17
+ end
18
+
19
+ module FanoutIteration # :nodoc:
20
+ private
21
+ def iterate_guarding_exceptions(collection)
22
+ exceptions = nil
23
+
24
+ collection.each do |s|
25
+ yield s
26
+ rescue Exception => e
27
+ exceptions ||= []
28
+ exceptions << e
29
+ end
30
+
31
+ if exceptions
32
+ exceptions = exceptions.flat_map do |exception|
33
+ exception.is_a?(InstrumentationSubscriberError) ? exception.exceptions : [exception]
34
+ end
35
+ if exceptions.size == 1
36
+ raise exceptions.first
37
+ else
38
+ raise InstrumentationSubscriberError.new(exceptions), cause: exceptions.first
39
+ end
40
+ end
41
+
42
+ collection
43
+ end
44
+ end
45
+
46
+ # This is a default queue implementation that ships with Notifications.
47
+ # It just pushes events to all registered log subscribers.
48
+ #
49
+ # This class is thread safe. All methods are reentrant.
50
+ class Fanout
51
+ def initialize
52
+ @mutex = Mutex.new
53
+ @string_subscribers = Concurrent::Map.new { |h, k| h.compute_if_absent(k) { [] } }
54
+ @other_subscribers = []
55
+ @all_listeners_for = Concurrent::Map.new
56
+ @groups_for = Concurrent::Map.new
57
+ @silenceable_groups_for = Concurrent::Map.new
58
+ end
59
+
60
+ def inspect # :nodoc:
61
+ total_patterns = @string_subscribers.size + @other_subscribers.size
62
+ "#<#{self.class} (#{total_patterns} patterns)>"
63
+ end
64
+
65
+ def subscribe(pattern = nil, callable = nil, monotonic: false, &block)
66
+ subscriber = Subscribers.new(pattern, callable || block, monotonic)
67
+ @mutex.synchronize do
68
+ case pattern
69
+ when String
70
+ @string_subscribers[pattern] << subscriber
71
+ clear_cache(pattern)
72
+ when NilClass, Regexp
73
+ @other_subscribers << subscriber
74
+ clear_cache
75
+ else
76
+ raise ArgumentError, "pattern must be specified as a String, Regexp or empty"
77
+ end
78
+ end
79
+ subscriber
80
+ end
81
+
82
+ def unsubscribe(subscriber_or_name)
83
+ @mutex.synchronize do
84
+ case subscriber_or_name
85
+ when String
86
+ @string_subscribers[subscriber_or_name].clear
87
+ clear_cache(subscriber_or_name)
88
+ @other_subscribers.each { |sub| sub.unsubscribe!(subscriber_or_name) }
89
+ else
90
+ pattern = subscriber_or_name.try(:pattern)
91
+ if String === pattern
92
+ @string_subscribers[pattern].delete(subscriber_or_name)
93
+ clear_cache(pattern)
94
+ else
95
+ @other_subscribers.delete(subscriber_or_name)
96
+ clear_cache
97
+ end
98
+ end
99
+ end
100
+ end
101
+
102
+ def clear_cache(key = nil) # :nodoc:
103
+ if key
104
+ @all_listeners_for.delete(key)
105
+ @groups_for.delete(key)
106
+ @silenceable_groups_for.delete(key)
107
+ else
108
+ @all_listeners_for.clear
109
+ @groups_for.clear
110
+ @silenceable_groups_for.clear
111
+ end
112
+ end
113
+
114
+ class BaseGroup # :nodoc:
115
+ include FanoutIteration
116
+
117
+ def initialize(listeners, name, id, payload)
118
+ @listeners = listeners
119
+ end
120
+
121
+ def each(&block)
122
+ iterate_guarding_exceptions(@listeners, &block)
123
+ end
124
+ end
125
+
126
+ class BaseTimeGroup < BaseGroup # :nodoc:
127
+ def start(name, id, payload)
128
+ @start_time = now
129
+ end
130
+
131
+ def finish(name, id, payload)
132
+ stop_time = now
133
+ each do |listener|
134
+ listener.call(name, @start_time, stop_time, id, payload)
135
+ end
136
+ end
137
+ end
138
+
139
+ class MonotonicTimedGroup < BaseTimeGroup # :nodoc:
140
+ private
141
+ def now
142
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
143
+ end
144
+ end
145
+
146
+ class TimedGroup < BaseTimeGroup # :nodoc:
147
+ private
148
+ def now
149
+ Time.now
150
+ end
151
+ end
152
+
153
+ class EventedGroup < BaseGroup # :nodoc:
154
+ def start(name, id, payload)
155
+ each do |s|
156
+ s.start(name, id, payload)
157
+ end
158
+ end
159
+
160
+ def finish(name, id, payload)
161
+ each do |s|
162
+ s.finish(name, id, payload)
163
+ end
164
+ end
165
+ end
166
+
167
+ class EventObjectGroup < BaseGroup # :nodoc:
168
+ def start(name, id, payload)
169
+ @event = build_event(name, id, payload)
170
+ @event.start!
171
+ end
172
+
173
+ def finish(name, id, payload)
174
+ @event.payload = payload
175
+ @event.finish!
176
+
177
+ each do |s|
178
+ s.call(@event)
179
+ end
180
+ end
181
+
182
+ private
183
+ def build_event(name, id, payload)
184
+ ActiveSupport::Notifications::Event.new name, nil, nil, id, payload
185
+ end
186
+ end
187
+
188
+ def groups_for(name) # :nodoc:
189
+ groups = @groups_for.compute_if_absent(name) do
190
+ all_listeners_for(name).reject(&:silenceable).group_by(&:group_class).transform_values do |s|
191
+ s.map(&:delegate)
192
+ end
193
+ end
194
+
195
+ silenceable_groups = @silenceable_groups_for.compute_if_absent(name) do
196
+ all_listeners_for(name).select(&:silenceable).group_by(&:group_class).transform_values do |s|
197
+ s.map(&:delegate)
198
+ end
199
+ end
200
+
201
+ unless silenceable_groups.empty?
202
+ groups = groups.dup
203
+ silenceable_groups.each do |group_class, subscriptions|
204
+ active_subscriptions = subscriptions.reject { |s| s.silenced?(name) }
205
+ unless active_subscriptions.empty?
206
+ groups[group_class] = (groups[group_class] || []) + active_subscriptions
207
+ end
208
+ end
209
+ end
210
+
211
+ groups
212
+ end
213
+
214
+ # A +Handle+ is used to record the start and finish time of event.
215
+ #
216
+ # Both #start and #finish must each be called exactly once.
217
+ #
218
+ # Where possible, it's best to use the block form: ActiveSupport::Notifications.instrument.
219
+ # +Handle+ is a low-level API intended for cases where the block form can't be used.
220
+ #
221
+ # handle = ActiveSupport::Notifications.instrumenter.build_handle("my.event", {})
222
+ # begin
223
+ # handle.start
224
+ # # work to be instrumented
225
+ # ensure
226
+ # handle.finish
227
+ # end
228
+ class Handle
229
+ include FanoutIteration
230
+
231
+ def initialize(notifier, name, id, payload) # :nodoc:
232
+ @name = name
233
+ @id = id
234
+ @payload = payload
235
+ @groups = notifier.groups_for(name).map do |group_klass, grouped_listeners|
236
+ group_klass.new(grouped_listeners, name, id, payload)
237
+ end
238
+ @state = :initialized
239
+ end
240
+
241
+ def start
242
+ ensure_state! :initialized
243
+ @state = :started
244
+
245
+ iterate_guarding_exceptions(@groups) do |group|
246
+ group.start(@name, @id, @payload)
247
+ end
248
+ end
249
+
250
+ def finish
251
+ finish_with_values(@name, @id, @payload)
252
+ end
253
+
254
+ def finish_with_values(name, id, payload) # :nodoc:
255
+ ensure_state! :started
256
+ @state = :finished
257
+
258
+ iterate_guarding_exceptions(@groups) do |group|
259
+ group.finish(name, id, payload)
260
+ end
261
+ end
262
+
263
+ private
264
+ def ensure_state!(expected)
265
+ if @state != expected
266
+ raise ArgumentError, "expected state to be #{expected.inspect} but was #{@state.inspect}"
267
+ end
268
+ end
269
+ end
270
+
271
+ include FanoutIteration
272
+
273
+ def build_handle(name, id, payload)
274
+ Handle.new(self, name, id, payload)
275
+ end
276
+
277
+ def start(name, id, payload)
278
+ handle_stack = (IsolatedExecutionState[:_fanout_handle_stack] ||= [])
279
+ handle = build_handle(name, id, payload)
280
+ handle_stack << handle
281
+ handle.start
282
+ end
283
+
284
+ def finish(name, id, payload, listeners = nil)
285
+ handle_stack = IsolatedExecutionState[:_fanout_handle_stack]
286
+ handle = handle_stack.pop
287
+ handle.finish_with_values(name, id, payload)
288
+ end
289
+
290
+ def publish(name, *args)
291
+ iterate_guarding_exceptions(listeners_for(name)) { |s| s.publish(name, *args) }
292
+ end
293
+
294
+ def publish_event(event)
295
+ iterate_guarding_exceptions(listeners_for(event.name)) { |s| s.publish_event(event) }
296
+ end
297
+
298
+ def all_listeners_for(name)
299
+ # this is correctly done double-checked locking (Concurrent::Map's lookups have volatile semantics)
300
+ @all_listeners_for[name] || @mutex.synchronize do
301
+ # use synchronisation when accessing @subscribers
302
+ @all_listeners_for[name] ||=
303
+ @string_subscribers[name] + @other_subscribers.select { |s| s.subscribed_to?(name) }
304
+ end
305
+ end
306
+
307
+ def listeners_for(name)
308
+ all_listeners_for(name).reject { |s| s.silenced?(name) }
309
+ end
310
+
311
+ def listening?(name)
312
+ all_listeners_for(name).any? { |s| !s.silenced?(name) }
313
+ end
314
+
315
+ # This is a sync queue, so there is no waiting.
316
+ def wait
317
+ end
318
+
319
+ module Subscribers # :nodoc:
320
+ def self.new(pattern, listener, monotonic)
321
+ subscriber_class = monotonic ? MonotonicTimed : Timed
322
+
323
+ if listener.respond_to?(:start) && listener.respond_to?(:finish)
324
+ subscriber_class = Evented
325
+ else
326
+ # Doing this to detect a single argument block or callable
327
+ # like `proc { |x| }` vs `proc { |*x| }`, `proc { |**x| }`,
328
+ # or `proc { |x, **y| }`
329
+ procish = listener.respond_to?(:parameters) ? listener : listener.method(:call)
330
+
331
+ if procish.arity == 1 && procish.parameters.length == 1
332
+ subscriber_class = EventObject
333
+ end
334
+ end
335
+
336
+ subscriber_class.new(pattern, listener)
337
+ end
338
+
339
+ class Matcher # :nodoc:
340
+ attr_reader :pattern, :exclusions
341
+
342
+ def self.wrap(pattern)
343
+ if String === pattern
344
+ pattern
345
+ elsif pattern.nil?
346
+ AllMessages.new
347
+ else
348
+ new(pattern)
349
+ end
350
+ end
351
+
352
+ def initialize(pattern)
353
+ @pattern = pattern
354
+ @exclusions = Set.new
355
+ end
356
+
357
+ def unsubscribe!(name)
358
+ exclusions << -name if pattern === name
359
+ end
360
+
361
+ def ===(name)
362
+ pattern === name && !exclusions.include?(name)
363
+ end
364
+
365
+ class AllMessages
366
+ def ===(name)
367
+ true
368
+ end
369
+
370
+ def unsubscribe!(*)
371
+ false
372
+ end
373
+ end
374
+ end
375
+
376
+ class Evented # :nodoc:
377
+ attr_reader :pattern, :delegate, :silenceable
378
+
379
+ def initialize(pattern, delegate)
380
+ @pattern = Matcher.wrap(pattern)
381
+ @delegate = delegate
382
+ @silenceable = delegate.respond_to?(:silenced?)
383
+ @can_publish = delegate.respond_to?(:publish)
384
+ @can_publish_event = delegate.respond_to?(:publish_event)
385
+ end
386
+
387
+ def group_class
388
+ EventedGroup
389
+ end
390
+
391
+ def publish(name, *args)
392
+ if @can_publish
393
+ @delegate.publish name, *args
394
+ end
395
+ end
396
+
397
+ def publish_event(event)
398
+ if @can_publish_event
399
+ @delegate.publish_event event
400
+ else
401
+ publish(event.name, event.time, event.end, event.transaction_id, event.payload)
402
+ end
403
+ end
404
+
405
+ def silenced?(name)
406
+ @silenceable && @delegate.silenced?(name)
407
+ end
408
+
409
+ def subscribed_to?(name)
410
+ pattern === name
411
+ end
412
+
413
+ def unsubscribe!(name)
414
+ pattern.unsubscribe!(name)
415
+ end
416
+ end
417
+
418
+ class Timed < Evented # :nodoc:
419
+ def group_class
420
+ TimedGroup
421
+ end
422
+
423
+ def publish(name, *args)
424
+ @delegate.call name, *args
425
+ end
426
+ end
427
+
428
+ class MonotonicTimed < Timed # :nodoc:
429
+ def group_class
430
+ MonotonicTimedGroup
431
+ end
432
+ end
433
+
434
+ class EventObject < Evented
435
+ def group_class
436
+ EventObjectGroup
437
+ end
438
+
439
+ def publish_event(event)
440
+ @delegate.call event
441
+ end
442
+ end
443
+ end
444
+ end
445
+ end
446
+ end
@@ -0,0 +1,240 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/module/delegation"
4
+ require "securerandom"
5
+
6
+ module ActiveSupport
7
+ module Notifications
8
+ # Instrumenters are stored in a thread local.
9
+ class Instrumenter
10
+ attr_reader :id
11
+
12
+ def initialize(notifier)
13
+ unless notifier.respond_to?(:build_handle)
14
+ notifier = LegacyHandle::Wrapper.new(notifier)
15
+ end
16
+
17
+ @id = unique_id
18
+ @notifier = notifier
19
+ end
20
+
21
+ class LegacyHandle # :nodoc:
22
+ class Wrapper # :nodoc:
23
+ def initialize(notifier)
24
+ @notifier = notifier
25
+ end
26
+
27
+ def build_handle(name, id, payload)
28
+ LegacyHandle.new(@notifier, name, id, payload)
29
+ end
30
+
31
+ delegate :start, :finish, to: :@notifier
32
+ end
33
+
34
+ def initialize(notifier, name, id, payload)
35
+ @notifier = notifier
36
+ @name = name
37
+ @id = id
38
+ @payload = payload
39
+ end
40
+
41
+ def start
42
+ @listener_state = @notifier.start @name, @id, @payload
43
+ end
44
+
45
+ def finish
46
+ @notifier.finish(@name, @id, @payload, @listener_state)
47
+ end
48
+ end
49
+
50
+ # Given a block, instrument it by measuring the time taken to execute
51
+ # and publish it. Without a block, simply send a message via the
52
+ # notifier. Notice that events get sent even if an error occurs in the
53
+ # passed-in block.
54
+ def instrument(name, payload = {})
55
+ handle = build_handle(name, payload)
56
+ handle.start
57
+ begin
58
+ yield payload if block_given?
59
+ rescue Exception => e
60
+ payload[:exception] = [e.class.name, e.message]
61
+ payload[:exception_object] = e
62
+ raise e
63
+ ensure
64
+ handle.finish
65
+ end
66
+ end
67
+
68
+ # Returns a "handle" for an event with the given +name+ and +payload+.
69
+ #
70
+ # #start and #finish must each be called exactly once on the returned object.
71
+ #
72
+ # Where possible, it's best to use #instrument, which will record the
73
+ # start and finish of the event and correctly handle any exceptions.
74
+ # +build_handle+ is a low-level API intended for cases where using
75
+ # +instrument+ isn't possible.
76
+ #
77
+ # See ActiveSupport::Notifications::Fanout::Handle.
78
+ def build_handle(name, payload)
79
+ @notifier.build_handle(name, @id, payload)
80
+ end
81
+
82
+ def new_event(name, payload = {}) # :nodoc:
83
+ Event.new(name, nil, nil, @id, payload)
84
+ end
85
+
86
+ # Send a start notification with +name+ and +payload+.
87
+ def start(name, payload)
88
+ @notifier.start name, @id, payload
89
+ end
90
+
91
+ # Send a finish notification with +name+ and +payload+.
92
+ def finish(name, payload)
93
+ @notifier.finish name, @id, payload
94
+ end
95
+
96
+ def finish_with_state(listeners_state, name, payload)
97
+ @notifier.finish name, @id, payload, listeners_state
98
+ end
99
+
100
+ private
101
+ def unique_id
102
+ SecureRandom.hex(10)
103
+ end
104
+ end
105
+
106
+ class Event
107
+ attr_reader :name, :transaction_id
108
+ attr_accessor :payload
109
+
110
+ def initialize(name, start, ending, transaction_id, payload)
111
+ @name = name
112
+ @payload = payload.dup
113
+ @time = start ? start.to_f * 1_000.0 : start
114
+ @transaction_id = transaction_id
115
+ @end = ending ? ending.to_f * 1_000.0 : ending
116
+ @cpu_time_start = 0.0
117
+ @cpu_time_finish = 0.0
118
+ @allocation_count_start = 0
119
+ @allocation_count_finish = 0
120
+ @gc_time_start = 0
121
+ @gc_time_finish = 0
122
+ end
123
+
124
+ def time
125
+ @time / 1000.0 if @time
126
+ end
127
+
128
+ def end
129
+ @end / 1000.0 if @end
130
+ end
131
+
132
+ def record # :nodoc:
133
+ start!
134
+ begin
135
+ yield payload if block_given?
136
+ rescue Exception => e
137
+ payload[:exception] = [e.class.name, e.message]
138
+ payload[:exception_object] = e
139
+ raise e
140
+ ensure
141
+ finish!
142
+ end
143
+ end
144
+
145
+ # Record information at the time this event starts
146
+ def start!
147
+ @time = now
148
+ @cpu_time_start = now_cpu
149
+ @gc_time_start = now_gc
150
+ @allocation_count_start = now_allocations
151
+ end
152
+
153
+ # Record information at the time this event finishes
154
+ def finish!
155
+ @cpu_time_finish = now_cpu
156
+ @gc_time_finish = now_gc
157
+ @end = now
158
+ @allocation_count_finish = now_allocations
159
+ end
160
+
161
+ # Returns the CPU time (in milliseconds) passed between the call to
162
+ # #start! and the call to #finish!.
163
+ def cpu_time
164
+ @cpu_time_finish - @cpu_time_start
165
+ end
166
+
167
+ # Returns the idle time time (in milliseconds) passed between the call to
168
+ # #start! and the call to #finish!.
169
+ def idle_time
170
+ diff = duration - cpu_time
171
+ diff > 0.0 ? diff : 0.0
172
+ end
173
+
174
+ # Returns the number of allocations made between the call to #start! and
175
+ # the call to #finish!.
176
+ def allocations
177
+ @allocation_count_finish - @allocation_count_start
178
+ end
179
+
180
+ # Returns the time spent in GC (in milliseconds) between the call to #start!
181
+ # and the call to #finish!
182
+ def gc_time
183
+ (@gc_time_finish - @gc_time_start) / 1_000_000.0
184
+ end
185
+
186
+ # Returns the difference in milliseconds between when the execution of the
187
+ # event started and when it ended.
188
+ #
189
+ # ActiveSupport::Notifications.subscribe('wait') do |event|
190
+ # @event = event
191
+ # end
192
+ #
193
+ # ActiveSupport::Notifications.instrument('wait') do
194
+ # sleep 1
195
+ # end
196
+ #
197
+ # @event.duration # => 1000.138
198
+ def duration
199
+ @end - @time
200
+ end
201
+
202
+ private
203
+ def now
204
+ Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
205
+ end
206
+
207
+ begin
208
+ Process.clock_gettime(Process::CLOCK_THREAD_CPUTIME_ID, :float_millisecond)
209
+
210
+ def now_cpu
211
+ Process.clock_gettime(Process::CLOCK_THREAD_CPUTIME_ID, :float_millisecond)
212
+ end
213
+ rescue
214
+ def now_cpu
215
+ 0.0
216
+ end
217
+ end
218
+
219
+ if GC.respond_to?(:total_time)
220
+ def now_gc
221
+ GC.total_time
222
+ end
223
+ else
224
+ def now_gc
225
+ 0
226
+ end
227
+ end
228
+
229
+ if GC.stat.key?(:total_allocated_objects)
230
+ def now_allocations
231
+ GC.stat(:total_allocated_objects)
232
+ end
233
+ else # Likely on JRuby, TruffleRuby
234
+ def now_allocations
235
+ 0
236
+ end
237
+ end
238
+ end
239
+ end
240
+ end