diff --git a/lib/agentry/bot.rb b/lib/agentry/bot.rb index 3790b88..0a6ae1a 100644 --- a/lib/agentry/bot.rb +++ b/lib/agentry/bot.rb @@ -46,9 +46,9 @@ def self.category_by_keyword end # Builds and Agentry.cache_size='s rebuild hold this lock, shared with the - # subclasses, and check again under it; reads never take it (off CRuby a - # pattern read takes its table's lock; a built trie never changes). - # Each class keeps what it built, freed with the class. + # subclasses, and check again under it; reads never take it: a write + # replaces the frozen table of patterns whole, and a built trie never + # changes. Each class keeps what it built, freed with the class. KEYWORDS_LOCK = Mutex.new # Lists an app builds on each call would pile up. MAX_LISTS = 64 @@ -63,12 +63,15 @@ def self.keywords_pattern(keywords) return pattern if built_from && unchanged?(keywords, built_from) KEYWORDS_LOCK.synchronize do - @pattern_by_list ||= Cache.send(:table).compare_by_identity - built_from, pattern = @pattern_by_list[keywords] + built_from, pattern = @pattern_by_list&.[](keywords) next pattern if built_from && unchanged?(keywords, built_from) - @pattern_by_list.clear if @pattern_by_list.size >= MAX_LISTS - (@pattern_by_list[keywords] = build_keywords_pattern(keywords)).last + built = build_keywords_pattern(keywords) + pattern_by_list = (@pattern_by_list.nil? || @pattern_by_list.size >= MAX_LISTS) ? + {}.compare_by_identity : @pattern_by_list.dup + pattern_by_list[keywords] = built + @pattern_by_list = pattern_by_list.freeze + built.last end end @@ -134,8 +137,12 @@ def self.self_and_subclasses end def self.rebuild_own_keywords - @pattern_by_list&.to_a&.each do |list, (keywords, _pattern)| - @pattern_by_list[list] = build_keywords_pattern(list) unless keywords_copy(list) == keywords + if @pattern_by_list + pattern_by_list = {}.compare_by_identity + @pattern_by_list.each do |list, built| + pattern_by_list[list] = (keywords_copy(list) == built.first) ? built : build_keywords_pattern(list) + end + @pattern_by_list = pattern_by_list.freeze end list, keywords, = @known_bots_trie @known_bots_trie = [list, *build_known_bots_trie(list)] if list && keywords_copy(list) != keywords diff --git a/lib/agentry/cache.rb b/lib/agentry/cache.rb index 3d2efa7..b38cfac 100644 --- a/lib/agentry/cache.rb +++ b/lib/agentry/cache.rb @@ -4,11 +4,13 @@ module Agentry # The detections of the last user agents, the parsed Accept-Language # headers, Client Hints and the versions compared, shared by the threads # of a process. Writes take the lock; on CRuby reads don't: under the GVL a - # Hash read is atomic, and a thread that misses detects again. + # Hash read is atomic, and a thread that misses detects again. JRuby reads a + # concurrent map, TruffleRuby a table locked on reads too. module Cache - # JRuby and TruffleRuby run threads in parallel: a Hash read racing an - # insert or a clear may raise or miss, so their reads lock each table. - # Only [] runs outside the writers' lock: size and to_a need no lock here. + # TruffleRuby and any other Ruby but CRuby and JRuby: threads run in + # parallel, and a Hash read racing an insert or a clear may raise or miss, + # so reads lock each table. Only [] runs outside the writers' lock: size + # needs no lock here. class LockedHash < Hash def initialize super @@ -28,12 +30,54 @@ def clear end end + # JRuby runs threads in parallel too, and has a map whose reads take no + # lock: a lock on each read kept every thread at one thread's pace (#93). + # Keys reach it as Ruby Strings, compared like Hash keys: JRuby would + # otherwise convert each to a Java String, losing its encoding and bytes + # that aren't valid, or raise for an encoding Java lacks. Keys are Strings. + class ConcurrentTable + RUBY_STRING = Java::OrgJruby::RubyString if RUBY_ENGINE == "jruby" + + def initialize + @map = java.util.concurrent.ConcurrentHashMap.new + end + + def [](key) + @map.get(key.to_java(RUBY_STRING)) + end + + def []=(key, value) + @map.put(key.to_java(RUBY_STRING), value) + value + end + + def size + @map.size + end + + def empty? + @map.isEmpty + end + + def keys + @map.keySet.to_a + end + + def clear + @map.clear + end + end + def self.table_class(engine) - (engine == "ruby") ? Hash : LockedHash + case engine + when "ruby" then Hash + when "jruby" then ConcurrentTable + else LockedHash + end end TABLE = table_class(RUBY_ENGINE) - private_constant :LockedHash, :TABLE + private_constant :LockedHash, :ConcurrentTable, :TABLE def self.table TABLE.new diff --git a/test/unit/cache_test.rb b/test/unit/cache_test.rb index 965e715..408055d 100644 --- a/test/unit/cache_test.rb +++ b/test/unit/cache_test.rb @@ -420,30 +420,38 @@ def read_caches_while_filling_and_emptying_them read_caches_while_filling_and_emptying_them end - test "reads every cache from threads that fill and empty it, with the tables JRuby and TruffleRuby use" do + test "reads every cache from threads that fill and empty it, with the tables TruffleRuby uses" do locked_hash = Agentry::Cache.const_get(:LockedHash) table_by_name = CACHE_TABLES.to_h { |name| [name, Agentry::Cache.instance_variable_get(name)] } - pattern_by_list = Agentry::Bot.instance_variable_get(:@pattern_by_list) table_by_name.each_key { |name| Agentry::Cache.instance_variable_set(name, locked_hash.new) } - Agentry::Bot.instance_variable_set(:@pattern_by_list, locked_hash.new.compare_by_identity) read_caches_while_filling_and_emptying_them ensure table_by_name&.each { |name, table| Agentry::Cache.instance_variable_set(name, table) } - Agentry::Bot.instance_variable_set(:@pattern_by_list, pattern_by_list) end - test "JRuby and TruffleRuby get the locked tables, Bot's keyword patterns included" do - locked_hash = Agentry::Cache.const_get(:LockedHash) + test "keeps apart user agents and headers whose bytes or encoding differ, whatever the encoding" do + sougou = "#{Agentry["CHROME"]} éSE 2.X MetaSr 1.0" + latin1 = sougou.encode("ISO-8859-1").b + iso8859_14 = "#{Agentry["CHROME"]} caf\xE9".force_encoding("ISO-8859-14") + accept_language = "fr-CH, fr;q=0.9, é" + requests = [sougou, latin1, iso8859_14] + answer = ->(user_agent, header) do + agentry = Agentry.new(user_agent, accept_language: header) + [agentry.id, agentry.accept_language.map(&:full)] + end + headers = [accept_language, accept_language.b, accept_language.encode("ISO-8859-1").b] - assert_equal Hash, Agentry::Cache.send(:table_class, "ruby") - assert_equal locked_hash, Agentry::Cache.send(:table_class, "jruby") - assert_equal locked_hash, Agentry::Cache.send(:table_class, "truffleruby") - Agentry::Bot.keywords_pattern(["alice"]) - assert_instance_of Agentry::Cache.const_get(:TABLE), Agentry::Bot.instance_variable_get(:@pattern_by_list) + Agentry.cache_size = 0 + expected = requests.product(headers).map { |user_agent, header| answer.call(user_agent, header) } + Agentry.cache_size = 1000 + + 2.times do + assert_equal expected, requests.product(headers).map { |user_agent, header| answer.call(user_agent, header) } + end end - test "the tables JRuby and TruffleRuby use read, write and clear under their lock" do + test "the tables TruffleRuby uses read, write and clear under their lock" do table = Agentry::Cache.const_get(:LockedHash).new lock = table.instance_variable_get(:@lock)