Handle 428 rate limiting.

This commit is contained in:
Greyson Parrelli
2021-05-05 12:49:18 -04:00
parent 02d060ca0a
commit 31e1c6f7aa
60 changed files with 1235 additions and 57 deletions

View File

@@ -176,6 +176,23 @@ class JobController {
.forEach(this::cancelJob);
}
@WorkerThread
synchronized void update(@NonNull JobUpdater updater) {
List<JobSpec> allJobs = jobStorage.getAllJobSpecs();
List<JobSpec> updatedJobs = new LinkedList<>();
for (JobSpec job : allJobs) {
JobSpec updated = updater.update(job, dataSerializer);
if (updated != job) {
updatedJobs.add(updated);
}
}
jobStorage.updateJobs(updatedJobs);
notifyAll();
}
@WorkerThread
synchronized void onRetry(@NonNull Job job, long backoffInterval) {
if (backoffInterval <= 0) {

View File

@@ -223,6 +223,15 @@ public class JobManager implements ConstraintObserver.Notifier {
runOnExecutor(() -> jobController.cancelAllInQueue(queue));
}
/**
* Perform an arbitrary update on enqueued jobs. Will not apply to jobs that are already running.
* You shouldn't use this if you can help it. You give yourself an opportunity to really screw
* things up.
*/
public void update(@NonNull JobUpdater updater) {
runOnExecutor(() -> jobController.update(updater));
}
/**
* Runs the specified job synchronously. Beware: All normal dependencies are respected, meaning
* you must take great care where you call this. It could take a very long time to complete!

View File

@@ -0,0 +1,19 @@
package org.thoughtcrime.securesms.jobmanager;
import androidx.annotation.NonNull;
import org.thoughtcrime.securesms.jobmanager.persistence.JobSpec;
public interface JobUpdater {
/**
* Called for each enqueued job, giving you an opportunity to update each one.
*
* @param jobSpec An object representing data about an enqueued job.
* @param serializer An object that can be used to serialize/deserialize data if necessary for
* your update.
*
* @return The updated JobSpec you want persisted. If you do not wish to make an update, return
* the literal same JobSpec instance you were provided.
*/
@NonNull JobSpec update(@NonNull JobSpec jobSpec, @NonNull Data.Serializer serializer);
}

View File

@@ -49,6 +49,10 @@ public final class JobSpec {
this.memoryOnly = memoryOnly;
}
public @NonNull JobSpec withNextRunAttemptTime(long updated) {
return new JobSpec(id, factoryKey, queueKey, createTime, updated, runAttempt, maxAttempts, lifespan, serializedData, serializedInputData, isRunning, memoryOnly);
}
public @NonNull String getId() {
return id;
}