@@ -12,6 +12,7 @@ import (
1212
1313 "golang.org/x/sync/singleflight"
1414
15+ "github.com/qiniu/go-sdk/v7/client"
1516 "github.com/qiniu/go-sdk/v7/internal/clientv2"
1617)
1718
@@ -209,9 +210,28 @@ func storeRegionV2Cache() {
209210
210211type UCApiOptions struct {
211212 UseHttps bool //
212- RetryMax int // 单域名重试次数
213+
214+ RetryMax int // 单域名重试次数
215+
216+ Hosts []string // api 请求的域名
217+
213218 // 主备域名冻结时间(默认:600s),当一个域名请求失败(单个域名会被重试 TryTimes 次),会被冻结一段时间,使用备用域名进行重试,在冻结时间内,域名不能被使用,当一个操作中所有域名竣备冻结操作不在进行重试,返回最后一次操作的错误。
214219 HostFreezeDuration time.Duration
220+
221+ Client * client.Client // api 请求使用的 client
222+ }
223+
224+ func (o * UCApiOptions ) init () {
225+ if len (o .Hosts ) == 0 {
226+ o .Hosts = getUcBackupHosts ()
227+ }
228+ }
229+
230+ func (o * UCApiOptions ) firstHost () string {
231+ if len (o .Hosts ) == 0 {
232+ return ""
233+ }
234+ return o .Hosts [0 ]
215235}
216236
217237func DefaultUCApiOptions () UCApiOptions {
@@ -223,6 +243,7 @@ func DefaultUCApiOptions() UCApiOptions {
223243}
224244
225245func getRegionByV2 (ak , bucket string , options UCApiOptions ) (* Region , error ) {
246+ options .init ()
226247
227248 regionV2CacheLock .RLock ()
228249 if regionV2CacheLoaded {
@@ -240,20 +261,22 @@ func getRegionByV2(ak, bucket string, options UCApiOptions) (*Region, error) {
240261 }()
241262 }
242263
243- regionCacheKey := makeRegionCacheKey (ak , bucket )
264+ regionCacheKey := makeRegionCacheKey (ak , bucket , options . Hosts )
244265 //check from cache
245266 if v , ok := regionV2Cache .Load (regionCacheKey ); ok && time .Now ().Before (v .(regionV2CacheValue ).Deadline ) {
246267 return v .(regionV2CacheValue ).Region , nil
247268 }
248269
249270 newRegion , err , _ := ucQueryV2Group .Do (regionCacheKey , func () (interface {}, error ) {
250- reqURL := fmt .Sprintf ("%s/v2/query?ak=%s&bucket=%s" , getUcHost (options .UseHttps ), ak , bucket )
271+ reqURL := fmt .Sprintf ("%s/v2/query?ak=%s&bucket=%s" , endpoint (options .UseHttps , options . firstHost () ), ak , bucket )
251272
252273 var ret UcQueryRet
253274 c := getUCClient (ucClientConfig {
254275 IsUcQueryApi : true ,
255276 RetryMax : options .RetryMax ,
277+ Hosts : options .Hosts ,
256278 HostFreezeDuration : options .HostFreezeDuration ,
279+ Client : options .Client ,
257280 }, nil )
258281 err := clientv2 .DoAndDecodeJsonResponse (c , clientv2.RequestParams {
259282 Context : context .Background (),
@@ -294,6 +317,7 @@ func getRegionByV2(ak, bucket string, options UCApiOptions) (*Region, error) {
294317 return newRegion .(* Region ), err
295318}
296319
297- func makeRegionCacheKey (ak , bucket string ) string {
298- return fmt .Sprintf ("%s:%s:%x" , ak , bucket , md5 .Sum ([]byte (getUcHost (false ))))
320+ func makeRegionCacheKey (ak , bucket string , ucHosts []string ) string {
321+ hostStrings := fmt .Sprintf ("%v" , ucHosts )
322+ return fmt .Sprintf ("%s:%s:%x" , ak , bucket , md5 .Sum ([]byte (hostStrings )))
299323}
0 commit comments