-
Notifications
You must be signed in to change notification settings - Fork 279
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
45c630765c628f16cd600ab26711d67e118c8767
- Loading branch information
Showing
43 changed files
with
970 additions
and
251 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#pragma once | ||
|
||
/// @file userver/ugrpc/byte_buffer_utils.hpp | ||
/// @brief Helper functions for working with `grpc::ByteBuffer` | ||
|
||
#include <cstddef> | ||
|
||
#include <google/protobuf/message.h> | ||
#include <grpcpp/support/byte_buffer.h> | ||
|
||
USERVER_NAMESPACE_BEGIN | ||
|
||
namespace ugrpc { | ||
|
||
/// @see @ref SerializeToByteBuffer | ||
inline constexpr std::size_t kDefaultSerializeBlockSize = 4096; | ||
|
||
/// @brief Serialize a Protobuf message to the wire format. | ||
/// @param message the message to serialize | ||
/// @param block_size can be used for performance tuning, too small chunk size | ||
/// results in extra allocations, too large chunk size results in wasting memory | ||
/// @throws std::runtime_error on serialization errors (supposedly only happens | ||
/// on extremely rare allocation failures or proto reflection malfunctioning). | ||
grpc::ByteBuffer SerializeToByteBuffer( | ||
const ::google::protobuf::Message& message, | ||
std::size_t block_size = kDefaultSerializeBlockSize); | ||
|
||
/// @brief Parse a Protobuf message from the wire format. | ||
/// @param buffer the buffer that might be tempered with during deserialization | ||
/// @param message will contain the parsing result on success | ||
/// @returns `true` on success, `false` if @a buffer does not contain a valid | ||
/// message, according to the derived type of @a message | ||
bool ParseFromByteBuffer(grpc::ByteBuffer&& buffer, | ||
::google::protobuf::Message& message); | ||
|
||
} // namespace ugrpc | ||
|
||
USERVER_NAMESPACE_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#pragma once | ||
|
||
/// @file userver/ugrpc/client/generic.hpp | ||
/// @brief @copybrief ugrpc::client::GenericClient | ||
|
||
#include <optional> | ||
#include <string_view> | ||
|
||
#include <grpcpp/client_context.h> | ||
#include <grpcpp/support/byte_buffer.h> | ||
|
||
#include <userver/ugrpc/client/impl/client_data.hpp> | ||
#include <userver/ugrpc/client/qos.hpp> | ||
#include <userver/ugrpc/client/rpc.hpp> | ||
|
||
USERVER_NAMESPACE_BEGIN | ||
|
||
namespace ugrpc::client { | ||
|
||
struct GenericOptions { | ||
/// Client QOS for this call. Note that there is no QOS dynamic config by | ||
/// default, so unless a timeout is specified here, only the deadline | ||
/// propagation mechanism will affect the gRPC deadline. | ||
Qos qos{}; | ||
|
||
/// If non-`nullopt`, metrics are accounted for specified fake call name. | ||
/// If `nullopt` (by default), writes a set of metrics per real call name. | ||
/// If the microservice serves as a proxy and has untrusted clients, it is | ||
/// a good idea to have this option set to non-`nullopt` to avoid | ||
/// the situations where an upstream client can spam various RPCs with | ||
/// non-existent names, which leads to this microservice spamming RPCs | ||
/// with non-existent names, which leads to creating storage for infinite | ||
/// metrics and causes OOM. | ||
std::optional<std::string_view> metrics_call_name{}; | ||
}; | ||
|
||
/// @ingroup userver_clients | ||
/// | ||
/// @brief Allows to talk to gRPC services (generic and normal) using dynamic | ||
/// method names. | ||
/// | ||
/// Created using @ref ClientFactory::MakeClient. | ||
/// | ||
/// `call_name` must be in the format `full.path.to.TheService/MethodName`. | ||
/// Note that unlike in base grpc++, there must be no initial `/` character. | ||
/// | ||
/// The API is mainly intended for proxies, where the request-response body is | ||
/// passed unchanged, with settings taken solely from the RPC metadata. | ||
/// In cases where the code needs to operate on the actual messages, | ||
/// serialization of requests and responses is left as an excercise to the user. | ||
/// | ||
/// Middlewares are customizable and are applied as usual, except that no | ||
/// message hooks are called, meaning that there won't be any logs of messages | ||
/// from the default middleware. | ||
/// | ||
/// Metrics are written per-method by default, which causes OOM in some corner | ||
/// cases, for details see @ref GenericOptions::metrics_call_name. | ||
/// | ||
/// ## Example GenericClient usage with known message types | ||
/// | ||
/// @snippet grpc/tests/src/generic_client_test.cpp sample | ||
class GenericClient final { | ||
public: | ||
GenericClient(GenericClient&&) noexcept = default; | ||
GenericClient& operator=(GenericClient&&) noexcept = delete; | ||
|
||
/// Initiate a `single request -> single response` RPC with the given name. | ||
client::UnaryCall<grpc::ByteBuffer> UnaryCall( | ||
std::string_view call_name, const grpc::ByteBuffer& request, | ||
std::unique_ptr<grpc::ClientContext> context = | ||
std::make_unique<grpc::ClientContext>(), | ||
const GenericOptions& options = {}) const; | ||
|
||
/// @cond | ||
// For internal use only. | ||
explicit GenericClient(impl::ClientParams&&); | ||
/// @endcond | ||
|
||
private: | ||
template <typename Client> | ||
friend impl::ClientData& impl::GetClientData(Client& client); | ||
|
||
impl::ClientData impl_; | ||
}; | ||
|
||
} // namespace ugrpc::client | ||
|
||
USERVER_NAMESPACE_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.