Victor Boivie | 6fa0cfa | 2021-03-30 22:54:41 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | #include "net/dcsctp/timer/timer.h" |
| 11 | |
Victor Boivie | 5d3bda5 | 2021-04-12 21:59:19 +0200 | [diff] [blame] | 12 | #include <algorithm> |
Victor Boivie | 6fa0cfa | 2021-03-30 22:54:41 +0200 | [diff] [blame] | 13 | #include <cstdint> |
Victor Boivie | 5d3bda5 | 2021-04-12 21:59:19 +0200 | [diff] [blame] | 14 | #include <limits> |
Victor Boivie | 6fa0cfa | 2021-03-30 22:54:41 +0200 | [diff] [blame] | 15 | #include <memory> |
Victor Boivie | 6fa0cfa | 2021-03-30 22:54:41 +0200 | [diff] [blame] | 16 | #include <utility> |
| 17 | |
| 18 | #include "absl/memory/memory.h" |
| 19 | #include "absl/strings/string_view.h" |
| 20 | #include "net/dcsctp/public/timeout.h" |
Victor Boivie | 5d3bda5 | 2021-04-12 21:59:19 +0200 | [diff] [blame] | 21 | #include "rtc_base/checks.h" |
Victor Boivie | 6fa0cfa | 2021-03-30 22:54:41 +0200 | [diff] [blame] | 22 | |
| 23 | namespace dcsctp { |
| 24 | namespace { |
Victor Boivie | 5d3bda5 | 2021-04-12 21:59:19 +0200 | [diff] [blame] | 25 | TimeoutID MakeTimeoutId(TimerID timer_id, TimerGeneration generation) { |
| 26 | return TimeoutID(static_cast<uint64_t>(*timer_id) << 32 | *generation); |
Victor Boivie | 6fa0cfa | 2021-03-30 22:54:41 +0200 | [diff] [blame] | 27 | } |
| 28 | |
Victor Boivie | cebbff7 | 2021-08-30 15:21:01 +0200 | [diff] [blame] | 29 | DurationMs GetBackoffDuration(const TimerOptions& options, |
Victor Boivie | 6fa0cfa | 2021-03-30 22:54:41 +0200 | [diff] [blame] | 30 | DurationMs base_duration, |
| 31 | int expiration_count) { |
Victor Boivie | cebbff7 | 2021-08-30 15:21:01 +0200 | [diff] [blame] | 32 | switch (options.backoff_algorithm) { |
Victor Boivie | 6fa0cfa | 2021-03-30 22:54:41 +0200 | [diff] [blame] | 33 | case TimerBackoffAlgorithm::kFixed: |
| 34 | return base_duration; |
Victor Boivie | 5d3bda5 | 2021-04-12 21:59:19 +0200 | [diff] [blame] | 35 | case TimerBackoffAlgorithm::kExponential: { |
| 36 | int32_t duration_ms = *base_duration; |
| 37 | |
| 38 | while (expiration_count > 0 && duration_ms < *Timer::kMaxTimerDuration) { |
| 39 | duration_ms *= 2; |
| 40 | --expiration_count; |
Victor Boivie | cebbff7 | 2021-08-30 15:21:01 +0200 | [diff] [blame] | 41 | |
| 42 | if (options.max_backoff_duration.has_value() && |
| 43 | duration_ms > **options.max_backoff_duration) { |
| 44 | return *options.max_backoff_duration; |
| 45 | } |
Victor Boivie | 5d3bda5 | 2021-04-12 21:59:19 +0200 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | return DurationMs(std::min(duration_ms, *Timer::kMaxTimerDuration)); |
| 49 | } |
Victor Boivie | 6fa0cfa | 2021-03-30 22:54:41 +0200 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | } // namespace |
| 53 | |
Victor Boivie | 5d3bda5 | 2021-04-12 21:59:19 +0200 | [diff] [blame] | 54 | constexpr DurationMs Timer::kMaxTimerDuration; |
| 55 | |
| 56 | Timer::Timer(TimerID id, |
Victor Boivie | 6fa0cfa | 2021-03-30 22:54:41 +0200 | [diff] [blame] | 57 | absl::string_view name, |
| 58 | OnExpired on_expired, |
| 59 | UnregisterHandler unregister_handler, |
| 60 | std::unique_ptr<Timeout> timeout, |
| 61 | const TimerOptions& options) |
| 62 | : id_(id), |
| 63 | name_(name), |
| 64 | options_(options), |
| 65 | on_expired_(std::move(on_expired)), |
| 66 | unregister_handler_(std::move(unregister_handler)), |
| 67 | timeout_(std::move(timeout)), |
| 68 | duration_(options.duration) {} |
| 69 | |
| 70 | Timer::~Timer() { |
| 71 | Stop(); |
| 72 | unregister_handler_(); |
| 73 | } |
| 74 | |
| 75 | void Timer::Start() { |
| 76 | expiration_count_ = 0; |
| 77 | if (!is_running()) { |
| 78 | is_running_ = true; |
Victor Boivie | 5d3bda5 | 2021-04-12 21:59:19 +0200 | [diff] [blame] | 79 | generation_ = TimerGeneration(*generation_ + 1); |
| 80 | timeout_->Start(duration_, MakeTimeoutId(id_, generation_)); |
Victor Boivie | 6fa0cfa | 2021-03-30 22:54:41 +0200 | [diff] [blame] | 81 | } else { |
| 82 | // Timer was running - stop and restart it, to make it expire in `duration_` |
| 83 | // from now. |
Victor Boivie | 5d3bda5 | 2021-04-12 21:59:19 +0200 | [diff] [blame] | 84 | generation_ = TimerGeneration(*generation_ + 1); |
| 85 | timeout_->Restart(duration_, MakeTimeoutId(id_, generation_)); |
Victor Boivie | 6fa0cfa | 2021-03-30 22:54:41 +0200 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
| 89 | void Timer::Stop() { |
| 90 | if (is_running()) { |
| 91 | timeout_->Stop(); |
| 92 | expiration_count_ = 0; |
| 93 | is_running_ = false; |
| 94 | } |
| 95 | } |
| 96 | |
Victor Boivie | 5d3bda5 | 2021-04-12 21:59:19 +0200 | [diff] [blame] | 97 | void Timer::Trigger(TimerGeneration generation) { |
Victor Boivie | 6fa0cfa | 2021-03-30 22:54:41 +0200 | [diff] [blame] | 98 | if (is_running_ && generation == generation_) { |
| 99 | ++expiration_count_; |
Victor Boivie | bb7ee95 | 2021-05-05 12:36:52 +0200 | [diff] [blame] | 100 | is_running_ = false; |
Victor Boivie | 9680d29 | 2021-08-30 10:23:49 +0200 | [diff] [blame] | 101 | if (!options_.max_restarts.has_value() || |
| 102 | expiration_count_ <= *options_.max_restarts) { |
Victor Boivie | bb7ee95 | 2021-05-05 12:36:52 +0200 | [diff] [blame] | 103 | // The timer should still be running after this triggers. Start a new |
| 104 | // timer. Note that it might be very quickly restarted again, if the |
| 105 | // `on_expired_` callback returns a new duration. |
| 106 | is_running_ = true; |
Victor Boivie | cebbff7 | 2021-08-30 15:21:01 +0200 | [diff] [blame] | 107 | DurationMs duration = |
| 108 | GetBackoffDuration(options_, duration_, expiration_count_); |
Victor Boivie | 5d3bda5 | 2021-04-12 21:59:19 +0200 | [diff] [blame] | 109 | generation_ = TimerGeneration(*generation_ + 1); |
| 110 | timeout_->Start(duration, MakeTimeoutId(id_, generation_)); |
Victor Boivie | 6fa0cfa | 2021-03-30 22:54:41 +0200 | [diff] [blame] | 111 | } |
Victor Boivie | bb7ee95 | 2021-05-05 12:36:52 +0200 | [diff] [blame] | 112 | |
| 113 | absl::optional<DurationMs> new_duration = on_expired_(); |
| 114 | if (new_duration.has_value() && new_duration != duration_) { |
| 115 | duration_ = new_duration.value(); |
| 116 | if (is_running_) { |
| 117 | // Restart it with new duration. |
| 118 | timeout_->Stop(); |
| 119 | |
Victor Boivie | cebbff7 | 2021-08-30 15:21:01 +0200 | [diff] [blame] | 120 | DurationMs duration = |
| 121 | GetBackoffDuration(options_, duration_, expiration_count_); |
Victor Boivie | bb7ee95 | 2021-05-05 12:36:52 +0200 | [diff] [blame] | 122 | generation_ = TimerGeneration(*generation_ + 1); |
| 123 | timeout_->Start(duration, MakeTimeoutId(id_, generation_)); |
| 124 | } |
| 125 | } |
Victor Boivie | 6fa0cfa | 2021-03-30 22:54:41 +0200 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| 129 | void TimerManager::HandleTimeout(TimeoutID timeout_id) { |
Victor Boivie | 5d3bda5 | 2021-04-12 21:59:19 +0200 | [diff] [blame] | 130 | TimerID timer_id(*timeout_id >> 32); |
| 131 | TimerGeneration generation(*timeout_id); |
Victor Boivie | 6fa0cfa | 2021-03-30 22:54:41 +0200 | [diff] [blame] | 132 | auto it = timers_.find(timer_id); |
| 133 | if (it != timers_.end()) { |
| 134 | it->second->Trigger(generation); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | std::unique_ptr<Timer> TimerManager::CreateTimer(absl::string_view name, |
| 139 | Timer::OnExpired on_expired, |
| 140 | const TimerOptions& options) { |
Victor Boivie | 5d3bda5 | 2021-04-12 21:59:19 +0200 | [diff] [blame] | 141 | next_id_ = TimerID(*next_id_ + 1); |
| 142 | TimerID id = next_id_; |
| 143 | // This would overflow after 4 billion timers created, which in SCTP would be |
| 144 | // after 800 million reconnections on a single socket. Ensure this will never |
| 145 | // happen. |
| 146 | RTC_CHECK_NE(*id, std::numeric_limits<uint32_t>::max()); |
Philipp Hancke | c3a1ea1 | 2021-11-24 08:27:05 +0100 | [diff] [blame^] | 147 | std::unique_ptr<Timeout> timeout = create_timeout_(); |
| 148 | RTC_CHECK(timeout != nullptr); |
Victor Boivie | 6fa0cfa | 2021-03-30 22:54:41 +0200 | [diff] [blame] | 149 | auto timer = absl::WrapUnique(new Timer( |
| 150 | id, name, std::move(on_expired), [this, id]() { timers_.erase(id); }, |
Philipp Hancke | c3a1ea1 | 2021-11-24 08:27:05 +0100 | [diff] [blame^] | 151 | std::move(timeout), options)); |
Victor Boivie | 6fa0cfa | 2021-03-30 22:54:41 +0200 | [diff] [blame] | 152 | timers_[id] = timer.get(); |
| 153 | return timer; |
| 154 | } |
| 155 | |
| 156 | } // namespace dcsctp |