| // Copyright 2018 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #include "src/uploader/upload_scheduler.h" |
| |
| namespace cobalt::uploader { |
| |
| // Definition of the static constant declared in shipping_manager.h. |
| // This must be less than 2^31. There appears to be a bug in |
| // std::condition_variable::wait_for() in which setting the wait time to |
| // std::chrono::seconds::max() effectively sets the wait time to zero. |
| constexpr std::chrono::seconds UploadScheduler::kMaxSeconds(999999999); |
| |
| UploadScheduler::UploadScheduler(UploadScheduleConfig config) |
| : current_interval_(config.initial_interval), |
| target_interval_(config.target_interval), |
| min_interval_(config.min_interval), |
| jitter_(config.jitter) { |
| CHECK_GE(jitter_, 0); |
| CHECK_LT(jitter_, 1); |
| CHECK_GE(min_interval_.count(), 0); |
| CHECK_LE(current_interval_.count(), target_interval_.count()); |
| CHECK_LE(min_interval_.count(), target_interval_.count()); |
| CHECK_LE(target_interval_.count(), kMaxSeconds.count()); |
| } |
| |
| UploadScheduler::UploadScheduler(std::chrono::seconds target_interval, |
| std::chrono::seconds min_interval) |
| : UploadScheduler(UploadScheduleConfig{target_interval, min_interval, target_interval, 0}) {} |
| |
| std::chrono::seconds UploadScheduler::Interval() { |
| auto interval = current_interval_; |
| if (current_interval_ < target_interval_) { |
| current_interval_ *= 2; |
| if (current_interval_ >= target_interval_) { |
| current_interval_ = target_interval_; |
| } |
| } |
| int interval_count = std::chrono::duration_cast<std::chrono::seconds>(interval).count(); |
| if (interval_count == 0) { |
| return interval; |
| } |
| |
| // Generate a random offset between [-n, n): n = jitter*current_interval. |
| auto unscaled_offset = static_cast<float>(rand() % (2 * interval_count) - interval_count); |
| std::chrono::seconds offset = std::chrono::seconds(static_cast<int>(unscaled_offset * jitter_)); |
| return interval + offset; |
| } |
| |
| } // namespace cobalt::uploader |