Batch Request Plugin
Sometimes, clients (both APP and web) expect to combine two requests into one. For example, there are two interfaces:
user information interface and article information interface. The client would like to receive both contents in a single
request to speed up the page rendering process. The conventional approach is to create a new interface, but this simple
encapsulation increases development and maintenance costs, as there may be additional requirements to merge user
information with other information.
Here, a batch request capability is provided through a gateway plugin: multiple interfaces can be requested in a batch,
and the responses can be combined in a specified way before being returned to the client.
Please note the following limitations:
- There should be no dependencies between the interfaces. For example, using the response parameters of interface A as
the request parameters of interface B would result in sequential requests.
- The request Content Type for multiple interfaces should be consistent. For example, if they are all URL encoded, as
all request parameters will be passed to multiple interfaces.
- The response type must be application/json.
Recommendations for usage:
- Suitable for non-core scenario interfaces with low request volume:
- We aim to strike a balance between development convenience and performance. Batch request processing involves
parsing and assembling the response body, which may affect forwarding efficiency. It is not recommended to use
this plugin for core scenarios with strict response time requirements.
Technical Solution
Similar to the logreplay plugin, batch request is an edge feature that is implemented without affecting the core
forwarding logic.
The batch request functionality is implemented using native forwarding, which is a low-cost approach.

Plugin Usage
Import the plugin in the main.go file of the gateway project
import (
_ "trpc.group/trpc-go/trpc-gateway/plugin/batchrequest"
)
- tRPC framework configuration file, enable the batch_request interceptor.
Note: Make sure to register it in server.service.filter, not in server.filter.
global:
server:
filter:
service:
- name: trpc.inews.trpc.gateway
filter:
- batch_request
plugins:
log:
gateway:
scan_shield:
Configure the plugin in the gateway routing configuration file, router.yaml
router:
- method: /v1/user/info
id: "xxxxxx"
target_service:
- service: trpc.user.service
plugins:
- name: batch_request
props:
code_path: code
msg_path: msg
success_code: 0
request_list:
- method: /v1/creation/user_ext_info
code_path: code
msg_path: msg
source_date_path: data
target_data_path: data.users
success_code: 0
ignore_err: false
- code_path: code
ignore_err: false
method: /v1/creation/module_access
msg_path: msg
source_date_path: data
success_code: 0
target_data_path: data.modules
client:
- name: trpc.user.service
plugins:
- name: batch_request
props:
plugins:
- name: batch_request
Configure the plugin using the console
