|
|
@@ -15,11 +15,15 @@
|
|
|
package sdk
|
|
|
|
|
|
import (
|
|
|
+ "context"
|
|
|
"fmt"
|
|
|
+ "net"
|
|
|
"net/http"
|
|
|
+ "runtime"
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
"sync"
|
|
|
+ "time"
|
|
|
|
|
|
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth"
|
|
|
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
|
|
|
@@ -38,6 +42,10 @@ 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 DefaultUserAgent = fmt.Sprintf("AlibabaCloud (%s; %s) Golang/%s Core/%s", runtime.GOOS, runtime.GOARCH, strings.Trim(runtime.Version(), "go"), Version)
|
|
|
|
|
|
var hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) {
|
|
|
return fn
|
|
|
@@ -47,9 +55,12 @@ var hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *
|
|
|
type Client struct {
|
|
|
regionId string
|
|
|
config *Config
|
|
|
+ userAgent map[string]string
|
|
|
signer auth.Signer
|
|
|
httpClient *http.Client
|
|
|
asyncTaskQueue chan func()
|
|
|
+ readTimeout time.Duration
|
|
|
+ connectTimeout time.Duration
|
|
|
|
|
|
debug bool
|
|
|
isRunning bool
|
|
|
@@ -85,6 +96,22 @@ func (client *Client) InitWithOptions(regionId string, config *Config, credentia
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+func (client *Client) SetReadTimeout(readTimeout time.Duration) {
|
|
|
+ client.readTimeout = readTimeout
|
|
|
+}
|
|
|
+
|
|
|
+func (client *Client) SetConnectTimeout(connectTimeout time.Duration) {
|
|
|
+ client.connectTimeout = connectTimeout
|
|
|
+}
|
|
|
+
|
|
|
+func (client *Client) GetReadTimeout() time.Duration {
|
|
|
+ return client.readTimeout
|
|
|
+}
|
|
|
+
|
|
|
+func (client *Client) GetConnectTimeout() time.Duration {
|
|
|
+ return client.connectTimeout
|
|
|
+}
|
|
|
+
|
|
|
// EnableAsync enable the async task queue
|
|
|
func (client *Client) EnableAsync(routinePoolSize, maxTaskQueueSize int) {
|
|
|
client.asyncTaskQueue = make(chan func(), maxTaskQueueSize)
|
|
|
@@ -132,6 +159,18 @@ func (client *Client) InitWithRamRoleArn(regionId, accessKeyId, accessKeySecret,
|
|
|
return client.InitWithOptions(regionId, config, credential)
|
|
|
}
|
|
|
|
|
|
+func (client *Client) InitWithRamRoleArnAndPolicy(regionId, accessKeyId, accessKeySecret, roleArn, roleSessionName, policy string) (err error) {
|
|
|
+ config := client.InitClientConfig()
|
|
|
+ credential := &credentials.RamRoleArnCredential{
|
|
|
+ AccessKeyId: accessKeyId,
|
|
|
+ AccessKeySecret: accessKeySecret,
|
|
|
+ RoleArn: roleArn,
|
|
|
+ RoleSessionName: roleSessionName,
|
|
|
+ Policy: policy,
|
|
|
+ }
|
|
|
+ return client.InitWithOptions(regionId, config, credential)
|
|
|
+}
|
|
|
+
|
|
|
func (client *Client) InitWithRsaKeyPair(regionId, publicKeyId, privateKey string, sessionExpiration int) (err error) {
|
|
|
config := client.InitClientConfig()
|
|
|
credential := &credentials.RsaKeyPairCredential{
|
|
|
@@ -202,22 +241,113 @@ func (client *Client) buildRequestWithSigner(request requests.AcsRequest, signer
|
|
|
finalSigner = client.signer
|
|
|
}
|
|
|
httpRequest, err = buildHttpRequest(request, finalSigner, regionId)
|
|
|
- if client.config.UserAgent != "" {
|
|
|
- httpRequest.Header.Set("User-Agent", client.config.UserAgent)
|
|
|
+ if err == nil {
|
|
|
+ userAgent := DefaultUserAgent + getSendUserAgent(client.config.UserAgent, client.userAgent, request.GetUserAgent())
|
|
|
+ httpRequest.Header.Set("User-Agent", userAgent)
|
|
|
}
|
|
|
+
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+func getSendUserAgent(configUserAgent string, clientUserAgent, requestUserAgent map[string]string) string {
|
|
|
+ realUserAgent := ""
|
|
|
+ for key1, value1 := range clientUserAgent {
|
|
|
+ for key2, _ := range requestUserAgent {
|
|
|
+ if key1 == key2 {
|
|
|
+ key1 = ""
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if key1 != "" {
|
|
|
+ realUserAgent += fmt.Sprintf(" %s/%s", key1, value1)
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for key, value := range requestUserAgent {
|
|
|
+ realUserAgent += fmt.Sprintf(" %s/%s", key, value)
|
|
|
+ }
|
|
|
+ if configUserAgent != "" {
|
|
|
+ return realUserAgent + fmt.Sprintf(" Extra/%s", configUserAgent)
|
|
|
+ }
|
|
|
+ return realUserAgent
|
|
|
+}
|
|
|
+
|
|
|
+func (client *Client) AppendUserAgent(key, value string) {
|
|
|
+ newkey := true
|
|
|
+
|
|
|
+ if client.userAgent == nil {
|
|
|
+ client.userAgent = make(map[string]string)
|
|
|
+ }
|
|
|
+ if strings.ToLower(key) != "core" && strings.ToLower(key) != "go" {
|
|
|
+ for tag, _ := range client.userAgent {
|
|
|
+ if tag == key {
|
|
|
+ client.userAgent[tag] = value
|
|
|
+ newkey = false
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if newkey {
|
|
|
+ client.userAgent[key] = value
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
func (client *Client) BuildRequestWithSigner(request requests.AcsRequest, signer auth.Signer) (err error) {
|
|
|
_, err = client.buildRequestWithSigner(request, signer)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+func (client *Client) getTimeout(request requests.AcsRequest) (time.Duration, time.Duration) {
|
|
|
+ readTimeout := defaultReadTimeout
|
|
|
+ connectTimeout := defaultConnectTimeout
|
|
|
+
|
|
|
+ reqReadTimeout := request.GetReadTimeout()
|
|
|
+ reqConnectTimeout := request.GetConnectTimeout()
|
|
|
+ if reqReadTimeout != 0*time.Millisecond {
|
|
|
+ readTimeout = reqReadTimeout
|
|
|
+ } else if client.readTimeout != 0*time.Millisecond {
|
|
|
+ readTimeout = client.readTimeout
|
|
|
+ }
|
|
|
+
|
|
|
+ if reqConnectTimeout != 0*time.Millisecond {
|
|
|
+ connectTimeout = reqConnectTimeout
|
|
|
+ } else if client.connectTimeout != 0*time.Millisecond {
|
|
|
+ connectTimeout = client.connectTimeout
|
|
|
+ }
|
|
|
+ return readTimeout, connectTimeout
|
|
|
+}
|
|
|
+
|
|
|
+func Timeout(connectTimeout, readTimeout time.Duration) func(cxt context.Context, net, addr string) (c net.Conn, err error) {
|
|
|
+ return func(ctx context.Context, network, address string) (net.Conn, error) {
|
|
|
+ conn, err := (&net.Dialer{
|
|
|
+ Timeout: connectTimeout,
|
|
|
+ KeepAlive: 0 * time.Second,
|
|
|
+ DualStack: true,
|
|
|
+ }).DialContext(ctx, network, address)
|
|
|
+
|
|
|
+ if err == nil {
|
|
|
+ conn.SetDeadline(time.Now().Add(readTimeout))
|
|
|
+ }
|
|
|
+
|
|
|
+ return conn, err
|
|
|
+ }
|
|
|
+}
|
|
|
+func (client *Client) setTimeout(request requests.AcsRequest) {
|
|
|
+ readTimeout, connectTimeout := client.getTimeout(request)
|
|
|
+ if trans, ok := client.httpClient.Transport.(*http.Transport); ok && trans != nil {
|
|
|
+ trans.DialContext = Timeout(connectTimeout, readTimeout)
|
|
|
+ client.httpClient.Transport = trans
|
|
|
+ } else {
|
|
|
+ client.httpClient.Transport = &http.Transport{
|
|
|
+ DialContext: Timeout(connectTimeout, readTimeout),
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
func (client *Client) DoActionWithSigner(request requests.AcsRequest, response responses.AcsResponse, signer auth.Signer) (err error) {
|
|
|
httpRequest, err := client.buildRequestWithSigner(request, signer)
|
|
|
if err != nil {
|
|
|
return
|
|
|
}
|
|
|
+ client.setTimeout(request)
|
|
|
+
|
|
|
var httpResponse *http.Response
|
|
|
for retryTimes := 0; retryTimes <= client.config.MaxRetryTime; retryTimes++ {
|
|
|
debug("> %s %s %s", httpRequest.Method, httpRequest.URL.RequestURI(), httpRequest.Proto)
|
|
|
@@ -240,13 +370,19 @@ func (client *Client) DoActionWithSigner(request requests.AcsRequest, response r
|
|
|
return
|
|
|
} else if retryTimes >= client.config.MaxRetryTime {
|
|
|
// timeout but reached the max retry times, return
|
|
|
- timeoutErrorMsg := fmt.Sprintf(errors.TimeoutErrorMessage, strconv.Itoa(retryTimes+1), strconv.Itoa(retryTimes+1))
|
|
|
+ var timeoutErrorMsg string
|
|
|
+ if strings.Contains(err.Error(), "read tcp") {
|
|
|
+ timeoutErrorMsg = fmt.Sprintf(errors.TimeoutErrorMessage, strconv.Itoa(retryTimes+1), strconv.Itoa(retryTimes+1)) + " Read timeout. Please set a valid ReadTimeout."
|
|
|
+ } else {
|
|
|
+ timeoutErrorMsg = fmt.Sprintf(errors.TimeoutErrorMessage, strconv.Itoa(retryTimes+1), strconv.Itoa(retryTimes+1)) + " Connect timeout. Please set a valid ConnectTimeout."
|
|
|
+ }
|
|
|
err = errors.NewClientError(errors.TimeoutErrorCode, timeoutErrorMsg, err)
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
// if status code >= 500 or timeout, will trigger retry
|
|
|
if client.config.AutoRetry && (err != nil || isServerError(httpResponse)) {
|
|
|
+ client.setTimeout(request)
|
|
|
// rewrite signatureNonce and signature
|
|
|
httpRequest, err = client.buildRequestWithSigner(request, signer)
|
|
|
// buildHttpRequest(request, finalSigner, regionId)
|
|
|
@@ -257,6 +393,7 @@ func (client *Client) DoActionWithSigner(request requests.AcsRequest, response r
|
|
|
}
|
|
|
break
|
|
|
}
|
|
|
+
|
|
|
err = responses.Unmarshal(response, httpResponse, request.GetAcceptFormat())
|
|
|
// wrap server errors
|
|
|
if serverErr, ok := err.(*errors.ServerError); ok {
|
|
|
@@ -345,6 +482,12 @@ func NewClientWithRamRoleArn(regionId string, accessKeyId, accessKeySecret, role
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+func NewClientWithRamRoleArnAndPolicy(regionId string, accessKeyId, accessKeySecret, roleArn, roleSessionName, policy string) (client *Client, err error) {
|
|
|
+ client = &Client{}
|
|
|
+ err = client.InitWithRamRoleArnAndPolicy(regionId, accessKeyId, accessKeySecret, roleArn, roleSessionName, policy)
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
func NewClientWithEcsRamRole(regionId string, roleName string) (client *Client, err error) {
|
|
|
client = &Client{}
|
|
|
err = client.InitWithEcsRamRole(regionId, roleName)
|