userver: userver/server/request/task_inherited_data.hpp Source File
Loading...
Searching...
No Matches
task_inherited_data.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/server/request/task_inherited_data.hpp
4/// @brief @copybrief server::request::TaskInheritedData
5
6#include <atomic>
7#include <chrono>
8#include <optional>
9#include <string_view>
10
11#include <userver/engine/deadline.hpp>
12#include <userver/engine/task/inherited_variable.hpp>
13#include <userver/server/request/impl/absolute_deadline.hpp>
14
15USERVER_NAMESPACE_BEGIN
16
17/// Server request related types and functions
18namespace server::request {
19
20/// @brief Signals when an operation has detected deadline expiration.
21class DeadlineSignal final {
22public:
23 DeadlineSignal() noexcept;
24 DeadlineSignal(const DeadlineSignal&) noexcept;
25 DeadlineSignal& operator=(const DeadlineSignal&) noexcept;
26
27 void SetExpired() noexcept;
28 bool IsExpired() const noexcept;
29
30private:
31 std::atomic<bool> value_{false};
32};
33
34/// @brief Per-request data that should be available inside handlers
35struct TaskInheritedData final {
36 /// The static path of the handler
37 std::string_view path;
38
39 /// The method of the request
40 std::string_view method;
41
42 /// The time when the request started being handled
43 std::chrono::steady_clock::time_point start_time{};
44
45 /// The time when there is no use handling the request anymore
46 engine::Deadline deadline;
47
48 /// Signals when an operation has detected deadline expiration
49 mutable DeadlineSignal deadline_signal{};
50
51 /// Original absolute deadline parsed from `X-Request-Deadline` header.
52 /// Intended solely for propagation to downstream services; use @ref deadline for the task deadline.
53 std::optional<TaskInheritedOriginalDeadline> original_deadline = std::nullopt;
54};
55
56/// @see TaskInheritedData for details on the contents.
57extern engine::TaskInheritedVariable<TaskInheritedData> kTaskInheritedData;
58
59/// @brief Returns TaskInheritedData::deadline, or an unreachable
60/// engine::Deadline if none was set.
61/// @note Use this method to set the deadline for tasks.
62engine::Deadline GetTaskInheritedDeadline() noexcept;
63
64/// @brief Returns TaskInheritedData::original_deadline, or std::nullopt if the header was absent or invalid.
65/// @warning Do not use this method to set the deadline for tasks. It is intended for deadline propagation only.
66/// Use @ref GetTaskInheritedDeadline() to set the deadline for tasks.
67std::optional<TaskInheritedOriginalDeadline> GetTaskInheritedOriginalDeadline() noexcept;
68
69/// @brief Marks that the current TaskInheritedData::deadline has expired.
71
72/// @brief Stops deadline propagation within its scope
73///
74/// By default, handler deadline is honored in requests created directly
75/// from the handler task, as well as from its child tasks. However, some
76/// requests need to be completed regardless of whether the initial request
77/// timed out, because they are needed for something other than forming the
78/// upstream response.
79///
80/// Deadline propagation is automatically blocked in tasks launched using:
81/// @see concurrent::BackgroundTaskStorage::AsyncDetach
82/// @see utils::AsyncBackground
83///
84/// @see concurrent::BackgroundTaskStorage::AsyncDetach does it by default.
85class [[nodiscard]] DeadlinePropagationBlocker final {
86public:
87 DeadlinePropagationBlocker();
88
89 DeadlinePropagationBlocker(DeadlinePropagationBlocker&&) = delete;
90 DeadlinePropagationBlocker& operator=(DeadlinePropagationBlocker&&) = delete;
91 ~DeadlinePropagationBlocker();
92
93private:
94 TaskInheritedData old_value_;
95};
96
97} // namespace server::request
98
99USERVER_NAMESPACE_END