From f9c9de69e43c48e54f58bb791379bda91fe81718 Mon Sep 17 00:00:00 2001 From: Alex Woods Date: Mon, 16 Sep 2024 14:08:11 -0700 Subject: [PATCH 1/3] SDK Eager loading --- CHANGELOG.md | 2 ++ lib/aws/rails/railtie.rb | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+) 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..76e3658b 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 @@ -74,6 +93,10 @@ def self.instrument_sdk_operations end end + def self.setup_sdk_eager_load + + end + # Register a middleware that will handle requests from the Elastic Beanstalk worker SQS Daemon. # This will only be added in the presence of the AWS_PROCESS_BEANSTALK_WORKER_REQUESTS environment variable. # The expectation is this variable should only be set on EB worker environments. From 210b55c6d885d0adf5b53162b4093f26cc08319a Mon Sep 17 00:00:00 2001 From: Alex Woods Date: Tue, 17 Sep 2024 09:09:33 -0700 Subject: [PATCH 2/3] Fix rubocop --- lib/aws/rails/railtie.rb | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/aws/rails/railtie.rb b/lib/aws/rails/railtie.rb index 76e3658b..5fe9aabb 100644 --- a/lib/aws/rails/railtie.rb +++ b/lib/aws/rails/railtie.rb @@ -93,10 +93,6 @@ def self.instrument_sdk_operations end end - def self.setup_sdk_eager_load - - end - # Register a middleware that will handle requests from the Elastic Beanstalk worker SQS Daemon. # This will only be added in the presence of the AWS_PROCESS_BEANSTALK_WORKER_REQUESTS environment variable. # The expectation is this variable should only be set on EB worker environments. From 57bff96afc646e960578651a87e5609bb21b33e2 Mon Sep 17 00:00:00 2001 From: Alex Woods Date: Tue, 17 Sep 2024 14:05:12 -0700 Subject: [PATCH 3/3] Add test --- spec/aws/rails/railtie_spec.rb | 5 +++++ spec/dummy/config/environments/test.rb | 1 + 2 files changed, 6 insertions(+) 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