当前位置:网站首页>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

边栏推荐
- H5c3 advanced - player
- 分布式解决方案之TCC
- Using LNMP to build WordPress sites
- openresty ngx_lua正則錶達式
- I closed the open source project alinesno cloud service
- Alibaba Tianchi SQL training camp task4 learning notes
- Ieventsystemhandler event interface
- PLC编程基础之数据类型、变量声明、全局变量和I/O映射(CODESYS篇 )
- 东南亚电商指南,卖家如何布局东南亚市场?
- Three. JS VR house viewing
猜你喜欢

【Note17】PECI(Platform Environment Control Interface)

VOT toolkit environment configuration and use

Leetcode weekly The 280 game of the week is still difficult for the special game of the week's beauty team ~ simple simulation + hash parity count + sorting simulation traversal

Three.js-01 入门

Unity Max and min constraint adjustment
![[speech processing] speech signal denoising based on Matlab GUI Hanning window fir notch filter [including Matlab source code 1711]](/img/03/8fa104b177698a15b7ffa70d4fb524.jpg)
[speech processing] speech signal denoising based on Matlab GUI Hanning window fir notch filter [including Matlab source code 1711]

LeetCode102. Sequence traversal of binary tree (output by layer and unified output)

【无标题】

Starting from 1.5, build a micro Service Framework -- log tracking traceid

openresty ngx_lua请求响应
随机推荐
Solve the problem of "no input file specified" when ThinkPHP starts
SPSS analysis of employment problems of college graduates
Element operation and element waiting in Web Automation
Metasploit (MSF) uses MS17_ 010 (eternal blue) encoding:: undefined conversionerror problem
C Primer Plus Chapter 9 question 10 binary conversion
d3dx9_ What if 29.dll is missing? System missing d3dx9_ Solution of 29.dll file
Selenium+pytest automated test framework practice
东南亚电商指南,卖家如何布局东南亚市场?
Masked Autoencoders Are Scalable Vision Learners (MAE)
Registration and skills of hoisting machinery command examination in 2022
Leetcode daily question 1189 The maximum number of "balloons" simple simulation questions~
Hcip day 11 (BGP agreement)
openresty ngx_ Lua request response
VOT Toolkit环境配置与使用
Spectrum analysis of ADC sampling sequence based on stm32
Yiwen gets rid of the garbage collector
Douban scoring applet Part-2
Methods modified by static
一文搞定垃圾回收器
Alibaba Tianchi SQL training camp task4 learning notes