Skip to content

Commit

Permalink
Automatically eager load all integrations when in Rails context
Browse files Browse the repository at this point in the history
In production mode it's preferable to load all the code upfront
even if it means loading some code that won't be used.

This is because loading the code on demand cause the first request
to use it to be slower, and to flush various VM caches such
as the global constant cache. It also degrade Copy on Write
performance for forking setups.
  • Loading branch information
byroot committed Mar 1, 2022
1 parent d5421ae commit a31d366
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/offsite_payments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ def self.integration(name)
Integrations.const_get("#{name.to_s.downcase}".camelize)
end

def self.eager_load!
Integrations.eager_load!
nil
end

mattr_accessor :mode
self.mode = :production

Expand All @@ -42,3 +47,5 @@ def self.test?

CURRENCIES_WITHOUT_FRACTIONS = [ 'BIF', 'BYR', 'CLP', 'CVE', 'DJF', 'GNF', 'ISK', 'JPY', 'KMF', 'KRW', 'PYG', 'RWF', 'TWD', 'UGX', 'VND', 'VUV', 'XAF', 'XOF', 'XPF' ]
end

require "offsite_payments/railtie" if defined? Rails::Railtie
7 changes: 7 additions & 0 deletions lib/offsite_payments/integrations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,12 @@ module Integrations
# Register for autoloading
autoload integration_class, f
end

def self.eager_load!
constants.each do |const_name|
const_get(const_name)
end
nil
end
end
end
7 changes: 7 additions & 0 deletions lib/offsite_payments/railtie.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module OffsitePayments
class Railtie < Rails::Railtie
config.before_eager_load do
config.eager_load_namespace << OffsitePayments
end
end
end
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require 'json'
require 'nokogiri'

require 'active_support'
require 'active_support/core_ext/integer/time'
require 'active_support/core_ext/numeric/time'
require 'active_support/core_ext/time/acts_like'
Expand Down

0 comments on commit a31d366

Please sign in to comment.