From 910c77a758d5da4e802b243ec3e5cbf95c8a3133 Mon Sep 17 00:00:00 2001 From: fumimowdan Date: Thu, 19 Oct 2023 09:27:36 +0100 Subject: [PATCH] Use concurrent Array and Hash To avoid concurrency issue with urn next method --- Gemfile | 2 ++ Gemfile.lock | 4 ++++ app/models/urn.rb | 4 ++-- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index 32e1a575..05fc138f 100644 --- a/Gemfile +++ b/Gemfile @@ -32,6 +32,8 @@ gem "phonelib" ############### gem "audited", "~> 5.4" +gem "concurrent-ruby", require: "concurrent" +gem "concurrent-ruby-ext" gem "config", "~> 4.2" gem "devise", "~> 4.9" gem "flipper" diff --git a/Gemfile.lock b/Gemfile.lock index 7ad12716..c8c09b58 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -100,6 +100,8 @@ GEM xpath (~> 3.2) coderay (1.1.3) concurrent-ruby (1.2.2) + concurrent-ruby-ext (1.2.2) + concurrent-ruby (= 1.2.2) config (4.2.1) deep_merge (~> 1.2, >= 1.2.1) dry-validation (~> 1.0, >= 1.0.0) @@ -476,6 +478,8 @@ DEPENDENCIES brakeman bundler-audit capybara + concurrent-ruby + concurrent-ruby-ext config (~> 4.2) dartsass-rails (~> 0.5.0) debug diff --git a/app/models/urn.rb b/app/models/urn.rb index 2160c548..9a2a31ad 100644 --- a/app/models/urn.rb +++ b/app/models/urn.rb @@ -77,7 +77,7 @@ def next(route) private def routes - @routes ||= Hash.new do |hash, route| + @routes ||= Concurrent::Hash.new do |hash, route| hash[route] = urn_enumerator( config.codes.fetch(route), config.seeds.fetch(route, Random.new_seed), @@ -102,7 +102,7 @@ def available_urns(code, seed, used_urns) end def urn_enumerator(code, seed, used_urns) - list = available_urns(code, seed, used_urns) + list = Concurrent::Array.new(available_urns(code, seed, used_urns)) error_msg = "you have exhausted urn for code #{code} you need to increase the size of the suffix" Enumerator.new do |yielder|