userver: userver/ugrpc/client/graceful_stream_finish.hpp Source File
Loading...
Searching...
No Matches
graceful_stream_finish.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file
4/// @brief Utilities to gracefully finish streams
5
6#include <cstddef>
7#include <exception>
8#include <optional>
9
10#include <userver/logging/log.hpp>
11#include <userver/ugrpc/client/impl/graceful_stream_finish.hpp>
12#include <userver/ugrpc/client/stream.hpp>
13
14USERVER_NAMESPACE_BEGIN
15
16namespace ugrpc::client {
17
18/// @brief Read all remaining messages from the stream and call `Finish`.
19///
20/// @warning The method will hang indefenitely if the stream
21/// is never interrupted or closed by the server
22///
23/// @return the number of messages received from the stream;
24/// nullopt if the operation failed (i.e. due to task
25/// cancellation, the stream being already closed for reads,
26/// or the server returning an error status)
27template <typename Response>
28std::optional<std::size_t> ReadRemainingAndFinish(Reader<Response>& stream) noexcept {
29 try {
30 return impl::ReadRemainingAndFinish<Reader<Response>, Response>(stream);
31 } catch (const std::exception& e) {
32 LOG_WARNING() << "Failed to read remaining messages and finish - " << e;
33 return std::nullopt;
34 }
35}
36
37/// @brief Announce end-of-input to the server, read all
38/// remaining messages from the stream and call `Finish`.
39///
40/// @warning The method will hang indefenitely if the stream
41/// is never interrupted or closed by the server
42///
43/// @return the number of messages received from the stream;
44/// nullopt if the operation failed (i.e. due to task
45/// cancellation, the stream being already closed for
46/// either reads or writes, or the server returning an error status)
47template <typename Request, typename Response>
48std::optional<std::size_t> ReadRemainingAndFinish(ReaderWriter<Request, Response>& stream) noexcept {
49 try {
50 const bool writes_done_success = stream.WritesDone();
51 const std::optional<std::size_t>
52 messages_remaining = impl::ReadRemainingAndFinish<ReaderWriter<Request, Response>, Response>(stream);
53 return writes_done_success ? messages_remaining : std::nullopt;
54 } catch (const std::exception& e) {
55 LOG_WARNING() << "Failed to read remaining messages and finish - " << e;
56 return std::nullopt;
57 }
58}
59
60} // namespace ugrpc::client
61
62USERVER_NAMESPACE_END