当前位置:网站首页>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
边栏推荐
- Spectrum analysis of ADC sampling sequence based on stm32
- VIM tail head intercept file import
- 实现反向代理客户端IP透传
- Binary tree (II) -- code implementation of heap
- Hj16 shopping list
- Global and Chinese market of water treatment technology 2022-2028: Research Report on technology, participants, trends, market size and share
- Event trigger requirements of the function called by the event trigger
- 透彻理解JVM类加载子系统
- Global and Chinese markets for reciprocating seal compressors 2022-2028: Research Report on technology, participants, trends, market size and share
- 2.13 summary
猜你喜欢
我把开源项目alinesno-cloud-service关闭了
Selenium+Pytest自动化测试框架实战
终于搞懂什么是动态规划的
Nanjing: full use of electronic contracts for commercial housing sales
Leetcode daily question 1189 The maximum number of "balloons" simple simulation questions~
fibonacci search
【无标题】
Using LNMP to build WordPress sites
audiopolicy
openresty ngx_ Lua request response
随机推荐
VOT toolkit environment configuration and use
Hcip day 11 (BGP agreement)
Global and Chinese markets for reciprocating seal compressors 2022-2028: Research Report on technology, participants, trends, market size and share
Registration and skills of hoisting machinery command examination in 2022
Three. JS VR house viewing
Solve the problem of "no input file specified" when ThinkPHP starts
[speech processing] speech signal denoising and denoising based on Matlab GUI low-pass filter [including Matlab source code 1708]
抖音__ac_signature
VOT Toolkit环境配置与使用
First, redis summarizes the installation types
Finally understand what dynamic planning is
Tiktok__ ac_ signature
Element operation and element waiting in Web Automation
Global and Chinese markets for welding products 2022-2028: Research Report on technology, participants, trends, market size and share
2022 registration examination for safety management personnel of hazardous chemical business units and simulated reexamination examination for safety management personnel of hazardous chemical busines
Metasploit (MSF) uses MS17_ 010 (eternal blue) encoding:: undefined conversionerror problem
Double pointeur de liste liée (pointeur rapide et lent, pointeur séquentiel, pointeur de tête et de queue)
Nail error code Encyclopedia
Element positioning of Web Automation
Marginal probability and conditional probability