-
Notifications
You must be signed in to change notification settings - Fork 762
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
74 additions
and
7 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
src/promptflow-azure/promptflow/azure/_utils/_token_cache.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import time | ||
|
||
from promptflow.core._connection_provider._utils import get_arm_token | ||
|
||
|
||
class SingletonMeta(type): | ||
_instances = {} | ||
|
||
def __call__(cls, *args, **kwargs): | ||
if cls not in cls._instances: | ||
instance = super().__call__(*args, **kwargs) | ||
cls._instances[cls] = instance | ||
return cls._instances[cls] | ||
|
||
|
||
class ArmTokenCache(metaclass=SingletonMeta): | ||
DEFAULT_TTL_SECS = 1800 | ||
|
||
def __init__(self): | ||
self._ttl_secs = self.DEFAULT_TTL_SECS | ||
self._cache = {} | ||
|
||
def _is_token_valid(self, entry): | ||
return time.time() < entry["expires_at"] | ||
|
||
def get_token(self, credential): | ||
if credential in self._cache: | ||
entry = self._cache[credential] | ||
if self._is_token_valid(entry): | ||
return entry["token"] | ||
|
||
token = self._fetch_token(credential) | ||
self._cache[credential] = {"token": token, "expires_at": time.time() + self._ttl_secs} | ||
return token | ||
|
||
def _fetch_token(self, credential): | ||
return get_arm_token(credential=credential) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters