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 <grpc++/grpc++.h>
13 #include <boost/format.hpp>
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 
23  template <typename T>
24  grpc::ChannelArguments getChannelArguments() {
25  grpc::ChannelArguments args;
26  args.SetServiceConfigJSON((boost::format(R"(
27  {
28  "methodConfig": [ {
29  "name": [
30  { "service": "%1%" }
31  ],
32  "retryPolicy": {
33  "maxAttempts": 5,
34  "initialBackoff": "5s",
35  "maxBackoff": "120s",
36  "backoffMultiplier": 1.6,
37  "retryableStatusCodes": [
38  "UNKNOWN",
39  "DEADLINE_EXCEEDED",
40  "ABORTED",
41  "INTERNAL"
42  ]
43  },
44  "maxRequestMessageBytes": %2%,
45  "maxResponseMessageBytes": %3%
46  } ]
47  })") % T::service_full_name()
48  % kMaxRequestMessageBytes
49  % kMaxResponseMessageBytes)
50  .str());
51  return args;
52  }
53  } // namespace details
54 
64  template <typename T>
66  const grpc::string &address,
67  std::shared_ptr<grpc::ChannelCredentials> credentials) {
68  return T::NewStub(grpc::CreateCustomChannel(
69  address, credentials, details::getChannelArguments<T>()));
70  }
71 
79  template <typename T>
80  auto createClient(const grpc::string &address) {
81  return createClientWithCredentials<T>(address,
82  grpc::InsecureChannelCredentials());
83  }
84 
93  template <typename T>
94  std::unique_ptr<typename T::Stub> createSecureClient(
95  const grpc::string &address, const std::string &root_certificate) {
96  auto options = grpc::SslCredentialsOptions();
97  options.pem_root_certs = root_certificate;
98  auto credentials = grpc::SslCredentials(options);
99 
100  return createClientWithCredentials<T>(address, credentials);
101  }
102  } // namespace network
103 } // namespace iroha
104 
105 #endif // IROHA_GRPC_CHANNEL_BUILDER_HPP
constexpr unsigned int kMaxResponseMessageBytes
Definition: grpc_channel_builder.hpp:20
grpc::ChannelArguments getChannelArguments()
Definition: grpc_channel_builder.hpp:24
std::unique_ptr< typename T::Stub > createSecureClient(const grpc::string &address, const std::string &root_certificate)
Definition: grpc_channel_builder.hpp:94
Definition: block_query.hpp:14
auto createClientWithCredentials(const grpc::string &address, std::shared_ptr< grpc::ChannelCredentials > credentials)
Definition: grpc_channel_builder.hpp:65
auto createClient(const grpc::string &address)
Definition: grpc_channel_builder.hpp:80
constexpr unsigned int kMaxRequestMessageBytes
Definition: grpc_channel_builder.hpp:18