def _get_provider_with_grace_period()

in src/dynamodb_encryption_sdk/material_providers/most_recent.py [0:0]


    def _get_provider_with_grace_period(self, version, ttl_action):
        # type: (int, bool) -> CryptographicMaterialsProvider
        """Ask the provider to retrieve a specific version of this material, falling back to the cache if
        another caller currently holds the lock for retrieval.

        :param int version: Version to request
        :param TtlActions ttl_action: The ttl action to take for this version
        :returns: Cryptographic materials provider for the requested version
        :rtype: CryptographicMaterialsProvider
        :raises AttributeError: if provider could not locate version
        """
        blocking_wait = bool(ttl_action is TtlActions.EXPIRED)
        acquired = self._lock.acquire(blocking_wait)  # pylint: disable=consider-using-with
        if not acquired:
            # We failed to acquire the lock.
            # If blocking, we will never reach this point.
            # If not blocking, we want whatever the latest local version is.
            _LOGGER.debug("Failed to acquire lock. Returning the last cached version.")
            _, provider = self._cache.get(version)
            return provider

        try:
            # If the entry was expired then we blocked waiting for the lock, so it's possible some other thread already
            # queried the provider store and re-populated the cache. If so, we don't want to re-query the provider
            # store, so check if the entry is back in the cache first
            if ttl_action is TtlActions.EXPIRED:
                try:
                    _, provider = self._cache.get(version)
                    return provider
                except KeyError:
                    pass
            provider = self._provider_store.provider(self._material_name, version)
            self._cache.put(version, (time.time(), provider))
            return provider
        finally:
            self._lock.release()