There are two main interfaces for implementing a middleware:
Methods ugrpc::client::MiddlewareBase::PreStartCall and ugrpc::client::MiddlewareBase::PostFinish are called for each RPC attempt.
PreStartCall is called before the first message is sent in each attempt. PostFinish is called after each attempt completes, regardless of how it completes (success, error, cancellation, abandonment, or network error).
For unary RPCs with retries enabled, both PreStartCall and PostFinish will be called multiple times - once per retry attempt.
PreStartCall hooks are called in the direct middlewares order. PostFinish hooks are called in the reversed order.
Streaming RPCs can have multiple requests and responses, but PreStartCall and PostFinish are called once per RPC in any case.
For more information about the middlewares order:
The PostFinish hook receives a ugrpc::client::CompletionStatus parameter, which is a utils::expected<grpc::Status, SpecialCaseCompletionType>.
This means the completion can be one of:
Register the Middleware component in the component system.
The static YAML config.
PreSendMessage hooks are called in the order of middlewares. PostRecvMessage hooks are called in the reverse order of middlewares.
For more information about the middlewares order:
These hooks are called on each message.
PreSendMessage:
PostRecvMessage:
The static YAML config and component registration are identical as in the example above. So, let's not focus on this.
We use a simple short-cut ugrpc::client::SimpleMiddlewareFactoryComponent in the example above. To declare static config options of your middleware see gRPC middlewares configuration.
To fully understand what happens when middleware hooks fail, you should understand the middlewares order:
All exceptions are rethrown to the user code from client's RPC creating methods, Read / Write (for streaming), and from methods that return the RPC status.
Note that if an exception occurs, the middleware pipeline is stopped and subsequent middleware hooks are not called.
There are two ways to implement a middleware component. You can see above ugrpc::client::SimpleMiddlewareFactoryComponent. This component is needed for simple cases without static config options of a middleware.
If you want to use static config options for your middleware, use ugrpc::client::MiddlewareFactoryComponentBase.
To override static config options of a middleware per a client see grpc_middlewares_config_override.
If your middleware needs a dynamic config value, do NOT do your own FindComponent<components::DynamicConfig>() in your middleware factory component, and do NOT depend, even transitively, on a component that does so itself (e.g. do not add components::DynamicConfig, or a component that depends on it, as a dependency of your middleware factory component). Instead, store ugrpc::client::ClientInfo::config_source, passed to CreateMiddleware, as a field of your middleware object, and read the config through it (.GetSnapshot()[key]) inside the middleware hooks (PreStartCall/PreSendMessage/PostRecvMessage/PostFinish).
CreateMiddleware is called once per client, so the resulting config_source is the same one that the rest of the client uses. This is important because a client's ClientFactoryComponent may be configured with use-constant-dynamic-configs: true (a "light" gRPC client without a blocking dependency on components::DynamicConfig, see ugrpc::client::ClientFactoryComponent) — in that case config_source is constant and does not go through components::DynamicConfig at all. A middleware that bypasses ClientInfo::config_source and does its own FindComponent<components::DynamicConfig>() (directly or transitively, through some other component it depends on) would silently reintroduce a blocking dependency for such a client.
Before starting to read specifics of client middlewares ordering:
There are simple cases above: we just set Auth group for one middleware.
Here we say that all client middlewares are located in these groups.
PreCore group is called firstly, then Logging and so forth...