MCPcopy Create free account
hub / github.com/aws/aws-cli / DeferredRefreshableToken

Class DeferredRefreshableToken

awscli/botocore/tokens.py:69–163  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

67
68
69class DeferredRefreshableToken:
70 # The time at which we'll attempt to refresh, but not block if someone else
71 # is refreshing.
72 _advisory_refresh_timeout = 15 * 60
73 # The time at which all threads will block waiting for a refreshed token
74 _mandatory_refresh_timeout = 10 * 60
75 # Refresh at most once every minute to avoid blocking every request
76 _attempt_timeout = 60
77
78 def __init__(self, method, refresh_using, time_fetcher=_utc_now):
79 self._time_fetcher = time_fetcher
80 self._refresh_using = refresh_using
81 self.method = method
82
83 # The frozen token is protected by this lock
84 self._refresh_lock = threading.Lock()
85 self._frozen_token = None
86 self._next_refresh = None
87
88 def get_frozen_token(self):
89 self._refresh()
90 return self._frozen_token
91
92 def _refresh(self):
93 # If we don't need to refresh just return
94 refresh_type = self._should_refresh()
95 if not refresh_type:
96 return None
97
98 # Block for refresh if we're in the mandatory refresh window
99 block_for_refresh = refresh_type == "mandatory"
100 if self._refresh_lock.acquire(block_for_refresh):
101 try:
102 self._protected_refresh()
103 finally:
104 self._refresh_lock.release()
105
106 def _protected_refresh(self):
107 # This should only be called after acquiring the refresh lock
108 # Another thread may have already refreshed, double check refresh
109 refresh_type = self._should_refresh()
110 if not refresh_type:
111 return None
112
113 try:
114 now = self._time_fetcher()
115 self._next_refresh = now + timedelta(seconds=self._attempt_timeout)
116 self._frozen_token = self._refresh_using()
117 except Exception:
118 logger.warning(
119 "Refreshing token failed during the %s refresh period.",
120 refresh_type,
121 exc_info=True,
122 )
123 if refresh_type == "mandatory":
124 # This refresh was mandatory, error must be propagated back
125 raise
126

Callers 1

load_tokenMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected