backoff_delay_provider.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_SYNC_ENGINE_BACKOFF_DELAY_PROVIDER_H_
  5. #define COMPONENTS_SYNC_ENGINE_BACKOFF_DELAY_PROVIDER_H_
  6. #include <memory>
  7. #include "base/time/time.h"
  8. namespace syncer {
  9. struct ModelNeutralState;
  10. // A component used to get time delays associated with exponential backoff.
  11. class BackoffDelayProvider {
  12. public:
  13. // Factory function to create a standard BackoffDelayProvider.
  14. static std::unique_ptr<BackoffDelayProvider> FromDefaults();
  15. // Similar to above, but causes sync to retry very quickly (see
  16. // polling_constants.h) when it encounters an error before exponential
  17. // backoff.
  18. //
  19. // *** NOTE *** This should only be used if kSyncShortInitialRetryOverride
  20. // was passed to command line.
  21. static std::unique_ptr<BackoffDelayProvider> WithShortInitialRetryOverride();
  22. BackoffDelayProvider(const BackoffDelayProvider&) = delete;
  23. BackoffDelayProvider& operator=(const BackoffDelayProvider&) = delete;
  24. virtual ~BackoffDelayProvider();
  25. // DDOS avoidance function. Calculates how long we should wait before trying
  26. // again after a failed sync attempt, where the last delay was |base_delay|.
  27. // TODO(tim): Look at URLRequestThrottlerEntryInterface.
  28. virtual base::TimeDelta GetDelay(const base::TimeDelta& last_delay);
  29. // Helper to calculate the initial value for exponential backoff.
  30. // See possible values and comments in polling_constants.h.
  31. virtual base::TimeDelta GetInitialDelay(const ModelNeutralState& state) const;
  32. // Test-only variant that avoids randomness in tests. |jitter_sign| must be -1
  33. // or 1 and determines whether the jitter in the delay will be positive or
  34. // negative.
  35. base::TimeDelta GetDelayForTesting(base::TimeDelta last_delay,
  36. int jitter_sign);
  37. protected:
  38. BackoffDelayProvider(const base::TimeDelta& default_initial_backoff,
  39. const base::TimeDelta& short_initial_backoff);
  40. private:
  41. const base::TimeDelta default_initial_backoff_;
  42. const base::TimeDelta short_initial_backoff_;
  43. };
  44. } // namespace syncer
  45. #endif // COMPONENTS_SYNC_ENGINE_BACKOFF_DELAY_PROVIDER_H_