Skip to content

Commit

Permalink
Add a delay between [0,59]min during token refresh
Browse files Browse the repository at this point in the history
This is required to reduce the load on the infrastructure
  • Loading branch information
SailReal committed Nov 29, 2023
1 parent 145673c commit d86b785
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public class LicenseHolder {
@Inject
LicenseValidator licenseValidator;

@Inject
RandomMinuteSleeper randomMinuteSleeper;

private static final Logger LOG = Logger.getLogger(LicenseHolder.class);
private DecodedJWT license;

Expand Down Expand Up @@ -106,6 +109,7 @@ public void set(String token) throws JWTVerificationException {
@Scheduled(cron = "0 0 1 * * ?", timeZone = "UTC", concurrentExecution = Scheduled.ConcurrentExecution.SKIP)
void refreshLicenseScheduler() throws InterruptedException {
if (license != null) {
randomMinuteSleeper.sleep();
var refreshUrl = licenseValidator.refreshUrl(license.getToken());
if (refreshUrl.isPresent()) {
var client = HttpClient.newBuilder().followRedirects(HttpClient.Redirect.NORMAL).build();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.cryptomator.hub.license;

import jakarta.enterprise.context.ApplicationScoped;

import java.util.Random;

@ApplicationScoped
public class RandomMinuteSleeper {

private static final long MINUTE_IN_MILLIS = 60 * 1000L;

void sleep() throws InterruptedException {
Thread.sleep(new Random().nextInt(0, 60) * MINUTE_IN_MILLIS);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,18 @@ class TestPostConstruct {
@InjectMock
LicenseValidator validator;

@InjectMock
RandomMinuteSleeper randomMinuteSleeper;

MockedStatic<Settings> settingsClass;

@BeforeEach
public void setup() {
public void setup() throws InterruptedException {
Query mockQuery = Mockito.mock(Query.class);
Mockito.doNothing().when(session).persist(Mockito.any());
Mockito.when(session.createQuery(Mockito.anyString())).thenReturn(mockQuery);
Mockito.when(mockQuery.getSingleResult()).thenReturn(0l);
Mockito.doNothing().when(randomMinuteSleeper).sleep();

Arc.container().instance(LicenseHolder.class).destroy();

Expand Down Expand Up @@ -131,14 +135,18 @@ class TestSetter {
@InjectMock
LicenseValidator validator;

@InjectMock
RandomMinuteSleeper randomMinuteSleeper;

MockedStatic<Settings> settingsClass;

@BeforeEach
public void setup() {
public void setup() throws InterruptedException {
Query mockQuery = Mockito.mock(Query.class);
Mockito.doNothing().when(session).persist(Mockito.any());
Mockito.when(session.createQuery(Mockito.anyString())).thenReturn(mockQuery);
Mockito.when(mockQuery.getSingleResult()).thenReturn(0l);
Mockito.doNothing().when(randomMinuteSleeper).sleep();

Arc.container().instance(LicenseHolder.class).destroy();

Expand Down Expand Up @@ -203,14 +211,18 @@ class TestRefreshLicense {
@InjectMock
LicenseValidator validator;

@InjectMock
RandomMinuteSleeper randomMinuteSleeper;

MockedStatic<Settings> settingsClass;

@BeforeEach
public void setup() {
public void setup() throws InterruptedException {
Query mockQuery = Mockito.mock(Query.class);
Mockito.doNothing().when(session).persist(Mockito.any());
Mockito.when(session.createQuery(Mockito.anyString())).thenReturn(mockQuery);
Mockito.when(mockQuery.getSingleResult()).thenReturn(0l);
Mockito.doNothing().when(randomMinuteSleeper).sleep();

Arc.container().instance(LicenseHolder.class).destroy();

Expand Down Expand Up @@ -389,6 +401,9 @@ class LicenseHolderInitPropsTest {
@InjectMock
LicenseValidator validator;

@InjectMock
RandomMinuteSleeper randomMinuteSleeper;

MockedStatic<Settings> settingsClass;

public static class ValidInitPropsInstanceTestProfile implements QuarkusTestProfile {
Expand All @@ -399,11 +414,12 @@ public Map<String, String> getConfigOverrides() {
}

@BeforeEach
public void setup() {
public void setup() throws InterruptedException {
Query mockQuery = Mockito.mock(Query.class);
Mockito.doNothing().when(session).persist(Mockito.any());
Mockito.when(session.createQuery(Mockito.anyString())).thenReturn(mockQuery);
Mockito.when(mockQuery.getSingleResult()).thenReturn(0l);
Mockito.doNothing().when(randomMinuteSleeper).sleep();

Arc.container().instance(LicenseHolder.class).destroy();

Expand Down

0 comments on commit d86b785

Please sign in to comment.