...
1
2
3
4 package grpc_retry_test
5
6 import (
7 "context"
8 "fmt"
9 "io"
10 "time"
11
12 "github.com/grpc-ecosystem/go-grpc-middleware/retry"
13 pb_testproto "github.com/grpc-ecosystem/go-grpc-middleware/testing/testproto"
14 "google.golang.org/grpc"
15 "google.golang.org/grpc/codes"
16 )
17
18 var cc *grpc.ClientConn
19
20 func newCtx(timeout time.Duration) context.Context {
21 ctx, _ := context.WithTimeout(context.TODO(), timeout)
22 return ctx
23 }
24
25
26 func Example_initialization() {
27 grpc.Dial("myservice.example.com",
28 grpc.WithStreamInterceptor(grpc_retry.StreamClientInterceptor()),
29 grpc.WithUnaryInterceptor(grpc_retry.UnaryClientInterceptor()),
30 )
31 }
32
33
34 func Example_initializationWithOptions() {
35 opts := []grpc_retry.CallOption{
36 grpc_retry.WithBackoff(grpc_retry.BackoffLinear(100 * time.Millisecond)),
37 grpc_retry.WithCodes(codes.NotFound, codes.Aborted),
38 }
39 grpc.Dial("myservice.example.com",
40 grpc.WithStreamInterceptor(grpc_retry.StreamClientInterceptor(opts...)),
41 grpc.WithUnaryInterceptor(grpc_retry.UnaryClientInterceptor(opts...)),
42 )
43 }
44
45
46
47
48 func Example_initializationWithExponentialBackoff() {
49 opts := []grpc_retry.CallOption{
50 grpc_retry.WithBackoff(grpc_retry.BackoffExponential(100 * time.Millisecond)),
51 }
52 grpc.Dial("myservice.example.com",
53 grpc.WithStreamInterceptor(grpc_retry.StreamClientInterceptor(opts...)),
54 grpc.WithUnaryInterceptor(grpc_retry.UnaryClientInterceptor(opts...)),
55 )
56 }
57
58
59 func Example_simpleCall() {
60 client := pb_testproto.NewTestServiceClient(cc)
61 stream, _ := client.PingList(newCtx(1*time.Second), &pb_testproto.PingRequest{}, grpc_retry.WithMax(3))
62
63 for {
64 pong, err := stream.Recv()
65 if err == io.EOF {
66 break
67 } else if err != nil {
68 return
69 }
70 fmt.Printf("got pong: %v", pong)
71 }
72 }
73
74
75
76
77
78
79
80
81
82
83 func ExampleWithPerRetryTimeout() {
84 client := pb_testproto.NewTestServiceClient(cc)
85 pong, _ := client.Ping(
86 newCtx(5*time.Second),
87 &pb_testproto.PingRequest{},
88 grpc_retry.WithMax(3),
89 grpc_retry.WithPerRetryTimeout(1*time.Second))
90
91 fmt.Printf("got pong: %v", pong)
92 }
93
View as plain text