Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,16 @@ module RefreshingIdentityProvider
SYNC_EXPIRATION_LENGTH = 300 # 5 minutes
ASYNC_EXPIRATION_LENGTH = 600 # 10 minutes

def initialize(_options = {})
CLIENT_EXCLUDE_OPTIONS = Set.new([:before_refresh]).freeze

# @param [Hash] options
# @option options [Proc] :before_refresh A Proc called before credentials are refreshed.
# It accepts `self` as the only argument.
def initialize(options = {})
@mutex = Mutex.new
@before_refresh = options.delete(:before_refresh) if options.is_a?(Hash)

@before_refresh&.call(self)
refresh
end

Expand All @@ -24,7 +32,11 @@ def identity
# Refresh credentials.
# @return [void]
def refresh!
@mutex.synchronize { refresh }
@mutex.synchronize do
@before_refresh&.call(self)

refresh
end
end

private
Expand All @@ -46,15 +58,29 @@ def refresh_if_near_expiration!
# every #refresh_if_near_expiration call, we check before doing so, and
# then we check within the mutex to avoid a race condition.
if near_expiration?(sync_expiration_length)
@mutex.synchronize do
refresh if near_expiration?(sync_expiration_length)
end
sync_refresh
elsif @async_refresh && near_expiration?(async_expiration_length)
unless @mutex.locked?
Thread.new do
@mutex.synchronize do
refresh if near_expiration?(async_expiration_length)
end
async_refresh
end
end

def sync_refresh
@mutex.synchronize do
if near_expiration?(sync_expiration_length)
@before_refresh&.call(self)
refresh
end
end
end

def async_refresh
return if @mutex.locked?

Thread.new do
@mutex.synchronize do
if near_expiration?(async_expiration_length)
@before_refresh&.call(self)
refresh
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion projections/shapes/sig/shapes/client.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module ShapeService
?logger: Logger,
?raise_response_errors: bool,
?request_min_compression_size_bytes: Integer,
?retry_backoff: #call(attempts),
?retry_backoff: Smithy::Client::Retry::ExponentialBackoff,
?retry_base_delay: untyped,
?retry_max_attempts: Integer,
?retry_max_delay: untyped,
Expand Down
2 changes: 1 addition & 1 deletion projections/weather/sig/weather/client.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module Weather
?logger: Logger,
?raise_response_errors: bool,
?request_min_compression_size_bytes: Integer,
?retry_backoff: #call(attempts),
?retry_backoff: Smithy::Client::Retry::ExponentialBackoff,
?retry_base_delay: untyped,
?retry_max_attempts: Integer,
?retry_max_delay: untyped,
Expand Down