userver: userver/ugrpc/impl/async_method_invocation.hpp Source File
⚠️ This is the documentation for an old userver version. Click here to switch to the latest version.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
async_method_invocation.hpp
1#pragma once
2
3#include <userver/engine/deadline.hpp>
4#include <userver/engine/single_use_event.hpp>
5
6USERVER_NAMESPACE_BEGIN
7
8namespace ugrpc::impl {
9
10class EventBase {
11 public:
12 /// @brief For use from the blocking call queue
13 /// @param `bool ok` returned by `grpc::CompletionQueue::Next`
14 virtual void Notify(bool ok) noexcept = 0;
15
16 protected:
17 // One should not call destructor by pointer to interface.
18 ~EventBase();
19};
20
21class AsyncMethodInvocation : public EventBase {
22 public:
23 virtual ~AsyncMethodInvocation();
24
25 /// @brief For use from coroutines
26 /// @return This object's `void* tag` for `grpc::CompletionQueue::Next`
27 void* GetTag() noexcept;
28
29 /// @see EventBase::Notify
30 void Notify(bool ok) noexcept override;
31
32 bool IsBusy() const noexcept;
33
34 enum class WaitStatus {
35 kOk,
36 kError,
37 kCancelled,
38 // This constant means that engine::Deadline specified for wait operation
39 // has expired. Note, that it is not related to the gRPC deadline
40 kDeadline,
41 };
42
43 /// @brief For use from coroutines
44 /// @return `bool ok` returned by `grpc::CompletionQueue::Next`
45 [[nodiscard]] WaitStatus Wait() noexcept;
46
47 /// @brief For use from coroutines
48 /// @return `bool ok` returned by `grpc::CompletionQueue::Next` or
49 /// information regarding readiness
50 [[nodiscard]] WaitStatus WaitUntil(engine::Deadline deadline) noexcept;
51
52 /// @brief Checks if the asynchronous call has completed
53 /// @return true if event returned from `grpc::CompletionQueue::Next`
54 [[nodiscard]] bool IsReady() const noexcept;
55
56 /// @cond
57 // For internal use only.
58 engine::impl::ContextAccessor* TryGetContextAccessor() noexcept;
59 /// @endcond
60 protected:
61 void WaitWhileBusy();
62
63 private:
64 bool ok_{false};
65 bool busy_{false};
66 engine::SingleUseEvent event_;
67};
68
69} // namespace ugrpc::impl
70
71USERVER_NAMESPACE_END