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 | |
| 29 | DurationMs GetBackoffDuration(TimerBackoffAlgorithm algorithm, |
| 30 | DurationMs base_duration, |
| 31 | int expiration_count) { |
| 32 | switch (algorithm) { |
| 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; |
| 41 | } |
| 42 | |
| 43 | return DurationMs(std::min(duration_ms, *Timer::kMaxTimerDuration)); |
| 44 | } |
Victor Boivie | 6fa0cfa | 2021-03-30 22:54:41 +0200 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | } // namespace |
| 48 | |
Victor Boivie | 5d3bda5 | 2021-04-12 21:59:19 +0200 | [diff] [blame] | 49 | constexpr DurationMs Timer::kMaxTimerDuration; |
| 50 | |
| 51 | Timer::Timer(TimerID id, |
Victor Boivie | 6fa0cfa | 2021-03-30 22:54:41 +0200 | [diff] [blame] | 52 | absl::string_view name, |
| 53 | OnExpired on_expired, |
| 54 | UnregisterHandler unregister_handler, |
| 55 | std::unique_ptr<Timeout> timeout, |
| 56 | const TimerOptions& options) |
| 57 | : id_(id), |
| 58 | name_(name), |
| 59 | options_(options), |
| 60 | on_expired_(std::move(on_expired)), |
| 61 | unregister_handler_(std::move(unregister_handler)), |
| 62 | timeout_(std::move(timeout)), |
| 63 | duration_(options.duration) {} |
| 64 | |
| 65 | Timer::~Timer() { |
| 66 | Stop(); |
| 67 | unregister_handler_(); |
| 68 | } |
| 69 | |
| 70 | void Timer::Start() { |
| 71 | expiration_count_ = 0; |
| 72 | if (!is_running()) { |
| 73 | is_running_ = true; |
Victor Boivie | 5d3bda5 | 2021-04-12 21:59:19 +0200 | [diff] [blame] | 74 | generation_ = TimerGeneration(*generation_ + 1); |
| 75 | timeout_->Start(duration_, MakeTimeoutId(id_, generation_)); |
Victor Boivie | 6fa0cfa | 2021-03-30 22:54:41 +0200 | [diff] [blame] | 76 | } else { |
| 77 | // Timer was running - stop and restart it, to make it expire in `duration_` |
| 78 | // from now. |
Victor Boivie | 5d3bda5 | 2021-04-12 21:59:19 +0200 | [diff] [blame] | 79 | generation_ = TimerGeneration(*generation_ + 1); |
| 80 | timeout_->Restart(duration_, MakeTimeoutId(id_, generation_)); |
Victor Boivie | 6fa0cfa | 2021-03-30 22:54:41 +0200 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | void Timer::Stop() { |
| 85 | if (is_running()) { |
| 86 | timeout_->Stop(); |
| 87 | expiration_count_ = 0; |
| 88 | is_running_ = false; |
| 89 | } |
| 90 | } |
| 91 | |
Victor Boivie | 5d3bda5 | 2021-04-12 21:59:19 +0200 | [diff] [blame] | 92 | void Timer::Trigger(TimerGeneration generation) { |
Victor Boivie | 6fa0cfa | 2021-03-30 22:54:41 +0200 | [diff] [blame] | 93 | if (is_running_ && generation == generation_) { |
| 94 | ++expiration_count_; |
Victor Boivie | bb7ee95 | 2021-05-05 12:36:52 +0200 | [diff] [blame] | 95 | is_running_ = false; |
Victor Boivie | 9680d29 | 2021-08-30 10:23:49 +0200 | [diff] [blame] | 96 | if (!options_.max_restarts.has_value() || |
| 97 | expiration_count_ <= *options_.max_restarts) { |
Victor Boivie | bb7ee95 | 2021-05-05 12:36:52 +0200 | [diff] [blame] | 98 | // The timer should still be running after this triggers. Start a new |
| 99 | // timer. Note that it might be very quickly restarted again, if the |
| 100 | // `on_expired_` callback returns a new duration. |
| 101 | is_running_ = true; |
Victor Boivie | 6fa0cfa | 2021-03-30 22:54:41 +0200 | [diff] [blame] | 102 | DurationMs duration = GetBackoffDuration(options_.backoff_algorithm, |
| 103 | duration_, expiration_count_); |
Victor Boivie | 5d3bda5 | 2021-04-12 21:59:19 +0200 | [diff] [blame] | 104 | generation_ = TimerGeneration(*generation_ + 1); |
| 105 | timeout_->Start(duration, MakeTimeoutId(id_, generation_)); |
Victor Boivie | 6fa0cfa | 2021-03-30 22:54:41 +0200 | [diff] [blame] | 106 | } |
Victor Boivie | bb7ee95 | 2021-05-05 12:36:52 +0200 | [diff] [blame] | 107 | |
| 108 | absl::optional<DurationMs> new_duration = on_expired_(); |
| 109 | if (new_duration.has_value() && new_duration != duration_) { |
| 110 | duration_ = new_duration.value(); |
| 111 | if (is_running_) { |
| 112 | // Restart it with new duration. |
| 113 | timeout_->Stop(); |
| 114 | |
| 115 | DurationMs duration = GetBackoffDuration(options_.backoff_algorithm, |
| 116 | duration_, expiration_count_); |
| 117 | generation_ = TimerGeneration(*generation_ + 1); |
| 118 | timeout_->Start(duration, MakeTimeoutId(id_, generation_)); |
| 119 | } |
| 120 | } |
Victor Boivie | 6fa0cfa | 2021-03-30 22:54:41 +0200 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
| 124 | void TimerManager::HandleTimeout(TimeoutID timeout_id) { |
Victor Boivie | 5d3bda5 | 2021-04-12 21:59:19 +0200 | [diff] [blame] | 125 | TimerID timer_id(*timeout_id >> 32); |
| 126 | TimerGeneration generation(*timeout_id); |
Victor Boivie | 6fa0cfa | 2021-03-30 22:54:41 +0200 | [diff] [blame] | 127 | auto it = timers_.find(timer_id); |
| 128 | if (it != timers_.end()) { |
| 129 | it->second->Trigger(generation); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | std::unique_ptr<Timer> TimerManager::CreateTimer(absl::string_view name, |
| 134 | Timer::OnExpired on_expired, |
| 135 | const TimerOptions& options) { |
Victor Boivie | 5d3bda5 | 2021-04-12 21:59:19 +0200 | [diff] [blame] | 136 | next_id_ = TimerID(*next_id_ + 1); |
| 137 | TimerID id = next_id_; |
| 138 | // This would overflow after 4 billion timers created, which in SCTP would be |
| 139 | // after 800 million reconnections on a single socket. Ensure this will never |
| 140 | // happen. |
| 141 | RTC_CHECK_NE(*id, std::numeric_limits<uint32_t>::max()); |
Victor Boivie | 6fa0cfa | 2021-03-30 22:54:41 +0200 | [diff] [blame] | 142 | auto timer = absl::WrapUnique(new Timer( |
| 143 | id, name, std::move(on_expired), [this, id]() { timers_.erase(id); }, |
| 144 | create_timeout_(), options)); |
| 145 | timers_[id] = timer.get(); |
| 146 | return timer; |
| 147 | } |
| 148 | |
| 149 | } // namespace dcsctp |