webrtc/net/dcsctp/timer/task_queue_timeout.h
Henrik Boström b951dc6f4c 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:40:24 +00:00

92 lines
4 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.
*/
#ifndef NET_DCSCTP_TIMER_TASK_QUEUE_TIMEOUT_H_
#define NET_DCSCTP_TIMER_TASK_QUEUE_TIMEOUT_H_
#include <memory>
#include <utility>
#include "api/task_queue/task_queue_base.h"
#include "net/dcsctp/public/timeout.h"
#include "rtc_base/task_utils/pending_task_safety_flag.h"
namespace dcsctp {
// The TaskQueueTimeoutFactory creates `Timeout` instances, which schedules
// itself to be triggered on the provided `task_queue`, which may be a thread,
// an actual TaskQueue or something else which supports posting a delayed task.
//
// Note that each `DcSctpSocket` must have its own `TaskQueueTimeoutFactory`,
// as the `TimeoutID` are not unique among sockets.
//
// This class must outlive any created Timeout that it has created. Note that
// the `DcSctpSocket` will ensure that all Timeouts are deleted when the socket
// is destructed, so this means that this class must outlive the `DcSctpSocket`.
//
// This class, and the timeouts created it, are not thread safe.
class TaskQueueTimeoutFactory {
public:
// The `get_time` function must return the current time, relative to any
// epoch. Whenever a timeout expires, the `on_expired` callback will be
// triggered, and then the client should provided `timeout_id` to
// `DcSctpSocketInterface::HandleTimeout`.
TaskQueueTimeoutFactory(webrtc::TaskQueueBase& task_queue,
std::function<TimeMs()> get_time,
std::function<void(TimeoutID timeout_id)> on_expired)
: task_queue_(task_queue),
get_time_(std::move(get_time)),
on_expired_(std::move(on_expired)) {}
// Creates an implementation of `Timeout`.
std::unique_ptr<Timeout> CreateTimeout(
webrtc::TaskQueueBase::DelayPrecision precision =
webrtc::TaskQueueBase::DelayPrecision::kLow) {
return std::make_unique<TaskQueueTimeout>(*this, precision);
}
private:
class TaskQueueTimeout : public Timeout {
public:
TaskQueueTimeout(TaskQueueTimeoutFactory& parent,
webrtc::TaskQueueBase::DelayPrecision precision);
~TaskQueueTimeout();
void Start(DurationMs duration_ms, TimeoutID timeout_id) override;
void Stop() override;
private:
TaskQueueTimeoutFactory& parent_;
const webrtc::TaskQueueBase::DelayPrecision precision_;
// A safety flag to ensure that posted tasks to the task queue don't
// reference these object when they go out of scope. Note that this safety
// flag will be re-created if the scheduled-but-not-yet-expired task is not
// to be run. This happens when there is a posted delayed task with an
// expiration time _further away_ than what is now the expected expiration
// time. In this scenario, a new delayed task has to be posted with a
// shorter duration and the old task has to be forgotten.
rtc::scoped_refptr<webrtc::PendingTaskSafetyFlag> pending_task_safety_flag_;
// The time when the posted delayed task is set to expire. Will be set to
// the infinite future if there is no such task running.
TimeMs posted_task_expiration_ = TimeMs::InfiniteFuture();
// The time when the timeout expires. It will be set to the infinite future
// if the timeout is not running/not started.
TimeMs timeout_expiration_ = TimeMs::InfiniteFuture();
// The current timeout ID that will be reported when expired.
TimeoutID timeout_id_ = TimeoutID(0);
};
RTC_NO_UNIQUE_ADDRESS webrtc::SequenceChecker thread_checker_;
webrtc::TaskQueueBase& task_queue_;
const std::function<TimeMs()> get_time_;
const std::function<void(TimeoutID)> on_expired_;
};
} // namespace dcsctp
#endif // NET_DCSCTP_TIMER_TASK_QUEUE_TIMEOUT_H_