Request Retry

Request Retry

When a service call fails, we can allow the framework to automatically retry a few times, which can improve the success rate of requests seen by users. In failover cluster mode, dubbo-go supports automatic retries.

1. Introduction

This example demonstrates how to configure the retry function when a client-side call fails. Full example source code

2. How to Use the Retry Feature

When creating a client using client.NewClient(), you can set the retry count using client.WithClientRetries() method.

  1. cli, err := client.NewClient(
  2. client.WithClientURL("tri://127.0.0.1:20000"),
  3. client.WithClientRetries(3),
  4. )

Alternatively, you can set the service-level timeout using client.WithRequestTimeout() (the following configuration applies to service svc).

  1. svc, err := greet.NewGreetService(cli, client.WithClientRetries(5))

You can also specify the retry count at the time of the call using client.WithCallRetries().

  1. resp, err := svc.Greet(context.Background(), &greet.GreetRequest{Name: "hello world"}, client.WithCallRetries(6))

From top to bottom, the priority of the above three methods increases, with client.WithCallRetries() having the highest priority.

3. Example Analysis

3.1 Server Introduction

Server proto file

Source file path: dubbo-go-sample/retry/proto/greet.proto

  1. syntax = "proto3";
  2. package greet;
  3. option go_package = "github.com/apache/dubbo-go-samples/retry/proto;greet";
  4. message GreetRequest {
  5. string name = 1;
  6. }
  7. message GreetResponse {
  8. string greeting = 1;
  9. }
  10. service GreetService {
  11. rpc Greet(GreetRequest) returns (GreetResponse) {}
  12. rpc GreetTimeout(GreetRequest) returns (GreetResponse) {}
  13. }

Server handler file

The Greet method responds directly, while the GreetRetry method simulates retries.

  1. package main
  2. import (
  3. "context"
  4. "github.com/pkg/errors"
  5. _ "dubbo.apache.org/dubbo-go/v3/imports"
  6. "dubbo.apache.org/dubbo-go/v3/protocol"
  7. "dubbo.apache.org/dubbo-go/v3/server"
  8. greet "github.com/apache/dubbo-go-samples/retry/proto"
  9. "github.com/dubbogo/gost/log/logger"
  10. )
  11. type GreetTripleServer struct {
  12. requestTime int
  13. }
  14. func (srv *GreetTripleServer) Greet(ctx context.Context, req *greet.GreetRequest) (*greet.GreetResponse, error) {
  15. resp := &greet.GreetResponse{Greeting: req.Name}
  16. logger.Info("Not need retry, request success")
  17. return resp, nil
  18. }
  19. func (srv *GreetTripleServer) GreetRetry(ctx context.Context, req *greet.GreetRequest) (*greet.GreetResponse, error) {
  20. if srv.requestTime < 3 {
  21. srv.requestTime++
  22. logger.Infof("retry %d times", srv.requestTime)
  23. return nil, errors.New("retry")
  24. }
  25. resp := &greet.GreetResponse{Greeting: req.Name}
  26. logger.Infof("retry success, current request time is %d", srv.requestTime)
  27. srv.requestTime = 0
  28. return resp, nil
  29. }
  30. func main() {
  31. srv, err := server.NewServer(
  32. server.WithServerProtocol(
  33. protocol.WithPort(20000),
  34. ),
  35. )
  36. if err != nil {
  37. panic(err)
  38. }
  39. if err := greet.RegisterGreetServiceHandler(srv, &GreetTripleServer{
  40. requestTime: 0,
  41. }); err != nil {
  42. panic(err)
  43. }
  44. if err := srv.Serve(); err != nil {
  45. logger.Error(err)
  46. }
  47. }

3.2 Client Introduction

The client file creates a client, sets the retry count to 3, and makes requests to Greet and GreetRetry, observing the server log output.

  1. package main
  2. import (
  3. "context"
  4. "dubbo.apache.org/dubbo-go/v3/client"
  5. _ "dubbo.apache.org/dubbo-go/v3/imports"
  6. greet "github.com/apache/dubbo-go-samples/retry/proto"
  7. "github.com/dubbogo/gost/log/logger"
  8. )
  9. func main() {
  10. cli, err := client.NewClient(
  11. client.WithClientURL("tri://127.0.0.1:20000"),
  12. client.WithClientRetries(3),
  13. )
  14. if err != nil {
  15. panic(err)
  16. }
  17. svc, err := greet.NewGreetService(cli)
  18. if err != nil {
  19. panic(err)
  20. }
  21. // request normal
  22. resp, err := svc.Greet(context.Background(), &greet.GreetRequest{Name: "hello world"})
  23. if err != nil {
  24. logger.Error(err)
  25. }
  26. logger.Infof("Greet response: %s", resp.Greeting)
  27. // request need retry
  28. resp, err = svc.GreetRetry(context.Background(), &greet.GreetRequest{Name: "hello world"})
  29. if err != nil {
  30. logger.Error(err)
  31. }
  32. logger.Infof("Greet response: %s", resp.Greeting)
  33. }

3.3 Case Effect

Start the server first, then start the client. When accessing GreetRetry, observe the server log output showing the retry count.

  1. 2024-01-23 22:39:11 INFO logger/logging.go:22 [Not need retry, request success]
  2. 2024-01-23 22:39:11 INFO logger/logging.go:42 retry [1] times
  3. 2024-01-23 22:39:11 INFO logger/logging.go:42 retry [2] times
  4. 2024-01-23 22:39:11 INFO logger/logging.go:42 retry [3] times
  5. 2024-01-23 22:39:11 INFO logger/logging.go:42 retry success, current request time is [3]

Feedback

Was this page helpful?

Yes No

Last modified September 30, 2024: Update & Translate Overview Docs (#3040) (d37ebceaea7)