private synchronized void cancelSuspension(long expectedGeneration) { if (expectedGeneration != generation.get()) { return; } policyState.setCurrentState(TransmissionPolicy.UNBLOCKED); suspensionDate = null; InternalLogger.INSTANCE.info("App throttling is cancelled."); }
/** * Clear the current thread state and and reset the back off counter. */ public void clearBackoff() { policyState.setCurrentState(TransmissionPolicy.UNBLOCKED); backoffManager.onDoneSending(); InternalLogger.INSTANCE.info("Backoff has been reset."); }
private synchronized void doSuspend(TransmissionPolicy policy, long suspendInSeconds) { try { if (policy == TransmissionPolicy.UNBLOCKED) { return; } Date date = Calendar.getInstance().getTime(); date.setTime(date.getTime() + (1000 * suspendInSeconds)); if (this.suspensionDate != null) { long diff = date.getTime() - suspensionDate.getTime(); if (diff <= 0) { return; } } long currentGeneration = generation.incrementAndGet(); threads.schedule(new UnSuspender(currentGeneration), suspendInSeconds, TimeUnit.SECONDS); policyState.setCurrentState(policy); suspensionDate = date; InternalLogger.INSTANCE.info("App is throttled, telemetries are blocked from now, for %s seconds", suspendInSeconds); } catch (ThreadDeath td) { throw td; } catch (Throwable t) { try { InternalLogger.INSTANCE.logAlways(InternalLogger.LoggingLevel.ERROR, "App is throttled but failed to block transmission exception: %s", t.toString()); } catch (ThreadDeath td) { throw td; } catch (Throwable t2) { // chomp } } }
/** * Suspend the transmission thread according to the current back off policy. */ public void backoff() { policyState.setCurrentState(TransmissionPolicy.BACKOFF); long backOffMillis = backoffManager.backOffCurrentSenderThreadValue(); if (backOffMillis > 0) { long backOffSeconds = backOffMillis / 1000; InternalLogger.INSTANCE.info("App is throttled, telemetry will be blocked for %s seconds.", backOffSeconds); this.suspendInSeconds(TransmissionPolicy.BACKOFF, backOffSeconds); } }
@Test public void testSetCurrentState() { TransmissionPolicyState tested = new TransmissionPolicyState(); tested.setCurrentState(TransmissionPolicy.BLOCKED_BUT_CAN_BE_PERSISTED); assertEquals(TransmissionPolicy.BLOCKED_BUT_CAN_BE_PERSISTED, tested.getCurrentState()); tested.setCurrentState(TransmissionPolicy.BLOCKED_AND_CANNOT_BE_PERSISTED); assertEquals(TransmissionPolicy.BLOCKED_AND_CANNOT_BE_PERSISTED, tested.getCurrentState()); } }