client.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * Licensed under the Apache License, Version 2.0 (the "License");
  3. * you may not use this file except in compliance with the License.
  4. * You may obtain a copy of the License at
  5. *
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. */
  14. package sdk
  15. import (
  16. "fmt"
  17. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth"
  18. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
  19. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/endpoints"
  20. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/errors"
  21. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
  22. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/responses"
  23. "net"
  24. "net/http"
  25. "strconv"
  26. "sync"
  27. )
  28. // this value will be replaced while build: -ldflags="-X sdk.version=x.x.x"
  29. var Version = "0.0.1"
  30. type Client struct {
  31. regionId string
  32. config *Config
  33. signer auth.Signer
  34. httpClient *http.Client
  35. asyncTaskQueue chan func()
  36. debug bool
  37. isRunning bool
  38. // void "panic(write to close channel)" cause of addAsync() after Shutdown()
  39. asyncChanLock *sync.RWMutex
  40. }
  41. func (client *Client) Init() (err error) {
  42. panic("not support yet")
  43. }
  44. func (client *Client) InitWithOptions(regionId string, config *Config, credential auth.Credential) (err error) {
  45. client.isRunning = true
  46. client.asyncChanLock = new(sync.RWMutex)
  47. client.regionId = regionId
  48. client.config = config
  49. if err != nil {
  50. return
  51. }
  52. client.httpClient = &http.Client{}
  53. if config.HttpTransport != nil {
  54. client.httpClient.Transport = config.HttpTransport
  55. }
  56. if config.Timeout > 0 {
  57. client.httpClient.Timeout = config.Timeout
  58. }
  59. if config.EnableAsync {
  60. client.EnableAsync(config.GoRoutinePoolSize, config.MaxTaskQueueSize)
  61. }
  62. client.signer, err = auth.NewSignerWithCredential(credential, client.ProcessCommonRequestWithSigner)
  63. return
  64. }
  65. func (client *Client) EnableAsync(routinePoolSize, maxTaskQueueSize int) {
  66. client.asyncTaskQueue = make(chan func(), maxTaskQueueSize)
  67. for i := 0; i < routinePoolSize; i++ {
  68. go func() {
  69. for client.isRunning {
  70. select {
  71. case task, notClosed := <-client.asyncTaskQueue:
  72. if notClosed {
  73. task()
  74. }
  75. }
  76. }
  77. }()
  78. }
  79. }
  80. func (client *Client) InitWithAccessKey(regionId, accessKeyId, accessKeySecret string) (err error) {
  81. config := client.InitClientConfig()
  82. credential := &credentials.BaseCredential{
  83. AccessKeyId: accessKeyId,
  84. AccessKeySecret: accessKeySecret,
  85. }
  86. return client.InitWithOptions(regionId, config, credential)
  87. }
  88. func (client *Client) InitWithStsToken(regionId, accessKeyId, accessKeySecret, securityToken string) (err error) {
  89. config := client.InitClientConfig()
  90. credential := &credentials.StsTokenCredential{
  91. AccessKeyId: accessKeyId,
  92. AccessKeySecret: accessKeySecret,
  93. AccessKeyStsToken: securityToken,
  94. }
  95. return client.InitWithOptions(regionId, config, credential)
  96. }
  97. func (client *Client) InitWithRamRoleArn(regionId, accessKeyId, accessKeySecret, roleArn, roleSessionName string) (err error) {
  98. config := client.InitClientConfig()
  99. credential := &credentials.RamRoleArnCredential{
  100. AccessKeyId: accessKeyId,
  101. AccessKeySecret: accessKeySecret,
  102. RoleArn: roleArn,
  103. RoleSessionName: roleSessionName,
  104. }
  105. return client.InitWithOptions(regionId, config, credential)
  106. }
  107. func (client *Client) InitWithRsaKeyPair(regionId, publicKeyId, privateKey string, sessionExpiration int) (err error) {
  108. config := client.InitClientConfig()
  109. credential := &credentials.RsaKeyPairCredential{
  110. PrivateKey: privateKey,
  111. PublicKeyId: publicKeyId,
  112. SessionExpiration: sessionExpiration,
  113. }
  114. return client.InitWithOptions(regionId, config, credential)
  115. }
  116. func (client *Client) InitWithEcsRamRole(regionId, roleName string) (err error) {
  117. config := client.InitClientConfig()
  118. credential := &credentials.EcsRamRoleCredential{
  119. RoleName: roleName,
  120. }
  121. return client.InitWithOptions(regionId, config, credential)
  122. }
  123. func (client *Client) InitClientConfig() (config *Config) {
  124. if client.config != nil {
  125. return client.config
  126. } else {
  127. return NewConfig()
  128. }
  129. }
  130. func (client *Client) DoAction(request requests.AcsRequest, response responses.AcsResponse) (err error) {
  131. return client.DoActionWithSigner(request, response, nil)
  132. }
  133. func (client *Client) DoActionWithSigner(request requests.AcsRequest, response responses.AcsResponse, signer auth.Signer) (err error) {
  134. // add clientVersion
  135. request.GetHeaders()["x-sdk-core-version"] = Version
  136. regionId := client.regionId
  137. if len(request.GetRegionId()) > 0 {
  138. regionId = request.GetRegionId()
  139. }
  140. // resolve endpoint
  141. resolveParam := &endpoints.ResolveParam{
  142. Domain: request.GetDomain(),
  143. Product: request.GetProduct(),
  144. RegionId: regionId,
  145. LocationProduct: request.GetLocationServiceCode(),
  146. LocationEndpointType: request.GetLocationEndpointType(),
  147. CommonApi: client.ProcessCommonRequest,
  148. }
  149. endpoint, err := endpoints.Resolve(resolveParam)
  150. if err != nil {
  151. return
  152. }
  153. request.SetDomain(endpoint)
  154. // init request params
  155. err = requests.InitParams(request)
  156. if err != nil {
  157. return
  158. }
  159. // signature
  160. var finalSigner auth.Signer
  161. if signer != nil {
  162. finalSigner = signer
  163. } else {
  164. finalSigner = client.signer
  165. }
  166. httpRequest, err := buildHttpRequest(request, finalSigner, regionId)
  167. if client.config.UserAgent != "" {
  168. httpRequest.Header.Set("User-Agent", client.config.UserAgent)
  169. }
  170. if err != nil {
  171. return
  172. }
  173. var httpResponse *http.Response
  174. for retryTimes := 0; retryTimes <= client.config.MaxRetryTime; retryTimes++ {
  175. httpResponse, err = client.httpClient.Do(httpRequest)
  176. var timeout bool
  177. // receive error
  178. if err != nil {
  179. if timeout = isTimeout(err); !timeout {
  180. // if not timeout error, return
  181. return
  182. } else if retryTimes >= client.config.MaxRetryTime {
  183. // timeout but reached the max retry times, return
  184. timeoutErrorMsg := fmt.Sprintf(errors.TimeoutErrorMessage, strconv.Itoa(retryTimes+1), strconv.Itoa(retryTimes+1))
  185. err = errors.NewClientError(errors.TimeoutErrorCode, timeoutErrorMsg, err)
  186. return
  187. }
  188. }
  189. // if status code >= 500 or timeout, will trigger retry
  190. if client.config.AutoRetry && (timeout || isServerError(httpResponse)) {
  191. // rewrite signatureNonce and signature
  192. httpRequest, err = buildHttpRequest(request, finalSigner, regionId)
  193. if err != nil {
  194. return
  195. }
  196. continue
  197. }
  198. break
  199. }
  200. err = responses.Unmarshal(response, httpResponse, request.GetAcceptFormat())
  201. // wrap server errors
  202. if serverErr, ok := err.(*errors.ServerError); ok {
  203. var wrapInfo = map[string]string{}
  204. wrapInfo["StringToSign"] = request.GetStringToSign()
  205. err = errors.WrapServerError(serverErr, wrapInfo)
  206. }
  207. return
  208. }
  209. func buildHttpRequest(request requests.AcsRequest, singer auth.Signer, regionId string) (httpRequest *http.Request, err error) {
  210. err = auth.Sign(request, singer, regionId)
  211. if err != nil {
  212. return
  213. }
  214. requestMethod := request.GetMethod()
  215. requestUrl := request.BuildUrl()
  216. body := request.GetBodyReader()
  217. httpRequest, err = http.NewRequest(requestMethod, requestUrl, body)
  218. if err != nil {
  219. return
  220. }
  221. for key, value := range request.GetHeaders() {
  222. httpRequest.Header[key] = []string{value}
  223. }
  224. // host is a special case
  225. if host, containsHost := request.GetHeaders()["Host"]; containsHost {
  226. httpRequest.Host = host
  227. }
  228. return
  229. }
  230. func isTimeout(err error) bool {
  231. if err == nil {
  232. return false
  233. }
  234. netErr, isNetError := err.(net.Error)
  235. return isNetError && netErr.Timeout()
  236. }
  237. func isServerError(httpResponse *http.Response) bool {
  238. return httpResponse.StatusCode >= http.StatusInternalServerError
  239. }
  240. /**
  241. only block when any one of the following occurs:
  242. 1. the asyncTaskQueue is full, increase the queue size to avoid this
  243. 2. Shutdown() in progressing, the client is being closed
  244. **/
  245. func (client *Client) AddAsyncTask(task func()) (err error) {
  246. if client.asyncTaskQueue != nil {
  247. client.asyncChanLock.RLock()
  248. defer client.asyncChanLock.RUnlock()
  249. if client.isRunning {
  250. client.asyncTaskQueue <- task
  251. }
  252. } else {
  253. err = errors.NewClientError(errors.AsyncFunctionNotEnabledCode, errors.AsyncFunctionNotEnabledMessage, nil)
  254. }
  255. return
  256. }
  257. func NewClient() (client *Client, err error) {
  258. client = &Client{}
  259. err = client.Init()
  260. return
  261. }
  262. func NewClientWithOptions(regionId string, config *Config, credential auth.Credential) (client *Client, err error) {
  263. client = &Client{}
  264. err = client.InitWithOptions(regionId, config, credential)
  265. return
  266. }
  267. func NewClientWithAccessKey(regionId, accessKeyId, accessKeySecret string) (client *Client, err error) {
  268. client = &Client{}
  269. err = client.InitWithAccessKey(regionId, accessKeyId, accessKeySecret)
  270. return
  271. }
  272. func NewClientWithStsToken(regionId, stsAccessKeyId, stsAccessKeySecret, stsToken string) (client *Client, err error) {
  273. client = &Client{}
  274. err = client.InitWithStsToken(regionId, stsAccessKeyId, stsAccessKeySecret, stsToken)
  275. return
  276. }
  277. func NewClientWithRamRoleArn(regionId string, accessKeyId, accessKeySecret, roleArn, roleSessionName string) (client *Client, err error) {
  278. client = &Client{}
  279. err = client.InitWithRamRoleArn(regionId, accessKeyId, accessKeySecret, roleArn, roleSessionName)
  280. return
  281. }
  282. func NewClientWithEcsRamRole(regionId string, roleName string) (client *Client, err error) {
  283. client = &Client{}
  284. err = client.InitWithEcsRamRole(regionId, roleName)
  285. return
  286. }
  287. func NewClientWithRsaKeyPair(regionId string, publicKeyId, privateKey string, sessionExpiration int) (client *Client, err error) {
  288. client = &Client{}
  289. err = client.InitWithRsaKeyPair(regionId, publicKeyId, privateKey, sessionExpiration)
  290. return
  291. }
  292. // Deprecated: Use NewClientWithRamRoleArn in this package instead.
  293. func NewClientWithStsRoleArn(regionId string, accessKeyId, accessKeySecret, roleArn, roleSessionName string) (client *Client, err error) {
  294. return NewClientWithRamRoleArn(regionId, accessKeyId, accessKeySecret, roleArn, roleSessionName)
  295. }
  296. // Deprecated: Use NewClientWithEcsRamRole in this package instead.
  297. func NewClientWithStsRoleNameOnEcs(regionId string, roleName string) (client *Client, err error) {
  298. return NewClientWithEcsRamRole(regionId, roleName)
  299. }
  300. func (client *Client) ProcessCommonRequest(request *requests.CommonRequest) (response *responses.CommonResponse, err error) {
  301. request.TransToAcsRequest()
  302. response = responses.NewCommonResponse()
  303. err = client.DoAction(request, response)
  304. return
  305. }
  306. func (client *Client) ProcessCommonRequestWithSigner(request *requests.CommonRequest, signerInterface interface{}) (response *responses.CommonResponse, err error) {
  307. if signer, isSigner := signerInterface.(auth.Signer); isSigner {
  308. request.TransToAcsRequest()
  309. response = responses.NewCommonResponse()
  310. err = client.DoActionWithSigner(request, response, signer)
  311. return
  312. } else {
  313. panic("should not be here")
  314. }
  315. }
  316. func (client *Client) Shutdown() {
  317. client.signer.Shutdown()
  318. // lock the addAsync()
  319. client.asyncChanLock.Lock()
  320. defer client.asyncChanLock.Unlock()
  321. client.isRunning = false
  322. close(client.asyncTaskQueue)
  323. }