当前位置:网站首页>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
边栏推荐
- Expectation, variance and covariance
- Paddy serving v0.9.0 heavy release multi machine multi card distributed reasoning framework
- The code generator has deoptimised the styling of xx/typescript. js as it exceeds the max of 500kb
- H5c3 advanced - player
- 透彻理解JVM类加载子系统
- Double pointeur de liste liée (pointeur rapide et lent, pointeur séquentiel, pointeur de tête et de queue)
- 基于STM32的ADC采样序列频谱分析
- Realize reverse proxy client IP transparent transmission
- Arduino 测量交流电流
- PLC编程基础之数据类型、变量声明、全局变量和I/O映射(CODESYS篇 )
猜你喜欢
audiopolicy
我对新中台模型的一些经验思考总结
Common JVM tools and optimization strategies
First, redis summarizes the installation types
Three. JS VR house viewing
Hcip day 12 (BGP black hole, anti ring, configuration)
d3dx9_ How to repair 31.dll_ d3dx9_ 31. Solution to missing DLL
Google Maps case
[speech processing] speech signal denoising based on Matlab GUI Hanning window fir notch filter [including Matlab source code 1711]
一文搞定class的微觀結構和指令
随机推荐
Exponential weighted average and its deviation elimination
Hcip day 12 (BGP black hole, anti ring, configuration)
[speech processing] speech signal denoising and denoising based on Matlab GUI low-pass filter [including Matlab source code 1708]
Spectrum analysis of ADC sampling sequence based on stm32
C Primer Plus Chapter 9 question 10 binary conversion
LeetCode145. Post order traversal of binary tree (three methods of recursion and iteration)
Arduino measures AC current
Evolution of APK reinforcement technology, APK reinforcement technology and shortcomings
Binary tree (III) -- heap sort optimization, top k problem
Lesson 1: serpentine matrix
Leetcode daily question 1189 The maximum number of "balloons" simple simulation questions~
Thoroughly understand JVM class loading subsystem
H5c3 advanced - player
Usage Summary of scriptable object in unity
一文搞定class的微观结构和指令
Media query: importing resources
Overview of Fourier analysis
Selenium+Pytest自动化测试框架实战
openresty ngx_ Lua regular expression
Expectation, variance and covariance