Selaa lähdekoodia

add timeout description to readme

wenzuochao 6 vuotta sitten
vanhempi
commit
9cb0020cf4
3 muutettua tiedostoa jossa 42 lisäystä ja 4 poistoa
  1. 20 1
      README.md
  2. 20 1
      README_zh.md
  3. 2 2
      sdk/client.go

+ 20 - 1
README.md

@@ -97,7 +97,7 @@ When you create an instance of client, you need to fill out three parameters: `R
 
 If the request has occured an error, you can view the HTTP request process by adding the environment variable `DEBUG=sdk`.
 
-## Keepalive
+## Keep-alive
 Alibaba Cloud Go SDK uses primordial `net/http` of Go language to send and accept requests,so it's  configuration is the same as `net/http`'s,you can use config to deliver configuration to the bottomed httpClient.
 
 ```go
@@ -115,6 +115,25 @@ ecsClient, err := ecs.NewClientWithOptions(config)
 * Due to the concurrency nature of the Go language, we recommend that you control the concurrent requests for the SDK at the application level.
 * In order to facilitate your use, we also provide a direct use of concurrent invocation mode, the relevant concurrency control by the SDK internal implementation.
 
+### Timeout
+
+```go
+// Request Timeout has a higher priority than client Timeout.
+// If you don't set any timeout, the default ReadTimeout is 10 second, and the default ConnectTimeout is 5 second.
+
+// Set request Timeout(Only the request is effected.)
+request.SetReadTimeout(10 * time.Second)              // Set request ReadTimeout to 10 second.
+readTimeout := request.GetReadTimeout()              // Get request ReadTimeout.
+request.SetConnectTimeout(5 * time.Second)           // Set request ConnectTimeout to 5 second.
+connectTimeout := request.GetConnectTimeout()        // Get request ConnectTimeout.
+
+// Set client Timeout(For all requests which is sent by the client.)
+client.SetReadTimeout(10 * time.Second)              // Set client ReadTimeout to 10 second.
+readTimeout := client.GetReadTimeout()              // Get client ReadTimeout.
+client.SetConnectTimeout(5 * time.Second)           // Set client ConnectTimeout to 5 second.
+connectTimeout := client.GetConnectTimeout()        // Get client ConnectTimeout.
+```
+
 ### Open SDK Client's concurrent function.
 
 ```go

+ 20 - 1
README_zh.md

@@ -87,7 +87,7 @@ func main() {
 
 如果您发送的请求出错,您可以通过添加环境变量 `DEBUG=sdk` 来查看 HTTP 请求过程。
 
-## Keepalive
+## Keep-alive
 阿里云 Go SDK 底层使用 Go 语言原生的 `net/http` 收发请求,因此配置方式与 `net/http`相同,您可以通过 config 直接将配置传递给底层的 httpClient
 
 ```go
@@ -105,6 +105,25 @@ ecsClient, err := ecs.NewClientWithOptions(config)
 * 因 Go 语言的并发特性,我们建议您在应用层面控制 SDK 的并发请求。
 * 为了方便您的使用,我们也提供了可直接使用的并发调用方式,相关的并发控制由 SDK 内部实现。
 
+### 超时机制
+
+```go
+// 请求设置的超时优先级高于客户端设置的超时.
+// 当您未设置任何超时时,则默认读超时为10秒,连接超时为5秒
+
+// 设置请求超时(仅对当前请求有效)
+request.SetReadTimeout(10 * time.Second)              // 设置请求读超时为10秒
+readTimeout := request.GetReadTimeout()              // 获取请求读超时
+request.SetConnectTimeout(5 * time.Second)           // 设置请求连接超时为5秒
+connectTimeout := request.GetConnectTimeout()        // 获取请求连接超时
+
+// 设置客户端超时(对所有通过该客户端发送的请求生效)
+client.SetReadTimeout(10 * time.Second)              // 设置客户端读超时为10秒
+readTimeout := client.GetReadTimeout()              // 获取客户端读超时
+client.SetConnectTimeout(5 * time.Second)           // 设置客户端连接超时为5秒
+connectTimeout := client.GetConnectTimeout()        // 获取客户端连接超时
+```
+
 ### 开启 SDK Client 的并发功能
 
 ```go

+ 2 - 2
sdk/client.go

@@ -42,8 +42,8 @@ func init() {
 
 // Version this value will be replaced while build: -ldflags="-X sdk.version=x.x.x"
 var Version = "0.0.1"
-var defaultConnectTimeout = 10 * time.Second
-var defaultReadTimeout = 5 * time.Second
+var defaultConnectTimeout = 5 * time.Second
+var defaultReadTimeout = 10 * time.Second
 
 var DefaultUserAgent = fmt.Sprintf("AlibabaCloud (%s; %s) Golang/%s Core/%s", runtime.GOOS, runtime.GOARCH, strings.Trim(runtime.Version(), "go"), Version)