Skip to content

Commit 00610fd

Browse files
authored
doc: conn type and transport protocols (#822)
1 parent 42c21c1 commit 00610fd

File tree

4 files changed

+84
-73
lines changed

4 files changed

+84
-73
lines changed

content/en/docs/kitex/Tutorials/basic-feature/connection_type.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
---
22
title: "Connection Type"
3-
date: 2021-09-28
3+
date: 2023-10-16
44
weight: 5
55
keywords: ["Kitex", "Short Connection", "Long Connection", "Connection Multiplexing"]
66
description: "Kitex supports short connections, long connection pool, connection multiplexing and connection pool status monitoring."
77
---
88

99
Kitex provides **Short Connection**, **Long Connection Pool** and **Connection Multiplexing** for different business scenarios.
10-
Kitex uses Long Connection Pool by default after v0.0.2, but adjusting the Pool Config according to your need is suggested.
10+
Kitex uses **Long Connection Pool by default** after v0.0.2, but adjusting the Pool Config according to your need is suggested.
1111

1212
## Short Connection
1313

14-
Every request needs to create a connection, the performance is bad, so it is not suggested normally.
15-
16-
Enable Short Connection:
14+
Every request needs to create a connection. The performance is bad, so it is not suggested in most cases. However, short connections should be used in some scenarios. For example, if there are too many instances on the caller side, it will increase the burden on callee services. Please choose according to the situation.
15+
To enable short connection:
1716

1817
```go
1918
xxxCli := xxxservice.NewClient("destServiceName", client.WithShortConnection())
2019
```
2120

22-
## Long Connection Pool
21+
## Long Connection Pool (Default)
2322

2423
Kitex enable Long Connection Pool after >= v0.0.2, default config params are as below:
2524

@@ -51,14 +50,14 @@ Parameter description:
5150
Each downstream address corresponds to a connection pool, the connection pool is a ring composed of connections, and the size of the ring is `MaxIdlePerAddress`.
5251

5352
When getting a connection of downstream address, proceed as follows:
54-
1. Try to fetch a connection from the ring, if fetching failed (no idle connections remained), then try to establish a new connection. In other words, the number of connections may exceed `MaxIdlePerAddress`
53+
1. Try to fetch a connection from the pool, if fetching failed (no idle connections remained), then try to establish a new connection. In other words, the number of connections may exceed `MaxIdlePerAddress`
5554
2. If fetching succeed, then check whether the idle time of the connection (since the last time it was placed in the connection pool) has exceeded MaxIdleTimeout. If yes, this connection will be closed and a new connection will be created.
5655

5756
When the connection is ready to be returned after used, proceed as follows:
5857

5958
1. Check whether the connection is normal, if not, close it directly
6059
2. Check whether the idle connection number exceeds `MaxIdleGlobal`, and if yes, close it directly
61-
3. Check whether free space remained in the ring of the target connection pool, if yes, put it into the pool, otherwise close it directly
60+
3. Check whether free space remained in the pool of the target connection pool. If yes, put it into the pool, otherwise close it directly
6261

6362
### Parameter Setting Suggestion
6463

@@ -68,12 +67,13 @@ The setting of parameters is suggested as follows:
6867
- For example, the cost of each request is 100ms, and the request spread to each downstream address is 100QPS, the value is suggested to set to 10, because each connection handles 10 requests per second, 100QPS requires 10 connections to handle
6968
- In the actual scenario, the fluctuation of traffic is also necessary to be considered. Pay attention, the connection within MaxIdleTimeout will be recycled if it is not used
7069
- Summary: this value be set too large or too small would lead to degenerating to the short connection
71-
- `MinIdlePerAddress`: Assuming that there are periodic requests and the period is greater than `MaxIdleTimeout`, setting this parameter can avoid creating a new connection every time.
72-
- The parameter consideration is similar to `MaxIdlePerAddress` and can be set according to the average latency of requests and the throughput.
73-
- For example, if `MinIdlePerAddress` is set to 5 and the response time of each request is 100ms. 50 requests can be processed per second (50QPS) without creating a new connection.
7470
- `MaxIdleGlobal`: should be larger than the total number of `downstream targets number * MaxIdlePerAddress`
7571
- Notice: this value is not very valuable, it is suggested to set it to a super large value. In subsequent versions, considers discarding this parameter and providing a new interface
72+
- Since v0.7.2, no limit for `MaxIdleGlobal` if not set.
7673
- `MaxIdleTimeout`: since the server will clean up inactive connections within 10min, the client also needs to clean up long-idle connections in time to avoid using invalid connections. This value cannot exceed 10min when the downstream is also a Kitex service
74+
- `MinIdlePerAddress` (Kitex >= v0.4.3): Assuming that there are periodic requests and the period is greater than `MaxIdleTimeout`, setting this parameter can avoid creating a new connection every time.
75+
- The parameter consideration is similar to `MaxIdlePerAddress` and can be set according to the average latency of requests and the throughput.
76+
- For example, if `MinIdlePerAddress` is set to 5 and the response time of each request is 100ms. 50 requests can be processed per second (50QPS) without creating a new connection.
7777

7878
## Connection Multiplexing
7979

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "Transport Protocol"
3-
date: 2022-07-20
3+
date: 2023-10-16
44
weight: 3
55
keywords: ["Kitex", "TTHeader", "HTTP2"]
66
description: Kitex supports transport protocols of TTHeader、HTTP2.
@@ -29,46 +29,52 @@ Here are the available combination options of transport protocols and message pr
2929
If you want to use custom implementations for the message or transport protocol, you can find help here [Extension of Codec](/docs/kitex/tutorials/framework-exten/codec/).
3030

3131
## Configuration
32-
33-
You can configure the transport protocol when initializing the client:
34-
32+
### Client
33+
#### Thrift
34+
1. Buffered: PurePayload (by default)
35+
2. configure Framed: prepend a 4-byte (int32) length before the Thrift pure payload
3536
```go
36-
// client option
37-
client.WithTransportProtocol(transport.XXX)
37+
cli := xxx.NewClient("service_name", client.WithTransportProtocol(transport.Framed))
3838
```
39-
40-
Kitex Server supports protocol detection for all supported protocols and doesn't require explicit configuration.
41-
42-
## Usage
43-
44-
### Thrift + TTHeader
45-
39+
3. configure TTHeader: the protocol for Byted Mesh (Service Mesh 协议 )
4640
```go
47-
// client side
4841
var opts []client.Option
4942
opts = append(opts, client.WithTransportProtocol(transport.TTHeader))
5043
opts = append(opts, client.WithMetaHandler(transmeta.ClientTTHeaderHandler))
51-
cli, err := xxxservice.NewClient(targetService, opts...)
5244

53-
// server side no need to config transport protocol
54-
var opts []server.Option
55-
opts = append(opts, server.WithMetaHandler(transmeta.ServerTTHeaderHandler))
56-
cli, err := xxxservice.NewServer(handler, opts...)
45+
cli := xxx.NewClient("service_name", opts)
46+
```
47+
4. configure TTHeaderFramed: TTHeader | Framed (Bit OR)
48+
```go
49+
var opts []client.Option
50+
opts = append(opts, client.WithTransportProtocol(transport.TTHeaderFramed))
51+
opts = append(opts, client.WithMetaHandler(transmeta.ClientTTHeaderHandler))
52+
cli := xxx.NewClient("service_name", opts)
5753
```
5854

59-
### gRPC
60-
55+
#### gRPC
56+
client configures gRPC protocol:
6157
```go
62-
// client side
6358
var opts []client.Option
6459
opts = append(opts, client.WithTransportProtocol(transport.GRPC))
6560
opts = append(opts, client.WithMetaHandler(transmeta.ClientHTTP2Handler))
66-
cli, err := xxxservice.NewClient(targetService, opts...)
61+
cli := xxx.NewClient("service_name", client.WithTransportProtocol(transport.GRPC))
62+
```
63+
NOTE: if there's no streaming API in the IDL, this option is needed for enabling gRPC protocol, otherwise kitex will only send protobuf binary (not gRPC).
6764

65+
### Server
66+
Multi-protocol is supported by default. Metahandlers should be configured to enable transparent information transmission.
6867

69-
// server side no need to config transport protocol
68+
#### Thrift (TTHeader)
69+
```go
70+
var opts []server.Option
71+
opts = append(opts, server.WithMetaHandler(transmeta.ServerTTHeaderHandler))
72+
svr, err := xxxservice.NewServer(handler, opts...)
73+
```
74+
#### gRPC
75+
```go
7076
var opts []server.Option
7177
opts = append(opts, server.WithMetaHandler(transmeta.ServerHTTP2Handler))
72-
cli, err := xxxservice.NewServer(handler, opts...)
73-
78+
svr, err := xxxservice.NewServer(handler, opts...)
7479
```
80+

content/zh/docs/kitex/Tutorials/basic-feature/connection_type.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "连接类型"
3-
date: 2021-09-28
3+
date: 2023-10-16
44
weight: 5
55
keywords: ["Kitex", "短连接", "长连接", "连接多路复用"]
66
description: "Kitex 支持短连接、长连接池、连接多路复用以及连接池状态监控。"
@@ -18,7 +18,7 @@ description: "Kitex 支持短连接、长连接池、连接多路复用以及连
1818
xxxCli := xxxservice.NewClient("destServiceName", client.WithShortConnection())
1919
```
2020

21-
## 长连接池
21+
## 长连接池(默认)
2222

2323
Kitex >= v0.0.2 默认配置了连接池,配置参数如下:
2424

@@ -41,25 +41,26 @@ xxxCli := xxxservice.NewClient("destServiceName", client.WithLongConnection(conn
4141

4242
- `MaxIdlePerAddress` 表示每个后端实例可允许的最大闲置连接数
4343
- `MaxIdleGlobal` 表示全局最大闲置连接数
44+
- 从 v0.7.2 开始, 如果未设置 `MaxIdleGlobal`,默认没有限制.
4445
- `MaxIdleTimeout` 表示连接的闲置时长,超过这个时长的连接会被关闭(最小值 3s,默认值 30s )
4546
- `MinIdlePerAddress`(Kitex >= v0.4.3)
4647
- 表示对每个后端实例维护的最小空闲连接数,这部分连接即使空闲时间超过 `MaxIdleTimeout` 也不会被清理。
4748
- 当前版本的`MinIdlePerAddress`的值不能超过5。
4849
### 实现
4950

50-
长连接池的实现方案是每个 address 对应一个连接池,这个连接池是一个由连接构成的 ring,ring 的大小为 MaxIdlePerAddress。
51+
长连接池的实现方案是每个 address 对应一个连接池,池的大小为 `MaxIdlePerAddress`
5152

5253
当选择好目标地址并需要获取一个连接时,按以下步骤处理 :
5354

54-
1. 首先尝试从这个 ring 中获取,如果获取失败没有空闲连接,则发起新的连接建立请求,即连接数量可能会超过 MaxIdlePerAddress
55-
2. 如果从 ring 中获取成功,则检查该连接的空闲时间自上次放入连接池后是否超过了 MaxIdleTimeout如果超过则关闭该连接并新建
55+
1. 首先尝试从连接池中获取,如果获取失败(没有空闲连接),则发起新的连接建立请求,即连接数量可能会超过 `MaxIdlePerAddress`
56+
2. 如果获取成功,则检查该连接的空闲时间(自上次放入连接池后)是否超过了 `MaxIdleTimeout`, 如果超过则关闭该连接并新建
5657
3. 全部成功后返回给上层使用
5758

5859
在连接使用完毕准备归还时,按以下步骤依次处理:
5960

6061
1. 检查连接是否正常,如果不正常则直接关闭
61-
2. 查看空闲连接是否超过全局的 MaxIdleGlobal,如果超过则直接关闭
62-
3. 待归还到的连接池的 ring 中是否还有空闲空间,如果有则直接放入,否则直接关闭
62+
2. 查看空闲连接是否超过全局的 `MaxIdleGlobal`,如果超过则直接关闭
63+
3. 待归还到的连接池是否还有空闲空间,如果有则直接放入,否则直接关闭
6364

6465
### 参数设置建议
6566

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "传输协议"
3-
date: 2022-06-17
3+
date: 2023-10-16
44
weight: 3
55
keywords: ["Kitex", "TTHeader", "HTTP2"]
66
description: Kitex 支持 TTHeader、HTTP2 传输协议。
@@ -14,60 +14,64 @@ description: Kitex 支持 TTHeader、HTTP2 传输协议。
1414
Kitex 目前支持两种传输协议:[TTHeader](../../../reference/transport_protocol_ttheader/)、HTTP2,但实际提供配置的 Transport Protocol 是:TTHeader、GRPC、Framed、TTHeaderFramed、PurePayload。
1515

1616
这里做一些说明:
17-
1817
- 因为 Kitex 对 Protobuf 的支持有 Kitex Protobuf 和 gRPC,为方便区分将 gRPC 作为传输协议的分类,框架会根据是否有配置 gRPC 决定使用哪个协议;
1918
- Framed 严格意义上并不是传输协议,只是标记 Payload Size 额外增加的 4 字节头,但消息协议对是否有 Framed 头并不是强制的,PurePayload 即没有任何头部的,所以将 Framed 也作为传输协议的分类;
2019
- Framed 和 TTHeader 也可以结合使用,所以有 TTHeaderFramed 。
2120

2221
消息协议可选的传输协议组合如下:
2322

24-
* Thrift: **TTHeader**(建议)、Framed、TTHeaderFramed
25-
* KitexProtobuf: **TTHeader**(建议)、Framed、TTHeaderFramed
23+
* Thrift: **TTHeader** (建议)、Framed、TTHeaderFramed
24+
* KitexProtobuf: **TTHeader** (建议)、Framed、TTHeaderFramed
2625
* gRPC: HTTP2
2726

2827
如果想自定义消息协议和传输协议参考:[编解码(协议)扩展](../../framework-exten/codec)
2928

30-
## 配置项
31-
32-
Client 初始化时通过 `WithTransportProtocol` 配置传输协议:
29+
## 配置方式
30+
### Client
31+
#### Thrift
3332

33+
1. 默认 Buffered:PurePayload
34+
2. 指定 Framed:PurePayload前增加 4 个字节(int32)指定 Payload Size
3435
```go
35-
// client option
36-
client.WithTransportProtocol(transport.XXX)
36+
cli := xxx.NewClient("service_name", client.WithTransportProtocol(transport.Framed))
3737
```
38-
39-
Server 支持协议探测(在 Kitex 默认支持的协议内),无需配置传输协议。
40-
41-
## 使用示例
42-
43-
### Thrift + TTHeader
44-
38+
3. 指定TTHeader:
4539
```go
46-
// client side
4740
var opts []client.Option
4841
opts = append(opts, client.WithTransportProtocol(transport.TTHeader))
4942
opts = append(opts, client.WithMetaHandler(transmeta.ClientTTHeaderHandler))
50-
cli, err := xxxservice.NewClient(targetService, opts...)
5143

52-
// server side no need to config transport protocol
53-
var opts []server.Option
54-
opts = append(opts, server.WithMetaHandler(transmeta.ServerTTHeaderHandler))
55-
cli, err := xxxservice.NewServer(handler, opts...)
44+
cli := xxx.NewClient("service_name", opts)
45+
```
46+
4. 指定 TTHeaderFramed:TTHeader | Framed (Bit OR)
47+
```go
48+
var opts []client.Option
49+
opts = append(opts, client.WithTransportProtocol(transport.TTHeaderFramed))
50+
opts = append(opts, client.WithMetaHandler(transmeta.ClientTTHeaderHandler))
51+
cli := xxx.NewClient("service_name", opts)
5652
```
57-
5853

5954
### gRPC
60-
55+
client 指定 gRPC 协议:
6156
```go
62-
// client side
6357
var opts []client.Option
6458
opts = append(opts, client.WithTransportProtocol(transport.GRPC))
6559
opts = append(opts, client.WithMetaHandler(transmeta.ClientHTTP2Handler))
66-
cli, err := xxxservice.NewClient(targetService, opts...)
67-
60+
cli := xxx.NewClient("service_name", client.WithTransportProtocol(transport.GRPC))
61+
```
62+
注意: 如果 IDL 中没有 Streaming API,则需要此选项来启用 gRPC 协议,否则 kitex 将仅发送 protobuf binary(而不是 gRPC)。
6863

69-
// server side no need to config transport protocol
64+
### Server
65+
支持协议探测(在 Kitex 默认支持的协议内),无需配置传输协议。为了支持基于 header 的信息透传,需要配置 metaHandler
66+
#### Thrift (TTHeader)
67+
```go
68+
var opts []server.Option
69+
opts = append(opts, server.WithMetaHandler(transmeta.ServerTTHeaderHandler))
70+
svr, err := xxxservice.NewServer(handler, opts...)
71+
```
72+
#### gRPC
73+
```go
7074
var opts []server.Option
7175
opts = append(opts, server.WithMetaHandler(transmeta.ServerHTTP2Handler))
72-
cli, err := xxxservice.NewServer(handler, opts...)
76+
svr, err := xxxservice.NewServer(handler, opts...)
7377
```

0 commit comments

Comments
 (0)