当前位置:网站首页>Use of grpc interceptor
Use of grpc interceptor
2022-07-05 23:01:00 【sweey_ lff】
gRPC As a universal RPC frame , Built in interceptor function . Including server-side interceptors and client-side interceptors , It's the same in use . The main function is to rpc Additional processing before and after the call .
Interceptors are used in many scenarios , For example, verify whether the user logs in before calling the interface , For example, judge the user in the interface useragent Do some anti climbing strategies and so on , In a large number of cases, the request is intercepted , Do the interface preprocessing , We can't write it in every interface , We need to build a unified interceptor .
Implement simple interceptors
1) Server side :grpc.UnaryInterceptor(interceptor)
interceptor Is a custom interceptor function , The parameters of the tracking function are known ,interceptor It's a :
type UnaryServerInterceptor func(ctx context.Context, req interface{}, info *UnaryServerInfo, handler UnaryHandler) (resp interface{}, err error)Parameter meaning :
- ctx context.Context: Request context
- req interface{}:RPC Method
- info *UnaryServerInfo:RPC All information about the method
- handler UnaryHandler:RPC The logic that the method really executes
Case study :
// Interceptor
interceptor := func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error){
fmt.Println(" Received a new request ")
res,err := handler(ctx, req)
fmt.Println(" Request completed ")
return res, err
}
opt := grpc.UnaryInterceptor(interceptor)
g := grpc.NewServer(opt)
//...2) client :grpc.WithUnaryInterceptor(interceptor)
interceptor Is a custom interceptor function , The parameters of the tracking function are known ,interceptor It's a :
type UnaryClientInterceptor func(ctx context.Context, method string, req, reply interface{}, cc *ClientConn, invoker UnaryInvoker, opts ...CallOption) errorCase study :
interceptor := func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error{
start := time.Now()
err := invoker(ctx, method, req, reply, cc, opts...)
fmt.Printf(" Time consuming :%s\n", time.Since(start))
return err
}
opt := grpc.WithUnaryInterceptor(interceptor)
conn, err := grpc.Dial(":8083", grpc.WithInsecure(), opt)
//...3) Some open source interceptors
https://github.com/grpc-ecosystem/go-grpc-middleware

边栏推荐
- LeetCode102. Sequence traversal of binary tree (output by layer and unified output)
- Overview of Fourier analysis
- openresty ngx_ Lua request response
- leecode-学习笔记
- Nail error code Encyclopedia
- Negative sampling
- 513. Find the value in the lower left corner of the tree
- Masked Autoencoders Are Scalable Vision Learners (MAE)
- 2022 G3 boiler water treatment simulation examination and G3 boiler water treatment simulation examination question bank
- Record several frequently asked questions (202207)
猜你喜欢

Activate function and its gradient

Unity Max and min constraint adjustment

Un article traite de la microstructure et des instructions de la classe

Nanjing: full use of electronic contracts for commercial housing sales

Selenium+Pytest自动化测试框架实战

Usage Summary of scriptable object in unity

My experience and summary of the new Zhongtai model

Business introduction of Zhengda international futures company

Arduino 测量交流电流

513. Find the value in the lower left corner of the tree
随机推荐
TypeError: this. getOptions is not a function
CJ mccullem autograph: to dear Portland
Global and Chinese markets of industrial pH meters 2022-2028: Research Report on technology, participants, trends, market size and share
Solve the problem of "no input file specified" when ThinkPHP starts
Nacos 的安装与服务的注册
Three.JS VR看房
Overview of Fourier analysis
Activate function and its gradient
Data type, variable declaration, global variable and i/o mapping of PLC programming basis (CoDeSys)
[speech processing] speech signal denoising and denoising based on MATLAB low-pass filter [including Matlab source code 1709]
audiopolicy
MoCo: Momentum Contrast for Unsupervised Visual Representation Learning
3 find the greatest common divisor and the least common multiple
APK加固技术的演变,APK加固技术和不足之处
终于搞懂什么是动态规划的
TCC of distributed solutions
Registration and skills of hoisting machinery command examination in 2022
Global and Chinese market of networked refrigerators 2022-2028: Research Report on technology, participants, trends, market size and share
Google Maps case
Binary tree (III) -- heap sort optimization, top k problem