location_resolver.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. "sync"
  18. "time"
  19. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
  20. )
  21. const (
  22. // EndpointCacheExpireTime ...
  23. EndpointCacheExpireTime = 3600 //Seconds
  24. )
  25. // Cache caches endpoint for specific product and region
  26. type Cache struct {
  27. sync.RWMutex
  28. cache map[string]interface{}
  29. }
  30. // Get ...
  31. func (c *Cache) Get(k string) (v interface{}) {
  32. c.RLock()
  33. v = c.cache[k]
  34. c.RUnlock()
  35. return
  36. }
  37. // Set ...
  38. func (c *Cache) Set(k string, v interface{}) {
  39. c.Lock()
  40. c.cache[k] = v
  41. c.Unlock()
  42. }
  43. var lastClearTimePerProduct = &Cache{cache: make(map[string]interface{})}
  44. var endpointCache = &Cache{cache: make(map[string]interface{})}
  45. // LocationResolver ...
  46. type LocationResolver struct {
  47. }
  48. func (resolver *LocationResolver) GetName() (name string) {
  49. name = "location resolver"
  50. return
  51. }
  52. // TryResolve resolves endpoint giving product and region
  53. func (resolver *LocationResolver) TryResolve(param *ResolveParam) (endpoint string, support bool, err error) {
  54. if len(param.LocationProduct) <= 0 {
  55. support = false
  56. return
  57. }
  58. //get from cache
  59. cacheKey := param.Product + "#" + param.RegionId
  60. var ok bool
  61. endpoint, ok = endpointCache.Get(cacheKey).(string)
  62. if ok && len(endpoint) > 0 && !CheckCacheIsExpire(cacheKey) {
  63. support = true
  64. return
  65. }
  66. //get from remote
  67. getEndpointRequest := requests.NewCommonRequest()
  68. getEndpointRequest.Product = "Location"
  69. getEndpointRequest.Version = "2015-06-12"
  70. getEndpointRequest.ApiName = "DescribeEndpoints"
  71. getEndpointRequest.Domain = "location-readonly.aliyuncs.com"
  72. getEndpointRequest.Method = "GET"
  73. getEndpointRequest.Scheme = requests.HTTPS
  74. getEndpointRequest.QueryParams["Id"] = param.RegionId
  75. getEndpointRequest.QueryParams["ServiceCode"] = param.LocationProduct
  76. if len(param.LocationEndpointType) > 0 {
  77. getEndpointRequest.QueryParams["Type"] = param.LocationEndpointType
  78. } else {
  79. getEndpointRequest.QueryParams["Type"] = "openAPI"
  80. }
  81. response, err := param.CommonApi(getEndpointRequest)
  82. if err != nil {
  83. support = false
  84. return
  85. }
  86. if !response.IsSuccess() {
  87. support = false
  88. return
  89. }
  90. var getEndpointResponse GetEndpointResponse
  91. err = json.Unmarshal([]byte(response.GetHttpContentString()), &getEndpointResponse)
  92. if err != nil {
  93. support = false
  94. return
  95. }
  96. if !getEndpointResponse.Success || getEndpointResponse.Endpoints == nil {
  97. support = false
  98. return
  99. }
  100. if len(getEndpointResponse.Endpoints.Endpoint) <= 0 {
  101. support = false
  102. return
  103. }
  104. if len(getEndpointResponse.Endpoints.Endpoint[0].Endpoint) > 0 {
  105. endpoint = getEndpointResponse.Endpoints.Endpoint[0].Endpoint
  106. endpointCache.Set(cacheKey, endpoint)
  107. lastClearTimePerProduct.Set(cacheKey, time.Now().Unix())
  108. support = true
  109. return
  110. }
  111. support = false
  112. return
  113. }
  114. // CheckCacheIsExpire ...
  115. func CheckCacheIsExpire(cacheKey string) bool {
  116. lastClearTime, ok := lastClearTimePerProduct.Get(cacheKey).(int64)
  117. if !ok {
  118. return true
  119. }
  120. if lastClearTime <= 0 {
  121. lastClearTime = time.Now().Unix()
  122. lastClearTimePerProduct.Set(cacheKey, lastClearTime)
  123. }
  124. now := time.Now().Unix()
  125. elapsedTime := now - lastClearTime
  126. if elapsedTime > EndpointCacheExpireTime {
  127. return true
  128. }
  129. return false
  130. }
  131. // GetEndpointResponse ...
  132. type GetEndpointResponse struct {
  133. Endpoints *EndpointsObj
  134. RequestId string
  135. Success bool
  136. }
  137. // EndpointsObj ...
  138. type EndpointsObj struct {
  139. Endpoint []EndpointObj
  140. }
  141. // EndpointObj ...
  142. type EndpointObj struct {
  143. // Protocols map[string]string
  144. Type string
  145. Namespace string
  146. Id string
  147. SerivceCode string
  148. Endpoint string
  149. }