@@ -237,13 +237,6 @@ func (e *entraidTokenManager) stop() (err error) {
237
237
// It returns the duration to the next token renewal based on the expiration refresh ratio and the lower bound duration.
238
238
// If the token is nil, it returns 0.
239
239
// 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
247
240
func (e * entraidTokenManager ) durationToRenewal (t * token.Token ) time.Duration {
248
241
// Fast path: nil token check
249
242
if t == nil {
@@ -272,14 +265,16 @@ func (e *entraidTokenManager) durationToRenewal(t *token.Token) time.Duration {
272
265
return 0
273
266
}
274
267
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
277
270
// Example:
278
271
// ttlMillis = 10000
279
272
// 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
283
278
refreshTimeMillis := t .ReceivedAt ().UnixMilli () + refreshMillis
284
279
285
280
// Calculate time until refresh
0 commit comments