@@ -478,6 +478,9 @@ class AuthManager(private val cs: CoroutineScope) : Disposable {
478478 Log .log(logger::trace, " launching autoRefreshJob" )
479479 return cs.launch(CoroutineName (" autoRefreshJob" )) {
480480
481+ var statisticsStartTime = Clock .System .now()
482+ var refreshCounter = 0
483+
481484 Log .log(logger::trace, " ${coroutineContext[CoroutineName ]} auto refresh job started" )
482485 var delay: Duration
483486 while (isActive) {
@@ -518,7 +521,10 @@ class AuthManager(private val cs: CoroutineScope) : Disposable {
518521 (credentials.expirationTime - Clock .System .now().toEpochMilliseconds())
519522 ).toDuration(DurationUnit .MILLISECONDS )
520523
521- if (expireIn <= 1 .minutes) {
524+ if (expireIn == ZERO ) {
525+ // don't refresh if expireIn is zero it will e refreshed on authentication exception
526+ delay = 5 .minutes
527+ } else if (expireIn <= 1 .minutes) {
522528 Log .log(
523529 logger::trace,
524530 " ${coroutineContext[CoroutineName ]} credentials expires in {} , refreshing account {}" ,
@@ -533,11 +539,30 @@ class AuthManager(private val cs: CoroutineScope) : Disposable {
533539 false
534540 }
535541
542+ refreshCounter++
543+
544+ // send statistics
545+ val now = Clock .System .now()
546+ val timeSinceLastStatistics = (now - statisticsStartTime).inWholeMilliseconds.toDuration(DurationUnit .MILLISECONDS )
547+ // if (timeSinceLastStatistics >= 1.hours) {
548+ // /todo: temp change to 1 hour
549+ if (timeSinceLastStatistics >= 20 .minutes) {
550+ statisticsStartTime = now
551+ reportAuthPosthogEvent(
552+ " AutoRefreshStats" , " AutoRefreshJob" , null , mapOf (
553+ " period.minutes" to timeSinceLastStatistics.inWholeMinutes,
554+ " refresh.counter" to refreshCounter
555+ )
556+ )
557+ refreshCounter = 0
558+ }
559+
536560 if (refreshSuccess) {
537561 fireChange()
538562 Log .log(logger::trace, " ${coroutineContext[CoroutineName ]} credentials for account refreshed {}" , account)
539- // immediately loop again and compute the next delay
540- delay = ZERO
563+ // after successful refresh wait 5 minutes , the token should be valid for 15 minutes
564+ // it should also let the credential store save and refresh its caches if any.
565+ delay = 5 .minutes
541566 } else {
542567 Log .log(logger::trace, " ${coroutineContext[CoroutineName ]} refresh failed, waiting 5 minutes" )
543568 delay = 5 .minutes
0 commit comments