From 718d48157e4fa4e02a0e95fe67a76873ab27cfd7 Mon Sep 17 00:00:00 2001 From: chaoranz758 <110596971+chaoranz758@users.noreply.github.com> Date: Wed, 26 Jul 2023 20:29:08 +0800 Subject: [PATCH] docs: Explain how the do function sets request timeout (#732) --- .../hertz/tutorials/basic-feature/client.md | 33 +++++++++++++++++++ .../hertz/tutorials/basic-feature/client.md | 33 +++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/content/en/docs/hertz/tutorials/basic-feature/client.md b/content/en/docs/hertz/tutorials/basic-feature/client.md index 535b8f8a61..ad17d77313 100644 --- a/content/en/docs/hertz/tutorials/basic-feature/client.md +++ b/content/en/docs/hertz/tutorials/basic-feature/client.md @@ -288,10 +288,43 @@ func main() { ## Request Timeout ```go +func WithReadTimeout(t time.Duration) RequestOption func (c *Client) DoTimeout(ctx context.Context, req *protocol.Request, resp *protocol.Response, timeout time.Duration) error func (c *Client) DoDeadline(ctx context.Context, req *protocol.Request, resp *protocol.Response, deadline time.Time) error ``` +### WithReadTimeout + +Although the `Do`, `DoRedirects`, `Get`, `Post` function cannot set the request timeout by passing parameters, it can be set through the `WithRequestTimeout` configuration item in the [Client Request Configuration](#client-request-config). + +Sample Code: + +```go +func main() { + c, err := client.NewClient() + if err != nil { + return + } + + // Do + req, res := &protocol.Request{}, &protocol.Response{} + req.SetOptions(config.WithRequestTimeout(5 * time.Second)) + req.SetMethod(consts.MethodGet) + req.SetRequestURI("http://localhost:8888/get") + err = c.Do(context.Background(), req, res) + + // DoRedirects + err = c.DoRedirects(context.Background(), req, res, 5) + + // Get + _, _, err = c.Get(context.Background(), nil, "http://localhost:8888/get", config.WithRequestTimeout(5*time.Second)) + + // Post + postArgs := &protocol.Args{} + _, _, err = c.Post(context.Background(), nil, "http://localhost:8888/post", postArgs, config.WithRequestTimeout(5*time.Second)) +} +``` + ### DoTimeout The `DoTimeout` function executes the given request and waits for a response within the given timeout period. diff --git a/content/zh/docs/hertz/tutorials/basic-feature/client.md b/content/zh/docs/hertz/tutorials/basic-feature/client.md index 9232b9f3c8..64f876805b 100644 --- a/content/zh/docs/hertz/tutorials/basic-feature/client.md +++ b/content/zh/docs/hertz/tutorials/basic-feature/client.md @@ -286,10 +286,43 @@ func main() { ## 请求超时 ```go +func WithReadTimeout(t time.Duration) RequestOption func (c *Client) DoTimeout(ctx context.Context, req *protocol.Request, resp *protocol.Response, timeout time.Duration) error func (c *Client) DoDeadline(ctx context.Context, req *protocol.Request, resp *protocol.Response, deadline time.Time) error ``` +### WithReadTimeout + +Do、DoRedirects、Get、Post 等请求函数虽然不能以传参的方式设置请求超时返回,但可以通过 [Client Request 配置](#client-request-配置) 中的 `WithRequestTimeout` 配置项来设置请求超时返回。 + +示例代码: + +```go +func main() { + c, err := client.NewClient() + if err != nil { + return + } + + // Do + req, res := &protocol.Request{}, &protocol.Response{} + req.SetOptions(config.WithRequestTimeout(5 * time.Second)) + req.SetMethod(consts.MethodGet) + req.SetRequestURI("http://localhost:8888/get") + err = c.Do(context.Background(), req, res) + + // DoRedirects + err = c.DoRedirects(context.Background(), req, res, 5) + + // Get + _, _, err = c.Get(context.Background(), nil, "http://localhost:8888/get", config.WithRequestTimeout(5*time.Second)) + + // Post + postArgs := &protocol.Args{} + _, _, err = c.Post(context.Background(), nil, "http://localhost:8888/post", postArgs, config.WithRequestTimeout(5*time.Second)) +} +``` + ### DoTimeout DoTimeout 函数执行给定的请求并在给定的超时时间内等待响应。