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 Jul 16, 2024
1 parent 874a32a commit 9075145
Show file tree
Hide file tree
Showing 2 changed files with 21 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

Check warning on line 241 in Library/Homebrew/cmd/fetch.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/cmd/fetch.rb#L240-L241

Added lines #L240 - L241 were not covered by tests

output_message = lambda do |downloadable, promise|
status = case promise.state
output_message = lambda do |downloadable, future|
status = case future.state

Check warning on line 244 in Library/Homebrew/cmd/fetch.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/cmd/fetch.rb#L243-L244

Added lines #L243 - L244 were not covered by tests
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

Check warning on line 257 in Library/Homebrew/cmd/fetch.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/cmd/fetch.rb#L255-L257

Added lines #L255 - L257 were not covered by tests

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

Check warning on line 262 in Library/Homebrew/cmd/fetch.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/cmd/fetch.rb#L262

Added line #L262 was not covered by tests
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)

Check warning on line 273 in Library/Homebrew/cmd/fetch.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/cmd/fetch.rb#L272-L273

Added lines #L272 - L273 were not covered by tests
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)

Check warning on line 280 in Library/Homebrew/cmd/fetch.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/cmd/fetch.rb#L276-L280

Added lines #L276 - L280 were not covered by tests
end

previous_pending_line_count = 0
remaining_downloads.each do |downloadable, promise|
remaining_downloads.each do |downloadable, future|

Check warning on line 284 in Library/Homebrew/cmd/fetch.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/cmd/fetch.rb#L283-L284

Added lines #L283 - L284 were not covered by tests
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)

Check warning on line 289 in Library/Homebrew/cmd/fetch.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/cmd/fetch.rb#L287-L289

Added lines #L287 - L289 were not covered by tests
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
14 changes: 9 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,15 @@ 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
# 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 9075145

Please sign in to comment.