Skip to content

Commit

Permalink
optimize client
Browse files Browse the repository at this point in the history
  • Loading branch information
YangSen-qn committed Oct 10, 2023
1 parent 68a3cb1 commit a238df1
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 27 deletions.
32 changes: 32 additions & 0 deletions iqshell/common/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package client

import (
"net"
"net/http"
"time"

"github.com/qiniu/go-sdk/v7/storage"
)

var defaultClient = storage.Client{
Client: &http.Client{
Timeout: 10 * time.Second,
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 10 * time.Second,
KeepAlive: 10 * time.Second,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 4000,
MaxIdleConnsPerHost: 1000,
IdleConnTimeout: 10 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},
},
}

func DefaultStorageClient() storage.Client {
return defaultClient
}
4 changes: 3 additions & 1 deletion iqshell/storage/bucket/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/qiniu/go-sdk/v7/auth/qbox"
"github.com/qiniu/go-sdk/v7/storage"

"github.com/qiniu/qshell/v2/iqshell/common/client"
"github.com/qiniu/qshell/v2/iqshell/common/data"
"github.com/qiniu/qshell/v2/iqshell/common/workspace"
)
Expand All @@ -17,7 +18,8 @@ func GetBucketManager() (manager *storage.BucketManager, err *data.CodeError) {

mac := qbox.NewMac(acc.AccessKey, acc.SecretKey)
cfg := workspace.GetStorageConfig()
manager = storage.NewBucketManager(mac, cfg)
c := client.DefaultStorageClient()
manager = storage.NewBucketManagerEx(mac, cfg, &c)
return
}

Expand Down
25 changes: 2 additions & 23 deletions iqshell/storage/object/download/downloader_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,16 @@ package download

import (
"fmt"
"net"
"net/http"
"time"

"github.com/qiniu/go-sdk/v7/storage"

"github.com/qiniu/qshell/v2/iqshell/common/client"
"github.com/qiniu/qshell/v2/iqshell/common/data"
"github.com/qiniu/qshell/v2/iqshell/common/log"
"github.com/qiniu/qshell/v2/iqshell/common/progress"
"github.com/qiniu/qshell/v2/iqshell/common/utils"
"github.com/qiniu/qshell/v2/iqshell/common/workspace"
)

var defaultClient = storage.Client{
Client: &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 10 * time.Second,
KeepAlive: 10 * time.Second,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 4000,
MaxIdleConnsPerHost: 1000,
IdleConnTimeout: 10 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},
},
}

type DownloadApiInfo struct {
downloadUrl string
Bucket string
Expand Down Expand Up @@ -107,7 +86,7 @@ func (d *downloaderFile) download(info *DownloadApiInfo) (response *http.Respons
if workspace.IsCmdInterrupt() {
return nil, data.CancelError
}
response, rErr := defaultClient.DoRequest(workspace.GetContext(), "GET", info.downloadUrl, headers)
response, rErr := client.DefaultStorageClient().DoRequest(workspace.GetContext(), "GET", info.downloadUrl, headers)
if info.CheckHash && len(info.FileHash) != 0 && response != nil && response.Header != nil {
etag := fmt.Sprintf(response.Header.Get("Etag"))
etag = utils.ParseEtag(etag)
Expand Down
4 changes: 3 additions & 1 deletion iqshell/storage/object/upload/uploader_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/qiniu/go-sdk/v7/storage"

"github.com/qiniu/qshell/v2/iqshell/common/client"
"github.com/qiniu/qshell/v2/iqshell/common/data"
"github.com/qiniu/qshell/v2/iqshell/common/log"
"github.com/qiniu/qshell/v2/iqshell/common/workspace"
Expand Down Expand Up @@ -49,7 +50,8 @@ func (f *formUploader) upload(info *ApiInfo) (ret *ApiResult, err *data.CodeErro
}
}

up := storage.NewFormUploader(f.cfg)
c := client.DefaultStorageClient()
up := storage.NewFormUploaderEx(f.cfg, &c)
if e := up.Put(workspace.GetContext(), &ret, token, info.SaveKey, file, fileStatus.Size(), f.ext); e != nil {
err = data.NewEmptyError().AppendDesc("form upload").AppendError(e)
} else {
Expand Down
4 changes: 3 additions & 1 deletion iqshell/storage/object/upload/uploader_resume_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/qiniu/go-sdk/v7/storage"

"github.com/qiniu/qshell/v2/iqshell/common/client"
"github.com/qiniu/qshell/v2/iqshell/common/data"
"github.com/qiniu/qshell/v2/iqshell/common/log"
"github.com/qiniu/qshell/v2/iqshell/common/workspace"
Expand Down Expand Up @@ -46,7 +47,8 @@ func (r *resumeV1Uploader) upload(info *ApiInfo) (*ApiResult, *data.CodeError) {

var progress int64 = 0
ret := &ApiResult{}
up := storage.NewResumeUploader(r.cfg)
c := client.DefaultStorageClient()
up := storage.NewResumeUploaderEx(r.cfg, &c)
extra := &storage.RputExtra{
Recorder: recorder,
Params: nil,
Expand Down
4 changes: 3 additions & 1 deletion iqshell/storage/object/upload/uploader_resume_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/qiniu/go-sdk/v7/storage"

"github.com/qiniu/qshell/v2/iqshell/common/client"
"github.com/qiniu/qshell/v2/iqshell/common/data"
"github.com/qiniu/qshell/v2/iqshell/common/log"
"github.com/qiniu/qshell/v2/iqshell/common/workspace"
Expand Down Expand Up @@ -46,7 +47,8 @@ func (r *resumeV2Uploader) upload(info *ApiInfo) (*ApiResult, *data.CodeError) {

var progress int64 = 0
ret := &ApiResult{}
up := storage.NewResumeUploaderV2(r.cfg)
c := client.DefaultStorageClient()
up := storage.NewResumeUploaderV2Ex(r.cfg, &c)
extra := &storage.RputV2Extra{
Recorder: recorder,
Metadata: nil,
Expand Down

0 comments on commit a238df1

Please sign in to comment.