@@ -32,6 +32,7 @@ def __init__(self, polyglot):
3232
3333 # Mutex to protect _oauthConfig
3434 self ._lock = threading .Lock ()
35+ self ._token_lock = threading .Lock ()
3536
3637 # This is the result of the getAll we get on startup.
3738 def customNsHandler (self , key , data ):
@@ -126,22 +127,22 @@ def _oAuthTokensRefresh(self):
126127 # Gets the access token, and refresh if necessary
127128 # Should be called only after config is done
128129 def getAccessToken (self ):
129- # Make sure we have received tokens before attempting to renew
130+ with self ._token_lock :
131+ # Make sure we have received tokens before attempting to renew
132+ if self ._oauthTokens is not None and self ._oauthTokens .get ('refresh_token' ):
133+ expiry = self ._oauthTokens .get ('expiry' )
130134
131- if self . _oauthTokens is not None and self . _oauthTokens . get ( 'refresh_token' ):
132- expiry = self . _oauthTokens . get ( ' expiry' )
135+ # If expired or expiring in less than 60 seconds, refresh
136+ if expiry is None or datetime . fromisoformat ( expiry ) - timedelta ( seconds = 60 ) < datetime . now ():
133137
134- # If expired or expiring in less than 60 seconds, refresh
135- if expiry is None or datetime .fromisoformat (expiry ) - timedelta (seconds = 60 ) < datetime .now ():
138+ LOGGER .info (f"Access tokens: Token is expired since { expiry } . Initiating refresh." )
139+ self ._oAuthTokensRefresh ()
140+ else :
141+ LOGGER .info (f"Access tokens: Token is still valid until { expiry } , no need to refresh" )
136142
137- LOGGER .info (f"Access tokens: Token is expired since { expiry } . Initiating refresh." )
138- self ._oAuthTokensRefresh ()
143+ return self ._oauthTokens .get ('access_token' )
139144 else :
140- LOGGER .info (f"Access tokens: Token is still valid until { expiry } , no need to refresh" )
141-
142- return self ._oauthTokens .get ('access_token' )
143- else :
144- raise ValueError ('Access token is not available' )
145+ raise ValueError ('Access token is not available' )
145146
146147 # This will update the oAuth settings with the changes in update
147148 def updateOauthSettings (self , update ):
0 commit comments