Skip to content

Commit 5c79b3d

Browse files
authored
Merge pull request #226 from tencentyun/feature_jojoliang_fe4cd14a
update domain
2 parents d7bc349 + 48b30bc commit 5c79b3d

File tree

7 files changed

+86
-27
lines changed

7 files changed

+86
-27
lines changed

bucket_inventory.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,6 @@ func (s *BucketService) PostInventory(ctx context.Context, id string, opt *Bucke
149149
method: http.MethodPost,
150150
body: opt,
151151
}
152-
resp, err := s.client.send(ctx, &sendOpt)
152+
resp, err := s.client.doRetry(ctx, &sendOpt)
153153
return resp, err
154154
}

bucket_location.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ func (s *BucketService) GetLocation(ctx context.Context) (*BucketGetLocationResu
2323
method: http.MethodGet,
2424
result: &res,
2525
}
26-
resp, err := s.client.send(ctx, &sendOpt)
26+
resp, err := s.client.doRetry(ctx, &sendOpt)
2727
return &res, resp, err
2828
}

bucket_part.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,6 @@ func (s *BucketService) ListMultipartUploads(ctx context.Context, opt *ListMulti
5252
result: &res,
5353
optQuery: opt,
5454
}
55-
resp, err := s.client.send(ctx, &sendOpt)
55+
resp, err := s.client.doRetry(ctx, &sendOpt)
5656
return &res, resp, err
5757
}

cos.go

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ var (
4242
hostSuffix = regexp.MustCompile(`^.*((cos|cos-internal|cos-website|ci)\.[a-z-1]+|file)\.(myqcloud\.com|tencentcos\.cn).*$`)
4343
hostPrefix = regexp.MustCompile(`^(http://|https://){0,1}([a-z0-9-]+-[0-9]+\.){0,1}((cos|cos-internal|cos-website|ci)\.[a-z-1]+|file)\.(myqcloud\.com|tencentcos\.cn).*$`)
4444
invalidBucketErr = fmt.Errorf("invalid bucket format, please check your cos.BaseURL")
45+
46+
switchHost = regexp.MustCompile(`([a-z0-9-]+-[0-9]+\.)((cos|cos-website)\.[a-z-1]+)\.(myqcloud\.com)(:[0-9]+){0,1}$`)
47+
oldDomainSuffix = ".myqcloud.com"
48+
newDomainSuffix = ".tencentcos.cn"
4549
)
4650

4751
// BaseURL 访问各 API 所需的基础 URL
@@ -89,9 +93,9 @@ func NewBucketURL(bucketName, region string, secure bool) (*url.URL, error) {
8993
}
9094

9195
type RetryOptions struct {
92-
Count int
93-
Interval time.Duration
94-
StatusCode []int
96+
Count int
97+
Interval time.Duration
98+
AutoSwitchHost bool
9599
}
96100
type Config struct {
97101
EnableCRC bool
@@ -148,8 +152,9 @@ func NewClient(uri *BaseURL, httpClient *http.Client) *Client {
148152
EnableCRC: true,
149153
RequestBodyClose: false,
150154
RetryOpt: RetryOptions{
151-
Count: 3,
152-
Interval: time.Duration(0),
155+
Count: 3,
156+
Interval: time.Duration(0),
157+
AutoSwitchHost: true,
153158
},
154159
},
155160
}
@@ -352,6 +357,22 @@ type sendOptions struct {
352357
disableCloseBody bool
353358
}
354359

