You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
2.9 KiB

  1. package sdk
  2. import (
  3. "context"
  4. "fmt"
  5. "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/auth"
  6. "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/request"
  7. "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/response"
  8. "net"
  9. "net/http"
  10. "runtime"
  11. "strings"
  12. "time"
  13. )
  14. var Version = "0.0.1"
  15. var defaultConnectTimeout = 5 * time.Second
  16. var defaultReadTimeout = 10 * time.Second
  17. var DefaultUserAgent = fmt.Sprintf("GaoreGoSdk (%s;%s) Golang/%s Core/%s", runtime.GOOS, runtime.GOARCH, strings.Trim(runtime.Version(), "go"), Version)
  18. type Client struct {
  19. Host string
  20. httpClient *http.Client
  21. isInsecure bool
  22. signer *auth.Creditial
  23. readTimeout time.Duration
  24. connectTimeout time.Duration
  25. config *Config
  26. }
  27. func (client *Client) InitWithOptions(host string, config *Config, creditial auth.Creditial) (err error) {
  28. client.httpClient = &http.Client{}
  29. if config.Transport != nil {
  30. client.httpClient.Transport = config.Transport
  31. } else if config.HttpTransport != nil {
  32. client.httpClient.Transport = config.HttpTransport
  33. }
  34. if config.Timeout > 0 {
  35. client.httpClient.Timeout = config.Timeout
  36. }
  37. return nil
  38. }
  39. func (client *Client) InitWithConfig() (config *Config) {
  40. if client.config != nil {
  41. return client.config
  42. } else {
  43. return NewConfig()
  44. }
  45. }
  46. func Timeout(connectTimeout time.Duration) func(ctx context.Context, net, addr string) (c net.Conn, err error) {
  47. return func(ctx context.Context, network, address string) (c net.Conn, err error) {
  48. return (&net.Dialer{
  49. Timeout: connectTimeout,
  50. DualStack: true,
  51. }).DialContext(ctx, network, address)
  52. }
  53. }
  54. func (client *Client) setTimeOut(request request.AcsRequest) {
  55. readTimeout, connectTimeout := client.getTimeOut(request)
  56. client.httpClient.Timeout = readTimeout
  57. if trans, ok := client.httpClient.Transport.(*http.Transport); ok && trans != nil {
  58. trans.DialContext = Timeout(connectTimeout)
  59. client.httpClient.Transport = trans
  60. } else if client.httpClient.Transport == nil {
  61. client.httpClient.Transport = &http.Transport{
  62. DialContext: Timeout(connectTimeout),
  63. }
  64. }
  65. }
  66. func (client *Client) getTimeOut(request request.AcsRequest) (time.Duration, time.Duration) {
  67. readTimeOut := defaultReadTimeout
  68. connectTimeOut := defaultConnectTimeout
  69. reqReadTimeout := request.GetReadTimeout()
  70. reqConnectTimeout := request.GetConnectTimeout()
  71. if reqReadTimeout != 0*time.Millisecond {
  72. readTimeOut = reqReadTimeout
  73. } else if client.readTimeout != 0*time.Microsecond {
  74. readTimeOut = client.readTimeout
  75. } else if client.httpClient.Timeout != 0 {
  76. readTimeOut = client.httpClient.Timeout
  77. }
  78. if reqConnectTimeout != 0*time.Microsecond {
  79. connectTimeOut = reqConnectTimeout
  80. } else if client.connectTimeout != 0*time.Millisecond {
  81. connectTimeOut = client.connectTimeout
  82. }
  83. return readTimeOut, connectTimeOut
  84. }
  85. func (client *Client) DoAction(request request.AcsRequest, response response.AcsResponse) (err error) {
  86. return nil
  87. }