|
| 1 | +// +build integration,perftest |
| 2 | + |
| 3 | +package main |
| 4 | + |
| 5 | +import ( |
| 6 | + "flag" |
| 7 | + "fmt" |
| 8 | + "net/http" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/aws/aws-sdk-go-v2/service/s3/s3manager" |
| 13 | +) |
| 14 | + |
| 15 | +type Config struct { |
| 16 | + Bucket string |
| 17 | + Size int64 |
| 18 | + LogVerbose bool |
| 19 | + |
| 20 | + SDK SDKConfig |
| 21 | + Client ClientConfig |
| 22 | +} |
| 23 | + |
| 24 | +func (c *Config) SetupFlags(prefix string, flagset *flag.FlagSet) { |
| 25 | + flagset.StringVar(&c.Bucket, "bucket", "", |
| 26 | + "The S3 bucket `name` to download the object from.") |
| 27 | + flagset.Int64Var(&c.Size, "size", 0, |
| 28 | + "The S3 object size in bytes to be first uploaded then downloaded") |
| 29 | + flagset.BoolVar(&c.LogVerbose, "verbose", false, |
| 30 | + "The output log will include verbose request information") |
| 31 | + |
| 32 | + c.SDK.SetupFlags(prefix, flagset) |
| 33 | + c.Client.SetupFlags(prefix, flagset) |
| 34 | +} |
| 35 | + |
| 36 | +func (c *Config) Validate() error { |
| 37 | + var errs Errors |
| 38 | + |
| 39 | + if len(c.Bucket) == 0 || c.Size <= 0 { |
| 40 | + errs = append(errs, fmt.Errorf("bucket and filename/size are required")) |
| 41 | + } |
| 42 | + |
| 43 | + if err := c.SDK.Validate(); err != nil { |
| 44 | + errs = append(errs, err) |
| 45 | + } |
| 46 | + if err := c.Client.Validate(); err != nil { |
| 47 | + errs = append(errs, err) |
| 48 | + } |
| 49 | + |
| 50 | + if len(errs) != 0 { |
| 51 | + return errs |
| 52 | + } |
| 53 | + |
| 54 | + return nil |
| 55 | +} |
| 56 | + |
| 57 | +type SDKConfig struct { |
| 58 | + PartSize int64 |
| 59 | + Concurrency int |
| 60 | + BufferProvider s3manager.WriterReadFromProvider |
| 61 | +} |
| 62 | + |
| 63 | +func (c *SDKConfig) SetupFlags(prefix string, flagset *flag.FlagSet) { |
| 64 | + prefix += "sdk." |
| 65 | + |
| 66 | + flagset.Int64Var(&c.PartSize, prefix+"part-size", s3manager.DefaultDownloadPartSize, |
| 67 | + "Specifies the `size` of parts of the object to download.") |
| 68 | + flagset.IntVar(&c.Concurrency, prefix+"concurrency", s3manager.DefaultDownloadConcurrency, |
| 69 | + "Specifies the number of parts to download `at once`.") |
| 70 | +} |
| 71 | + |
| 72 | +func (c *SDKConfig) Validate() error { |
| 73 | + return nil |
| 74 | +} |
| 75 | + |
| 76 | +type ClientConfig struct { |
| 77 | + KeepAlive bool |
| 78 | + Timeouts Timeouts |
| 79 | + |
| 80 | + MaxIdleConns int |
| 81 | + MaxIdleConnsPerHost int |
| 82 | +} |
| 83 | + |
| 84 | +func (c *ClientConfig) SetupFlags(prefix string, flagset *flag.FlagSet) { |
| 85 | + prefix += "client." |
| 86 | + |
| 87 | + flagset.BoolVar(&c.KeepAlive, prefix+"http-keep-alive", true, |
| 88 | + "Specifies if HTTP keep alive is enabled.") |
| 89 | + |
| 90 | + defTR := http.DefaultTransport.(*http.Transport) |
| 91 | + |
| 92 | + flagset.IntVar(&c.MaxIdleConns, prefix+"idle-conns", defTR.MaxIdleConns, |
| 93 | + "Specifies max idle connection pool size.") |
| 94 | + |
| 95 | + flagset.IntVar(&c.MaxIdleConnsPerHost, prefix+"idle-conns-host", http.DefaultMaxIdleConnsPerHost, |
| 96 | + "Specifies max idle connection pool per host, will be truncated by idle-conns.") |
| 97 | + |
| 98 | + c.Timeouts.SetupFlags(prefix, flagset) |
| 99 | +} |
| 100 | + |
| 101 | +func (c *ClientConfig) Validate() error { |
| 102 | + var errs Errors |
| 103 | + |
| 104 | + if err := c.Timeouts.Validate(); err != nil { |
| 105 | + errs = append(errs, err) |
| 106 | + } |
| 107 | + |
| 108 | + if len(errs) != 0 { |
| 109 | + return errs |
| 110 | + } |
| 111 | + return nil |
| 112 | +} |
| 113 | + |
| 114 | +type Timeouts struct { |
| 115 | + Connect time.Duration |
| 116 | + TLSHandshake time.Duration |
| 117 | + ExpectContinue time.Duration |
| 118 | + ResponseHeader time.Duration |
| 119 | +} |
| 120 | + |
| 121 | +func (c *Timeouts) SetupFlags(prefix string, flagset *flag.FlagSet) { |
| 122 | + prefix += "timeout." |
| 123 | + |
| 124 | + flagset.DurationVar(&c.Connect, prefix+"connect", 30*time.Second, |
| 125 | + "The `timeout` connecting to the remote host.") |
| 126 | + |
| 127 | + defTR := http.DefaultTransport.(*http.Transport) |
| 128 | + |
| 129 | + flagset.DurationVar(&c.TLSHandshake, prefix+"tls", defTR.TLSHandshakeTimeout, |
| 130 | + "The `timeout` waiting for the TLS handshake to complete.") |
| 131 | + |
| 132 | + flagset.DurationVar(&c.ExpectContinue, prefix+"expect-continue", defTR.ExpectContinueTimeout, |
| 133 | + "The `timeout` waiting for the TLS handshake to complete.") |
| 134 | + |
| 135 | + flagset.DurationVar(&c.ResponseHeader, prefix+"response-header", defTR.ResponseHeaderTimeout, |
| 136 | + "The `timeout` waiting for the TLS handshake to complete.") |
| 137 | +} |
| 138 | + |
| 139 | +func (c *Timeouts) Validate() error { |
| 140 | + return nil |
| 141 | +} |
| 142 | + |
| 143 | +type Errors []error |
| 144 | + |
| 145 | +func (es Errors) Error() string { |
| 146 | + var buf strings.Builder |
| 147 | + for _, e := range es { |
| 148 | + buf.WriteString(e.Error()) |
| 149 | + } |
| 150 | + |
| 151 | + return buf.String() |
| 152 | +} |
0 commit comments