Skip to content

Commit

Permalink
Merge pull request #644 from mensfeld/master
Browse files Browse the repository at this point in the history
Make translations loading thread-safe
  • Loading branch information
radar committed Dec 5, 2022
2 parents fbc9b2b + 6db0531 commit 75fc49b
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/i18n/backend/simple.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ module Backend
class Simple
module Implementation
include Base

# Mutex to ensure that concurrent translations loading will be thread-safe
MUTEX = Mutex.new

def initialized?
@initialized ||= false
Expand Down Expand Up @@ -68,7 +71,11 @@ def translations(do_init: false)
# call `init_translations`
init_translations if do_init && !initialized?

@translations ||= Concurrent::Hash.new { |h, k| h[k] = Concurrent::Hash.new }
@translations ||= Concurrent::Hash.new do |h, k|
MUTEX.synchronize do
h[k] = Concurrent::Hash.new
end
end
end

protected
Expand Down

0 comments on commit 75fc49b

Please sign in to comment.