hyperledger/iroha
Iroha - A simple, decentralized ledger http://iroha.tech
grpc_channel_builder.hpp
Go to the documentation of this file.
1 
6 #ifndef IROHA_GRPC_CHANNEL_BUILDER_HPP
7 #define IROHA_GRPC_CHANNEL_BUILDER_HPP
8 
9 #include <limits>
10 #include <memory>
11 
12 #include <fmt/format.h>
13 #include <grpc++/grpc++.h>
14 
15 namespace iroha {
16  namespace network {
17  namespace details {
18  constexpr unsigned int kMaxRequestMessageBytes =
19  std::numeric_limits<int>::max();
20  constexpr unsigned int kMaxResponseMessageBytes =
21  std::numeric_limits<int>::max();
22  constexpr unsigned int kClientRequestRetryAttempts = 3;
23  constexpr unsigned int kClientRequestTimeoutSeconds = 10;
24 
25  template <typename T>
26  grpc::ChannelArguments getChannelArguments() {
27  grpc::ChannelArguments args;
28  args.SetServiceConfigJSON(fmt::format(R"(
29  {{
30  "methodConfig": [ {{
31  "name": [
32  {{ "service": "{}" }}
33  ],
34  "retryPolicy": {{
35  "maxAttempts": {},
36  "initialBackoff": "1s",
37  "maxBackoff": "2s",
38  "backoffMultiplier": 1.2,
39  "retryableStatusCodes": [
40  "UNKNOWN",
41  "DEADLINE_EXCEEDED",
42  "ABORTED",
43  "INTERNAL"
44  ]
45  }},
46  "maxRequestMessageBytes": {},
47  "maxResponseMessageBytes": {},
48  "timeout": "{}s"
49  }} ]
50  }})",
51  T::service_full_name(),
52  kClientRequestRetryAttempts,
53  kMaxRequestMessageBytes,
54  kMaxResponseMessageBytes,
55  kClientRequestTimeoutSeconds));
56  return args;
57  }
58  } // namespace details
59 
69  template <typename T>
71  const grpc::string &address,
72  std::shared_ptr<grpc::ChannelCredentials> credentials) {
73  return T::NewStub(grpc::CreateCustomChannel(
74  address, credentials, details::getChannelArguments<T>()));
75  }
76 
84  template <typename T>
85  auto createClient(const grpc::string &address) {
86  return createClientWithCredentials<T>(address,
87  grpc::InsecureChannelCredentials());
88  }
89 
98  template <typename T>
99  std::unique_ptr<typename T::Stub> createSecureClient(
100  const grpc::string &address, const std::string &root_certificate) {
101  auto options = grpc::SslCredentialsOptions();
102  options.pem_root_certs = root_certificate;
103  auto credentials = grpc::SslCredentials(options);
104 
105  return createClientWithCredentials<T>(address, credentials);
106  }
107  } // namespace network
108 } // namespace iroha
109 
110 #endif // IROHA_GRPC_CHANNEL_BUILDER_HPP
constexpr unsigned int kClientRequestRetryAttempts
Definition: grpc_channel_builder.hpp:22
constexpr unsigned int kMaxResponseMessageBytes
Definition: grpc_channel_builder.hpp:20
grpc::ChannelArguments getChannelArguments()
Definition: grpc_channel_builder.hpp:26
std::unique_ptr< typename T::Stub > createSecureClient(const grpc::string &address, const std::string &root_certificate)
Definition: grpc_channel_builder.hpp:99
constexpr unsigned int kClientRequestTimeoutSeconds
Definition: grpc_channel_builder.hpp:23
Definition: block_query.hpp:15
auto createClientWithCredentials(const grpc::string &address, std::shared_ptr< grpc::ChannelCredentials > credentials)
Definition: grpc_channel_builder.hpp:70
auto createClient(const grpc::string &address)
Definition: grpc_channel_builder.hpp:85
constexpr unsigned int kMaxRequestMessageBytes
Definition: grpc_channel_builder.hpp:18