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 <string>
8
9#include <userver/engine/deadline.hpp>
10#include <userver/engine/task/inherited_variable.hpp>
11
12USERVER_NAMESPACE_BEGIN
13
14/// Server request related types and functions
15namespace server::request {
16
17/// @brief Signals when an operation has detected deadline expiration.
18class DeadlineSignal final {
19public:
20 DeadlineSignal() noexcept;
21 DeadlineSignal(const DeadlineSignal&) noexcept;
22 DeadlineSignal& operator=(const DeadlineSignal&) noexcept;
23
24 void SetExpired() noexcept;
25 bool IsExpired() const noexcept;
26
27private:
28 std::atomic<bool> value_{false};
29};
30
31/// @brief Per-request data that should be available inside handlers
32struct TaskInheritedData final {
33 /// The static path of the handler
35
36 /// The method of the request
38
39 /// The time when the request started being handled
40 std::chrono::steady_clock::time_point start_time{};
41
42 /// The time when there is no use handling the request anymore
43 engine::Deadline deadline;
44
45 /// Signals when an operation has detected deadline expiration
46 mutable DeadlineSignal deadline_signal{};
47};
48
49/// @see TaskInheritedData for details on the contents.
50extern engine::TaskInheritedVariable<TaskInheritedData> kTaskInheritedData;
51
52/// @brief Returns TaskInheritedData::deadline, or an unreachable
53/// engine::Deadline if none was set.
54engine::Deadline GetTaskInheritedDeadline() noexcept;
55
56/// @brief Marks that the current TaskInheritedData::deadline has expired.
58
59/// @brief Stops deadline propagation within its scope
60///
61/// By default, handler deadline is honored in requests created directly
62/// from the handler task, as well as from its child tasks. However, some
63/// requests need to be completed regardless of whether the initial request
64/// timed out, because they are needed for something other than forming the
65/// upstream response.
66///
67/// Deadline propagation is automatically blocked in tasks launched using:
68/// @see concurrent::BackgroundTaskStorage::AsyncDetach
69/// @see utils::AsyncBackground
70///
71/// @see concurrent::BackgroundTaskStorage::AsyncDetach does it by default.
72class [[nodiscard]] DeadlinePropagationBlocker final {
73public:
74 DeadlinePropagationBlocker();
75
76 DeadlinePropagationBlocker(DeadlinePropagationBlocker&&) = delete;
77 DeadlinePropagationBlocker& operator=(DeadlinePropagationBlocker&&) = delete;
78 ~DeadlinePropagationBlocker();
79
80private:
81 TaskInheritedData old_value_;
82};
83
84} // namespace server::request
85
86USERVER_NAMESPACE_END