lib/blobby/http_store.rb in blobby-1.1.0 vs lib/blobby/http_store.rb in blobby-1.1.1
- old
+ new
@@ -1,5 +1,7 @@
+# frozen_string_literal: true
+
require "blobby/key_constraint"
require "net/http"
module Blobby
@@ -23,11 +25,11 @@
def available?
with_http_connection do
true
end
- rescue
+ rescue StandardError
false
end
def [](key)
KeyConstraint.must_allow!(key)
@@ -40,26 +42,28 @@
Net::HTTP.start(base_uri.host, base_uri.port) do |http|
yield http, base_uri.path
end
rescue *retryable_exceptions => e
raise e if remaining_retry_intervals.empty?
+
sleep(remaining_retry_intervals.shift) && retry
end
end
protected
def retryable_exceptions
[EOFError, Errno::ECONNRESET]
end
- def retry_intervals(n)
+ def retry_intervals(count)
# exponential backoff: [0.5, 1, 2, 4, 8, ...]
scaling_factor = (0.5 + Kernel.rand * 0.1) # a little random avoids throbbing
- Array.new(n) { |i| (2**i) * scaling_factor }
+ Array.new(count) { |i| (2**i) * scaling_factor }
end
+ # Represents an object in the store.
class StoredObject
def initialize(store, key)
@store = store
@key = key
@@ -76,31 +80,30 @@
def read(&block)
with_http_connection do |http, path|
http.request_get(path) do |response|
case response
- when Net::HTTPNotFound then
+ when Net::HTTPNotFound
return nil
- when Net::HTTPSuccess then
+ when Net::HTTPSuccess
if block_given?
response.read_body(&block)
return nil
- else
- return response.read_body
end
+ return response.read_body
end
response.error!
end
end
end
def write(content)
- if content.respond_to?(:read)
- content = content.read
- else
- content = content.dup
- end
+ content = if content.respond_to?(:read)
+ content.read
+ else
+ content.dup
+ end
with_http_connection do |http, path|
put = Net::HTTP::Put.new(path)
put.body = content
put["Content-Type"] = "application/octet-stream"
response = http.request(put)
@@ -113,12 +116,12 @@
def delete
with_http_connection do |http, path|
delete = Net::HTTP::Delete.new(path)
response = http.request(delete)
case response
- when Net::HTTPSuccess then
+ when Net::HTTPSuccess
true
- when Net::HTTPNotFound then
+ when Net::HTTPNotFound
false
else
response.error!
end
end