diff --git a/gems/smithy-client/lib/smithy-client/refreshing_identity_provider.rb b/gems/smithy-client/lib/smithy-client/refreshing_identity_provider.rb index 9489dca54..9e47d89ba 100644 --- a/gems/smithy-client/lib/smithy-client/refreshing_identity_provider.rb +++ b/gems/smithy-client/lib/smithy-client/refreshing_identity_provider.rb @@ -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 @@ -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 @@ -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 diff --git a/projections/shapes/sig/shapes/client.rbs b/projections/shapes/sig/shapes/client.rbs index 41eb50040..d594c578e 100644 --- a/projections/shapes/sig/shapes/client.rbs +++ b/projections/shapes/sig/shapes/client.rbs @@ -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, diff --git a/projections/weather/sig/weather/client.rbs b/projections/weather/sig/weather/client.rbs index 407767816..9cbec29b1 100644 --- a/projections/weather/sig/weather/client.rbs +++ b/projections/weather/sig/weather/client.rbs @@ -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,