diff --git a/CHANGELOG.md b/CHANGELOG.md index f592853f..6516cae7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ Unreleased Changes ------------------ +* Feature - Add SDK eager loading to optimize load times. See: https://github.com/aws/aws-sdk-ruby/pull/3105. + 4.0.3 (2024-07-31) ------------------ diff --git a/lib/aws/rails/railtie.rb b/lib/aws/rails/railtie.rb index 01cc1903..5fe9aabb 100644 --- a/lib/aws/rails/railtie.rb +++ b/lib/aws/rails/railtie.rb @@ -18,6 +18,23 @@ class Railtie < ::Rails::Railtie Aws::Rails.add_sqsd_middleware(app) end + initializer 'aws-sdk-rails.sdk_eager_load' do + config.before_eager_load do + config.eager_load_namespaces << Aws + end + + Aws.define_singleton_method(:eager_load!) do + Aws.constants.each do |c| + m = Aws.const_get(c) + next unless m.is_a?(Module) + + m.constants.each do |constant| + m.const_get(constant) + end + end + end + end + rake_tasks do load 'tasks/dynamo_db/session_store.rake' load 'tasks/aws_record/migrate.rake' @@ -66,6 +83,8 @@ def self.use_rails_encrypted_credentials # name of: put_object.S3.aws def self.instrument_sdk_operations Aws.constants.each do |c| + next if Aws.autoload?(c) + m = Aws.const_get(c) if m.is_a?(Module) && m.const_defined?(:Client) && m.const_get(:Client).superclass == Seahorse::Client::Base diff --git a/spec/aws/rails/railtie_spec.rb b/spec/aws/rails/railtie_spec.rb index 16395cde..e0351208 100644 --- a/spec/aws/rails/railtie_spec.rb +++ b/spec/aws/rails/railtie_spec.rb @@ -23,6 +23,11 @@ module Rails expect(Aws.config[:logger]).to eq ::Rails.logger end + it 'sets up eager loading for sdk services' do + expect(Aws.methods).to include(:eager_load!) + expect(::Rails.application.config.eager_load_namespaces).to include(Aws) + end + describe '.use_rails_encrypted_credentials' do let(:rails_creds) { ::Rails.application.credentials.aws } it 'sets aws credentials' do diff --git a/spec/dummy/config/environments/test.rb b/spec/dummy/config/environments/test.rb index 53acbfea..e10d3fa5 100644 --- a/spec/dummy/config/environments/test.rb +++ b/spec/dummy/config/environments/test.rb @@ -3,4 +3,5 @@ config.action_mailbox.ingress = :ses config.action_mailbox.ses.subscribed_topic = 'arn:aws:sns:eu-west-1:012345678910:example-topic' config.action_dispatch.show_exceptions = :none + config.eager_load = true end