From a70d5cfca20ba8f54e7cfcfb9e468758df54edad Mon Sep 17 00:00:00 2001 From: "yuuji.yaginuma" Date: Sun, 13 Aug 2017 08:51:49 +0900 Subject: [PATCH] Eager load the constants In a thread-based backend like Sidekiq, there is a possibility that autoload may occur simultaneously in multiple threads, and as a result, it is presumed that an error may be caused by contention of autoload. In order to avoid the above issue, eager load the constants on boot. Maybe fixes #101 --- lib/global_id.rb | 11 ++++++++--- lib/global_id/railtie.rb | 1 + 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/global_id.rb b/lib/global_id.rb index 52eea73..a32526b 100644 --- a/lib/global_id.rb +++ b/lib/global_id.rb @@ -1,9 +1,14 @@ require 'global_id/global_id' +require 'active_support' autoload :SignedGlobalID, 'global_id/signed_global_id' class GlobalID - autoload :Locator, 'global_id/locator' - autoload :Identification, 'global_id/identification' - autoload :Verifier, 'global_id/verifier' + extend ActiveSupport::Autoload + + eager_autoload do + autoload :Locator + autoload :Identification + autoload :Verifier + end end diff --git a/lib/global_id/railtie.rb b/lib/global_id/railtie.rb index 88cd171..8402f10 100644 --- a/lib/global_id/railtie.rb +++ b/lib/global_id/railtie.rb @@ -11,6 +11,7 @@ class GlobalID # Set up the signed GlobalID verifier and include Active Record support. class Railtie < Rails::Railtie # :nodoc: config.global_id = ActiveSupport::OrderedOptions.new + config.eager_load_namespaces << GlobalID initializer 'global_id' do |app|