Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: packet size limit #188

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ import "crypto/tls"

4. 实例角色

有关实例角色的相关概念请参阅:[腾讯云实例角色](https://cloud.tencent.com/document/product/213/47668)
有关实例角色的相关概念请参阅:[腾讯云实例角色](https://cloud.tencent.com/document/product/213/47668)

在您为实例绑定角色后,您可以在实例中访问相关元数据接口获取临时凭证。相关代码如下:

Expand Down Expand Up @@ -481,6 +481,25 @@ func (c *Client) DescribeInstances(request *DescribeInstancesRequest) (response

详细使用请参阅示例:[使用 Common Client 进行调用](https://github.com/TencentCloud/tencentcloud-sdk-go/blob/master/examples/common/common_client.go)

## 自定义 Header

[RunInstancesRequest示例](examples/cvm/v20170312/run_instances.go)
```go
request := cvm.NewRunInstancesRequest()
request.SetHeader(map[string]string{
"X-TC-TraceId": "ffe0c072-8a5d-4e17-8887-a8a60252abca",
})
```

[CommonRequest示例](examples/common/common_client.go)

```go
request := tchttp.NewCommonRequest("cvm", "2017-03-12", "DescribeZones")
request.SetHeader(map[string]string{
"X-TC-TraceId": "ffe0c072-8a5d-4e17-8887-a8a60252abca",
})
```

# 请求重试

## 网络错误重试
Expand Down
5 changes: 5 additions & 0 deletions examples/common/common_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ func main() {
// // 3. []byte
// bodyBytes := []byte(bodyStr)

// set custom headers
request.SetHeader(map[string]string{
"X-TC-TraceId": "ffe0c072-8a5d-4e17-8887-a8a60252abca",
})

// 设置action所需的请求数据
err := request.SetActionParameters(body)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions examples/cvm/v20170312/run_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ func main() {
VpcId: common.StringPtr("vpc-8ek64x3d"),
}

// set custom headers
request.SetHeader(map[string]string{
"X-TC-TraceId": "ffe0c072-8a5d-4e17-8887-a8a60252abca",
})

// get response structure
response, err := client.RunInstances(request)
// API errors
Expand Down
66 changes: 64 additions & 2 deletions tencentcloud/common/client.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package common

import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"math"
"net/http"
"net/http/httputil"
"strconv"
Expand Down Expand Up @@ -53,6 +56,10 @@ func (c *Client) Send(request tchttp.Request, response tchttp.Response) (err err
request.SetHttpMethod(c.httpProfile.ReqMethod)
}

if request.GetPacketSizeLimit() == 0 {
request.SetPacketSizeLimit(math.MaxInt64)
}

tchttp.CompleteCommonParams(request, c.GetRegion())

// reflect to inject client if field ClientToken exists and retry feature is enabled
Expand Down Expand Up @@ -106,13 +113,33 @@ func (c *Client) sendWithSignature(request tchttp.Request, response tchttp.Respo
}

func (c *Client) sendWithoutSignature(request tchttp.Request, response tchttp.Response) error {
httpRequest, err := http.NewRequestWithContext(request.GetContext(), request.GetHttpMethod(), request.GetUrl(), request.GetBodyReader())
err := tchttp.ConstructParams(request)
if err != nil {
return err
}

httpUrl := request.GetUrl()
httpBody, err := ioutil.ReadAll(request.GetBodyReader())
if err != nil {
return err
}
sizeLimit := request.GetPacketSizeLimit()
httpMethod := request.GetHttpMethod()
err = checkRequestSize(httpMethod, httpUrl, "", sizeLimit, httpBody)
if err != nil {
return err
}

httpRequest, err := http.NewRequestWithContext(request.GetContext(), httpMethod, httpUrl, bytes.NewReader(httpBody))
if err != nil {
return err
}
if request.GetHttpMethod() == "POST" {
httpRequest.Header.Set("Content-Type", "application/x-www-form-urlencoded")
}
for k, v := range request.GetHeader() {
httpRequest.Header.Set(k, v)
}
httpResponse, err := c.sendWithRateLimitRetry(httpRequest, isRetryable(request))
if err != nil {
return err
Expand All @@ -132,13 +159,32 @@ func (c *Client) sendWithSignatureV1(request tchttp.Request, response tchttp.Res
if err != nil {
return err
}
httpRequest, err := http.NewRequestWithContext(request.GetContext(), request.GetHttpMethod(), request.GetUrl(), request.GetBodyReader())

httpUrl := request.GetUrl()
httpBody, err := ioutil.ReadAll(request.GetBodyReader())
if err != nil {
return err
}
sizeLimit := request.GetPacketSizeLimit()
httpMethod := request.GetHttpMethod()
signMethod := c.signMethod
err = checkRequestSize(httpMethod, httpUrl, signMethod, sizeLimit, httpBody)
if err != nil {
return err
}

httpRequest, err := http.NewRequestWithContext(request.GetContext(), httpMethod, httpUrl, bytes.NewReader(httpBody))
if err != nil {
return err
}
if request.GetHttpMethod() == "POST" {
httpRequest.Header.Set("Content-Type", "application/x-www-form-urlencoded")
}

for k, v := range request.GetHeader() {
httpRequest.Header.Set(k, v)
}

httpResponse, err := c.sendWithRateLimitRetry(httpRequest, isRetryable(request))
if err != nil {
return err
Expand Down Expand Up @@ -183,6 +229,16 @@ func (c *Client) sendWithSignatureV3(request tchttp.Request, response tchttp.Res
}
}

for k, v := range request.GetHeader() {
switch k {
case "X-TC-Action", "X-TC-Version", "X-TC-Timestamp", "X-TC-RequestClient",
"X-TC-Language", "Content-Type", "X-TC-Region", "X-TC-Token":
log.Printf("Skip header \"%s\": can not specify built-in header", k)
default:
headers[k] = v
}
}

if !isOctetStream && request.GetContentType() == octetStream {
isOctetStream = true
b, _ := json.Marshal(request)
Expand Down Expand Up @@ -287,6 +343,12 @@ func (c *Client) sendWithSignatureV3(request tchttp.Request, response tchttp.Res
if canonicalQueryString != "" {
url = url + "?" + canonicalQueryString
}

err = checkRequestSize(httpRequestMethod, url, algorithm, request.GetPacketSizeLimit(), []byte(requestPayload))
if err != nil {
return err
}

httpRequest, err := http.NewRequestWithContext(request.GetContext(), httpRequestMethod, url, strings.NewReader(requestPayload))
if err != nil {
return err
Expand Down
Loading