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

边栏推荐
猜你喜欢

Ieventsystemhandler event interface

TypeError: this. getOptions is not a function
![[digital signal denoising] improved wavelet modulus maxima digital signal denoising based on MATLAB [including Matlab source code 1710]](/img/b4/af689abb3ad4e25988f2d17152406e.jpg)
[digital signal denoising] improved wavelet modulus maxima digital signal denoising based on MATLAB [including Matlab source code 1710]

Selenium+pytest automated test framework practice

Element positioning of Web Automation

两数之和、三数之和(排序+双指针)

Un article traite de la microstructure et des instructions de la classe

Hcip day 11 (BGP agreement)

fibonacci search
![[speech processing] speech signal denoising and denoising based on Matlab GUI low-pass filter [including Matlab source code 1708]](/img/df/9aa83ac5bd9f614942310a040a6dff.jpg)
[speech processing] speech signal denoising and denoising based on Matlab GUI low-pass filter [including Matlab source code 1708]
随机推荐
Element positioning of Web Automation
Paddy serving v0.9.0 heavy release multi machine multi card distributed reasoning framework
Boring boring
Composition of interface
Use the rewrite rule to rewrite all accesses to the a domain name to the B domain name
链表之双指针(快慢指针,先后指针,首尾指针)
Getting started stm32--gpio (running lantern) (nanny level)
Thoroughly understand JVM class loading subsystem
从 1.5 开始搭建一个微服务框架——日志追踪 traceId
SPSS analysis of employment problems of college graduates
Methods modified by static
Editor extensions in unity
Request preview display of binary data and Base64 format data
Vision Transformer (ViT)
从 1.5 开始搭建一个微服务框架——日志追踪 traceId
Function default parameters, function placeholder parameters, function overloading and precautions
媒体查询:引入资源
Three. Js-01 getting started
TOPSIS code part of good and bad solution distance method
Google Maps case