Class DefaultCredentialsRefreshService

    • Field Detail

      • LOGGER

        private static final org.slf4j.Logger LOGGER
      • scheduler

        private final java.util.concurrent.ScheduledExecutorService scheduler
        Scheduler used to schedule credentials refresh.

        Default is a single-threaded scheduler, which should be enough for most scenarios, assuming that credentials expire after a few minutes or hours. This default scheduler is automatically disposed of when the DefaultCredentialsRefreshService is closed.

        If an external scheduler is passed in, it is the developer's responsibility to close it.

      • privateScheduler

        private final boolean privateScheduler
      • refreshDelayStrategy

        private final java.util.function.Function<java.time.Duration,​java.time.Duration> refreshDelayStrategy
        Strategy to schedule credentials refresh after credentials retrieval.

        Typical strategies schedule refresh after a ratio of the time before expiration (e.g. 80 % of the time before expiration) or after a fixed time before expiration (e.g. 20 seconds before credentials expire).

        See Also:
        ratioRefreshDelayStrategy(double), fixedDelayBeforeExpirationRefreshDelayStrategy(Duration)
      • approachingExpirationStrategy

        private final java.util.function.Function<java.time.Duration,​java.lang.Boolean> approachingExpirationStrategy
        Strategy to provide a hint about whether credentials should be renewed now or not before attempting to connect.

        This can avoid a connection to use almost expired credentials if this connection is created just before credentials are refreshed in the background, but does not benefit from the refresh.

        Note setting such a strategy may require knowledge of the credentials validity and must be consistent with the refreshDelayStrategy chosen. For example, for a validity of 60 minutes and a refreshDelayStrategy that instructs to refresh 10 minutes before credentials expire, this strategy could hint that credentials that expire in 11 minutes or less (1 minute before a refresh is actually scheduled) should be refreshed, which would trigger an early refresh.

        The default strategy always return false.

    • Constructor Detail

      • DefaultCredentialsRefreshService

        public DefaultCredentialsRefreshService​(java.util.concurrent.ScheduledExecutorService scheduler,
                                                java.util.function.Function<java.time.Duration,​java.time.Duration> refreshDelayStrategy,
                                                java.util.function.Function<java.time.Duration,​java.lang.Boolean> approachingExpirationStrategy)
        Constructor. Consider using DefaultCredentialsRefreshService.DefaultCredentialsRefreshServiceBuilder to create instances.
        Parameters:
        scheduler -
        refreshDelayStrategy -
        approachingExpirationStrategy -
    • Method Detail

      • ratioRefreshDelayStrategy

        public static java.util.function.Function<java.time.Duration,​java.time.Duration> ratioRefreshDelayStrategy​(double ratio)
        Delay before refresh is a ratio of the time before expiration.

        E.g. if time before expiration is 60 minutes and specified ratio is 0.8, refresh will be scheduled in 60 x 0.8 = 48 minutes.

        Parameters:
        ratio -
        Returns:
        the delay before refreshing
      • fixedDelayBeforeExpirationRefreshDelayStrategy

        public static java.util.function.Function<java.time.Duration,​java.time.Duration> fixedDelayBeforeExpirationRefreshDelayStrategy​(java.time.Duration duration)
        Delay before refresh is time before expiration - specified duration.

        E.g. if time before expiration is 60 minutes and specified duration is 10 minutes, refresh will be scheduled in 60 - 10 = 50 minutes.

        Parameters:
        duration -
        Returns:
        the delay before refreshing
      • fixedTimeApproachingExpirationStrategy

        public static java.util.function.Function<java.time.Duration,​java.lang.Boolean> fixedTimeApproachingExpirationStrategy​(java.time.Duration limitBeforeExpiration)
        Advise to refresh credentials if TTL <= limit.
        Parameters:
        limitBeforeExpiration -
        Returns:
        true if credentials should be refreshed, false otherwise
      • refresh

        private static java.lang.Runnable refresh​(java.util.concurrent.ScheduledExecutorService scheduler,
                                                  DefaultCredentialsRefreshService.CredentialsProviderState credentialsProviderState,
                                                  java.util.function.Function<java.time.Duration,​java.time.Duration> refreshDelayStrategy)
      • register

        public java.lang.String register​(CredentialsProvider credentialsProvider,
                                         java.util.concurrent.Callable<java.lang.Boolean> refreshAction)
        Description copied from interface: CredentialsRefreshService
        Register a new entity that needs credentials renewal.

        The registered callback must return true if the action was performed correctly, throw an exception if something goes wrong, and return false if it became stale and wants to be unregistered.

        Implementations are free to automatically unregister an entity whose callback has failed a given number of times.

        Specified by:
        register in interface CredentialsRefreshService
        Parameters:
        credentialsProvider - the credentials provider
        refreshAction - the action to perform after credentials renewal
        Returns:
        a tracking ID for the registration
      • unregister

        public void unregister​(CredentialsProvider credentialsProvider,
                               java.lang.String registrationId)
        Description copied from interface: CredentialsRefreshService
        Unregister the entity with the given registration ID.

        Its state is cleaned up and its registered callback will not be called again.

        Specified by:
        unregister in interface CredentialsRefreshService
        Parameters:
        credentialsProvider - the credentials provider
        registrationId - the registration ID
      • isApproachingExpiration

        public boolean isApproachingExpiration​(java.time.Duration timeBeforeExpiration)
        Description copied from interface: CredentialsRefreshService
        Provide a hint about whether credentials should be renewed now or not before attempting to connect.

        This can avoid a connection to use almost expired credentials if this connection is created just before credentials are refreshed in the background, but does not benefit from the refresh.

        Specified by:
        isApproachingExpiration in interface CredentialsRefreshService
        Returns:
        true if credentials should be renewed, false otherwise
      • close

        public void close()