Skip to content

Commit f13c6e0

Browse files
docs: update hostclient config hook (#792)
Co-authored-by: Jiun Lee <[email protected]>
1 parent 56bb471 commit f13c6e0

File tree

4 files changed

+30
-13
lines changed

4 files changed

+30
-13
lines changed

content/en/docs/hertz/reference/config.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ Server Connection limitation:
7373

7474
## Client
7575

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

7878
```go
7979
package main
@@ -86,8 +86,8 @@ func main() {
8686
}
8787
```
8888

89-
| Configuration Name | Type | Description |
90-
| :---- | :---- | :---- |
89+
| Configuration Name | Type | Description |
90+
| :---- | :---- |:----|
9191
| WithDialTimeout | time.Duration | Connection establishment timeout. Default: 1s. |
9292
| WithMaxConnsPerHost | int | Set the maximum number of connections for every host. Default: 512. |
9393
| WithMaxIdleConnDuration | time.Duration | Set the idle connection timeout, which will close the connection after the timeout Default: 10s. |
@@ -101,4 +101,5 @@ func main() {
101101
| WithDialer | network.Dialer | Set the network library used by the client. Default: netpoll. |
102102
| WithResponseBodyStream | bool | Set whether to use stream processing. Default: false. |
103103
| WithDialFunc | client.DialFunc | Set Dial Function. |
104-
| WithWriteTimeout | time.Duration | The timeout of data writing. Default:infinite.|
104+
| WithWriteTimeout | time.Duration | The timeout of data writing. Default:infinite. |
105+
| 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. |

content/en/docs/hertz/tutorials/basic-feature/client.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,15 @@ func main() {
5959
| WithDialer | network.Dialer | specific dialer. |
6060
| WithResponseBodyStream | false | determine whether read body in stream or not, default not read in stream. |
6161
| WithDisableHeaderNamesNormalizing | false | whether disable header names normalizing, default not disabled, for example, cONTENT-lenGTH -> Content-Length. |
62-
| WithName | "" | client name which used in User-Agent Header. |
63-
| WithNoDefaultUserAgentHeader | false | whether no default User-Agent header, default with User-Agent header. |
62+
| WithName | "" | set client name which used in User-Agent Header. |
63+
| WithNoDefaultUserAgentHeader | false | whether default no User-Agent header, default with User-Agent header. |
6464
| WithDisablePathNormalizing | false | whether disable path normalizing, default specification path, for example, http://localhost:8080/hello/../ hello -> http://localhost:8080/hello. |
6565
| WithRetryConfig | nil | retry configuration, for specific configuration information, please refer to [retry](/docs/hertz/tutorials/basic-feature/retry/). |
6666
| WithWriteTimeout | 0s | write timeout. |
6767
| WithConnStateObserve | nil, 5s | set function to observe and record the connection status of HTTP client, as well as observe execution intervals. |
6868
| WithDialFunc | network.Dialer | set dialer function. |
69+
| WithHostClientConfigHook | nil | Set the hook function for re-configure the host client. |
70+
6971

7072
Sample Code:
7173

@@ -102,6 +104,12 @@ func main() {
102104
client.WithWriteTimeout(10*time.Second),
103105
client.WithConnStateObserve(stateFunc, observeInterval),
104106
client.WithDialFunc(customDialFunc, netpoll.NewDialer()),
107+
client.WithHostClientConfigHook(func(hc interface{}) error {
108+
if hct, ok := hc.(*http1.HostClient); ok {
109+
hct.Addr = "FOO.BAR:443"
110+
}
111+
return nil
112+
})
105113
)
106114
if err != nil {
107115
return

content/zh/docs/hertz/reference/config.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,20 @@ func main() {
8585
}
8686
```
8787

88-
| 配置名称 | 类型 | 说明 |
89-
| :---- | :---- | :---- |
88+
| 配置名称 | 类型 | 说明 |
89+
| :---- | :---- |:---|
9090
| WithDialTimeout | time.Duration | 连接建立超时时间,默认 1s |
9191
| WithMaxConnsPerHost | int | 设置为每个 host 建立的最大连接数,默认 512 |
9292
| WithMaxIdleConnDuration | time.Duration | 设置空闲连接超时时间,当超时后会关闭该连接,默认 10s |
9393
| WithMaxConnDuration | time.Duration | 设置连接存活的最大时长,超过这个时间的连接在完成当前请求后会被关闭,默认无限长 |
9494
| WithMaxConnWaitTimeout | time.Duration | 设置等待空闲连接的最大时间,默认不等待 |
9595
| WithKeepAlive | bool | 是否使用长连接,默认开启 |
96-
| WithRetryConfig | ...retry.Option | 设置 client 的 retry config。Hertz 版本需 >= v0.4.0|
96+
| WithRetryConfig | ...retry.Option | 设置 client 的 retry config。Hertz 版本需 >= v0.4.0 |
9797
| ~~WithMaxIdempotentCallAttempts~~ | int | 设置最大调用次数,调用失败则会重试。默认 1 次即不重试。v0.4.0 版本废止,该版本之前可用,建议升级 Hertz 版本 >= v0.4.0 并使用 WithRetryConfig 替代 |
9898
| WithClientReadTimeout | time.Duration | 设置读取 response 的最长时间,默认无限长 |
9999
| WithTLSConfig | *tls.Config | 双向 TLS 认证时,设置 client 的 TLS config |
100100
| WithDialer | network.Dialer | 设置 client 使用的网络库,默认 netpoll |
101101
| WithResponseBodyStream | bool | 设置是否使用流式处理,默认关闭 |
102102
| WithDialFunc | client.DialFunc | 设置 Dial Function |
103103
| WithWriteTimeout | time.Duration | 写入数据超时时间,默认值:无限长 |
104+
| WithHostClientConfigHook | func(hc interface{}) error | 设置 hook 函数来重新配置 host client,传入的 func 需要将参数 hc 断言为需要的结构体,比如 http1.HostClient ,再进行具体处理 |

content/zh/docs/hertz/tutorials/basic-feature/client.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ func main() {
4444

4545
## Client 配置
4646

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

6869
示例代码:
6970

@@ -100,6 +101,12 @@ func main() {
100101
client.WithWriteTimeout(10*time.Second),
101102
client.WithConnStateObserve(stateFunc, observeInterval),
102103
client.WithDialFunc(customDialFunc, netpoll.NewDialer()),
104+
client.WithHostClientConfigHook(func(hc interface{}) error {
105+
if hct, ok := hc.(*http1.HostClient); ok {
106+
hct.Addr = "FOO.BAR:443"
107+
}
108+
return nil
109+
})
103110
)
104111
if err != nil {
105112
return

0 commit comments

Comments
 (0)