Skip to content

Commit b4d6e80

Browse files
committed
Added day truncation for refresh from
1 parent e749722 commit b4d6e80

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

src/main/java/com/uid2/admin/salt/SaltRotation.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import java.util.stream.Collectors;
1717

1818
public class SaltRotation {
19-
private final static long THIRTY_DAYS_IN_MS = Duration.ofDays(30).toMillis();
19+
private static final long THIRTY_DAYS_IN_MS = Duration.ofDays(30).toMillis();
2020

2121
private final IKeyGenerator keyGenerator;
2222
private final boolean isRefreshFromEnabled;
@@ -31,8 +31,7 @@ public Result rotateSalts(
3131
SaltSnapshot lastSnapshot,
3232
Duration[] minAges,
3333
double fraction,
34-
TargetDate targetDate
35-
) throws Exception {
34+
TargetDate targetDate) throws Exception {
3635
var preRotationSalts = lastSnapshot.getAllRotatingSalts();
3736
var nextEffective = targetDate.asInstant();
3837
var nextExpires = nextEffective.plus(7, ChronoUnit.DAYS);
@@ -78,7 +77,7 @@ private Set<SaltEntry> findRefreshableSalts(SaltEntry[] preRotationSalts, Target
7877

7978
private boolean isRefreshable(TargetDate targetDate, SaltEntry salt) {
8079
if (this.isRefreshFromEnabled) {
81-
return salt.refreshFrom().equals(targetDate.asEpochMs());
80+
return Instant.ofEpochMilli(salt.refreshFrom()).truncatedTo(ChronoUnit.DAYS).equals(targetDate.asInstant());
8281
}
8382

8483
return true;
@@ -115,7 +114,7 @@ private SaltEntry updateSalt(SaltEntry oldSalt, TargetDate targetDate, boolean s
115114

116115
private long calculateRefreshFrom(SaltEntry salt, TargetDate targetDate) {
117116
long multiplier = targetDate.saltAgeInDays(salt) / 30 + 1;
118-
return salt.lastUpdated() + (multiplier * THIRTY_DAYS_IN_MS);
117+
return Instant.ofEpochMilli(salt.lastUpdated()).truncatedTo(ChronoUnit.DAYS).toEpochMilli() + (multiplier * THIRTY_DAYS_IN_MS);
119118
}
120119

121120
private String calculatePreviousSalt(SaltEntry salt, boolean shouldRotate, TargetDate targetDate) {
@@ -132,8 +131,7 @@ private List<SaltEntry> pickSaltsToRotate(
132131
Set<SaltEntry> refreshableSalts,
133132
TargetDate targetDate,
134133
Duration[] minAges,
135-
int numSaltsToRotate
136-
) {
134+
int numSaltsToRotate) {
137135
var thresholds = Arrays.stream(minAges)
138136
.map(minAge -> targetDate.asInstant().minusSeconds(minAge.getSeconds()))
139137
.sorted()
@@ -161,8 +159,7 @@ private List<SaltEntry> pickSaltsToRotateInTimeWindow(
161159
Set<SaltEntry> refreshableSalts,
162160
int maxIndexes,
163161
long minLastUpdated,
164-
long maxLastUpdated
165-
) {
162+
long maxLastUpdated) {
166163
ArrayList<SaltEntry> candidateSalts = refreshableSalts.stream()
167164
.filter(salt -> minLastUpdated <= salt.lastUpdated() && salt.lastUpdated() < maxLastUpdated)
168165
.collect(Collectors.toCollection(ArrayList::new));
@@ -194,7 +191,7 @@ private void logSaltAges(String saltCountType, TargetDate targetDate, Collection
194191
}
195192

196193
@Getter
197-
public static class Result {
194+
public static final class Result {
198195
private final SaltSnapshot snapshot; // can be null if new snapshot is not needed
199196
private final String reason; // why you are not getting a new snapshot
200197

0 commit comments

Comments
 (0)