Skip to content

Commit 934c9f3

Browse files
committed
fix(manager): precision as variable, fix typo
1 parent 7998ce1 commit 934c9f3

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

manager/entraid_manager.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"github.com/redis/go-redis-entraid/token"
1212
)
1313

14+
const RefreshRationPrecision = 10000
15+
1416
// entraidTokenManager is a struct that implements the TokenManager interface.
1517
type entraidTokenManager struct {
1618
// idp is the identity provider used to obtain the token.
@@ -244,7 +246,7 @@ func (e *entraidTokenManager) durationToRenewal(t *token.Token) time.Duration {
244246
}
245247

246248
// Get current time in milliseconds (UTC)
247-
nowMillis := time.Now().UTC().UnixMilli()
249+
nowMillis := time.Now().UnixMilli()
248250

249251
// Get expiration time in milliseconds
250252
expMillis := t.ExpirationOn().UnixMilli()
@@ -272,9 +274,10 @@ func (e *entraidTokenManager) durationToRenewal(t *token.Token) time.Duration {
272274
// e.expirationRefreshRatio = 0.001
273275
// - with int math and 100 precision: 10000 * (0.001*100) = 0ms
274276
// - with int math and 10000 precision: 10000 * (0.001*10000) = 100ms
277+
precision := int64(RefreshRationPrecision)
275278
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
278281
refreshTimeMillis := t.ReceivedAt().UnixMilli() + refreshMillis
279282

280283
// Calculate time until refresh
@@ -285,15 +288,14 @@ func (e *entraidTokenManager) durationToRenewal(t *token.Token) time.Duration {
285288
return 0
286289
}
287290

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
291293

292294
// 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
295297
}
296298

297299
// Otherwise use time until refresh
298-
return timeUntilRefreshDur
300+
return time.Duration(timeUntilRefresh) * time.Millisecond
299301
}

manager/token_manager.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ type TokenManagerOptions struct {
1616
// The value should be between 0 and 1.
1717
// For example, if the expiration time is 1 hour and the ratio is 0.75,
1818
// the token will be refreshed after 45 minutes. (the token is refreshed when 75% of its lifetime has passed)
19+
// Precision is 4 decimal places.
20+
// Closer to 1, the token will be refreshed later. We recommend not going above 0.9.
1921
//
2022
// default: 0.7
2123
ExpirationRefreshRatio float64

0 commit comments

Comments
 (0)