1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
# frozen_string_literal: true
require 'thread'
module Aws
module S3
class BucketRegionCache
def initialize
@regions = {}
@listeners = []
@mutex = Mutex.new
end
# Registers a block as a callback. This listener is called when a
# new bucket/region pair is added to the cache.
#
# Aws::S3.bucket_region_cache.bucket_added do |bucket_name, region_name|
# # ...
# end
#
# This happens when a request is made against the classic endpoint,
# "s3.amazonaws.com" and an error is returned requiring the request
# to be resent with Signature Version 4. At this point, multiple
# requests are made to discover the bucket region so that a v4
# signature can be generated.
#
# An application can register listeners here to avoid these extra
# requests in the future. By constructing an {S3::Client} with
# the proper region, a proper signature can be generated and redirects
# avoided.
# @return [void]
def bucket_added(&block)
if block
@mutex.synchronize { @listeners << block }
else
raise ArgumentError, 'missing required block'
end
end
# @param [String] bucket_name
# @return [String,nil] Returns the cached region for the named bucket.
# Returns `nil` if the bucket is not in the cache.
# @api private
def [](bucket_name)
@mutex.synchronize { @regions[bucket_name] }
end
# Caches a bucket's region. Calling this method will trigger each
# of the {#bucket_added} listener callbacks.
# @param [String] bucket_name
# @param [String] region_name
# @return [void]
# @api private
def []=(bucket_name, region_name)
@mutex.synchronize do
@regions[bucket_name] = region_name
@listeners.each { |block| block.call(bucket_name, region_name) }
end
end
# @param [String] key
# @return [Boolean]
def key?(key)
@mutex.synchronize do
@regions.key?(key)
end
end
# @api private
def clear
@mutex.synchronize { @regions = {} }
end
# @return [Hash] Returns a hash of cached bucket names and region names.
def to_hash
@mutex.synchronize do
@regions.dup
end
end
alias to_h to_hash
end
end
end
|