package sdk import ( "context" "fmt" "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/auth" "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/request" "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/response" "net" "net/http" "runtime" "strings" "time" ) var Version = "0.0.1" var defaultConnectTimeout = 5 * time.Second var defaultReadTimeout = 10 * time.Second var DefaultUserAgent = fmt.Sprintf("GaoreGoSdk (%s;%s) Golang/%s Core/%s", runtime.GOOS, runtime.GOARCH, strings.Trim(runtime.Version(), "go"), Version) type Client struct { Host string httpClient *http.Client isInsecure bool signer *auth.Creditial readTimeout time.Duration connectTimeout time.Duration config *Config } func (client *Client) InitWithOptions(host string, config *Config, creditial auth.Creditial) (err error) { client.httpClient = &http.Client{} if config.Transport != nil { client.httpClient.Transport = config.Transport } else if config.HttpTransport != nil { client.httpClient.Transport = config.HttpTransport } if config.Timeout > 0 { client.httpClient.Timeout = config.Timeout } return nil } func (client *Client) InitWithConfig() (config *Config) { if client.config != nil { return client.config } else { return NewConfig() } } func Timeout(connectTimeout time.Duration) func(ctx context.Context, net, addr string) (c net.Conn, err error) { return func(ctx context.Context, network, address string) (c net.Conn, err error) { return (&net.Dialer{ Timeout: connectTimeout, DualStack: true, }).DialContext(ctx, network, address) } } func (client *Client) setTimeOut(request request.AcsRequest) { readTimeout, connectTimeout := client.getTimeOut(request) client.httpClient.Timeout = readTimeout if trans, ok := client.httpClient.Transport.(*http.Transport); ok && trans != nil { trans.DialContext = Timeout(connectTimeout) client.httpClient.Transport = trans } else if client.httpClient.Transport == nil { client.httpClient.Transport = &http.Transport{ DialContext: Timeout(connectTimeout), } } } func (client *Client) getTimeOut(request request.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.Microsecond { readTimeOut = client.readTimeout } else if client.httpClient.Timeout != 0 { readTimeOut = client.httpClient.Timeout } if reqConnectTimeout != 0*time.Microsecond { connectTimeOut = reqConnectTimeout } else if client.connectTimeout != 0*time.Millisecond { connectTimeOut = client.connectTimeout } return readTimeOut, connectTimeOut } func (client *Client) DoAction(request request.AcsRequest, response response.AcsResponse) (err error) { return nil }