Skip to content

Commit

Permalink
docs: Explain how the do function sets request timeout (#732)
Browse files Browse the repository at this point in the history
  • Loading branch information
chaoranz758 authored Jul 26, 2023
1 parent 563feb1 commit 718d481
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
33 changes: 33 additions & 0 deletions content/en/docs/hertz/tutorials/basic-feature/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
33 changes: 33 additions & 0 deletions content/zh/docs/hertz/tutorials/basic-feature/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 函数执行给定的请求并在给定的超时时间内等待响应。
Expand Down

1 comment on commit 718d481

@vercel
Copy link

@vercel vercel bot commented on 718d481 Jul 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.