@@ -238,30 +238,99 @@ func (e *entraidTokenManager) stop() (err error) {
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
240
func (e * entraidTokenManager ) durationToRenewal (t * token.Token ) time.Duration {
241
+ return e .durationToRenewalV5 (t )
242
+ /*
243
+ if t == nil {
244
+ return 0
245
+ }
246
+ now := time.Now().UTC()
247
+
248
+ expirationRefreshTime := t.ReceivedAt().Add(time.Duration(float64(t.TTL()) * float64(time.Millisecond) * e.expirationRefreshRatio))
249
+ timeTillExpiration := time.Until(t.ExpirationOn())
250
+
251
+ // if the expirationRefreshTime is in the past, return 0 to renew the token NOW
252
+ if expirationRefreshTime.Before(now) {
253
+ return 0
254
+ }
255
+
256
+ // if the timeTillExpiration is less than the lower bound (or 0), return 0 to renew the token NOW
257
+ if timeTillExpiration <= e.lowerBoundDuration || timeTillExpiration <= 0 {
258
+ return 0
259
+ }
260
+
261
+ // Calculate the time to renew the token based on the expiration refresh ratio
262
+ duration := time.Until(expirationRefreshTime)
263
+
264
+ // if the duration will take us past the lower bound, return the duration to lower bound
265
+ if timeTillExpiration-e.lowerBoundDuration < duration {
266
+ return timeTillExpiration - e.lowerBoundDuration
267
+ }
268
+
269
+ // return the calculated duration
270
+ return duration
271
+ */
272
+ }
273
+
274
+ // durationToRenewalV5 is an ultra-optimized version that uses minimal operations
275
+ // and integer math for maximum performance, matching the logic of durationToRenewal.
276
+ // It calculates the duration until the next token renewal based on:
277
+ // 1. The token's TTL (in milliseconds) and expiration refresh ratio
278
+ // 2. The lower bound duration for refresh
279
+ // 3. The current time and token's expiration time
280
+ func (e * entraidTokenManager ) durationToRenewalV5 (t * token.Token ) time.Duration {
281
+ // Fast path: nil token check
241
282
if t == nil {
242
283
return 0
243
284
}
244
- expirationRefreshTime := t .ReceivedAt ().Add (time .Duration (float64 (t .TTL ()) * float64 (time .Second ) * e .expirationRefreshRatio ))
245
- timeTillExpiration := time .Until (t .ExpirationOn ())
246
- now := time .Now ().UTC ()
247
285
248
- if expirationRefreshTime .Before (now ) {
286
+ // Get current time in milliseconds (UTC)
287
+ nowMillis := time .Now ().UTC ().UnixMilli ()
288
+
289
+ // Get expiration time in milliseconds
290
+ expMillis := t .ExpirationOn ().UnixMilli ()
291
+
292
+ // Fast path: token already expired
293
+ if expMillis <= nowMillis {
294
+ return 0
295
+ }
296
+
297
+ // Calculate time until expiration in milliseconds
298
+ timeTillExpiration := expMillis - nowMillis
299
+
300
+ // Get lower bound in milliseconds
301
+ lowerBoundMillis := e .lowerBoundDuration .Milliseconds ()
302
+
303
+ // Fast path: time until expiration is less than lower bound
304
+ if timeTillExpiration <= lowerBoundMillis {
249
305
return 0
250
306
}
251
307
252
- // if the timeTillExpiration is less than the lower bound (or 0), return 0 to renew the token NOW
253
- if timeTillExpiration <= e .lowerBoundDuration || timeTillExpiration <= 0 {
308
+ // Calculate refresh time using integer math:
309
+ // 1. TTL is already in milliseconds
310
+ // 2. Multiply by refresh ratio (as integer percentage)
311
+ // 3. Add to received time
312
+ ttlMillis := t .TTL () // Already in milliseconds
313
+ refreshRatioInt := int64 (e .expirationRefreshRatio * 100 ) // Convert to integer percentage
314
+ refreshMillis := (ttlMillis * refreshRatioInt ) / 100 // Integer division for ratio
315
+ refreshTimeMillis := t .ReceivedAt ().UnixMilli () + refreshMillis
316
+
317
+ // Calculate time until refresh
318
+ timeUntilRefresh := refreshTimeMillis - nowMillis
319
+
320
+ // Fast path: refresh time is in the past
321
+ if timeUntilRefresh <= 0 {
254
322
return 0
255
323
}
256
324
257
- // Calculate the time to renew the token based on the expiration refresh ratio
258
- duration := time .Until (expirationRefreshTime )
325
+ // Convert to time.Duration for final calculations
326
+ timeUntilRefreshDur := time .Duration (timeUntilRefresh ) * time .Millisecond
327
+ timeTillExpirationDur := time .Duration (timeTillExpiration ) * time .Millisecond
259
328
260
- // if the duration will take us past the lower bound, return the duration to lower bound
261
- if timeTillExpiration - e .lowerBoundDuration < duration {
262
- return timeTillExpiration - e .lowerBoundDuration
329
+ // If refresh would occur after lower bound, use time until lower bound
330
+ if timeTillExpirationDur - e .lowerBoundDuration < timeUntilRefreshDur {
331
+ return timeTillExpirationDur - e .lowerBoundDuration
263
332
}
264
333
265
- // return the calculated duration
266
- return duration
334
+ // Otherwise use time until refresh
335
+ return timeUntilRefreshDur
267
336
}
0 commit comments