Hi, I need suggestions how to implement oauth refresh token periodically with coroutines. I want to refresh the token before the client throws unauthorized exception, i'm using ktor client in intellij plugin.
I have a ktor client that is initialized with an account and credentials , upon initialization i want to start a task that will wait for expiration time - 5.minutes, refresh the token and start a new task for the next refresh.
something like that:
launch{
val timeLeft = computeTimeLeft(currentCredentials) - 5.minutes
delay(timeLeft)
retry(3.times, withDelay 30.seconds){
val newCredentials = refreshToken(currentCredentials)
if(newCredentials != null){
currentCredentials = newCredentials
}
}
onSuccess{
start a new task for the next refresh
}
}
does it makes sense to do a while(true) loop in coroutime ?
launch{
while(true){
val timeLeft = computeTimeLeft(currentCredentials) - 5.minutes
delay(timeLeft)
retry(3.times, withDelay 30.seconds){
val newCredentials = refreshToken(currentCredentials)
if(newCredentials != null){
currentCredentials = newCredentials
}
}
}