Skip to content

Commit

Permalink
Improve performance by parallelizing `ActiveStorage::Service::MirrorS…
Browse files Browse the repository at this point in the history
…ervice#delete` and `ActiveStorage::Service::MirrorService#delete_prefixed`
  • Loading branch information
heka1024 committed May 5, 2024
1 parent 2b14603 commit ecfbb5e
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions activestorage/lib/active_storage/service/mirror_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ def self.build(primary:, mirrors:, name:, configurator:, **options) # :nodoc:

def initialize(primary:, mirrors:)
@primary, @mirrors = primary, mirrors
@executor = Concurrent::ThreadPoolExecutor.new(
min_threads: 1,
max_threads: mirrors.size,
max_queue: 0,
fallback_policy: :caller_runs,
idle_time: 60
)
end

# Upload the +io+ to the +key+ specified to all services. The upload to the primary service is done synchronously
Expand Down Expand Up @@ -75,10 +82,12 @@ def each_service(&block)
end

def perform_across_services(method, *args)
# FIXME: Convert to be threaded
each_service.collect do |service|
service.public_send method, *args
tasks = each_service.collect do |service|
Concurrent::Promise.execute(executor: @executor) do
service.public_send method, *args
end
end
tasks.each(&:value!)
end
end
end

0 comments on commit ecfbb5e

Please sign in to comment.