diff --git a/iqshell/common/client/client.go b/iqshell/common/client/client.go new file mode 100644 index 00000000..d8b1a09d --- /dev/null +++ b/iqshell/common/client/client.go @@ -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 +} diff --git a/iqshell/storage/bucket/bucket.go b/iqshell/storage/bucket/bucket.go index e4c6cf0f..29babcbe 100644 --- a/iqshell/storage/bucket/bucket.go +++ b/iqshell/storage/bucket/bucket.go @@ -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" ) @@ -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 } diff --git a/iqshell/storage/object/download/downloader_file.go b/iqshell/storage/object/download/downloader_file.go index b9227f7b..30bacbba 100644 --- a/iqshell/storage/object/download/downloader_file.go +++ b/iqshell/storage/object/download/downloader_file.go @@ -2,12 +2,9 @@ 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" @@ -15,24 +12,6 @@ import ( "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 @@ -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) diff --git a/iqshell/storage/object/upload/uploader_form.go b/iqshell/storage/object/upload/uploader_form.go index 216226cd..d1b6caca 100644 --- a/iqshell/storage/object/upload/uploader_form.go +++ b/iqshell/storage/object/upload/uploader_form.go @@ -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" @@ -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 { diff --git a/iqshell/storage/object/upload/uploader_resume_v1.go b/iqshell/storage/object/upload/uploader_resume_v1.go index bb0b4082..22b17de7 100644 --- a/iqshell/storage/object/upload/uploader_resume_v1.go +++ b/iqshell/storage/object/upload/uploader_resume_v1.go @@ -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" @@ -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, diff --git a/iqshell/storage/object/upload/uploader_resume_v2.go b/iqshell/storage/object/upload/uploader_resume_v2.go index ea120f98..670c1aed 100644 --- a/iqshell/storage/object/upload/uploader_resume_v2.go +++ b/iqshell/storage/object/upload/uploader_resume_v2.go @@ -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" @@ -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,