location_resolver.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.LocationEndpointType) > 0 {
  56. getEndpointRequest.QueryParams["Type"] = param.LocationEndpointType
  57. } else {
  58. getEndpointRequest.QueryParams["Type"] = "openAPI"
  59. }
  60. response, err := param.CommonApi(getEndpointRequest)
  61. var getEndpointResponse GetEndpointResponse
  62. if !response.IsSuccess() {
  63. support = false
  64. return
  65. }
  66. json.Unmarshal([]byte(response.GetHttpContentString()), &getEndpointResponse)
  67. if !getEndpointResponse.Success || getEndpointResponse.Endpoints == nil {
  68. support = false
  69. return
  70. }
  71. if len(getEndpointResponse.Endpoints.Endpoint) <= 0 {
  72. support = false
  73. return
  74. }
  75. if len(getEndpointResponse.Endpoints.Endpoint[0].Endpoint) > 0 {
  76. endpoint = getEndpointResponse.Endpoints.Endpoint[0].Endpoint
  77. endpointCache.Lock()
  78. endpointCache.cache[cacheKey] = endpoint
  79. endpointCache.Unlock()
  80. lastClearTimePerProduct.Lock()
  81. lastClearTimePerProduct.cache[cacheKey] = time.Now().Unix()
  82. lastClearTimePerProduct.Unlock()
  83. support = true
  84. return
  85. }
  86. support = false
  87. return
  88. }
  89. func CheckCacheIsExpire(cacheKey string) bool {
  90. lastClearTime := lastClearTimePerProduct.cache[cacheKey]
  91. if lastClearTime <= 0 {
  92. lastClearTime = time.Now().Unix()
  93. lastClearTimePerProduct.Lock()
  94. lastClearTimePerProduct.cache[cacheKey] = lastClearTime
  95. lastClearTimePerProduct.Unlock()
  96. }
  97. now := time.Now().Unix()
  98. elapsedTime := now - lastClearTime
  99. if elapsedTime > EndpointCacheExpireTime {
  100. return true
  101. }
  102. return false
  103. }
  104. type GetEndpointResponse struct {
  105. Endpoints *EndpointsObj
  106. RequestId string
  107. Success bool
  108. }
  109. type EndpointsObj struct {
  110. Endpoint []EndpointObj
  111. }
  112. type EndpointObj struct {
  113. Protocols map[string]string
  114. Type string
  115. Namespace string
  116. Id string
  117. SerivceCode string
  118. Endpoint string
  119. }