@@ -12,6 +12,7 @@ import (
12
12
13
13
"golang.org/x/sync/singleflight"
14
14
15
+ "github.com/qiniu/go-sdk/v7/client"
15
16
"github.com/qiniu/go-sdk/v7/internal/clientv2"
16
17
)
17
18
@@ -209,9 +210,28 @@ func storeRegionV2Cache() {
209
210
210
211
type UCApiOptions struct {
211
212
UseHttps bool //
212
- RetryMax int // 单域名重试次数
213
+
214
+ RetryMax int // 单域名重试次数
215
+
216
+ Hosts []string // api 请求的域名
217
+
213
218
// 主备域名冻结时间(默认:600s),当一个域名请求失败(单个域名会被重试 TryTimes 次),会被冻结一段时间,使用备用域名进行重试,在冻结时间内,域名不能被使用,当一个操作中所有域名竣备冻结操作不在进行重试,返回最后一次操作的错误。
214
219
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 ]
215
235
}
216
236
217
237
func DefaultUCApiOptions () UCApiOptions {
@@ -223,6 +243,7 @@ func DefaultUCApiOptions() UCApiOptions {
223
243
}
224
244
225
245
func getRegionByV2 (ak , bucket string , options UCApiOptions ) (* Region , error ) {
246
+ options .init ()
226
247
227
248
regionV2CacheLock .RLock ()
228
249
if regionV2CacheLoaded {
@@ -240,20 +261,22 @@ func getRegionByV2(ak, bucket string, options UCApiOptions) (*Region, error) {
240
261
}()
241
262
}
242
263
243
- regionCacheKey := makeRegionCacheKey (ak , bucket )
264
+ regionCacheKey := makeRegionCacheKey (ak , bucket , options . Hosts )
244
265
//check from cache
245
266
if v , ok := regionV2Cache .Load (regionCacheKey ); ok && time .Now ().Before (v .(regionV2CacheValue ).Deadline ) {
246
267
return v .(regionV2CacheValue ).Region , nil
247
268
}
248
269
249
270
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 )
251
272
252
273
var ret UcQueryRet
253
274
c := getUCClient (ucClientConfig {
254
275
IsUcQueryApi : true ,
255
276
RetryMax : options .RetryMax ,
277
+ Hosts : options .Hosts ,
256
278
HostFreezeDuration : options .HostFreezeDuration ,
279
+ Client : options .Client ,
257
280
}, nil )
258
281
err := clientv2 .DoAndDecodeJsonResponse (c , clientv2.RequestParams {
259
282
Context : context .Background (),
@@ -294,6 +317,7 @@ func getRegionByV2(ak, bucket string, options UCApiOptions) (*Region, error) {
294
317
return newRegion .(* Region ), err
295
318
}
296
319
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 )))
299
323
}
0 commit comments