diff --git a/core/src/main/java/com/microsoft/applicationinsights/internal/channel/common/TransmissionPolicyManager.java b/core/src/main/java/com/microsoft/applicationinsights/internal/channel/common/TransmissionPolicyManager.java index c074b48d553..1dd935a2423 100644 --- a/core/src/main/java/com/microsoft/applicationinsights/internal/channel/common/TransmissionPolicyManager.java +++ b/core/src/main/java/com/microsoft/applicationinsights/internal/channel/common/TransmissionPolicyManager.java @@ -127,9 +127,10 @@ public void backoff() { * Clear the current thread state and and reset the back off counter. */ public void clearBackoff() { - policyState.setCurrentState(TransmissionPolicy.UNBLOCKED); + if (policyState.setCurrentState(TransmissionPolicy.UNBLOCKED)) { + InternalLogger.INSTANCE.trace("Backoff has been reset."); + } backoffManager.onDoneSending(); -// InternalLogger.INSTANCE.info("Backoff has been reset."); } /** diff --git a/core/src/main/java/com/microsoft/applicationinsights/internal/channel/common/TransmissionPolicyState.java b/core/src/main/java/com/microsoft/applicationinsights/internal/channel/common/TransmissionPolicyState.java index 14506e520db..218db192536 100644 --- a/core/src/main/java/com/microsoft/applicationinsights/internal/channel/common/TransmissionPolicyState.java +++ b/core/src/main/java/com/microsoft/applicationinsights/internal/channel/common/TransmissionPolicyState.java @@ -21,19 +21,21 @@ package com.microsoft.applicationinsights.internal.channel.common; +import java.util.concurrent.atomic.AtomicReference; + /** * Created by gupele on 6/29/2015. */ final class TransmissionPolicyState implements TransmissionPolicyStateFetcher, TransmissionPolicyStateSetter { - private volatile TransmissionPolicy currentState = TransmissionPolicy.UNBLOCKED; + private AtomicReference currentState = new AtomicReference<>(TransmissionPolicy.UNBLOCKED); @Override public TransmissionPolicy getCurrentState() { - return currentState; + return currentState.get(); } @Override - public void setCurrentState(TransmissionPolicy currentState) { - this.currentState = currentState; + public boolean setCurrentState(TransmissionPolicy newState) { + return this.currentState.getAndSet(newState) != newState; } } diff --git a/core/src/main/java/com/microsoft/applicationinsights/internal/channel/common/TransmissionPolicyStateSetter.java b/core/src/main/java/com/microsoft/applicationinsights/internal/channel/common/TransmissionPolicyStateSetter.java index 423e8d27264..00b62e13ac3 100644 --- a/core/src/main/java/com/microsoft/applicationinsights/internal/channel/common/TransmissionPolicyStateSetter.java +++ b/core/src/main/java/com/microsoft/applicationinsights/internal/channel/common/TransmissionPolicyStateSetter.java @@ -25,5 +25,10 @@ * Created by gupele on 6/29/2015. */ interface TransmissionPolicyStateSetter { - void setCurrentState(TransmissionPolicy currentState); + /** + * Sets the current TransmissionPolicy state. + * @param newState The new value for current state + * @return true if the state changed (newState != currentState); false otherwise. + */ + boolean setCurrentState(TransmissionPolicy newState); }