Skip to content

Commit 7998ce1

Browse files
committed
fix(manager): use integer math with higher precision
1 parent fa01924 commit 7998ce1

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

manager/entraid_manager.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -237,13 +237,6 @@ func (e *entraidTokenManager) stop() (err error) {
237237
// It returns the duration to the next token renewal based on the expiration refresh ratio and the lower bound duration.
238238
// If the token is nil, it returns 0.
239239
// If the time till expiration is less than the lower bound duration, it returns 0 to renew the token now.
240-
//
241-
// This is an optimized version that uses minimal operations
242-
// and integer math for maximum performance, matching the logic of durationToRenewal.
243-
// It calculates the duration until the next token renewal based on:
244-
// 1. The token's TTL (in milliseconds) and expiration refresh ratio
245-
// 2. The lower bound duration for refresh
246-
// 3. The current time and token's expiration time
247240
func (e *entraidTokenManager) durationToRenewal(t *token.Token) time.Duration {
248241
// Fast path: nil token check
249242
if t == nil {
@@ -272,14 +265,16 @@ func (e *entraidTokenManager) durationToRenewal(t *token.Token) time.Duration {
272265
return 0
273266
}
274267

275-
ttlMillis := t.TTL() // Already in milliseconds
276-
// let's not lose the precision here, examples use 0.001, which would be lost with integer math
268+
// Calculate refresh time using integer math with higher precision
269+
// example tests use 0.001, which would be lost with lower precision
277270
// Example:
278271
// ttlMillis = 10000
279272
// e.expirationRefreshRatio = 0.001
280-
// - with int math: 10000 * (0.001*100) = 0ms
281-
// - with float math: 10000 * 0.001 = 10ms
282-
refreshMillis := int64(float64(ttlMillis) * e.expirationRefreshRatio)
273+
// - with int math and 100 precision: 10000 * (0.001*100) = 0ms
274+
// - with int math and 10000 precision: 10000 * (0.001*10000) = 100ms
275+
ttlMillis := t.TTL() // Already in milliseconds
276+
refreshRationInt := int64(e.expirationRefreshRatio * 10000)
277+
refreshMillis := ttlMillis * refreshRationInt / 10000
283278
refreshTimeMillis := t.ReceivedAt().UnixMilli() + refreshMillis
284279

285280
// Calculate time until refresh

0 commit comments

Comments
 (0)