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