Skip to content

Commit

Permalink
BOS support upload acceleration and fix retry policy bug
Browse files Browse the repository at this point in the history
  • Loading branch information
duanliguo committed Apr 17, 2020
1 parent 2e3ebef commit a49231a
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 67 deletions.
23 changes: 15 additions & 8 deletions bce/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import (
// will define its own client in case of specific extension.
type Client interface {
SendRequest(*BceRequest, *BceResponse) error
GetBceClientConfig() *BceClientConfiguration
}

// BceClient defines the general client to access the BCE services.
Expand All @@ -71,7 +72,9 @@ func (c *BceClient) buildHttpRequest(request *BceRequest) {
request.BuildHttpRequest()

// Set the client specific configurations
request.SetEndpoint(c.Config.Endpoint)
if request.Endpoint() == "" {
request.SetEndpoint(c.Config.Endpoint)
}
if request.Protocol() == "" {
request.SetProtocol(DEFAULT_PROTOCOL)
}
Expand Down Expand Up @@ -118,7 +121,7 @@ func (c *BceClient) SendRequest(req *BceRequest, resp *BceResponse) error {
// The request body should be temporarily saved if retry to send the http request
var retryBuf bytes.Buffer
var teeReader io.Reader
if req.Body() != nil {
if c.Config.Retry.ShouldRetry(nil, 0) && req.Body() != nil {
teeReader = io.TeeReader(req.Body(), &retryBuf)
req.Request.SetBody(ioutil.NopCloser(teeReader))
}
Expand Down Expand Up @@ -169,6 +172,10 @@ func (c *BceClient) SendRequest(req *BceRequest, resp *BceResponse) error {
}
}

func (c *BceClient) GetBceClientConfig() *BceClientConfiguration {
return c.Config
}

func NewBceClient(conf *BceClientConfiguration, sign auth.Signer) *BceClient {
return &BceClient{conf, sign}
}
Expand All @@ -182,12 +189,12 @@ func NewBceClientWithAkSk(ak, sk, endPoint string) (*BceClient, error) {
HeadersToSign: auth.DEFAULT_HEADERS_TO_SIGN,
ExpireSeconds: auth.DEFAULT_EXPIRE_SECONDS}
defaultConf := &BceClientConfiguration{
Endpoint: endPoint,
Region: DEFAULT_REGION,
UserAgent: DEFAULT_USER_AGENT,
Credentials: credentials,
SignOption: defaultSignOptions,
Retry: DEFAULT_RETRY_POLICY,
Endpoint: endPoint,
Region: DEFAULT_REGION,
UserAgent: DEFAULT_USER_AGENT,
Credentials: credentials,
SignOption: defaultSignOptions,
Retry: DEFAULT_RETRY_POLICY,
ConnectionTimeoutInMillis: DEFAULT_CONNECTION_TIMEOUT_IN_MILLIS}
v1Signer := &auth.BceV1Signer{}

Expand Down
3 changes: 3 additions & 0 deletions bce/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ type BceClientConfiguration struct {
SignOption *auth.SignOptions
Retry RetryPolicy
ConnectionTimeoutInMillis int
// CnameEnabled should be true when use custom domain as endpoint to visit bos resource
CnameEnabled bool
BackupEndpoint string
}

func (c *BceClientConfiguration) String() string {
Expand Down
4 changes: 4 additions & 0 deletions bce/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ func (b *BackOffRetryPolicy) ShouldRetry(err BceError, attempts int) bool {
return false
}

if err == nil {
return true
}

// Always retry on IO error
if _, ok := err.(net.Error); ok {
return true
Expand Down
Loading

0 comments on commit a49231a

Please sign in to comment.