mirror of
https://github.com/mollyim/webrtc.git
synced 2025-05-14 22:30:40 +01:00

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}
99 lines
4.2 KiB
C++
99 lines
4.2 KiB
C++
/*
|
|
* 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/task_queue_timeout.h"
|
|
|
|
#include "rtc_base/logging.h"
|
|
#include "rtc_base/task_utils/pending_task_safety_flag.h"
|
|
#include "rtc_base/task_utils/to_queued_task.h"
|
|
|
|
namespace dcsctp {
|
|
|
|
TaskQueueTimeoutFactory::TaskQueueTimeout::TaskQueueTimeout(
|
|
TaskQueueTimeoutFactory& parent,
|
|
webrtc::TaskQueueBase::DelayPrecision precision)
|
|
: parent_(parent),
|
|
precision_(precision),
|
|
pending_task_safety_flag_(webrtc::PendingTaskSafetyFlag::Create()) {}
|
|
|
|
TaskQueueTimeoutFactory::TaskQueueTimeout::~TaskQueueTimeout() {
|
|
RTC_DCHECK_RUN_ON(&parent_.thread_checker_);
|
|
pending_task_safety_flag_->SetNotAlive();
|
|
}
|
|
|
|
void TaskQueueTimeoutFactory::TaskQueueTimeout::Start(DurationMs duration_ms,
|
|
TimeoutID timeout_id) {
|
|
RTC_DCHECK_RUN_ON(&parent_.thread_checker_);
|
|
RTC_DCHECK(timeout_expiration_ == TimeMs::InfiniteFuture());
|
|
timeout_expiration_ = parent_.get_time_() + duration_ms;
|
|
timeout_id_ = timeout_id;
|
|
|
|
if (timeout_expiration_ >= posted_task_expiration_) {
|
|
// There is already a running task, and it's scheduled to expire sooner than
|
|
// the new expiration time. Don't do anything; The `timeout_expiration_` has
|
|
// already been updated and if the delayed task _does_ expire and the timer
|
|
// hasn't been stopped, that will be noticed in the timeout handler, and the
|
|
// task will be re-scheduled. Most timers are stopped before they expire.
|
|
return;
|
|
}
|
|
|
|
if (posted_task_expiration_ != TimeMs::InfiniteFuture()) {
|
|
RTC_DLOG(LS_VERBOSE) << "New timeout duration is less than scheduled - "
|
|
"ghosting old delayed task.";
|
|
// There is already a scheduled delayed task, but its expiration time is
|
|
// further away than the new expiration, so it can't be used. It will be
|
|
// "killed" by replacing the safety flag. This is not expected to happen
|
|
// especially often; Mainly when a timer did exponential backoff and
|
|
// later recovered.
|
|
pending_task_safety_flag_->SetNotAlive();
|
|
pending_task_safety_flag_ = webrtc::PendingTaskSafetyFlag::Create();
|
|
}
|
|
|
|
posted_task_expiration_ = timeout_expiration_;
|
|
parent_.task_queue_.PostDelayedTaskWithPrecision(
|
|
precision_,
|
|
webrtc::ToQueuedTask(
|
|
pending_task_safety_flag_,
|
|
[timeout_id, this]() {
|
|
RTC_DLOG(LS_VERBOSE) << "Timout expired: " << timeout_id.value();
|
|
RTC_DCHECK_RUN_ON(&parent_.thread_checker_);
|
|
RTC_DCHECK(posted_task_expiration_ != TimeMs::InfiniteFuture());
|
|
posted_task_expiration_ = TimeMs::InfiniteFuture();
|
|
|
|
if (timeout_expiration_ == TimeMs::InfiniteFuture()) {
|
|
// The timeout was stopped before it expired. Very common.
|
|
} else {
|
|
// Note that the timeout might have been restarted, which updated
|
|
// `timeout_expiration_` but left the scheduled task running. So
|
|
// if it's not quite time to trigger the timeout yet, schedule a
|
|
// new delayed task with what's remaining and retry at that point
|
|
// in time.
|
|
DurationMs remaining = timeout_expiration_ - parent_.get_time_();
|
|
timeout_expiration_ = TimeMs::InfiniteFuture();
|
|
if (*remaining > 0) {
|
|
Start(remaining, timeout_id_);
|
|
} else {
|
|
// It has actually triggered.
|
|
RTC_DLOG(LS_VERBOSE)
|
|
<< "Timout triggered: " << timeout_id.value();
|
|
parent_.on_expired_(timeout_id_);
|
|
}
|
|
}
|
|
}),
|
|
duration_ms.value());
|
|
}
|
|
|
|
void TaskQueueTimeoutFactory::TaskQueueTimeout::Stop() {
|
|
// As the TaskQueue doesn't support deleting a posted task, just mark the
|
|
// timeout as not running.
|
|
RTC_DCHECK_RUN_ON(&parent_.thread_checker_);
|
|
timeout_expiration_ = TimeMs::InfiniteFuture();
|
|
}
|
|
|
|
} // namespace dcsctp
|