Skip to content

Commit

Permalink
Eager load the constants
Browse files Browse the repository at this point in the history
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
  • Loading branch information
y-yagi committed Aug 15, 2017
1 parent b7c5f6d commit 797a383
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/global_id.rb
@@ -1,9 +1,19 @@
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

def self.eager_load!
super
require 'global_id/signed_global_id'
end
end
1 change: 1 addition & 0 deletions lib/global_id/railtie.rb
Expand Up @@ -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|

Expand Down

0 comments on commit 797a383

Please sign in to comment.