Skip to content

Commit

Permalink
feat: add response timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
juchaosong committed Aug 22, 2024
1 parent 3b423bf commit 0361350
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 27 deletions.
5 changes: 4 additions & 1 deletion pkg/cmd/record/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package record
import (
"context"
"fmt"
"time"

openv1alpha1resource "buf.build/gen/go/coscene-io/coscene-openapi/protocolbuffers/go/coscene/openapi/dataplatform/v1alpha1/resources"
"github.com/coscene-io/cocli/internal/config"
Expand All @@ -37,6 +38,7 @@ func NewCreateCommand(cfgPath *string) *cobra.Command {
labelDisplayNames []string
thumbnail = ""
multiOpts = &upload_utils.MultipartOpts{}
timeout time.Duration
)

cmd := &cobra.Command{
Expand Down Expand Up @@ -92,7 +94,7 @@ func NewCreateCommand(cfgPath *string) *cobra.Command {
mc, err := minio.New(pm.GetCurrentProfile().EndPoint, &minio.Options{
Creds: credentials.NewStaticV4(generateSecurityTokenRes.GetAccessKeyId(), generateSecurityTokenRes.GetAccessKeySecret(), generateSecurityTokenRes.GetSessionToken()),
Secure: true,
Transport: cmd_utils.DefaultTransport,
Transport: cmd_utils.NewTransport(timeout),
})
if err != nil {
log.Fatalf("unable to create minio client: %v", err)
Expand All @@ -118,6 +120,7 @@ func NewCreateCommand(cfgPath *string) *cobra.Command {
cmd.Flags().StringVarP(&thumbnail, "thumbnail", "i", "", "thumbnail path of the record.")
cmd.Flags().UintVarP(&multiOpts.Threads, "parallel", "P", 4, "upload number of parts in parallel")
cmd.Flags().StringVarP(&multiOpts.Size, "part-size", "s", "128Mib", "each part size")
cmd.Flags().DurationVar(&timeout, "response-timeout", 5*time.Minute, "server response time out")

return cmd
}
5 changes: 4 additions & 1 deletion pkg/cmd/record/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"os"
"path"
"path/filepath"
"time"

openv1alpha1resource "buf.build/gen/go/coscene-io/coscene-openapi/protocolbuffers/go/coscene/openapi/dataplatform/v1alpha1/resources"
"github.com/coscene-io/cocli/api"
Expand All @@ -45,6 +46,7 @@ func NewUploadCommand(cfgPath *string) *cobra.Command {
includeHidden = false
projectSlug = ""
multiOpts = &upload_utils.MultipartOpts{}
timeout time.Duration
)

cmd := &cobra.Command{
Expand Down Expand Up @@ -91,7 +93,7 @@ func NewUploadCommand(cfgPath *string) *cobra.Command {
Creds: credentials.NewStaticV4(generateSecurityTokenRes.GetAccessKeyId(), generateSecurityTokenRes.GetAccessKeySecret(), generateSecurityTokenRes.GetSessionToken()),
Secure: true,
Region: "",
Transport: cmd_utils.DefaultTransport,
Transport: cmd_utils.NewTransport(timeout),
})
if err != nil {
log.Fatalf("unable to create minio client: %v", err)
Expand Down Expand Up @@ -145,6 +147,7 @@ func NewUploadCommand(cfgPath *string) *cobra.Command {
cmd.Flags().StringVarP(&projectSlug, "project", "p", "", "the slug of the working project")
cmd.Flags().UintVarP(&multiOpts.Threads, "parallel", "P", 4, "upload number of parts in parallel")
cmd.Flags().StringVarP(&multiOpts.Size, "part-size", "s", "128Mib", "each part size")
cmd.Flags().DurationVar(&timeout, "response-timeout", 5*time.Minute, "server response time out")

return cmd
}
Expand Down
53 changes: 28 additions & 25 deletions pkg/cmd_utils/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,32 @@ import (
"time"
)

var DefaultTransport = &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
MaxIdleConns: 256,
MaxIdleConnsPerHost: 16,
ResponseHeaderTimeout: 3 * time.Minute,
IdleConnTimeout: time.Minute,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 10 * time.Second,
// Set this value so that the underlying transport round-tripper
// doesn't try to auto decode the body of objects with
// content-encoding set to `gzip`.
//
// Refer:
// https://golang.org/src/net/http/transport.go?h=roundTrip#L1843
DisableCompression: true,
TLSClientConfig: &tls.Config{
// Can't use SSLv3 because of POODLE and BEAST
// Can't use TLSv1.0 because of POODLE and BEAST using CBC cipher
// Can't use TLSv1.1 because of RC4 cipher usage
MinVersion: tls.VersionTLS12,
},
func NewTransport(timeout time.Duration) *http.Transport {
return &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
MaxIdleConns: 256,
MaxIdleConnsPerHost: 16,
ResponseHeaderTimeout: timeout,
IdleConnTimeout: time.Minute,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 10 * time.Second,
// Set this value so that the underlying transport round-tripper
// doesn't try to auto decode the body of objects with
// content-encoding set to `gzip`.
//
// Refer:
// https://golang.org/src/net/http/transport.go?h=roundTrip#L1843
DisableCompression: true,
TLSClientConfig: &tls.Config{
// Can't use SSLv3 because of POODLE and BEAST
// Can't use TLSv1.0 because of POODLE and BEAST using CBC cipher
// Can't use TLSv1.1 because of RC4 cipher usage
MinVersion: tls.VersionTLS12,
},
}

}

0 comments on commit 0361350

Please sign in to comment.