package com.discord.utilities.networking; import d0.z.d.m; import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.ExecutorService; import kotlin.Unit; import kotlin.jvm.functions.Function0; import kotlin.jvm.internal.DefaultConstructorMarker; /* compiled from: Backoff.kt */ public final class Backoff { private long current; private int fails; private final int failureThreshold; private boolean isPending; private final boolean jitter; private final long maxBackoffMs; private final long minBackoffMs; private final Scheduler scheduler; /* compiled from: Backoff.kt */ public interface Scheduler { void cancel(); void schedule(Function0 function0, long j); } /* compiled from: Backoff.kt */ public static final class TimerScheduler extends Timer implements Scheduler { private final ExecutorService delegateExecutor; private final String tag; private TimerTask timeoutTimerTask; public TimerScheduler() { this(null, null, 3, null); } public TimerScheduler(String str, ExecutorService executorService) { m.checkNotNullParameter(str, "tag"); this.tag = str; this.delegateExecutor = executorService; } /* JADX INFO: this call moved to the top of the method (can break code semantics) */ public /* synthetic */ TimerScheduler(String str, ExecutorService executorService, int i, DefaultConstructorMarker defaultConstructorMarker) { this((i & 1) != 0 ? "TimerScheduler" : str, (i & 2) != 0 ? null : executorService); } public static final /* synthetic */ ExecutorService access$getDelegateExecutor$p(TimerScheduler timerScheduler) { return timerScheduler.delegateExecutor; } public static final /* synthetic */ String access$getTag$p(TimerScheduler timerScheduler) { return timerScheduler.tag; } @Override // java.util.Timer, com.discord.utilities.networking.Backoff.Scheduler public synchronized void cancel() { TimerTask timerTask = this.timeoutTimerTask; if (timerTask != null) { timerTask.cancel(); } } @Override // com.discord.utilities.networking.Backoff.Scheduler public synchronized void schedule(Function0 function0, long j) { m.checkNotNullParameter(function0, "action"); TimerTask timerTask = this.timeoutTimerTask; if (timerTask != null) { timerTask.cancel(); } Backoff$TimerScheduler$schedule$1 backoff$TimerScheduler$schedule$1 = new Backoff$TimerScheduler$schedule$1(this, function0); this.timeoutTimerTask = backoff$TimerScheduler$schedule$1; schedule(backoff$TimerScheduler$schedule$1, j); } } public Backoff() { this(0, 0, 0, false, null, 31, null); } public Backoff(long j, long j2, int i, boolean z2, Scheduler scheduler) { m.checkNotNullParameter(scheduler, "scheduler"); this.minBackoffMs = j; this.maxBackoffMs = j2; this.failureThreshold = i; this.jitter = z2; this.scheduler = scheduler; this.current = j; } /* JADX WARNING: Illegal instructions before constructor call */ public /* synthetic */ Backoff(long j, long j2, int i, boolean z2, Scheduler scheduler, int i2, DefaultConstructorMarker defaultConstructorMarker) { this(r0, (i2 & 2) != 0 ? ((long) 10) * r0 : j2, (i2 & 4) != 0 ? Integer.MAX_VALUE : i, (i2 & 8) != 0 ? true : z2, (i2 & 16) != 0 ? new TimerScheduler(null, null, 3, null) : scheduler); long j3 = (i2 & 1) != 0 ? 500 : j; } public static final /* synthetic */ void access$executeFailureCallback(Backoff backoff, Function0 function0) { backoff.executeFailureCallback(function0); } private final synchronized void executeFailureCallback(Function0 function0) { this.isPending = false; function0.mo1invoke(); } /* JADX DEBUG: Multi-variable search result rejected for r0v0, resolved type: com.discord.utilities.networking.Backoff */ /* JADX WARN: Multi-variable type inference failed */ public static /* synthetic */ long fail$default(Backoff backoff, Function0 function0, int i, Object obj) { if ((i & 1) != 0) { function0 = null; } return backoff.fail(function0); } public final synchronized void cancel() { this.scheduler.cancel(); this.isPending = false; } public final synchronized long fail(Function0 function0) { this.fails++; double random = this.jitter ? Math.random() : 1.0d; long j = this.current; this.current = Math.min(j + ((long) (((double) (((long) 2) * j)) * random)), this.maxBackoffMs); if (function0 != null && !this.isPending) { this.isPending = true; this.scheduler.schedule(new Backoff$fail$1(this, function0), this.current); } return this.current; } public final boolean hasReachedFailureThreshold() { return this.fails > this.failureThreshold; } public final boolean isPending() { return this.isPending; } public final synchronized void succeed() { cancel(); this.fails = 0; this.current = this.minBackoffMs; } }