Skip to content

Commit 8161040

Browse files
committed
chore: Refactor token management and handling logic with 10 max retry attempts
1 parent a324478 commit 8161040

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

authenticationhandler/tokenmanager.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,20 @@ import (
1313
// CheckAndRefreshAuthToken checks the token's validity and refreshes it if necessary.
1414
// It returns true if the token is valid post any required operations and false with an error otherwise.
1515
func (h *AuthTokenHandler) CheckAndRefreshAuthToken(apiHandler apihandler.APIHandler, httpClient *http.Client, clientCredentials ClientCredentials, tokenRefreshBufferPeriod time.Duration) (bool, error) {
16-
if !h.isTokenValid(tokenRefreshBufferPeriod) {
16+
const maxConsecutiveRefreshAttempts = 10
17+
refreshAttempts := 0
18+
19+
for !h.isTokenValid(tokenRefreshBufferPeriod) {
1720
h.Logger.Debug("Token found to be invalid or close to expiry, handling token acquisition or refresh.")
1821
if err := h.obtainNewToken(apiHandler, httpClient, clientCredentials); err != nil {
1922
h.Logger.Error("Failed to obtain new token", zap.Error(err))
2023
return false, err
2124
}
25+
26+
refreshAttempts++
27+
if refreshAttempts >= maxConsecutiveRefreshAttempts {
28+
return false, fmt.Errorf("exceeded maximum consecutive token refresh attempts (%d): token lifetime is likely too short", maxConsecutiveRefreshAttempts)
29+
}
2230
}
2331

2432
if err := h.refreshTokenIfNeeded(apiHandler, httpClient, clientCredentials, tokenRefreshBufferPeriod); err != nil {

0 commit comments

Comments
 (0)