当前位置:网站首页>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) error
Case 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
边栏推荐
- Commonly used probability distributions: Bernoulli distribution, binomial distribution, polynomial distribution, Gaussian distribution, exponential distribution, Laplace distribution and Dirac delta d
- Nail error code Encyclopedia
- 【Note17】PECI(Platform Environment Control Interface)
- Ieventsystemhandler event interface
- Data type, variable declaration, global variable and i/o mapping of PLC programming basis (CoDeSys)
- Openresty ngx Lua regular expression
- 终于搞懂什么是动态规划的
- Yiwen gets rid of the garbage collector
- 2.13 summary
- Global and Chinese market of networked refrigerators 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢
第一讲:蛇形矩阵
Expectation, variance and covariance
Marginal probability and conditional probability
Google Maps case
我对新中台模型的一些经验思考总结
查看网页最后修改时间方法以及原理简介
Registration of Electrical Engineering (elementary) examination in 2022 and the latest analysis of Electrical Engineering (elementary)
Vcomp110.dll download -vcomp110 What if DLL is lost
One article deals with the microstructure and instructions of class
Error when LabVIEW opens Ni instance finder
随机推荐
Three. JS VR house viewing
openresty ngx_lua请求响应
Marginal probability and conditional probability
[speech processing] speech signal denoising and denoising based on MATLAB low-pass filter [including Matlab source code 1709]
Nacos 的安装与服务的注册
关于MySQL的30条优化技巧,超实用
东南亚电商指南,卖家如何布局东南亚市场?
Data type, variable declaration, global variable and i/o mapping of PLC programming basis (CoDeSys)
Binary tree (III) -- heap sort optimization, top k problem
Starting from 1.5, build a micro Service Framework -- log tracking traceid
Metasploit (MSF) uses MS17_ 010 (eternal blue) encoding:: undefined conversionerror problem
The method and principle of viewing the last modification time of the web page
[digital signal denoising] improved wavelet modulus maxima digital signal denoising based on MATLAB [including Matlab source code 1710]
终于搞懂什么是动态规划的
Metasploit(msf)利用ms17_010(永恒之蓝)出现Encoding::UndefinedConversionError问题
Record several frequently asked questions (202207)
Fix the memory structure of JVM in one article
Media query: importing resources
[secretly kill little buddy pytorch20 days] - [Day2] - [example of picture data modeling process]
CJ mccullem autograph: to dear Portland