@@ -11,6 +11,8 @@ import (
11
11
"github.com/redis/go-redis-entraid/token"
12
12
)
13
13
14
+ const RefreshRationPrecision = 10000
15
+
14
16
// entraidTokenManager is a struct that implements the TokenManager interface.
15
17
type entraidTokenManager struct {
16
18
// idp is the identity provider used to obtain the token.
@@ -244,7 +246,7 @@ func (e *entraidTokenManager) durationToRenewal(t *token.Token) time.Duration {
244
246
}
245
247
246
248
// Get current time in milliseconds (UTC)
247
- nowMillis := time .Now ().UTC (). UnixMilli ()
249
+ nowMillis := time .Now ().UnixMilli ()
248
250
249
251
// Get expiration time in milliseconds
250
252
expMillis := t .ExpirationOn ().UnixMilli ()
@@ -272,9 +274,10 @@ func (e *entraidTokenManager) durationToRenewal(t *token.Token) time.Duration {
272
274
// e.expirationRefreshRatio = 0.001
273
275
// - with int math and 100 precision: 10000 * (0.001*100) = 0ms
274
276
// - with int math and 10000 precision: 10000 * (0.001*10000) = 100ms
277
+ precision := int64 (RefreshRationPrecision )
275
278
ttlMillis := t .TTL () // Already in milliseconds
276
- refreshRationInt := int64 (e .expirationRefreshRatio * 10000 )
277
- refreshMillis := ttlMillis * refreshRationInt / 10000
279
+ refreshRatioInt := int64 (e .expirationRefreshRatio * float64 ( precision ) )
280
+ refreshMillis := ttlMillis * refreshRatioInt / precision
278
281
refreshTimeMillis := t .ReceivedAt ().UnixMilli () + refreshMillis
279
282
280
283
// Calculate time until refresh
@@ -285,15 +288,14 @@ func (e *entraidTokenManager) durationToRenewal(t *token.Token) time.Duration {
285
288
return 0
286
289
}
287
290
288
- // Convert to time.Duration for final calculations
289
- timeUntilRefreshDur := time .Duration (timeUntilRefresh ) * time .Millisecond
290
- timeTillExpirationDur := time .Duration (timeTillExpiration ) * time .Millisecond
291
+ // Subtract lower bound from time till expiration
292
+ timeTillExpiration = timeTillExpiration - lowerBoundMillis
291
293
292
294
// If refresh would occur after lower bound, use time until lower bound
293
- if timeTillExpirationDur - e . lowerBoundDuration < timeUntilRefreshDur {
294
- return timeTillExpirationDur - e . lowerBoundDuration
295
+ if timeTillExpiration < timeUntilRefresh {
296
+ return time . Duration ( timeTillExpiration ) * time . Millisecond
295
297
}
296
298
297
299
// Otherwise use time until refresh
298
- return timeUntilRefreshDur
300
+ return time . Duration ( timeUntilRefresh ) * time . Millisecond
299
301
}
0 commit comments