grpc_common.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #pragma once
  2. #include <grpc++/grpc++.h>
  3. #include <grpc++/resource_quota.h>
  4. #include <util/datetime/base.h>
  5. #include <unordered_map>
  6. #include <util/generic/string.h>
  7. constexpr ui64 DEFAULT_GRPC_MESSAGE_SIZE_LIMIT = 64000000;
  8. namespace NGrpc {
  9. struct TGRpcClientConfig {
  10. TString Locator; // format host:port
  11. TDuration Timeout = TDuration::Max(); // request timeout
  12. ui64 MaxMessageSize = DEFAULT_GRPC_MESSAGE_SIZE_LIMIT; // Max request and response size
  13. ui64 MaxInboundMessageSize = 0; // overrides MaxMessageSize for incoming requests
  14. ui64 MaxOutboundMessageSize = 0; // overrides MaxMessageSize for outgoing requests
  15. ui32 MaxInFlight = 0;
  16. bool EnableSsl = false;
  17. grpc::SslCredentialsOptions SslCredentials;
  18. grpc_compression_algorithm CompressionAlgoritm = GRPC_COMPRESS_NONE;
  19. ui64 MemQuota = 0;
  20. std::unordered_map<TString, TString> StringChannelParams;
  21. std::unordered_map<TString, int> IntChannelParams;
  22. TString LoadBalancingPolicy = { };
  23. TString SslTargetNameOverride = { };
  24. TGRpcClientConfig() = default;
  25. TGRpcClientConfig(const TGRpcClientConfig&) = default;
  26. TGRpcClientConfig(TGRpcClientConfig&&) = default;
  27. TGRpcClientConfig& operator=(const TGRpcClientConfig&) = default;
  28. TGRpcClientConfig& operator=(TGRpcClientConfig&&) = default;
  29. TGRpcClientConfig(const TString& locator, TDuration timeout = TDuration::Max(),
  30. ui64 maxMessageSize = DEFAULT_GRPC_MESSAGE_SIZE_LIMIT, ui32 maxInFlight = 0, const TString& caCert = "", const TString& clientCert = "",
  31. const TString& clientPrivateKey = "", grpc_compression_algorithm compressionAlgorithm = GRPC_COMPRESS_NONE, bool enableSsl = false)
  32. : Locator(locator)
  33. , Timeout(timeout)
  34. , MaxMessageSize(maxMessageSize)
  35. , MaxInFlight(maxInFlight)
  36. , EnableSsl(enableSsl)
  37. , SslCredentials{.pem_root_certs = caCert, .pem_private_key = clientPrivateKey, .pem_cert_chain = clientCert}
  38. , CompressionAlgoritm(compressionAlgorithm)
  39. {}
  40. };
  41. inline std::shared_ptr<grpc::ChannelInterface> CreateChannelInterface(const TGRpcClientConfig& config, grpc_socket_mutator* mutator = nullptr){
  42. grpc::ChannelArguments args;
  43. args.SetMaxReceiveMessageSize(config.MaxInboundMessageSize ? config.MaxInboundMessageSize : config.MaxMessageSize);
  44. args.SetMaxSendMessageSize(config.MaxOutboundMessageSize ? config.MaxOutboundMessageSize : config.MaxMessageSize);
  45. args.SetCompressionAlgorithm(config.CompressionAlgoritm);
  46. for (const auto& kvp: config.StringChannelParams) {
  47. args.SetString(kvp.first, kvp.second);
  48. }
  49. for (const auto& kvp: config.IntChannelParams) {
  50. args.SetInt(kvp.first, kvp.second);
  51. }
  52. if (config.MemQuota) {
  53. grpc::ResourceQuota quota;
  54. quota.Resize(config.MemQuota);
  55. args.SetResourceQuota(quota);
  56. }
  57. if (mutator) {
  58. args.SetSocketMutator(mutator);
  59. }
  60. if (!config.LoadBalancingPolicy.empty()) {
  61. args.SetLoadBalancingPolicyName(config.LoadBalancingPolicy);
  62. }
  63. if (!config.SslTargetNameOverride.empty()) {
  64. args.SetSslTargetNameOverride(config.SslTargetNameOverride);
  65. }
  66. if (config.EnableSsl || config.SslCredentials.pem_root_certs) {
  67. return grpc::CreateCustomChannel(config.Locator, grpc::SslCredentials(config.SslCredentials), args);
  68. } else {
  69. return grpc::CreateCustomChannel(config.Locator, grpc::InsecureChannelCredentials(), args);
  70. }
  71. }
  72. } // namespace NGRpc