Skip to content

Commit

Permalink
feat: Added 1-5min of jitter for refreshal of access token
Browse files Browse the repository at this point in the history
  • Loading branch information
JigarJoshi committed Aug 8, 2022
1 parent fee291c commit e942711
Showing 1 changed file with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.io.IOException;
import java.util.Base64;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
Expand All @@ -36,7 +37,7 @@ class OAuth2TokenService implements TokenService {
private final AtomicLong nextRefreshTime;
private final Object lock;
private final AuthGrpc.AuthBlockingStub authBlockingStub;

private final Random random;
private static final Logger log = LoggerFactory.getLogger(OAuth2TokenService.class);

OAuth2TokenService(TigrisConfiguration.OAuth2Config oAuth2Config, ManagedChannel channel) {
Expand All @@ -46,6 +47,7 @@ class OAuth2TokenService implements TokenService {
this.lock = new Object();
this.lastRefreshed = new AtomicLong(0L);
this.nextRefreshTime = new AtomicLong(0L);
this.random = new Random();
}

@Override
Expand Down Expand Up @@ -77,8 +79,12 @@ private void refresh() {
refreshToken.set(response.getRefreshToken());
lastRefreshed.set(System.currentTimeMillis());
// refresh before 5 minute of expiry
// add 1-5 min (i.e. 60_000 - 300_000 ms) of random jitter
nextRefreshTime.set(
getExpirationTimeInMillis(accessToken.get()) - TimeUnit.MINUTES.toMillis(5));
getExpirationTimeInMillis(accessToken.get())
- TimeUnit.MINUTES.toMillis(5)
- random.nextInt(300_000)
+ 60_000);
} catch (IOException ioException) {
log.error("Failed to refresh access token", ioException);
}
Expand Down

0 comments on commit e942711

Please sign in to comment.