location_resolver.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 endpoints
  15. import (
  16. "encoding/json"
  17. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
  18. "sync"
  19. "time"
  20. )
  21. const (
  22. EndpointCacheExpireTime = 3600 //Seconds
  23. )
  24. var lastClearTimePerProduct = struct {
  25. sync.RWMutex
  26. cache map[string]int64
  27. }{cache: make(map[string]int64)}
  28. var endpointCache = struct {
  29. sync.RWMutex
  30. cache map[string]string
  31. }{cache: make(map[string]string)}
  32. type LocationResolver struct {
  33. }
  34. func (resolver *LocationResolver) TryResolve(param *ResolveParam) (endpoint string, support bool, err error) {
  35. if len(param.LocationProduct) <= 0 {
  36. support = false
  37. return
  38. }
  39. //get from cache
  40. cacheKey := param.Product + "#" + param.RegionId
  41. if endpointCache.cache != nil && len(endpointCache.cache[cacheKey]) > 0 && !CheckCacheIsExpire(cacheKey) {
  42. endpoint = endpointCache.cache[cacheKey]
  43. support = true
  44. return
  45. }
  46. //get from remote
  47. getEndpointRequest := requests.NewCommonRequest()
  48. getEndpointRequest.Product = "Location"
  49. getEndpointRequest.Version = "2015-06-12"
  50. getEndpointRequest.ApiName = "DescribeEndpoints"
  51. getEndpointRequest.Domain = "location.aliyuncs.com"
  52. getEndpointRequest.Method = "GET"
  53. getEndpointRequest.QueryParams["Id"] = param.RegionId
  54. getEndpointRequest.QueryParams["ServiceCode"] = param.LocationProduct
  55. if len(param.LocationEndpoint) > 0 {
  56. getEndpointRequest.QueryParams["Type"] = param.LocationEndpoint
  57. } else {
  58. getEndpointRequest.QueryParams["Type"] = "openAPI"
  59. }
  60. response, err := param.CommonApi(getEndpointRequest)
  61. //{"Endpoints":{"Endpoint":[{"Protocols":{"Protocols":["HTTP","HTTPS"]},"Type":"openAPI","Namespace":"26842","Id":"cn-hangzhou","SerivceCode":"apigateway","Endpoint":"apigateway.cn-hangzhou.aliyuncs.com"}]},"RequestId":"3287538B-19A0-4550-9995-143C5EDBD955","Success":true}
  62. var getEndpointResponse GetEndpointResponse
  63. if !response.IsSuccess() {
  64. support = false
  65. return
  66. }
  67. json.Unmarshal([]byte(response.GetHttpContentString()), &getEndpointResponse)
  68. if !getEndpointResponse.Success || getEndpointResponse.Endpoints == nil {
  69. support = false
  70. return
  71. }
  72. if len(getEndpointResponse.Endpoints.Endpoint) <= 0 {
  73. support = false
  74. return
  75. }
  76. if len(getEndpointResponse.Endpoints.Endpoint[0].Endpoint) > 0 {
  77. endpoint = getEndpointResponse.Endpoints.Endpoint[0].Endpoint
  78. endpointCache.Lock()
  79. endpointCache.cache[cacheKey] = endpoint
  80. endpointCache.Unlock()
  81. lastClearTimePerProduct.Lock()
  82. lastClearTimePerProduct.cache[cacheKey] = time.Now().Unix()
  83. lastClearTimePerProduct.Unlock()
  84. support = true
  85. return
  86. }
  87. support = false
  88. return
  89. }
  90. func CheckCacheIsExpire(cacheKey string) bool {
  91. lastClearTime := lastClearTimePerProduct.cache[cacheKey]
  92. if lastClearTime <= 0 {
  93. lastClearTime = time.Now().Unix()
  94. lastClearTimePerProduct.Lock()
  95. lastClearTimePerProduct.cache[cacheKey] = lastClearTime
  96. lastClearTimePerProduct.Unlock()
  97. }
  98. now := time.Now().Unix()
  99. elapsedTime := now - lastClearTime
  100. if elapsedTime > EndpointCacheExpireTime {
  101. return true
  102. }
  103. return false
  104. }
  105. type GetEndpointResponse struct {
  106. Endpoints *EndpointsObj
  107. RequestId string
  108. Success bool
  109. }
  110. type EndpointsObj struct {
  111. Endpoint []EndpointObj
  112. }
  113. type EndpointObj struct {
  114. Protocols map[string]string
  115. Type string
  116. Namespace string
  117. Id string
  118. SerivceCode string
  119. Endpoint string
  120. }