Skip to content

Commit

Permalink
doc: Server EnableContextTimeout (#1027)
Browse files Browse the repository at this point in the history
  • Loading branch information
felix021 authored Mar 13, 2024
1 parent 87a07d2 commit 72b5b52
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 3 deletions.
60 changes: 59 additions & 1 deletion content/en/docs/kitex/Tutorials/service-governance/timeout.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,20 +152,78 @@ NOTE:
1. After receiving an exit signal, the server will wait for an ExitWaitTime before exit;
2. If the waiting time is exceeded, the server will forcibly terminate all requests being processed (the client will receive an error).

##### EnableContextTimeout

> github.com/cloudwego/kitex >= v0.9.0
(For non-streaming API) If timeout is available in the request's TTHeader, set it into server request ctx.

For detailed usage, please refer to the example code below.

NOTE:
- For Streaming API requests: enabled by default via another way
- Including GRPC/Protobuf and Thrift Streaming
- Client's `ctx.Deadline()` is sent via header `grpc-timeout` to server which will be added to the server request's ctx
- For Non-streaming API requests: disabled by default
- Client should enable TTHeader Transport and ClientTTHeaderMetaHandler, and specify RPC Timeout via client/callopt `WithRPCTimeout` option
- Server should enable this Option and ServerTTHeaderMetaHandler to read the RPCTimeout from requests' TTHeader
- If there's a need in your business code to get this value, call `rpcinfo.GetRPCInfo(ctx).Config().RPCTimeout()`

#### Configuration method

##### By code - Server Option

###### WithReadWriteTimeout

Specified when initializing a server:

```go
import "github.com/cloudwego/kitex/server"

svr := xxx.NewServer(handler,
server.WithReadWriteTimeout(5 * time.Second),
)
```

###### WithExitWaitTime

Specified when initializing a server:

```go
import "github.com/cloudwego/kitex/server"

svr := yourservice.NewServer(handler,
server.WithExitWaitTime(5 * time.Second),
)
```
Note: The two configuration items can be specified independently as needed.

###### WithEnableContextTimeout

This Option should be used with TTHeader; please refer to the example code below.

Client
- Specify TTHeader as Transport Protocol
- Enable [transmeta.ClientTTHeaderHandler](https://github.com/cloudwego/kitex/blob/v0.9.0/pkg/transmeta/ttheader.go#L45)
- Specify an RPC timeout by client.WithRPCTimeout (or by callopt.WithRPCTimeout when sending a request)
```go
cli := yourservice.MustNewClient(
serverName,
client.WithTransportProtocol(transport.TTHeader),
client.WithMetaHandler(transmeta.ClientTTHeaderHandler),
client.WithRPCTimeout(time.Second),
)
```

Server
- Specify this option
- Enable [transmeta.ServerTTHeaderHandler](https://github.com/cloudwego/kitex/blob/v0.9.0/pkg/transmeta/ttheader.go#L46)
```
svr := yourservice.NewServer(handler,
server.WithMetaHandler(transmeta.ServerTTHeaderHandler),
server.WithEnableContextTimeout(true),
)
```


## FAQ

Expand Down
60 changes: 58 additions & 2 deletions content/zh/docs/kitex/Tutorials/service-governance/timeout.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,20 +151,76 @@ rpctimeout.SetBusinessTimeoutThreshold(10 * time.Millisecond)
1. Server 在收到退出信号时的等待时间;
2. 如果超过该等待时间,Server 将会强制结束所有在处理的请求(客户端会收到错误)。

##### EnableContextTimeout

> github.com/cloudwego/kitex >= v0.9.0
(针对非 Streaming API)如果从请求的 TTHeader 中读到了 timeout,则写入 server 请求的 ctx 。

具体用法请参考后文示例代码。

说明:
- Streaming API:已默认开启(通过另外的实现)
- 含 GRPC/Protobuf 和 Thrift Streaming
- client 的 `ctx.Deadline()` 会通过 `grpc_timeout` header 发送给 server,写入 server 的 ctx
- 非 Streaming API:默认未开启
- 在 client 端需启用 TTHeader Transport 和 ClientTTHeaderMetaHandler,并通过 client/callopt `WithRPCTimeout` option 指定 RPC 超时时间
- 在 server 端需启用此 Option 及 ServerTTHeaderMetaHandler,才能从请求的 TTHeader 中读取到 RPCTimeout
- 如果业务代码有需要,可用 `rpcinfo.GetRPCInfo(ctx).Config().RPCTimeout()` 获得这个值


#### 配置方式

##### 代码配置 - Server Option

###### WithReadWriteTimeout

在初始化 Server 时指定:
```go
import "github.com/cloudwego/kitex/server"

svr := xxx.NewServer(handler,
svr := yourservice.NewServer(handler,
server.WithReadWriteTimeout(5 * time.Second),
)
```

###### WithExitWaitTime

在初始化 Server 时指定:
```go
import "github.com/cloudwego/kitex/server"

svr := yourservice.NewServer(handler,
server.WithExitWaitTime(5 * time.Second),
)
```
注:两个配置项可以按需独立配置。

###### WithEnableContextTimeout

该 Option 需配合 TTHeader 使用,详见下方示例代码。

Client
- 指定 Transport Protocol 为 TTHeader
- 启用 [transmeta.ClientTTHeaderHandler](https://github.com/cloudwego/kitex/blob/v0.9.0/pkg/transmeta/ttheader.go#L45)
- 通过 client.WithRPCTimeout (或者在请求时使用 callopt.WithRPCTimeout)指定超时
```go
cli := yourservice.MustNewClient(
serverName,
client.WithTransportProtocol(transport.TTHeader),
client.WithMetaHandler(transmeta.ClientTTHeaderHandler),
client.WithRPCTimeout(time.Second),
)
```

Server
- 指定该 Option
- 启用 [transmeta.ServerTTHeaderHandler](https://github.com/cloudwego/kitex/blob/v0.9.0/pkg/transmeta/ttheader.go#L46)
```
svr := yourservice.NewServer(handler,
server.WithMetaHandler(transmeta.ServerTTHeaderHandler),
server.WithEnableContextTimeout(true),
)
```

## Q&A

Expand Down

0 comments on commit 72b5b52

Please sign in to comment.