batchrequest

package module
v0.0.0-...-5c29987 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 1, 2024 License: Apache-2.0, BSD-3-Clause, MIT Imports: 16 Imported by: 0

README

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.

batch_request.png

Plugin Usage

Import the plugin in the main.go file of the gateway project
  • Add the import statement
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:                             # Global configuration
server: # Server configuration
  filter:                                          # Interceptor list for all service handlers
  service: # Business services provided, can have multiple
    - name: trpc.inews.trpc.gateway                # Route name of the service
      filter:
        - batch_request                            # Gateway plugin registered in the service, so that it can be dynamically loaded in router.yaml
plugins: # Plugin configuration
  log:                                            # Log configuration
  gateway: # Plugin type is gateway
    scan_shield:                                  # Scan shield plugin
Configure the plugin in the gateway routing configuration file, router.yaml
router: # Routing configuration
  - method: /v1/user/info
    id: "xxxxxx"
    target_service:
      - service: trpc.user.service
    plugins:
      - name: batch_request                      # Route-level plugin: Tencent authentication plugin
        props:
          code_path: code                         # Status code field name, default is code; supports nested structures, e.g., {"common":{"code":0}}, then fill in common.code
          msg_path: msg                           # msg field path, default is msg; supports nested structures, e.g., {"common":{"msg":"success"}}, then fill in common.msg
          success_code: 0                         # Success status code, default is 0
          request_list:
            - method: /v1/creation/user_ext_info   # Requested interface name
              code_path: code                      # Status code path in the original response, e.g., {"common":{"code":0}}, fill in common.code by default
              msg_path: msg                        # msg path in the original response, e.g., {"common":{"msg":"ok"}}, fill in common.msg by default
              source_date_path: data               # Target data path in the original response body, e.g., {"data":{"source":{}}}, write data.source
              target_data_path: data.users          # Path where the target data is inserted into the result response body, e.g., put it in {"data":{"target":{}}}, write data.target
              success_code: 0                      # Success status code in the original response, default is 0
              ignore_err: false                    # Whether to ignore errors, if true, continue requesting other interfaces if this interface request fails
            - 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: # Upstream service configuration, consistent with the tRPC protocol
  - name: trpc.user.service
    plugins:
      - name: batch_request                  # Service-level configuration
        props:
plugins:
  - name: batch_request                      # Global configuration
Configure the plugin using the console

img.png

Documentation

Overview

Package batchrequest is a plugin for batch requests

Index

Constants

This section is empty.

Variables

View Source
var FasthttpDo = fasthttp.Do

FasthttpDo rename the method FasthttpDo

Functions

func DoRequest

func DoRequest(ctx context.Context, request *Request) ([]byte, error)

DoRequest makes a local request

func ServerFilter

func ServerFilter(ctx context.Context, _ interface{}, _ filter.ServerHandleFunc) (interface{}, error)

ServerFilter is the server-side interceptor

Types

type Options

type Options struct {
	// Name of the status code field, default is "code"; supports nested structure, e.g.: {"common":{"code":0}},
	// then fill in "common.code"
	CodePath string `yaml:"code_path"`
	// Path of the "msg" field, default is "msg"; supports nested structure, e.g.: {"common":{"msg":"success"}},
	// then fill in "common.msg"
	MsgPath string `yaml:"msg_path"`
	// Success status code, default is 0
	SuccessCode int64 `yaml:"success_code"`
	// Success response body
	SuccessBody []byte `yaml:"-"`
	// List of request interfaces
	RequestList []*Request `yaml:"request_list"`
}

Options represents the plugin configuration

type Plugin

type Plugin struct {
}

Plugin defines the plugin

func (*Plugin) CheckConfig

func (p *Plugin) CheckConfig(_ string, decoder plugin.Decoder) error

CheckConfig validates the plugin configuration and returns a parsed configuration object with the correct types. Used in the ServerFilter method for parsing.

func (*Plugin) Setup

func (p *Plugin) Setup(string, plugin.Decoder) error

Setup initializes the plugin

func (*Plugin) Type

func (p *Plugin) Type() string

Type returns the plugin type

type Request

type Request struct {
	// Name of the request interface
	Method string `yaml:"method"`
	// Path of the target data in the original response body, e.g.: {"data":{"source":{}}}, then fill in "data.source"
	SourceDatePath string `yaml:"source_date_path"`
	// Path where the target data is inserted into the result response body, e.g.: put it into {"data":{"target":{}}},
	// then fill in "data.target"
	TargetDataPath string `yaml:"target_data_path"`
	// Ignore errors
	IgnoreErr bool `yaml:"ignore_err"`
	// Path of the status code in the original response, e.g.: {"common":{"code":0}}, then fill in "common.code",
	// default is "code"
	CodePath string `yaml:"code_path"`
	// Path of the "msg" in the original response, e.g.: {"common":{"msg":"ok"}}, then fill in "common.msg",
	// default is "msg"
	MsgPath string `yaml:"msg_path"`
	// Success status code in the original response, default is 0
	SuccessCode int64 `yaml:"success_code"`
}

Request represents the request configuration

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL