Stores request-specific data during request processing.
For example: you can store some data in HandleRequestThrow() method and access this data in GetResponseDataForLogging() method.
Use typed tags to store and retrieve per-request custom data.
The context can be used to pass data between middleware, authentication checkers, handlers, and logging hooks.
Example usage:
Define a shared tag in a header:
inline const utils::AnyStorageDataTag<server::request::StorageContext, std::string> kUsername;
Store the value in an authentication checker:
Read it in a handler:
return "Hello world, " + ctx.
GetData(kUsername) +
"!\n";
- Examples
- postgresql/functional_tests/basic_chaos/postgres_service.cpp, samples/config_service/main.cpp, samples/flatbuf_service/main.cpp, samples/hello_service/src/hello_handler.cpp, samples/hello_service/src/hello_handler.hpp, samples/kafka_service/src/producer_handler.cpp, samples/kafka_service/src/producer_handler.hpp, samples/mongo_service/main.cpp, samples/multipart_service/main.cpp, samples/postgres_auth/auth_bearer.cpp, samples/postgres_auth/postgres_service.cpp, samples/postgres_service/main.cpp, samples/redis_service/main.cpp, samples/scylla_service/src/main.cpp, samples/testsuite-support/src/logcapture.cpp, samples/testsuite-support/src/metrics.cpp, samples/testsuite-support/src/metrics.hpp, samples/testsuite-support/src/now.cpp, samples/testsuite-support/src/testpoint.cpp, samples/websocket_client/main.cpp, samples/websocket_service/main.cpp, samples/ydb_service/views/bson-reading/post/view.cpp, samples/ydb_service/views/bson-upserting/post/view.cpp, samples/ydb_service/views/select-rows/post/view.cpp, samples/ydb_service/views/upsert-2rows/post/view.cpp, samples/ydb_service/views/upsert-row/post/view.cpp, samples/ydb_service/views/upsert-rows/post/view.cpp, samples/ydb_topic_writer_service/src/write_handler.cpp, and samples/ydb_topic_writer_service/src/write_handler.hpp.
Definition at line 45 of file request_context.hpp.
|
|
| RequestContext (RequestContext &&) noexcept |
|
| RequestContext (const RequestContext &)=delete |
| const dynamic_config::Snapshot & | GetInitialDynamicConfig () const |
| | Returns the dynamic config snapshot captured at the start of HTTP request processing.
|
| template<typename Data> |
| Data & | SetData (const utils::AnyStorageDataTag< StorageContext, Data > &tag, Data data) |
| | Stores data under the tag if no value was previously stored.
|
| template<typename Data, typename... Args> |
| Data & | EmplaceData (const utils::AnyStorageDataTag< StorageContext, Data > &tag, Args &&... args) |
| | Emplaces data under the tag if no value was previously stored.
|
| template<typename Data> |
| Data & | GetData (const utils::AnyStorageDataTag< StorageContext, Data > &tag) |
| template<typename Data> |
| const Data & | GetData (const utils::AnyStorageDataTag< StorageContext, Data > &tag) const |
| template<typename Data> |
| Data * | GetDataOptional (const utils::AnyStorageDataTag< StorageContext, Data > &tag) noexcept |
| template<typename Data> |
| const Data * | GetDataOptional (const utils::AnyStorageDataTag< StorageContext, Data > &tag) const noexcept |
| template<typename Data> |
| void | EraseData (const utils::AnyStorageDataTag< StorageContext, Data > &tag) noexcept |
| | Erases data for the tag. Erasing an empty slot is harmless.
|
| template<typename Data> |
| Data & | SetUserData (Data data) |
| | Stores user data if it was not previously stored in this.
|
| template<typename Data, typename... Args> |
| Data & | EmplaceUserData (Args &&... args) |
| | Emplaces user data if it was not previously stored in this.
|
| template<typename Data> |
| Data & | GetUserData () |
| template<typename Data> |
| const Data & | GetUserData () const |
| template<typename Data> |
| std::remove_reference_t< Data > * | GetUserDataOptional () |
| template<typename Data> |
| const std::remove_reference_t< Data > * | GetUserDataOptional () const |
| void | EraseUserData () noexcept |
| | Erases the user data.
|
| template<typename Data> |
| Data & | SetData (std::string name, Data data) |
| | Stores the data with specified name if it was not previously stored in this.
|
| template<typename Data, typename... Args> |
| Data & | EmplaceData (std::string name, Args &&... args) |
| | Emplaces the data with specified name if it was not previously stored in this.
|
| template<typename Data> |
| Data & | GetData (std::string_view name) |
| template<typename Data> |
| const Data & | GetData (std::string_view name) const |
| template<typename Data> |
| std::remove_reference_t< Data > * | GetDataOptional (std::string_view name) |
| template<typename Data> |
| const std::remove_reference_t< Data > * | GetDataOptional (std::string_view name) const |
| void | EraseData (std::string_view name) noexcept |
| | Erase data with specified name.
|
| void | SetHandlerMetricsShard (std::string_view path, utils::statistics::LabelsSpan labels) |
| | Set the metrics shard (path + labels) for this request. When set, handler metrics will be accumulated on a new subpath "http.handler.path.*" and the provided labels would be set to each metric.
|
|
impl::InternalRequestContext & | GetInternalContext () noexcept |
Returns the dynamic config snapshot captured at the start of HTTP request processing.
The snapshot is available in the initial section of HTTP middlewares (before the Next() call), and in request logging hooks. It is reset before calling a streaming handler. For non-streaming HTTP handlers, the snapshot is available for the entire duration of request handling, including response logging hooks. For WebSocket handlers, it is available during the HTTP handshake and reset before calling Handle().
- Exceptions
-
| std::logic_error | if the snapshot has not been initialized or has already been reset. |