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