当前位置:网站首页>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
边栏推荐
- Metasploit(msf)利用ms17_010(永恒之蓝)出现Encoding::UndefinedConversionError问题
- TCC of distributed solutions
- Metasploit (MSF) uses MS17_ 010 (eternal blue) encoding:: undefined conversionerror problem
- 鏈錶之雙指針(快慢指針,先後指針,首尾指針)
- openresty ngx_ Lua request response
- 利用LNMP实现wordpress站点搭建
- 一文搞定垃圾回收器
- 分布式解决方案选型
- Error when LabVIEW opens Ni instance finder
- Registration of Electrical Engineering (elementary) examination in 2022 and the latest analysis of Electrical Engineering (elementary)
猜你喜欢
Hcip day 11 (BGP agreement)
Paddle Serving v0.9.0 重磅发布多机多卡分布式推理框架
【Note17】PECI(Platform Environment Control Interface)
audiopolicy
Arduino 测量交流电流
基于STM32的ADC采样序列频谱分析
[untitled]
Thoroughly understand JVM class loading subsystem
Metasploit(msf)利用ms17_010(永恒之蓝)出现Encoding::UndefinedConversionError问题
[speech processing] speech signal denoising and denoising based on MATLAB low-pass filter [including Matlab source code 1709]
随机推荐
Thoroughly understand JVM class loading subsystem
Evolution of APK reinforcement technology, APK reinforcement technology and shortcomings
Error when LabVIEW opens Ni instance finder
Common model making instructions
Using LNMP to build WordPress sites
【Note17】PECI(Platform Environment Control Interface)
Global and Chinese market of diesel fire pump 2022-2028: Research Report on technology, participants, trends, market size and share
Arduino measures AC current
Element positioning of Web Automation
SPSS analysis of employment problems of college graduates
Three. Js-01 getting started
使用rewrite规则实现将所有到a域名的访问rewrite到b域名
The code generator has deoptimised the styling of xx/typescript. js as it exceeds the max of 500kb
513. Find the value in the lower left corner of the tree
VOT toolkit environment configuration and use
傅里叶分析概述
Binary tree (II) -- code implementation of heap
谷歌地图案例
[speech processing] speech signal denoising and denoising based on Matlab GUI low-pass filter [including Matlab source code 1708]
[untitled]