Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

159 lines
5.0 KiB
C++
Raw Normal View History

/*
* Copyright (c) 2021 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "net/dcsctp/timer/timer.h"
#include <algorithm>
#include <cstdint>
#include <limits>
#include <memory>
#include <utility>
#include "absl/memory/memory.h"
#include "absl/strings/string_view.h"
#include "net/dcsctp/public/timeout.h"
#include "rtc_base/checks.h"
namespace dcsctp {
namespace {
using ::webrtc::TimeDelta;
TimeoutID MakeTimeoutId(TimerID timer_id, TimerGeneration generation) {
return TimeoutID(static_cast<uint64_t>(*timer_id) << 32 | *generation);
}
TimeDelta GetBackoffDuration(const TimerOptions& options,
TimeDelta base_duration,
int expiration_count) {
switch (options.backoff_algorithm) {
case TimerBackoffAlgorithm::kFixed:
return base_duration;
case TimerBackoffAlgorithm::kExponential: {
TimeDelta duration = base_duration;
while (expiration_count > 0 && duration < Timer::kMaxTimerDuration) {
duration = duration * 2;
--expiration_count;
if (duration > options.max_backoff_duration) {
return options.max_backoff_duration;
}
}
return TimeDelta(std::min(duration, Timer::kMaxTimerDuration));
}
}
}
} // namespace
constexpr TimeDelta Timer::kMaxTimerDuration;
Timer::Timer(TimerID id,
absl::string_view name,
OnExpired on_expired,
UnregisterHandler unregister_handler,
std::unique_ptr<Timeout> timeout,
const TimerOptions& options)
: id_(id),
name_(name),
options_(options),
on_expired_(std::move(on_expired)),
unregister_handler_(std::move(unregister_handler)),
timeout_(std::move(timeout)),
duration_(options.duration) {}
Timer::~Timer() {
Stop();
unregister_handler_();
}
void Timer::Start() {
expiration_count_ = 0;
if (!is_running()) {
is_running_ = true;
generation_ = TimerGeneration(*generation_ + 1);
timeout_->Start(DurationMs(duration_), MakeTimeoutId(id_, generation_));
} else {
// Timer was running - stop and restart it, to make it expire in `duration_`
// from now.
generation_ = TimerGeneration(*generation_ + 1);
timeout_->Restart(DurationMs(duration_), MakeTimeoutId(id_, generation_));
}
}
void Timer::Stop() {
if (is_running()) {
timeout_->Stop();
expiration_count_ = 0;
is_running_ = false;
}
}
void Timer::Trigger(TimerGeneration generation) {
if (is_running_ && generation == generation_) {
++expiration_count_;
dcsctp: Handle starting timer from timer callback This was caught in an integration test which had stricter assertions than the FakeTimeout which is used in unit tests, so the first thing was to add the same assertions to the FakeTimeout. The issue is that when a Timer triggers, and if it's set to automatically restart (possibly with an exponential backoff), the `is_running_` field was set to true while the timer callback was called to allow the client to know that the timer is in fact running, but the timer was actually not started until the callback returned. Which made sense, as the callback can with its return value override the duration, which should affect the backoff algorithm. The problem was when a timer was manually started within the callback. As the Timer itself thought that it was already running, it first would Stop() the underlying Timeout, then Start(). But calling Stop() on a timeout that is not running is illegal, which set of assertions. So the solution is to don't lie; Don't say that a timer is running when it's not. Make sure that the timer is running when the timer callback is triggered, which makes it consistent at all times. That may result in unnecessary timeout invocations (stopping and starting), but that's not too expensive. Bug: webrtc:12614 Change-Id: I7b4447ccd88bd43d181e158f0d29b0770c8a3fd6 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/217522 Reviewed-by: Florent Castelli <orphis@webrtc.org> Commit-Queue: Victor Boivie <boivie@webrtc.org> Cr-Commit-Position: refs/heads/master@{#33926}
2021-05-05 12:36:52 +02:00
is_running_ = false;
if (!options_.max_restarts.has_value() ||
expiration_count_ <= *options_.max_restarts) {
dcsctp: Handle starting timer from timer callback This was caught in an integration test which had stricter assertions than the FakeTimeout which is used in unit tests, so the first thing was to add the same assertions to the FakeTimeout. The issue is that when a Timer triggers, and if it's set to automatically restart (possibly with an exponential backoff), the `is_running_` field was set to true while the timer callback was called to allow the client to know that the timer is in fact running, but the timer was actually not started until the callback returned. Which made sense, as the callback can with its return value override the duration, which should affect the backoff algorithm. The problem was when a timer was manually started within the callback. As the Timer itself thought that it was already running, it first would Stop() the underlying Timeout, then Start(). But calling Stop() on a timeout that is not running is illegal, which set of assertions. So the solution is to don't lie; Don't say that a timer is running when it's not. Make sure that the timer is running when the timer callback is triggered, which makes it consistent at all times. That may result in unnecessary timeout invocations (stopping and starting), but that's not too expensive. Bug: webrtc:12614 Change-Id: I7b4447ccd88bd43d181e158f0d29b0770c8a3fd6 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/217522 Reviewed-by: Florent Castelli <orphis@webrtc.org> Commit-Queue: Victor Boivie <boivie@webrtc.org> Cr-Commit-Position: refs/heads/master@{#33926}
2021-05-05 12:36:52 +02:00
// The timer should still be running after this triggers. Start a new
// timer. Note that it might be very quickly restarted again, if the
// `on_expired_` callback returns a new duration.
is_running_ = true;
TimeDelta duration =
GetBackoffDuration(options_, duration_, expiration_count_);
generation_ = TimerGeneration(*generation_ + 1);
timeout_->Start(DurationMs(duration), MakeTimeoutId(id_, generation_));
}
dcsctp: Handle starting timer from timer callback This was caught in an integration test which had stricter assertions than the FakeTimeout which is used in unit tests, so the first thing was to add the same assertions to the FakeTimeout. The issue is that when a Timer triggers, and if it's set to automatically restart (possibly with an exponential backoff), the `is_running_` field was set to true while the timer callback was called to allow the client to know that the timer is in fact running, but the timer was actually not started until the callback returned. Which made sense, as the callback can with its return value override the duration, which should affect the backoff algorithm. The problem was when a timer was manually started within the callback. As the Timer itself thought that it was already running, it first would Stop() the underlying Timeout, then Start(). But calling Stop() on a timeout that is not running is illegal, which set of assertions. So the solution is to don't lie; Don't say that a timer is running when it's not. Make sure that the timer is running when the timer callback is triggered, which makes it consistent at all times. That may result in unnecessary timeout invocations (stopping and starting), but that's not too expensive. Bug: webrtc:12614 Change-Id: I7b4447ccd88bd43d181e158f0d29b0770c8a3fd6 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/217522 Reviewed-by: Florent Castelli <orphis@webrtc.org> Commit-Queue: Victor Boivie <boivie@webrtc.org> Cr-Commit-Position: refs/heads/master@{#33926}
2021-05-05 12:36:52 +02:00
TimeDelta new_duration = on_expired_();
RTC_DCHECK(new_duration != TimeDelta::PlusInfinity());
if (new_duration > TimeDelta::Zero() && new_duration != duration_) {
duration_ = new_duration;
dcsctp: Handle starting timer from timer callback This was caught in an integration test which had stricter assertions than the FakeTimeout which is used in unit tests, so the first thing was to add the same assertions to the FakeTimeout. The issue is that when a Timer triggers, and if it's set to automatically restart (possibly with an exponential backoff), the `is_running_` field was set to true while the timer callback was called to allow the client to know that the timer is in fact running, but the timer was actually not started until the callback returned. Which made sense, as the callback can with its return value override the duration, which should affect the backoff algorithm. The problem was when a timer was manually started within the callback. As the Timer itself thought that it was already running, it first would Stop() the underlying Timeout, then Start(). But calling Stop() on a timeout that is not running is illegal, which set of assertions. So the solution is to don't lie; Don't say that a timer is running when it's not. Make sure that the timer is running when the timer callback is triggered, which makes it consistent at all times. That may result in unnecessary timeout invocations (stopping and starting), but that's not too expensive. Bug: webrtc:12614 Change-Id: I7b4447ccd88bd43d181e158f0d29b0770c8a3fd6 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/217522 Reviewed-by: Florent Castelli <orphis@webrtc.org> Commit-Queue: Victor Boivie <boivie@webrtc.org> Cr-Commit-Position: refs/heads/master@{#33926}
2021-05-05 12:36:52 +02:00
if (is_running_) {
// Restart it with new duration.
timeout_->Stop();
TimeDelta duration =
GetBackoffDuration(options_, duration_, expiration_count_);
dcsctp: Handle starting timer from timer callback This was caught in an integration test which had stricter assertions than the FakeTimeout which is used in unit tests, so the first thing was to add the same assertions to the FakeTimeout. The issue is that when a Timer triggers, and if it's set to automatically restart (possibly with an exponential backoff), the `is_running_` field was set to true while the timer callback was called to allow the client to know that the timer is in fact running, but the timer was actually not started until the callback returned. Which made sense, as the callback can with its return value override the duration, which should affect the backoff algorithm. The problem was when a timer was manually started within the callback. As the Timer itself thought that it was already running, it first would Stop() the underlying Timeout, then Start(). But calling Stop() on a timeout that is not running is illegal, which set of assertions. So the solution is to don't lie; Don't say that a timer is running when it's not. Make sure that the timer is running when the timer callback is triggered, which makes it consistent at all times. That may result in unnecessary timeout invocations (stopping and starting), but that's not too expensive. Bug: webrtc:12614 Change-Id: I7b4447ccd88bd43d181e158f0d29b0770c8a3fd6 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/217522 Reviewed-by: Florent Castelli <orphis@webrtc.org> Commit-Queue: Victor Boivie <boivie@webrtc.org> Cr-Commit-Position: refs/heads/master@{#33926}
2021-05-05 12:36:52 +02:00
generation_ = TimerGeneration(*generation_ + 1);
timeout_->Start(DurationMs(duration), MakeTimeoutId(id_, generation_));
dcsctp: Handle starting timer from timer callback This was caught in an integration test which had stricter assertions than the FakeTimeout which is used in unit tests, so the first thing was to add the same assertions to the FakeTimeout. The issue is that when a Timer triggers, and if it's set to automatically restart (possibly with an exponential backoff), the `is_running_` field was set to true while the timer callback was called to allow the client to know that the timer is in fact running, but the timer was actually not started until the callback returned. Which made sense, as the callback can with its return value override the duration, which should affect the backoff algorithm. The problem was when a timer was manually started within the callback. As the Timer itself thought that it was already running, it first would Stop() the underlying Timeout, then Start(). But calling Stop() on a timeout that is not running is illegal, which set of assertions. So the solution is to don't lie; Don't say that a timer is running when it's not. Make sure that the timer is running when the timer callback is triggered, which makes it consistent at all times. That may result in unnecessary timeout invocations (stopping and starting), but that's not too expensive. Bug: webrtc:12614 Change-Id: I7b4447ccd88bd43d181e158f0d29b0770c8a3fd6 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/217522 Reviewed-by: Florent Castelli <orphis@webrtc.org> Commit-Queue: Victor Boivie <boivie@webrtc.org> Cr-Commit-Position: refs/heads/master@{#33926}
2021-05-05 12:36:52 +02:00
}
}
}
}
void TimerManager::HandleTimeout(TimeoutID timeout_id) {
TimerID timer_id(*timeout_id >> 32);
TimerGeneration generation(*timeout_id);
auto it = timers_.find(timer_id);
if (it != timers_.end()) {
it->second->Trigger(generation);
}
}
std::unique_ptr<Timer> TimerManager::CreateTimer(absl::string_view name,
Timer::OnExpired on_expired,
const TimerOptions& options) {
next_id_ = TimerID(*next_id_ + 1);
TimerID id = next_id_;
// This would overflow after 4 billion timers created, which in SCTP would be
// after 800 million reconnections on a single socket. Ensure this will never
// happen.
RTC_CHECK_NE(*id, std::numeric_limits<uint32_t>::max());
Allow specifying delayed task precision of dcsctp::Timer. Context: The timer precision of PostDelayedTask() is about to be lowered to include up to 17 ms leeway. In order not to break use cases that require high precision timers, PostDelayedHighPrecisionTask() will continue to have the same precision that PostDelayedTask() has today. webrtc::TaskQueueBase has an enum (kLow, kHigh) to decide which precision to use when calling PostDelayedTaskWithPrecision(). See go/postdelayedtask-precision-in-webrtc for motivation and a table of delayed task use cases in WebRTC that are "high" or "low" precision. Most timers in DCSCTP are believed to only be needing low precision (see table), but the delayed_ack_timer_ of DataTracker[1] is an example of a use case that is likely to break if the timer precision is lowered (if ACK is sent too late, retransmissions may occur). So this is considered a high precision use case. This CL makes it possible to specify the precision of dcsctp::Timer. In a follow-up CL we will update delayed_ack_timer_ to kHigh precision. [1] https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/net/dcsctp/rx/data_tracker.cc;l=340 Bug: webrtc:13604 Change-Id: I8eec5ce37044096978b5dd1985fbb00bc0d8fb7e Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/249081 Reviewed-by: Victor Boivie <boivie@webrtc.org> Reviewed-by: Tomas Gunnarsson <tommi@webrtc.org> Commit-Queue: Henrik Boström <hbos@webrtc.org> Cr-Commit-Position: refs/heads/main@{#35809}
2022-01-26 18:38:13 +01:00
std::unique_ptr<Timeout> timeout = create_timeout_(options.precision);
RTC_CHECK(timeout != nullptr);
auto timer = absl::WrapUnique(new Timer(
id, name, std::move(on_expired), [this, id]() { timers_.erase(id); },
std::move(timeout), options));
timers_[id] = timer.get();
return timer;
}
} // namespace dcsctp