location_resolver.go 3.5 KB

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