grpc_request_base.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #pragma once
  2. #include <google/protobuf/message.h>
  3. #include <library/cpp/threading/future/future.h>
  4. #include <grpc++/server_context.h>
  5. namespace grpc {
  6. class ByteBuffer;
  7. }
  8. namespace NGrpc {
  9. extern const char* GRPC_USER_AGENT_HEADER;
  10. struct TAuthState {
  11. enum EAuthState {
  12. AS_NOT_PERFORMED,
  13. AS_OK,
  14. AS_FAIL,
  15. AS_UNAVAILABLE
  16. };
  17. TAuthState(bool needAuth)
  18. : NeedAuth(needAuth)
  19. , State(AS_NOT_PERFORMED)
  20. {}
  21. bool NeedAuth;
  22. EAuthState State;
  23. };
  24. //! An interface that may be used to limit concurrency of requests
  25. class IGRpcRequestLimiter: public TThrRefBase {
  26. public:
  27. virtual bool IncRequest() = 0;
  28. virtual void DecRequest() = 0;
  29. };
  30. using IGRpcRequestLimiterPtr = TIntrusivePtr<IGRpcRequestLimiter>;
  31. //! State of current request
  32. class IRequestContextBase: public TThrRefBase {
  33. public:
  34. enum class EFinishStatus {
  35. OK,
  36. ERROR,
  37. CANCEL
  38. };
  39. using TAsyncFinishResult = NThreading::TFuture<EFinishStatus>;
  40. using TOnNextReply = std::function<void (size_t left)>;
  41. //! Get pointer to the request's message.
  42. virtual const NProtoBuf::Message* GetRequest() const = 0;
  43. //! Get mutable pointer to the request's message.
  44. virtual NProtoBuf::Message* GetRequestMut() = 0;
  45. //! Get current auth state
  46. virtual TAuthState& GetAuthState() = 0;
  47. //! Send common response (The request shoult be created for protobuf response type)
  48. //! Implementation can swap protobuf message
  49. virtual void Reply(NProtoBuf::Message* resp, ui32 status = 0) = 0;
  50. //! Send serialised response (The request shoult be created for bytes response type)
  51. //! Implementation can swap ByteBuffer
  52. virtual void Reply(grpc::ByteBuffer* resp, ui32 status = 0) = 0;
  53. //! Send grpc UNAUTHENTICATED status
  54. virtual void ReplyUnauthenticated(const TString& in) = 0;
  55. //! Send grpc error
  56. virtual void ReplyError(grpc::StatusCode code, const TString& msg, const TString& details = "") = 0;
  57. //! Returns deadline (server epoch related) if peer set it on its side, or Instanse::Max() otherwise
  58. virtual TInstant Deadline() const = 0;
  59. //! Returns available peer metadata keys
  60. virtual TSet<TStringBuf> GetPeerMetaKeys() const = 0;
  61. //! Returns peer optional metavalue
  62. virtual TVector<TStringBuf> GetPeerMetaValues(TStringBuf key) const = 0;
  63. virtual TVector<TStringBuf> FindClientCert() const = 0;
  64. //! Returns request compression level
  65. virtual grpc_compression_level GetCompressionLevel() const = 0;
  66. //! Returns protobuf arena allocator associated with current request
  67. //! Lifetime of the arena is lifetime of the context
  68. virtual google::protobuf::Arena* GetArena() = 0;
  69. //! Add trailing metadata in to grpc context
  70. //! The metadata will be send at the time of rpc finish
  71. virtual void AddTrailingMetadata(const TString& key, const TString& value) = 0;
  72. //! Use validated database name for counters
  73. virtual void UseDatabase(const TString& database) = 0;
  74. // Streaming part
  75. //! Set callback. The callback will be called when response deliverid to the client
  76. //! after that we can call Reply again in streaming mode. Yes, GRpc says there is only one
  77. //! reply in flight
  78. virtual void SetNextReplyCallback(TOnNextReply&& cb) = 0;
  79. //! Finish streaming reply
  80. virtual void FinishStreamingOk() = 0;
  81. //! Returns future to get cancel of finish notification
  82. virtual TAsyncFinishResult GetFinishFuture() = 0;
  83. //! Returns peer address
  84. virtual TString GetPeer() const = 0;
  85. //! Returns true if server is using ssl
  86. virtual bool SslServer() const = 0;
  87. //! Returns true if client was not interested in result (but we still must send response to make grpc happy)
  88. virtual bool IsClientLost() const = 0;
  89. };
  90. } // namespace NGrpc