Skip to content

Commit

Permalink
put ucHost into region cacheKey
Browse files Browse the repository at this point in the history
  • Loading branch information
bachue committed Sep 7, 2023
1 parent 0dce4b5 commit 6d92c0a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
18 changes: 12 additions & 6 deletions storage/region_uc_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

// 此处废弃,但为了兼容老版本,单独放置一个文件
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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),
})
Expand All @@ -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))))
}
13 changes: 7 additions & 6 deletions storage/region_uc_v4.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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),
})
Expand Down

0 comments on commit 6d92c0a

Please sign in to comment.