location_resolver.go 3.6 KB

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