Skip to content

Commit

Permalink
uc client: use client v1
Browse files Browse the repository at this point in the history
  • Loading branch information
YangSen-qn committed Jul 12, 2023
1 parent aeb365e commit e22a1d3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
34 changes: 33 additions & 1 deletion internal/clientv2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ type client struct {

func NewClient(cli Client, interceptors ...Interceptor) Client {
if cli == nil {
cli = http.DefaultClient
if clientV1.DefaultClient.Client != nil {
cli = NewClientWithClientV1(&clientV1.DefaultClient)
} else if http.DefaultClient != nil {
cli = http.DefaultClient
} else {
cli = &http.Client{}
}
}

var is interceptorList = interceptors
Expand Down Expand Up @@ -96,3 +102,29 @@ func DoAndDecodeJsonResponse(c Client, options RequestParams, ret interface{}) (

return resp, nil
}

type clientV1Wrapper struct {
c *clientV1.Client
}

func (c *clientV1Wrapper) Do(req *http.Request) (*http.Response, error) {
return c.c.Do(req.Context(), req)
}

func NewClientWithClientV1(c *clientV1.Client) Client {
if c == nil {
c = &clientV1.DefaultClient
}

if c.Client == nil {
if clientV1.DefaultClient.Client != nil {
c.Client = clientV1.DefaultClient.Client
} else {
c.Client = &http.Client{}
}
}

return &clientV1Wrapper{
c: c,
}
}
5 changes: 4 additions & 1 deletion storage/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"github.com/qiniu/go-sdk/v7/auth"
"github.com/qiniu/go-sdk/v7/client"
"github.com/qiniu/go-sdk/v7/internal/clientv2"
"github.com/qiniu/go-sdk/v7/internal/hostprovider"
"strings"
Expand Down Expand Up @@ -334,6 +335,8 @@ type ucClientConfig struct {

// 主备域名冻结时间(默认:600s),当一个域名请求失败(单个域名会被重试 TryTimes 次),会被冻结一段时间,使用备用域名进行重试,在冻结时间内,域名不能被使用,当一个操作中所有域名竣备冻结操作不在进行重试,返回最后一次操作的错误。
HostFreezeDuration time.Duration

Client *client.Client
}

func getUCClient(config ucClientConfig, mac *auth.Credentials) clientv2.Client {
Expand Down Expand Up @@ -375,5 +378,5 @@ func getUCClient(config ucClientConfig, mac *auth.Credentials) clientv2.Client {
}))
}

return clientv2.NewClient(nil, is...)
return clientv2.NewClient(clientv2.NewClientWithClientV1(config.Client), is...)
}
1 change: 1 addition & 0 deletions storage/uc.go
Original file line number Diff line number Diff line change
Expand Up @@ -743,5 +743,6 @@ func (m *BucketManager) getUCClient() clientv2.Client {
IsUcQueryApi: false,
RetryMax: m.options.RetryMax,
HostFreezeDuration: m.options.HostFreezeDuration,
Client: m.Client,
}, m.Mac)
}

0 comments on commit e22a1d3

Please sign in to comment.