Skip to content

Commit

Permalink
docs: update hostclient config hook (#792)
Browse files Browse the repository at this point in the history
Co-authored-by: Jiun Lee <[email protected]>
  • Loading branch information
lim-yoona and Skyenought authored Oct 8, 2023
1 parent 56bb471 commit f13c6e0
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
9 changes: 5 additions & 4 deletions content/en/docs/hertz/reference/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ Server Connection limitation:

## Client

The configuration items on the Client side all use `server.xxx` when initializing the Server, such as:
The configuration items on the Client side all use `client.xxx` when initializing the Client, such as:

```go
package main
Expand All @@ -86,8 +86,8 @@ func main() {
}
```

| Configuration Name | Type | Description |
| :---- | :---- | :---- |
| Configuration Name | Type | Description |
| :---- | :---- |:----|
| WithDialTimeout | time.Duration | Connection establishment timeout. Default: 1s. |
| WithMaxConnsPerHost | int | Set the maximum number of connections for every host. Default: 512. |
| WithMaxIdleConnDuration | time.Duration | Set the idle connection timeout, which will close the connection after the timeout Default: 10s. |
Expand All @@ -101,4 +101,5 @@ func main() {
| WithDialer | network.Dialer | Set the network library used by the client. Default: netpoll. |
| WithResponseBodyStream | bool | Set whether to use stream processing. Default: false. |
| WithDialFunc | client.DialFunc | Set Dial Function. |
| WithWriteTimeout | time.Duration | The timeout of data writing. Default:infinite.|
| WithWriteTimeout | time.Duration | The timeout of data writing. Default:infinite. |
| WithHostClientConfigHook | func(hc interface{}) error | Set the function hook for re-configure the host client. The function needs to assert the parameter hc as the required struct, such as http1.HostClient, and then perform specific processing. |
12 changes: 10 additions & 2 deletions content/en/docs/hertz/tutorials/basic-feature/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,15 @@ func main() {
| WithDialer | network.Dialer | specific dialer. |
| WithResponseBodyStream | false | determine whether read body in stream or not, default not read in stream. |
| WithDisableHeaderNamesNormalizing | false | whether disable header names normalizing, default not disabled, for example, cONTENT-lenGTH -> Content-Length. |
| WithName | "" | client name which used in User-Agent Header. |
| WithNoDefaultUserAgentHeader | false | whether no default User-Agent header, default with User-Agent header. |
| WithName | "" | set client name which used in User-Agent Header. |
| WithNoDefaultUserAgentHeader | false | whether default no User-Agent header, default with User-Agent header. |
| WithDisablePathNormalizing | false | whether disable path normalizing, default specification path, for example, http://localhost:8080/hello/../ hello -> http://localhost:8080/hello. |
| WithRetryConfig | nil | retry configuration, for specific configuration information, please refer to [retry](/docs/hertz/tutorials/basic-feature/retry/). |
| WithWriteTimeout | 0s | write timeout. |
| WithConnStateObserve | nil, 5s | set function to observe and record the connection status of HTTP client, as well as observe execution intervals. |
| WithDialFunc | network.Dialer | set dialer function. |
| WithHostClientConfigHook | nil | Set the hook function for re-configure the host client. |


Sample Code:

Expand Down Expand Up @@ -102,6 +104,12 @@ func main() {
client.WithWriteTimeout(10*time.Second),
client.WithConnStateObserve(stateFunc, observeInterval),
client.WithDialFunc(customDialFunc, netpoll.NewDialer()),
client.WithHostClientConfigHook(func(hc interface{}) error {
if hct, ok := hc.(*http1.HostClient); ok {
hct.Addr = "FOO.BAR:443"
}
return nil
})
)
if err != nil {
return
Expand Down
7 changes: 4 additions & 3 deletions content/zh/docs/hertz/reference/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,20 @@ func main() {
}
```

| 配置名称 | 类型 | 说明 |
| :---- | :---- | :---- |
| 配置名称 | 类型 | 说明 |
| :---- | :---- |:---|
| WithDialTimeout | time.Duration | 连接建立超时时间,默认 1s |
| WithMaxConnsPerHost | int | 设置为每个 host 建立的最大连接数,默认 512 |
| WithMaxIdleConnDuration | time.Duration | 设置空闲连接超时时间,当超时后会关闭该连接,默认 10s |
| WithMaxConnDuration | time.Duration | 设置连接存活的最大时长,超过这个时间的连接在完成当前请求后会被关闭,默认无限长 |
| WithMaxConnWaitTimeout | time.Duration | 设置等待空闲连接的最大时间,默认不等待 |
| WithKeepAlive | bool | 是否使用长连接,默认开启 |
| WithRetryConfig | ...retry.Option | 设置 client 的 retry config。Hertz 版本需 >= v0.4.0|
| WithRetryConfig | ...retry.Option | 设置 client 的 retry config。Hertz 版本需 >= v0.4.0 |
| ~~WithMaxIdempotentCallAttempts~~ | int | 设置最大调用次数,调用失败则会重试。默认 1 次即不重试。v0.4.0 版本废止,该版本之前可用,建议升级 Hertz 版本 >= v0.4.0 并使用 WithRetryConfig 替代 |
| WithClientReadTimeout | time.Duration | 设置读取 response 的最长时间,默认无限长 |
| WithTLSConfig | *tls.Config | 双向 TLS 认证时,设置 client 的 TLS config |
| WithDialer | network.Dialer | 设置 client 使用的网络库,默认 netpoll |
| WithResponseBodyStream | bool | 设置是否使用流式处理,默认关闭 |
| WithDialFunc | client.DialFunc | 设置 Dial Function |
| WithWriteTimeout | time.Duration | 写入数据超时时间,默认值:无限长 |
| WithHostClientConfigHook | func(hc interface{}) error | 设置 hook 函数来重新配置 host client,传入的 func 需要将参数 hc 断言为需要的结构体,比如 http1.HostClient ,再进行具体处理 |
15 changes: 11 additions & 4 deletions content/zh/docs/hertz/tutorials/basic-feature/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func main() {

## Client 配置

| 配置项 | 默认值 | 描述 |
|------------------------------------| -------------- | ---- |
| 配置项 | 默认值 | 描述 |
|------------------------------------|----------------|----|
| WithDialTimeout | 1s | 拨号超时时间 |
| WithMaxConnsPerHost | 512 | 每个主机可能建立的最大连接数 |
| WithMaxIdleConnDuration | 10s | 最大的空闲连接持续时间,空闲的连接在此持续时间后被关闭 |
Expand All @@ -57,13 +57,14 @@ func main() {
| WithDialer | network.Dialer | 设置指定的拨号器 |
| WithResponseBodyStream | false | 是否在流中读取 body,默认不在流中读取 |
| WithDisableHeaderNamesNormalizing | false | 是否禁用头名称规范化,默认不禁用,如 cONTENT-lenGTH -> Content-Length |
| WithName | "" | 用户代理头中使用的客户端名称 |
| WithNoDefaultUserAgentHeader | false | 是否没有默认的 User-Agent 头,默认有 User-Agent 头 |
| WithName | "" | 设置用户代理头中使用的客户端名称 |
| WithNoDefaultUserAgentHeader | false | 设置是否应该在请求中包含默认的 User-Agent 头, 默认包含 User-Agent 头 |
| WithDisablePathNormalizing | false | 是否禁用路径规范化,默认规范路径,如 http://localhost:8080/hello/../ hello -> http://localhost:8080/hello |
| WithRetryConfig | nil | HTTP 客户端的重试配置,重试配置详细说明请看 [重试](/zh/docs/hertz/tutorials/basic-feature/retry/) |
| WithWriteTimeout | 0s | HTTP 客户端的写入超时时间 |
| WithConnStateObserve | nil, 5s | 设置观察和记录 HTTP 客户端的连接状态的函数以及观察执行间隔 |
| WithDialFunc | network.Dialer | 设置 HTTP 客户端拨号器函数,会覆盖自定义拨号器 |
| WithHostClientConfigHook | nil | 设置 hook 函数来重新配置 host client |

示例代码:

Expand Down Expand Up @@ -100,6 +101,12 @@ func main() {
client.WithWriteTimeout(10*time.Second),
client.WithConnStateObserve(stateFunc, observeInterval),
client.WithDialFunc(customDialFunc, netpoll.NewDialer()),
client.WithHostClientConfigHook(func(hc interface{}) error {
if hct, ok := hc.(*http1.HostClient); ok {
hct.Addr = "FOO.BAR:443"
}
return nil
})
)
if err != nil {
return
Expand Down

1 comment on commit f13c6e0

@vercel
Copy link

@vercel vercel bot commented on f13c6e0 Oct 8, 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.