Timeout Duration

Configure Timeout Duration

1. Introduction

This example demonstrates how to set the request timeout duration when initiating a call from the Dubbo-go client. You can view the complete example source code here.

2. How to Set the Request Timeout Duration

When creating a client, you can set a global timeout using the client.WithRequestTimeout() method (shared by all service proxies using this client).

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

You can also create service-level timeout using client.WithRequestTimeout() (all method calls initiated from the following service proxy svc will use this timeout).

  1. svc, err := greet.NewGreetService(cli, client.WithRequestTimeout(5 * time.Second))

Additionally, you can specify a timeout when making the call using client.WithCallRequestTimeout().

  1. resp, err := svc.GreetTimeout(context.Background(), &greet.GreetRequest{Name: "hello world"}, client.WithCallRequestTimeout(10 * time.Second))

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

3. Example Details

3.1 Introduction to the Server

Server Proto File

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

  1. syntax = "proto3";
  2. package greet;
  3. option go_package = "github.com/apache/dubbo-go-samples/timeout/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 GreetTimeout method waits for five seconds before responding (simulating a timeout).

Source file path: dubbo-go-sample/timeout/go-server/handler.go

  1. package main
  2. import (
  3. "context"
  4. "time"
  5. greet "github.com/apache/dubbo-go-samples/timeout/proto"
  6. )
  7. type GreetTripleServer struct {
  8. }
  9. func (srv *GreetTripleServer) Greet(ctx context.Context, req *greet.GreetRequest) (*greet.GreetResponse, error) {
  10. resp := &greet.GreetResponse{Greeting: req.Name}
  11. return resp, nil
  12. }
  13. func (srv *GreetTripleServer) GreetTimeout(ctx context.Context, req *greet.GreetRequest) (*greet.GreetResponse, error) {
  14. time.Sleep(5 * time.Second)
  15. resp := &greet.GreetResponse{Greeting: req.Name}
  16. return resp, nil
  17. }

3.2 Introduction to the Client

Client file, creating a client, setting the timeout to 3s, requesting Greet and GreetTimeout, and outputting the response results.

Source file path: dubbo-go-sample/timeout/go-client/client.go

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

3.3 Example Effect

Start the server first, then start the client. You will observe that the GreetTimeout request times out while the Greet request responds normally.

  1. [call [greet.GreetService.GreetTimeout] service timeout]
  2. Greet response: [hello world]

Feedback

Was this page helpful?

Yes No

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