Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[master] Retry before fail when SwiftServer connection closed #11

Merged
merged 1 commit into from
Dec 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/swift_storage/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module SwiftStorage
class Configuration
attr_accessor :auth_version, :tenant, :username, :password, :endpoint, :temp_url_key,
:auth_method, :authtenant_type
:auth_method, :authtenant_type, :retries

def initialize
@auth_version = ENV['SWIFT_STORAGE_AUTH_VERSION'] || '1.0'
Expand All @@ -10,6 +10,7 @@ def initialize
@password = ENV['SWIFT_STORAGE_PASSWORD']
@endpoint = ENV['SWIFT_STORAGE_ENDPOINT']
@temp_url_key = ENV['SWIFT_STORAGE_TEMP_URL_KEY']
@retries = 3

@auth_method = :password
@authtenant_type = 'tenantName' # `tenantName` or `tenantId`
Expand Down
20 changes: 17 additions & 3 deletions lib/swift_storage/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@ class SwiftStorage::Service
:storage_host,
:storage_port,
:storage_path,
:temp_url_key
:temp_url_key,
:retries

def initialize(tenant: configuration.tenant,
username: configuration.username,
password: configuration.password,
endpoint: configuration.endpoint,
temp_url_key: configuration.temp_url_key)
temp_url_key: configuration.temp_url_key,
retries: configuration.retries)
@temp_url_key = temp_url_key
@retries = retries

%w(tenant username password endpoint).each do |n|
eval("#{n} or raise ArgumentError, '#{n} is required'")
Expand Down Expand Up @@ -115,6 +118,9 @@ def request(path_or_url,
json_data: nil,
input_stream: nil,
output_stream: nil)

tries = retries

headers ||= {}
headers.merge!(Headers::AUTH_TOKEN => auth_token) if authenticated?
headers.merge!(Headers::CONTENT_TYPE => 'application/json') if json_data
Expand Down Expand Up @@ -181,7 +187,15 @@ def request(path_or_url,
end
end

response = s.request(req, &output_proc)
begin
response = s.request(req, &output_proc)
rescue Errno::EPIPE, Errno::EAGAIN, Errno::EWOULDBLOCK, Timeout::Error, Errno::EINVAL, EOFError
# Server closed the connection, retry
sleep 5
retry unless (tries -= 1) <= 0
raise OpenStack::Exception::Connection, "Unable to connect to OpenStack::Swift after #{retries} retries"
end

begin
check_response!(response)
rescue AuthError
Expand Down