userver: userver/ugrpc/client/channels.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
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, grpc::CompletionQueue& queue,
23 engine::Deadline deadline, engine::TaskProcessor& blocking_task_processor);
24
25[[nodiscard]] bool TryWaitForConnected(
26 impl::ChannelCache::Token& token, grpc::CompletionQueue& queue,
27 engine::Deadline deadline, engine::TaskProcessor& blocking_task_processor);
28
29} // namespace impl
30
31/// @brief Create a new gRPC channel (connection pool) for the endpoint
32///
33/// Channel creation is expensive, reuse channels if possible.
34///
35/// Each channel is fully thread-safe: it can be used across multiple threads,
36/// possibly from multiple clients and using multiple `CompletionQueue`
37/// instances at the same time.
38///
39/// @param blocking_task_processor task processor for blocking channel creation
40/// @param channel_credentials channel credentials
41/// @param endpoint string host:port
42/// @returns shared pointer to the gRPC channel
43std::shared_ptr<grpc::Channel> MakeChannel(
44 engine::TaskProcessor& blocking_task_processor,
45 std::shared_ptr<grpc::ChannelCredentials> channel_credentials,
46 const std::string& endpoint);
47
48/// @brief Wait until the channel state of `client` is `READY`. If the current
49/// state is already `READY`, returns `true` immediately. In case of multiple
50/// underlying channels, waits for all of them.
51/// @returns `true` if the state changed before `deadline` expired
52/// @note The wait operation does not support task cancellations
53template <typename Client>
54[[nodiscard]] bool TryWaitForConnected(
55 Client& client, engine::Deadline deadline,
56 engine::TaskProcessor& blocking_task_processor) {
57 return impl::TryWaitForConnected(
58 impl::GetClientData(client).GetChannelToken(),
59 impl::GetClientData(client).GetQueue(), deadline,
60 blocking_task_processor);
61}
62
63} // namespace ugrpc::client
64
65USERVER_NAMESPACE_END