grpc_request_base.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 current auth state
  44. virtual TAuthState& GetAuthState() = 0;
  45. //! Send common response (The request shoult be created for protobuf response type)
  46. //! Implementation can swap protobuf message
  47. virtual void Reply(NProtoBuf::Message* resp, ui32 status = 0) = 0;
  48. //! Send serialised response (The request shoult be created for bytes response type)
  49. //! Implementation can swap ByteBuffer
  50. virtual void Reply(grpc::ByteBuffer* resp, ui32 status = 0) = 0;
  51. //! Send grpc UNAUTHENTICATED status
  52. virtual void ReplyUnauthenticated(const TString& in) = 0;
  53. //! Send grpc error
  54. virtual void ReplyError(grpc::StatusCode code, const TString& msg) = 0;
  55. //! Returns deadline (server epoch related) if peer set it on its side, or Instanse::Max() otherwise
  56. virtual TInstant Deadline() const = 0;
  57. //! Returns available peer metadata keys
  58. virtual TSet<TStringBuf> GetPeerMetaKeys() const = 0;
  59. //! Returns peer optional metavalue
  60. virtual TVector<TStringBuf> GetPeerMetaValues(TStringBuf key) const = 0;
  61. //! Returns request compression level
  62. virtual grpc_compression_level GetCompressionLevel() const = 0;
  63. //! Returns protobuf arena allocator associated with current request
  64. //! Lifetime of the arena is lifetime of the context
  65. virtual google::protobuf::Arena* GetArena() = 0;
  66. //! Add trailing metadata in to grpc context
  67. //! The metadata will be send at the time of rpc finish
  68. virtual void AddTrailingMetadata(const TString& key, const TString& value) = 0;
  69. //! Use validated database name for counters
  70. virtual void UseDatabase(const TString& database) = 0;
  71. // Streaming part
  72. //! Set callback. The callback will be called when response deliverid to the client
  73. //! after that we can call Reply again in streaming mode. Yes, GRpc says there is only one
  74. //! reply in flight
  75. virtual void SetNextReplyCallback(TOnNextReply&& cb) = 0;
  76. //! Finish streaming reply
  77. virtual void FinishStreamingOk() = 0;
  78. //! Returns future to get cancel of finish notification
  79. virtual TAsyncFinishResult GetFinishFuture() = 0;
  80. //! Returns peer address
  81. virtual TString GetPeer() const = 0;
  82. //! Returns true if server is using ssl
  83. virtual bool SslServer() const = 0;
  84. };
  85. } // namespace NGrpc