All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.backendless.utils.timeout.TimeOutManagerImpl Maven / Gradle / Ivy

There is a newer version: 7.0-alpha
Show newest version
package com.backendless.utils.timeout;

public class TimeOutManagerImpl implements TimeOutManager
{
  private static final int INITIAL_TIMEOUT = 200;
  private static final int MAX_TIMEOUT = 60 * 1000; //1 min
  private static final int REPEAT_TIMES_BEFORE_INCREASE = 10;

  private int repeatedTimes;
  private int currentTimeOut;

  public TimeOutManagerImpl()
  {
    reset();
  }

  @Override
  public int nextTimeout()
  {
    if( currentTimeOut > MAX_TIMEOUT )
      return MAX_TIMEOUT;

    if( repeatedTimes++ > 0 && ( repeatedTimes % REPEAT_TIMES_BEFORE_INCREASE ) == 0 )
    {
      currentTimeOut *= 2;
    }

    return currentTimeOut;
  }

  @Override
  public int repeatedTimes()
  {
    return repeatedTimes;
  }

  @Override
  public void reset()
  {
     repeatedTimes = 0;
     currentTimeOut = INITIAL_TIMEOUT;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy