Skip to content
This repository has been archived by the owner on Apr 14, 2021. It is now read-only.

Commit

Permalink
Merge pull request #3586 from pducks32/configurable-timeout
Browse files Browse the repository at this point in the history
Configurable timeout/retries/redirects
  • Loading branch information
indirect committed Apr 24, 2015
2 parents 5e6894a + afbad47 commit 9191d8f
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 27 deletions.
2 changes: 1 addition & 1 deletion lib/bundler/cli.rb
Expand Up @@ -20,7 +20,7 @@ def initialize(*args)
current_cmd = args.last[:current_command].name
custom_gemfile = options[:gemfile] || Bundler.settings[:gemfile]
ENV['BUNDLE_GEMFILE'] = File.expand_path(custom_gemfile) if custom_gemfile
Bundler::Retry.attempts = options[:retry] || Bundler.settings[:retry] || Bundler::Retry::DEFAULT_ATTEMPTS
Bundler.settings[:retry] = options[:retry] if options[:retry]
Bundler.rubygems.ui = UI::RGProxy.new(Bundler.ui)
auto_install if AUTO_INSTALL_CMDS.include?(current_cmd)
rescue UnknownArgumentError => e
Expand Down
6 changes: 3 additions & 3 deletions lib/bundler/fetcher.rb
Expand Up @@ -58,9 +58,9 @@ class << self
attr_accessor :disable_endpoint, :api_timeout, :redirect_limit, :max_retries
end

self.redirect_limit = 5 # How many redirects to allow in one request
self.api_timeout = 10 # How long to wait for each API call
self.max_retries = 3 # How many retries for the API call
self.redirect_limit = Bundler.settings[:redirect] # How many redirects to allow in one request
self.api_timeout = Bundler.settings[:timeout] # How long to wait for each API call
self.max_retries = Bundler.settings[:retry] # How many retries for the API call

def initialize(remote)
@remote = remote
Expand Down
21 changes: 11 additions & 10 deletions lib/bundler/retry.rb
@@ -1,23 +1,24 @@
module Bundler
# General purpose class for retrying code that may fail
class Retry
DEFAULT_ATTEMPTS = 2
attr_accessor :name, :total_runs, :current_run

class << self
attr_accessor :attempts
def default_attempts
default_retries + 1
end
alias_method :attempts, :default_attempts

def default_retries
Bundler.settings[:retry]
end
end

def initialize(name, exceptions = nil, attempts = nil)
def initialize(name, exceptions = nil, retries = self.class.default_retries)
@name = name
attempts ||= default_attempts
@retries = retries
@exceptions = Array(exceptions) || []
@total_runs = attempts.next # will run once, then upto attempts.times
end

def default_attempts
return Integer(self.class.attempts) if self.class.attempts
DEFAULT_ATTEMPTS
@total_runs = @retries + 1 # will run once, then upto attempts.times
end

def attempt(&block)
Expand Down
14 changes: 12 additions & 2 deletions lib/bundler/settings.rb
Expand Up @@ -3,6 +3,8 @@
module Bundler
class Settings
BOOL_KEYS = %w(frozen cache_all no_prune disable_local_branch_check gem.mit gem.coc).freeze
NUMBER_KEYS = %w(retry timeout redirect).freeze
DEFAULT_CONFIG = {:retry => 3, :timeout => 10, :redirect => 5}

def initialize(root = nil)
@root = root
Expand All @@ -12,10 +14,13 @@ def initialize(root = nil)

def [](name)
key = key_for(name)
value = (@local_config[key] || ENV[key] || @global_config[key])
value = (@local_config[key] || ENV[key] || @global_config[key] || DEFAULT_CONFIG[name])

if !value.nil? && is_bool(name)
case
when !value.nil? && is_bool(name)
to_bool(value)
when !value.nil? && is_num(name)
value.to_i
else
value
end
Expand Down Expand Up @@ -84,6 +89,7 @@ def locations(key)
locations[:local] = @local_config[key] if @local_config.key?(key)
locations[:env] = ENV[key] if ENV[key]
locations[:global] = @global_config[key] if @global_config.key?(key)
locations[:default] = DEFAULT_CONFIG[key] if DEFAULT_CONFIG.key?(key)
locations
end

Expand Down Expand Up @@ -169,6 +175,10 @@ def to_bool(value)
!(value.nil? || value == '' || value =~ /^(false|f|no|n|0)$/i || value == false)
end

def is_num(value)
NUMBER_KEYS.include?(value.to_s)
end

def get_array(key)
self[key] ? self[key].split(":").map { |w| w.to_sym } : []
end
Expand Down
11 changes: 0 additions & 11 deletions spec/bundler/retry_spec.rb
Expand Up @@ -11,17 +11,6 @@
expect(attempts).to eq(1)
end

it "defaults to retrying twice" do
attempts = 0
expect {
Bundler::Retry.new(nil).attempt do
attempts += 1
raise "nope"
end
}.to raise_error("nope")
expect(attempts).to eq(3)
end

it "returns the first valid result" do
jobs = [Proc.new{ raise "foo" }, Proc.new{ :bar }, Proc.new{ raise "foo" }]
attempts = 0
Expand Down
15 changes: 15 additions & 0 deletions spec/bundler/settings_spec.rb
Expand Up @@ -15,6 +15,21 @@
end
end

describe "#[]" do
context "when not set" do
context "when default value present" do
it "retrieves value" do
expect(settings[:retry]).to be 3
end
end

it "returns nil" do
expect(settings[:buttermilk]).to be nil
end
end
end


describe "#mirror_for" do
let(:uri) { URI("https://rubygems.org/") }

Expand Down

0 comments on commit 9191d8f

Please sign in to comment.