location_resolver.go 3.8 KB

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