From 2be21a9238fc6f86001088beb2b9d8ac23f18b6a Mon Sep 17 00:00:00 2001 From: Emi Matchu Date: Mon, 16 Dec 2024 13:19:16 -0800 Subject: [PATCH] Use new `DTIRequests.load_many` helper in `Item.preload_nc_trade_values` --- app/models/item.rb | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/app/models/item.rb b/app/models/item.rb index cb921400..7fcce27a 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -1,6 +1,3 @@ -require "async" -require "async/barrier" - class Item < ApplicationRecord include PrettyParam include Item::Dyeworks @@ -671,21 +668,10 @@ class Item < ApplicationRecord end def self.preload_nc_trade_values(items) - # Only allow 10 trade values to be loaded at a time. - barrier = Async::Barrier.new - semaphore = Async::Semaphore.new(10, parent: barrier) - - Sync do + DTIRequests.load_many(max_at_once: 10) do |task| # Load all the trade values in concurrent async tasks. (The # `nc_trade_value` caches the value in the Item object.) - items.each do |item| - semaphore.async { item.nc_trade_value } - end - - # Wait until all tasks are done. - barrier.wait - ensure - barrier.stop # If something goes wrong, clean up all tasks. + items.each { |item| task.async { item.nc_trade_value } } end items