diff --git a/lib/rom/factory/sequences.rb b/lib/rom/factory/sequences.rb index a4aabe1..ccaad32 100644 --- a/lib/rom/factory/sequences.rb +++ b/lib/rom/factory/sequences.rb @@ -19,17 +19,20 @@ def self.[](relation) # @api private def initialize + @mutex = Mutex.new reset end # @api private def next(key) - registry[key] += 1 + @mutex.synchronize do + registry[key] += 1 + end end # @api private def reset - @registry = Concurrent::Map.new { |h, k| h[k] = 0 } + @registry = Concurrent::Map.new { |h, k| h.compute_if_absent(k) { 0 } } self end end