360+
func toSwitchHost(oldURL *url.URL) *url.URL {
361+
// 判断域名是否能够切换
362+
if !switchHost.MatchString(oldURL.Host) {
363+
return oldURL
364+
}
365+
newURL, _ := url.Parse(oldURL.String())
366+
hostAndPort := strings.SplitN(newURL.Host, ":", 2)
367+
newHost := hostAndPort[0]
368+
newHost = newHost[:len(newHost)-len(oldDomainSuffix)] + newDomainSuffix
369+
if len(hostAndPort) > 1 {
370+
newHost += ":" + hostAndPort[1]
371+
}
372+
newURL.Host = newHost
373+
return newURL
374+
}
375+
355376
func (c *Client) doRetry(ctx context.Context, opt *sendOptions) (resp *Response, err error) {
356377
if opt.body != nil {
357378
if _, ok := opt.body.(io.Reader); ok {
@@ -360,36 +381,31 @@ func (c *Client) doRetry(ctx context.Context, opt *sendOptions) (resp *Response,
360381
}
361382
}
362383
count := 1
363-
if count < c.Conf.RetryOpt.Count {
384+
if c.Conf.RetryOpt.Count > 0 {
364385
count = c.Conf.RetryOpt.Count
365386
}
366-
nr := 0
367387
interval := c.Conf.RetryOpt.Interval
368-
for nr < count {
388+
for nr := 0; nr < count; nr++ {
369389
resp, err = c.send(ctx, opt)
370390
if err != nil && err != invalidBucketErr {
371-
if resp != nil && resp.StatusCode <= 499 {
372-
dobreak := true
373-
for _, v := range c.Conf.RetryOpt.StatusCode {
374-
if resp.StatusCode == v {
375-
dobreak = false
376-
break
377-
}
378-
}
379-
if dobreak {
380-
break
391+
// 不重试
392+
if resp != nil && resp.StatusCode < 500 {
393+
break
394+
}
395+
if c.Conf.RetryOpt.AutoSwitchHost {
396+
// 收不到报文 或者 不存在RequestId
397+
if resp == nil || resp.Header.Get("X-Cos-Request-Id") == "" {
398+
opt.baseURL = toSwitchHost(opt.baseURL)
381399
}
382400
}
383-
nr++
384-
if interval > 0 && nr < count {
401+
if interval > 0 && nr+1 < count {
385402
time.Sleep(interval)
386403
}
387404
continue
388405
}
389406
break
390407
}
391408
return
392-
393409
}
394410
func (c *Client) send(ctx context.Context, opt *sendOptions) (resp *Response, err error) {
395411
req, err := c.newRequest(ctx, opt.baseURL, opt.uri, opt.method, opt.body, opt.optQuery, opt.optHeader)

cos_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,47 @@ func Test_addHeaderOptions(t *testing.T) {
179179
t.Errorf("addHeaderOptions failed")
180180
}
181181
}
182+
183+
func Test_SwitchHost(t *testing.T) {
184+
u, _ := url.Parse("https://example-125000000.cos.ap-chengdu.myqcloud.com/123")
185+
res := toSwitchHost(u)
186+
want := "https://example-125000000.cos.ap-chengdu.tencentcos.cn/123"
187+
if res.String() != want {
188+
t.Errorf("toSwitchHost failed, expect: %v, res: %v", want, res.String())
189+
}
190+
191+
u, _ = url.Parse("https://example-125000000.cos.ap-chengdu.tencentcos.cn/123")
192+
res = toSwitchHost(u)
193+
want = "https://example-125000000.cos.ap-chengdu.tencentcos.cn/123"
194+
if res.String() != want {
195+
t.Errorf("toSwitchHost failed, expect: %v, res: %v", want, res.String())
196+
}
197+
198+
u, _ = url.Parse("https://service.cos.myqcloud.com/123")
199+
res = toSwitchHost(u)
200+
want = "https://service.cos.myqcloud.com/123"
201+
if res.String() != want {
202+
t.Errorf("toSwitchHost failed, expect: %v, res: %v", want, res.String())
203+
}
204+
205+
u, _ = url.Parse("https://example-125000000.file.myqcloud.com/123")
206+
res = toSwitchHost(u)
207+
want = "https://example-125000000.file.myqcloud.com/123"
208+
if res.String() != want {
209+
t.Errorf("toSwitchHost failed, expect: %v, res: %v", want, res.String())
210+
}
211+
212+
u, _ = url.Parse("http://example-125000000.cos.ap-chengdu.myqcloud.com:80/123")
213+
res = toSwitchHost(u)
214+
want = "http://example-125000000.cos.ap-chengdu.tencentcos.cn:80/123"
215+
if res.String() != want {
216+
t.Errorf("toSwitchHost failed, expect: %v, res: %v", want, res.String())
217+
}
218+
219+
u, _ = url.Parse("https://example-125000000.cos-website.ap-chengdu.myqcloud.com:443/123")
220+
res = toSwitchHost(u)
221+
want = "https://example-125000000.cos-website.ap-chengdu.tencentcos.cn:443/123"
222+
if res.String() != want {
223+
t.Errorf("toSwitchHost failed, expect: %v, res: %v", want, res.String())
224+
}
225+
}

object_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ func TestObjectService_GetRetry(t *testing.T) {
127127
ResponseHeaderTimeout: 1 * time.Second,
128128
},
129129
})
130-
client.Conf.RetryOpt.StatusCode = []int{499}
131130
name := "test/hello.txt"
132131
contentLength := 1024 * 1024 * 10
133132
data := make([]byte, contentLength)
@@ -140,7 +139,7 @@ func TestObjectService_GetRetry(t *testing.T) {
140139
atomic.AddInt32(&index, 1)
141140
if atomic.LoadInt32(&index)%3 != 0 {
142141
if atomic.LoadInt32(&index) > 6 {
143-
w.WriteHeader(499)
142+
w.WriteHeader(500)
144143
return
145144
}
146145
time.Sleep(time.Second * 2)

service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@ func (s *ServiceService) Get(ctx context.Context, opt ...*ServiceGetOptions) (*S
4747
optQuery: sopt,
4848
result: &res,
4949
}
50-
resp, err := s.client.send(ctx, &sendOpt)
50+
resp, err := s.client.doRetry(ctx, &sendOpt)
5151
return &res, resp, err
5252
}

0 commit comments

Comments
 (0)