Yu-Han Yang | c87c4c3 | 2018-02-20 17:05:59 -0800 | [diff] [blame] | 1 | package com.android.server.location; |
| 2 | |
| 3 | /** |
| 4 | * A simple implementation of exponential backoff. |
| 5 | */ |
| 6 | class ExponentialBackOff { |
| 7 | private static final int MULTIPLIER = 2; |
| 8 | private final long mInitIntervalMillis; |
| 9 | private final long mMaxIntervalMillis; |
| 10 | private long mCurrentIntervalMillis; |
| 11 | |
| 12 | ExponentialBackOff(long initIntervalMillis, long maxIntervalMillis) { |
| 13 | mInitIntervalMillis = initIntervalMillis; |
| 14 | mMaxIntervalMillis = maxIntervalMillis; |
| 15 | |
| 16 | mCurrentIntervalMillis = mInitIntervalMillis / MULTIPLIER; |
| 17 | } |
| 18 | |
| 19 | long nextBackoffMillis() { |
| 20 | if (mCurrentIntervalMillis > mMaxIntervalMillis) { |
| 21 | return mMaxIntervalMillis; |
| 22 | } |
| 23 | |
| 24 | mCurrentIntervalMillis *= MULTIPLIER; |
| 25 | return mCurrentIntervalMillis; |
| 26 | } |
| 27 | |
| 28 | void reset() { |
| 29 | mCurrentIntervalMillis = mInitIntervalMillis / MULTIPLIER; |
| 30 | } |
| 31 | } |
| 32 | |