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