-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#52317] android-client: Add exponential backoff to management WS rec…
…onnections
- Loading branch information
Showing
2 changed files
with
46 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...s/android-client/app/src/main/java/com/antmicro/update/rdfm/utilities/BackoffCounter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.antmicro.update.rdfm.utilities; | ||
|
||
public class BackoffCounter { | ||
private long mCurrent; | ||
private final long mInitialValue; | ||
private final long mMaxValue; | ||
|
||
public BackoffCounter(long initialValue, long maxValue) { | ||
if (initialValue > maxValue) { | ||
throw new IllegalArgumentException("initialValue is larger than maxValue"); | ||
} | ||
mCurrent = initialValue; | ||
mInitialValue = initialValue; | ||
mMaxValue = maxValue; | ||
} | ||
|
||
/** | ||
* Advance the backoff counter and return the new value. | ||
* | ||
* @return new backoff value | ||
*/ | ||
public long next() { | ||
mCurrent = Math.multiplyExact(mCurrent, 2L); | ||
if (mCurrent > mMaxValue) { | ||
mCurrent = mMaxValue; | ||
} | ||
return mCurrent; | ||
} | ||
|
||
/** | ||
* Reset the counter to its initial value | ||
*/ | ||
public void reset() { | ||
mCurrent = mInitialValue; | ||
} | ||
|
||
|
||
} |