From 3f2258a8352e90c1e8e9449687c69021aea23860 Mon Sep 17 00:00:00 2001 From: heka1024 Date: Sun, 5 May 2024 17:31:59 +0900 Subject: [PATCH] Improve performance by parallelizing `ActiveStorage::Service::MirrorService#delete` and `ActiveStorage::Service::MirrorService#delete_prefixed` --- .../lib/active_storage/service/mirror_service.rb | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/activestorage/lib/active_storage/service/mirror_service.rb b/activestorage/lib/active_storage/service/mirror_service.rb index 164e872ffe..28f31e7aec 100644 --- a/activestorage/lib/active_storage/service/mirror_service.rb +++ b/activestorage/lib/active_storage/service/mirror_service.rb @@ -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 @@ -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