Skip to content

Commit

Permalink
Use futures instead of promises.
Browse files Browse the repository at this point in the history
  • Loading branch information
reitermarkus committed Aug 14, 2024
1 parent 1dc2b68 commit 2c0b28c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
26 changes: 12 additions & 14 deletions Library/Homebrew/cmd/fetch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -240,23 +240,23 @@ def run
$stdout.print Tty.hide_cursor
$stdout.flush

output_message = lambda do |downloadable, promise|
status = case promise.state
output_message = lambda do |downloadable, future|
status = case future.state
when :fulfilled
"#{Tty.green}✔︎#{Tty.reset}"
when :rejected
"#{Tty.red}#{Tty.reset}"
when :pending
when :pending, :processing
"#{Tty.blue}#{spinner}#{Tty.reset}"
else
raise promise.state
raise future.state.to_s
end

message = "#{downloadable.download_type.capitalize} #{downloadable.name}"
$stdout.puts "#{status} #{message}"
$stdout.flush

if promise.rejected? && (e = promise.reason).is_a?(ChecksumMismatchError)
if future.rejected? && (e = future.reason).is_a?(ChecksumMismatchError)
opoo "#{downloadable.download_type.capitalize} reports different checksum: #{e.expected}"
Homebrew.failed = true if downloadable.is_a?(Resource::Patch)
next 2
Expand All @@ -269,24 +269,24 @@ def run
begin
finished_states = [:fulfilled, :rejected]

finished_downloads, remaining_downloads = remaining_downloads.partition do |_, promise|
finished_states.include?(promise.state)
finished_downloads, remaining_downloads = remaining_downloads.partition do |_, future|
finished_states.include?(future.state)
end

finished_downloads.each do |downloadable, promise|
finished_downloads.each do |downloadable, future|
previous_pending_line_count -= 1
print "\033[K"
$stdout.flush
output_message.call(downloadable, promise)
output_message.call(downloadable, future)
end

previous_pending_line_count = 0
remaining_downloads.each do |downloadable, promise|
remaining_downloads.each do |downloadable, future|
break if previous_pending_line_count >= [concurrency, (Tty.height - 1)].min

print "\033[K"
$stdout.flush
previous_pending_line_count += output_message.call(downloadable, promise)
previous_pending_line_count += output_message.call(downloadable, future)
end

if previous_pending_line_count.positive?
Expand Down Expand Up @@ -317,9 +317,7 @@ def downloads
end

def fetch_downloadable(downloadable)
downloadable.clear_cache if args.force?

downloads[downloadable] ||= download_queue.enqueue(RetryableDownload.new(downloadable))
downloads[downloadable] ||= download_queue.enqueue(RetryableDownload.new(downloadable), force: args.force?)
end
end
end
Expand Down
15 changes: 10 additions & 5 deletions Library/Homebrew/download_queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# frozen_string_literal: true

require "downloadable"
require "concurrent/promise"
require "concurrent/promises"
require "concurrent/executors"

module Homebrew
Expand All @@ -16,11 +16,16 @@ def initialize(size = 1)
@pool = Concurrent::FixedThreadPool.new(size)
end

sig { params(downloadable: Downloadable).returns(Concurrent::Promise) }
def enqueue(downloadable)
Concurrent::Promise.execute(executor: pool) do
downloadable.fetch(quiet: pool.max_length > 1)
sig { params(downloadable: Downloadable, force: T::Boolean).returns(Concurrent::Promises::Future) }
def enqueue(downloadable, force: false)
quiet = pool.max_length > 1
# Passing in arguments from outside into the future is a common `concurrent-ruby` pattern.
# rubocop:disable Lint/ShadowingOuterLocalVariable
Concurrent::Promises.future_on(pool, downloadable, force, quiet) do |downloadable, force, quiet|
downloadable.clear_cache if force
downloadable.fetch(quiet:)
end
# rubocop:enable Lint/ShadowingOuterLocalVariable
end

sig { void }
Expand Down

0 comments on commit 2c0b28c

Please sign in to comment.