Add jitter to backup scheduling.

This commit is contained in:
Nicholas
2023-05-09 10:38:54 -04:00
committed by Cody Henthorne
parent 77751c1d28
commit e46564cb7e
5 changed files with 77 additions and 10 deletions

View File

@@ -6,14 +6,19 @@ import android.content.Context;
import androidx.annotation.NonNull;
import org.thoughtcrime.securesms.jobs.LocalBackupJob;
import org.thoughtcrime.securesms.keyvalue.SettingsValues;
import org.thoughtcrime.securesms.keyvalue.SignalStore;
import org.thoughtcrime.securesms.util.JavaTimeExtensionsKt;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import java.time.LocalDateTime;
import java.util.Random;
import java.util.concurrent.TimeUnit;
public class LocalBackupListener extends PersistentAlarmManagerListener {
private static final int BACKUP_JITTER_WINDOW_SECONDS = Math.toIntExact(TimeUnit.MINUTES.toSeconds(10));
@Override
protected boolean shouldScheduleExact() {
return true;
@@ -44,6 +49,11 @@ public class LocalBackupListener extends PersistentAlarmManagerListener {
int hour = SignalStore.settings().getBackupHour();
int minute = SignalStore.settings().getBackupMinute();
LocalDateTime next = now.withHour(hour).withMinute(minute).withSecond(0);
int jitter = (new Random().nextInt(BACKUP_JITTER_WINDOW_SECONDS)) - (BACKUP_JITTER_WINDOW_SECONDS / 2);
next.plusSeconds(jitter);
if (now.isAfter(next)) {
next = next.plusDays(1);
}