From 6d92c0aabefe1fc3fb97ad3b87cc585af2e71ce5 Mon Sep 17 00:00:00 2001 From: Rong Zhou Date: Thu, 7 Sep 2023 07:43:26 +0000 Subject: [PATCH] put ucHost into region cacheKey --- storage/region_uc_v2.go | 18 ++++++++++++------ storage/region_uc_v4.go | 13 +++++++------ 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/storage/region_uc_v2.go b/storage/region_uc_v2.go index 79d53b3a..3e2fbc1d 100644 --- a/storage/region_uc_v2.go +++ b/storage/region_uc_v2.go @@ -2,14 +2,16 @@ package storage import ( "context" + "crypto/md5" "encoding/json" "fmt" - "github.com/qiniu/go-sdk/v7/internal/clientv2" - "golang.org/x/sync/singleflight" "os" "path/filepath" "sync" "time" + + "github.com/qiniu/go-sdk/v7/internal/clientv2" + "golang.org/x/sync/singleflight" ) // 此处废弃,但为了兼容老版本,单独放置一个文件 @@ -201,13 +203,13 @@ func getRegionByV2(ak, bucket string, options UCApiOptions) (*Region, error) { }() } - regionID := fmt.Sprintf("%s:%s", ak, bucket) + regionCacheKey := makeRegionCacheKey(ak, bucket) //check from cache - if v, ok := regionV2Cache.Load(regionID); ok && time.Now().Before(v.(regionV2CacheValue).Deadline) { + if v, ok := regionV2Cache.Load(regionCacheKey); ok && time.Now().Before(v.(regionV2CacheValue).Deadline) { return v.(regionV2CacheValue).Region, nil } - newRegion, err, _ := ucQueryV2Group.Do(regionID, func() (interface{}, error) { + newRegion, err, _ := ucQueryV2Group.Do(regionCacheKey, func() (interface{}, error) { reqURL := fmt.Sprintf("%s/v2/query?ak=%s&bucket=%s", getUcHost(options.UseHttps), ak, bucket) var ret UcQueryRet @@ -271,7 +273,7 @@ func getRegionByV2(ak, bucket string, options UCApiOptions) (*Region, error) { IoSrcHost: ioSrcHost, } - regionV2Cache.Store(regionID, regionV2CacheValue{ + regionV2Cache.Store(regionCacheKey, regionV2CacheValue{ Region: region, Deadline: time.Now().Add(time.Duration(ret.TTL) * time.Second), }) @@ -288,3 +290,7 @@ func getRegionByV2(ak, bucket string, options UCApiOptions) (*Region, error) { return newRegion.(*Region), err } + +func makeRegionCacheKey(ak, bucket string) string { + return fmt.Sprintf("%s:%s:%x", ak, bucket, md5.Sum([]byte(getUcHost(false)))) +} diff --git a/storage/region_uc_v4.go b/storage/region_uc_v4.go index a5f66fda..5dffc92a 100644 --- a/storage/region_uc_v4.go +++ b/storage/region_uc_v4.go @@ -4,13 +4,14 @@ import ( "context" "encoding/json" "fmt" - "github.com/qiniu/go-sdk/v7/internal/clientv2" - "golang.org/x/sync/singleflight" "math" "os" "path/filepath" "sync" "time" + + "github.com/qiniu/go-sdk/v7/internal/clientv2" + "golang.org/x/sync/singleflight" ) type ucQueryV4Ret struct { @@ -137,14 +138,14 @@ func getRegionByV4(ak, bucket string, options UCApiOptions) (*RegionGroup, error }() } - regionID := fmt.Sprintf("%s:%s", ak, bucket) + regionCacheKey := makeRegionCacheKey(ak, bucket) //check from cache - if v, ok := regionV4Cache.Load(regionID); ok && time.Now().Before(v.(regionV4CacheValue).Deadline) { + if v, ok := regionV4Cache.Load(regionCacheKey); ok && time.Now().Before(v.(regionV4CacheValue).Deadline) { cacheValue, _ := v.(regionV4CacheValue) return NewRegionGroup(cacheValue.getRegions()...), nil } - newRegion, err, _ := ucQueryV4Group.Do(regionID, func() (interface{}, error) { + newRegion, err, _ := ucQueryV4Group.Do(regionCacheKey, func() (interface{}, error) { reqURL := fmt.Sprintf("%s/v4/query?ak=%s&bucket=%s", getUcHost(options.UseHttps), ak, bucket) var ret ucQueryV4Ret @@ -181,7 +182,7 @@ func getRegionByV4(ak, bucket string, options UCApiOptions) (*RegionGroup, error }) } - regionV4Cache.Store(regionID, regionV4CacheValue{ + regionV4Cache.Store(regionCacheKey, regionV4CacheValue{ Regions: regions, Deadline: time.Now().Add(time.Duration(ttl) * time.Second), })