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 | #ifndef NET_DCSCTP_TIMER_TIMER_H_ |
| 11 | #define NET_DCSCTP_TIMER_TIMER_H_ |
| 12 | |
| 13 | #include <stdint.h> |
| 14 | |
Victor Boivie | 5d3bda5 | 2021-04-12 21:59:19 +0200 | [diff] [blame] | 15 | #include <algorithm> |
Victor Boivie | 6fa0cfa | 2021-03-30 22:54:41 +0200 | [diff] [blame] | 16 | #include <functional> |
Victor Boivie | 3ec9e03 | 2021-08-18 15:22:42 +0200 | [diff] [blame] | 17 | #include <map> |
Victor Boivie | 6fa0cfa | 2021-03-30 22:54:41 +0200 | [diff] [blame] | 18 | #include <memory> |
| 19 | #include <string> |
Victor Boivie | 6fa0cfa | 2021-03-30 22:54:41 +0200 | [diff] [blame] | 20 | #include <utility> |
| 21 | |
| 22 | #include "absl/strings/string_view.h" |
| 23 | #include "absl/types/optional.h" |
Victor Boivie | 5d3bda5 | 2021-04-12 21:59:19 +0200 | [diff] [blame] | 24 | #include "net/dcsctp/public/strong_alias.h" |
Victor Boivie | 6fa0cfa | 2021-03-30 22:54:41 +0200 | [diff] [blame] | 25 | #include "net/dcsctp/public/timeout.h" |
| 26 | |
| 27 | namespace dcsctp { |
| 28 | |
Victor Boivie | 5d3bda5 | 2021-04-12 21:59:19 +0200 | [diff] [blame] | 29 | using TimerID = StrongAlias<class TimerIDTag, uint32_t>; |
| 30 | using TimerGeneration = StrongAlias<class TimerGenerationTag, uint32_t>; |
| 31 | |
Victor Boivie | 6fa0cfa | 2021-03-30 22:54:41 +0200 | [diff] [blame] | 32 | enum class TimerBackoffAlgorithm { |
| 33 | // The base duration will be used for any restart. |
| 34 | kFixed, |
| 35 | // An exponential backoff is used for restarts, with a 2x multiplier, meaning |
| 36 | // that every restart will use a duration that is twice as long as the |
| 37 | // previous. |
| 38 | kExponential, |
| 39 | }; |
| 40 | |
| 41 | struct TimerOptions { |
| 42 | explicit TimerOptions(DurationMs duration) |
| 43 | : TimerOptions(duration, TimerBackoffAlgorithm::kExponential) {} |
| 44 | TimerOptions(DurationMs duration, TimerBackoffAlgorithm backoff_algorithm) |
Victor Boivie | 9680d29 | 2021-08-30 10:23:49 +0200 | [diff] [blame] | 45 | : TimerOptions(duration, backoff_algorithm, absl::nullopt) {} |
Victor Boivie | 6fa0cfa | 2021-03-30 22:54:41 +0200 | [diff] [blame] | 46 | TimerOptions(DurationMs duration, |
| 47 | TimerBackoffAlgorithm backoff_algorithm, |
Victor Boivie | 9680d29 | 2021-08-30 10:23:49 +0200 | [diff] [blame] | 48 | absl::optional<int> max_restarts) |
Victor Boivie | cebbff7 | 2021-08-30 15:21:01 +0200 | [diff] [blame^] | 49 | : TimerOptions(duration, backoff_algorithm, max_restarts, absl::nullopt) { |
| 50 | } |
| 51 | TimerOptions(DurationMs duration, |
| 52 | TimerBackoffAlgorithm backoff_algorithm, |
| 53 | absl::optional<int> max_restarts, |
| 54 | absl::optional<DurationMs> max_backoff_duration) |
Victor Boivie | 6fa0cfa | 2021-03-30 22:54:41 +0200 | [diff] [blame] | 55 | : duration(duration), |
| 56 | backoff_algorithm(backoff_algorithm), |
Victor Boivie | cebbff7 | 2021-08-30 15:21:01 +0200 | [diff] [blame^] | 57 | max_restarts(max_restarts), |
| 58 | max_backoff_duration(max_backoff_duration) {} |
Victor Boivie | 6fa0cfa | 2021-03-30 22:54:41 +0200 | [diff] [blame] | 59 | |
| 60 | // The initial timer duration. Can be overridden with `set_duration`. |
| 61 | const DurationMs duration; |
| 62 | // If the duration should be increased (using exponential backoff) when it is |
| 63 | // restarted. If not set, the same duration will be used. |
| 64 | const TimerBackoffAlgorithm backoff_algorithm; |
Victor Boivie | 9680d29 | 2021-08-30 10:23:49 +0200 | [diff] [blame] | 65 | // The maximum number of times that the timer will be automatically restarted, |
| 66 | // or absl::nullopt if there is no limit. |
| 67 | const absl::optional<int> max_restarts; |
Victor Boivie | cebbff7 | 2021-08-30 15:21:01 +0200 | [diff] [blame^] | 68 | // The maximum timeout value for exponential backoff. |
| 69 | const absl::optional<DurationMs> max_backoff_duration; |
Victor Boivie | 6fa0cfa | 2021-03-30 22:54:41 +0200 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | // A high-level timer (in contrast to the low-level `Timeout` class). |
| 73 | // |
| 74 | // Timers are started and can be stopped or restarted. When a timer expires, |
| 75 | // the provided `on_expired` callback will be triggered. A timer is |
| 76 | // automatically restarted, as long as the number of restarts is below the |
| 77 | // configurable `max_restarts` parameter. The `is_running` property can be |
| 78 | // queried to know if it's still running after having expired. |
| 79 | // |
| 80 | // When a timer is restarted, it will use a configurable `backoff_algorithm` to |
| 81 | // possibly adjust the duration of the next expiry. It is also possible to |
| 82 | // return a new base duration (which is the duration before it's adjusted by the |
| 83 | // backoff algorithm). |
| 84 | class Timer { |
| 85 | public: |
Victor Boivie | 5d3bda5 | 2021-04-12 21:59:19 +0200 | [diff] [blame] | 86 | // The maximum timer duration - one day. |
| 87 | static constexpr DurationMs kMaxTimerDuration = DurationMs(24 * 3600 * 1000); |
| 88 | |
Victor Boivie | 6fa0cfa | 2021-03-30 22:54:41 +0200 | [diff] [blame] | 89 | // When expired, the timer handler can optionally return a new duration which |
| 90 | // will be set as `duration` and used as base duration when the timer is |
| 91 | // restarted and as input to the backoff algorithm. |
| 92 | using OnExpired = std::function<absl::optional<DurationMs>()>; |
| 93 | |
| 94 | // TimerManager will have pointers to these instances, so they must not move. |
| 95 | Timer(const Timer&) = delete; |
| 96 | Timer& operator=(const Timer&) = delete; |
| 97 | |
| 98 | ~Timer(); |
| 99 | |
| 100 | // Starts the timer if it's stopped or restarts the timer if it's already |
| 101 | // running. The `expiration_count` will be reset. |
| 102 | void Start(); |
| 103 | |
| 104 | // Stops the timer. This can also be called when the timer is already stopped. |
| 105 | // The `expiration_count` will be reset. |
| 106 | void Stop(); |
| 107 | |
| 108 | // Sets the base duration. The actual timer duration may be larger depending |
| 109 | // on the backoff algorithm. |
Victor Boivie | 5d3bda5 | 2021-04-12 21:59:19 +0200 | [diff] [blame] | 110 | void set_duration(DurationMs duration) { |
| 111 | duration_ = std::min(duration, kMaxTimerDuration); |
| 112 | } |
Victor Boivie | 6fa0cfa | 2021-03-30 22:54:41 +0200 | [diff] [blame] | 113 | |
| 114 | // Retrieves the base duration. The actual timer duration may be larger |
| 115 | // depending on the backoff algorithm. |
| 116 | DurationMs duration() const { return duration_; } |
| 117 | |
| 118 | // Returns the number of times the timer has expired. |
| 119 | int expiration_count() const { return expiration_count_; } |
| 120 | |
| 121 | // Returns the timer's options. |
| 122 | const TimerOptions& options() const { return options_; } |
| 123 | |
| 124 | // Returns the name of the timer. |
| 125 | absl::string_view name() const { return name_; } |
| 126 | |
| 127 | // Indicates if this timer is currently running. |
| 128 | bool is_running() const { return is_running_; } |
| 129 | |
| 130 | private: |
| 131 | friend class TimerManager; |
| 132 | using UnregisterHandler = std::function<void()>; |
Victor Boivie | 5d3bda5 | 2021-04-12 21:59:19 +0200 | [diff] [blame] | 133 | Timer(TimerID id, |
Victor Boivie | 6fa0cfa | 2021-03-30 22:54:41 +0200 | [diff] [blame] | 134 | absl::string_view name, |
| 135 | OnExpired on_expired, |
| 136 | UnregisterHandler unregister, |
| 137 | std::unique_ptr<Timeout> timeout, |
| 138 | const TimerOptions& options); |
| 139 | |
| 140 | // Called by TimerManager. Will trigger the callback and increment |
| 141 | // `expiration_count`. The timer will automatically be restarted at the |
| 142 | // duration as decided by the backoff algorithm, unless the |
| 143 | // `TimerOptions::max_restarts` has been reached and then it will be stopped |
| 144 | // and `is_running()` will return false. |
Victor Boivie | 5d3bda5 | 2021-04-12 21:59:19 +0200 | [diff] [blame] | 145 | void Trigger(TimerGeneration generation); |
Victor Boivie | 6fa0cfa | 2021-03-30 22:54:41 +0200 | [diff] [blame] | 146 | |
Victor Boivie | 5d3bda5 | 2021-04-12 21:59:19 +0200 | [diff] [blame] | 147 | const TimerID id_; |
Victor Boivie | 6fa0cfa | 2021-03-30 22:54:41 +0200 | [diff] [blame] | 148 | const std::string name_; |
| 149 | const TimerOptions options_; |
| 150 | const OnExpired on_expired_; |
| 151 | const UnregisterHandler unregister_handler_; |
| 152 | const std::unique_ptr<Timeout> timeout_; |
| 153 | |
| 154 | DurationMs duration_; |
| 155 | |
Victor Boivie | 5d3bda5 | 2021-04-12 21:59:19 +0200 | [diff] [blame] | 156 | // Increased on each start, and is matched on Trigger, to avoid races. And by |
| 157 | // race, meaning that a timeout - which may be evaluated/expired on a |
| 158 | // different thread while this thread has stopped that timer already. Note |
| 159 | // that the entire socket is not thread-safe, so `TimerManager::HandleTimeout` |
| 160 | // is never executed concurrently with any timer starting/stopping. |
| 161 | // |
| 162 | // This will wrap around after 4 billion timer restarts, and if it wraps |
| 163 | // around, it would just trigger _this_ timer in advance (but it's hard to |
| 164 | // restart it 4 billion times within its duration). |
| 165 | TimerGeneration generation_ = TimerGeneration(0); |
Victor Boivie | 6fa0cfa | 2021-03-30 22:54:41 +0200 | [diff] [blame] | 166 | bool is_running_ = false; |
| 167 | // Incremented each time time has expired and reset when stopped or restarted. |
| 168 | int expiration_count_ = 0; |
| 169 | }; |
| 170 | |
| 171 | // Creates and manages timers. |
| 172 | class TimerManager { |
| 173 | public: |
| 174 | explicit TimerManager( |
| 175 | std::function<std::unique_ptr<Timeout>()> create_timeout) |
| 176 | : create_timeout_(std::move(create_timeout)) {} |
| 177 | |
| 178 | // Creates a timer with name `name` that will expire (when started) after |
| 179 | // `options.duration` and call `on_expired`. There are more `options` that |
| 180 | // affects the behavior. Note that timers are created initially stopped. |
| 181 | std::unique_ptr<Timer> CreateTimer(absl::string_view name, |
| 182 | Timer::OnExpired on_expired, |
| 183 | const TimerOptions& options); |
| 184 | |
| 185 | void HandleTimeout(TimeoutID timeout_id); |
| 186 | |
| 187 | private: |
| 188 | const std::function<std::unique_ptr<Timeout>()> create_timeout_; |
Victor Boivie | 3ec9e03 | 2021-08-18 15:22:42 +0200 | [diff] [blame] | 189 | std::map<TimerID, Timer*> timers_; |
Victor Boivie | 5d3bda5 | 2021-04-12 21:59:19 +0200 | [diff] [blame] | 190 | TimerID next_id_ = TimerID(0); |
Victor Boivie | 6fa0cfa | 2021-03-30 22:54:41 +0200 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | } // namespace dcsctp |
| 194 | |
| 195 | #endif // NET_DCSCTP_TIMER_TIMER_H_ |