当前位置:网站首页>Grpc middleware implements grpc call retry
Grpc middleware implements grpc call retry
2022-07-24 14:19:00 【Scrambled eggs with tomatoes】
proto
syntax = "proto3";
option go_package = ".;proto";
package proto;
service Greeter {
rpc SayHello (HelloRequest) returns (HelloResponse);
}
message HelloRequest {
string name = 1;
}
message HelloResponse {
string message = 1;
}
Generate corresponding go Code
protoc -I . demo.proto --go_out=plugins=grpc:.
server
package main
import (
"ShopBefore/rpc/grpc_retry/proto"
"context"
"google.golang.org/grpc"
"log"
"net"
"time"
)
type Server struct{
}
func (s Server) SayHello(ctx context.Context, request *proto.HelloRequest) (*proto.HelloResponse, error) {
log.Println(" Receive a request .....")
// Set one second timeout , Sleep here for two seconds
time.Sleep(2 * time.Second)
return &proto.HelloResponse{
Message: "Hello" + request.Name}, nil
}
func main() {
server := grpc.NewServer()
proto.RegisterGreeterServer(server, &Server{
})
listen, err := net.Listen("tcp", ":8081")
if err != nil {
panic(err)
}
err = server.Serve(listen)
if err != nil {
panic(err)
}
}
client
package main
import (
"ShopBefore/rpc/grpc_retry/proto"
"context"
"fmt"
"github.com/grpc-ecosystem/go-grpc-middleware/retry"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials/insecure"
"time"
)
func main() {
retryOpts := []grpc_retry.CallOption{
// max retries
grpc_retry.WithMax(3),
// Timeout time
grpc_retry.WithPerRetryTimeout(1 * time.Second),
// Only return the corresponding code Will retry
grpc_retry.WithCodes(codes.Unknown, codes.DeadlineExceeded, codes.Unavailable),
}
conn, err := grpc.Dial("127.0.0.1:8081",
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithUnaryInterceptor(grpc_retry.UnaryClientInterceptor(retryOpts...)))
defer func(conn *grpc.ClientConn) {
err := conn.Close()
if err != nil {
panic(err)
}
}(conn)
if err != nil {
panic(err)
}
client := proto.NewGreeterClient(conn)
hello, err := client.SayHello(context.Background(), &proto.HelloRequest{
Name: " fried eggs with tomatoes "})
if err != nil {
panic(err)
}
fmt.Println(hello)
}
test
server

client

client Send a request , Because it will time out every time , Enter and try again ,server I did receive three requests . Last client It is also normal to make a timeout error
边栏推荐
- Moving the mouse into select options will trigger the mouseleave event processing scheme
- Detailed explanation of switch link aggregation [Huawei ENSP]
- 学习scipy minimize
- Binlog and iptables prevent nmap scanning, xtrabackup full + incremental backup, and the relationship between redlog and binlog
- [oauth2] II. Authorization method of oauth2
- 2022 IAA industry category development insight series report - phase II
- Detailed analysis of common command modules of ansible service
- On the number of solutions of indefinite equations
- TypeError: 'str' object does not support item assignment
- Simple understanding and implementation of unity delegate
猜你喜欢

Centos7安装达梦单机数据库

Detailed explanation of IO model (easy to understand)
![Rasa 3.x learning series -rasa [3.2.3] - new version released on July 18, 2022](/img/fd/c7bff1ce199e8b600761d77828c674.png)
Rasa 3.x learning series -rasa [3.2.3] - new version released on July 18, 2022

Not configured in app.json (uni releases wechat applet)

Nmap security testing tool tutorial

Simple understanding and implementation of unity delegate

C language -- program environment and preprocessing

Unity行人随机行走不碰撞

不要灰心,大名鼎鼎的YOLO、PageRank影响力爆棚的研究,曾被CS顶会拒稿
![Rasa 3.x learning series -rasa [3.2.4] - 2022-07-21 new release](/img/1e/27f107d514ded6641410cc5a45764b.png)
Rasa 3.x learning series -rasa [3.2.4] - 2022-07-21 new release
随机推荐
C unsafe unmanaged object pointer conversion
【NLP】下一站,Embodied AI
Where can Huatai Securities open an account? Is it safe to use a mobile phone
Nodejs uses the express framework to post the request message "badrequesterror:request aborted"
TypeError: 'str' object does not support item assignment
DDD based on ABP -- Entity creation and update
[oauth2] II. Known changes in oauth2.1
Data modification and insertion
Can't remember regular expressions? Here I have sorted out 99 common rules
2022 IAA industry category development insight series report - phase II
[NLP] next stop, embossed AI
5年接触近百位老板,身为猎头的我,发现升职的秘密不过4个字
Matlab program for natural gas flow calculation
How to install PHP 5.6 on Ubuntu 18.04 and Debian 9
Introduction to Xiaoxiong school
Mmdrawercontroller first loading sidebar height problem
字符串——剑指 Offer 58 - II. 左旋转字符串
The difference and relation among list, set and map
Conversion of timestamp and time in Excel
【机器学习】之 主成分分析PCA