userver: userver/ugrpc/client/channels.hpp Source File
Loading...
Searching...
No Matches
channels.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/ugrpc/client/channels.hpp
4/// @brief Utilities for managing gRPC connections
5
6#include <grpcpp/channel.h>
7#include <grpcpp/completion_queue.h>
8#include <grpcpp/security/credentials.h>
9
10#include <userver/engine/deadline.hpp>
11#include <userver/engine/task/task_processor_fwd.hpp>
12
13#include <userver/ugrpc/client/impl/client_data.hpp>
14
15USERVER_NAMESPACE_BEGIN
16
17namespace ugrpc::client {
18
19namespace impl {
20
21[[nodiscard]] bool TryWaitForConnected(
22 grpc::Channel& channel,
23 grpc::CompletionQueue& queue,
24 engine::Deadline deadline,
25 engine::TaskProcessor& blocking_task_processor
26);
27
28[[nodiscard]] bool TryWaitForConnected(
29 impl::ChannelCache::Token& token,
30 grpc::CompletionQueue& queue,
31 engine::Deadline deadline,
32 engine::TaskProcessor& blocking_task_processor
33);
34
35} // namespace impl
36
37/// @brief Create a new gRPC channel (connection pool) for the endpoint
38///
39/// Channel creation is expensive, reuse channels if possible.
40///
41/// Each channel is fully thread-safe: it can be used across multiple threads,
42/// possibly from multiple clients and using multiple `CompletionQueue`
43/// instances at the same time.
44///
45/// @param blocking_task_processor task processor for blocking channel creation
46/// @param channel_credentials channel credentials
47/// @param endpoint string host:port
48/// @returns shared pointer to the gRPC channel
49std::shared_ptr<grpc::Channel> MakeChannel(
50 engine::TaskProcessor& blocking_task_processor,
51 std::shared_ptr<grpc::ChannelCredentials> channel_credentials,
52 const std::string& endpoint
53);
54
55/// @brief Wait until the channel state of `client` is `READY`. If the current
56/// state is already `READY`, returns `true` immediately. In case of multiple
57/// underlying channels, waits for all of them.
58/// @returns `true` if the state changed before `deadline` expired
59/// @note The wait operation does not support task cancellations
60template <typename Client>
61[[nodiscard]] bool
62TryWaitForConnected(Client& client, engine::Deadline deadline, engine::TaskProcessor& blocking_task_processor) {
63 return impl::TryWaitForConnected(
64 impl::GetClientData(client).GetChannelToken(),
65 impl::GetClientData(client).NextQueue(),
66 deadline,
67 blocking_task_processor
68 );
69}
70
71} // namespace ugrpc::client
72
73USERVER_NAMESPACE_END