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

边栏推荐
- Metasploit(msf)利用ms17_010(永恒之蓝)出现Encoding::UndefinedConversionError问题
- Activate function and its gradient
- Starting from 1.5, build a micro Service Framework -- log tracking traceid
- Global and Chinese markets for reciprocating seal compressors 2022-2028: Research Report on technology, participants, trends, market size and share
- Evolution of APK reinforcement technology, APK reinforcement technology and shortcomings
- Common model making instructions
- audiopolicy
- 使用rewrite规则实现将所有到a域名的访问rewrite到b域名
- Composition of interface
- My experience and summary of the new Zhongtai model
猜你喜欢

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

Getting started stm32--gpio (running lantern) (nanny level)

Element operation and element waiting in Web Automation

Simple and beautiful method of PPT color matching

查看网页最后修改时间方法以及原理简介

How to quickly understand complex businesses and systematically think about problems?

openresty ngx_lua请求响应

My experience and summary of the new Zhongtai model

第十七周作业

链表之双指针(快慢指针,先后指针,首尾指针)
随机推荐
audiopolicy
npm ELECTRON_ Mirror is set as domestic source (npmmirror China mirror)
链表之双指针(快慢指针,先后指针,首尾指针)
从 1.5 开始搭建一个微服务框架——日志追踪 traceId
Finally understand what dynamic planning is
Evolution of APK reinforcement technology, APK reinforcement technology and shortcomings
Leecode learning notes
The code generator has deoptimised the styling of xx/typescript. js as it exceeds the max of 500kb
openresty ngx_ Lua request response
openresty ngx_ Lua regular expression
Spectrum analysis of ADC sampling sequence based on stm32
Arduino measures AC current
Hj16 shopping list
2022 G3 boiler water treatment simulation examination and G3 boiler water treatment simulation examination question bank
Global and Chinese markets for welding products 2022-2028: Research Report on technology, participants, trends, market size and share
3 find the greatest common divisor and the least common multiple
Global and Chinese market of networked refrigerators 2022-2028: Research Report on technology, participants, trends, market size and share
抖音__ac_signature
判断二叉树是否为完全二叉树
2022.02.13 - SX10-30. Home raiding II