Skip to content

Commit 5611baf

Browse files
committed
- User-Agent header added
1 parent 91da4eb commit 5611baf

File tree

5 files changed

+37
-3
lines changed

5 files changed

+37
-3
lines changed

blobporter.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ func init() {
7171
var defaultNumberOfWorkers = runtime.NumCPU() * numOfWorkersFactor
7272
var defaultNumberOfReaders = runtime.NumCPU() * numOfReadersFactor
7373

74+
// set user agent info
75+
util.SetUserAgentInfo(programVersion)
76+
7477
blockSizeStr = "8MB" // default size for blob blocks
7578
const (
7679
dblockSize = 8 * util.MB

sources/http.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ func (f *HTTPPipeline) ExecuteReader(partitionsQ chan pipeline.PartsPartition, p
187187

188188
header := fmt.Sprintf("bytes=%v-%v", p.Offset, p.Offset-1+uint64(p.BytesToRead))
189189
req.Header.Set("Range", header)
190+
userAgent, _ := util.GetUserAgentInfo()
191+
req.Header.Set("User-Agent", userAgent)
190192

191193
//set the close header only when the block is larger than the blob
192194
//to minimize the number of open when transfering small files.

targets/azureblock.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,17 @@ func (t *AzureBlock) ProcessWrittenPart(result *pipeline.WorkerResult, listInfo
121121
func (t *AzureBlock) WritePart(part *pipeline.Part) (duration time.Duration, startTime time.Time, numOfRetries int, err error) {
122122

123123
headers := make(map[string]string)
124+
userAgent, _ := util.GetUserAgentInfo()
125+
headers["User-Agent"] = userAgent
126+
124127
//if the max retries is exceeded, panic will happen, hence no error is returned.
125128
duration, startTime, numOfRetries = util.RetriableOperation(func(r int) error {
126129
//computation of the MD5 happens is done by the readers.
127130
if part.IsMD5Computed() {
128131
headers["Content-MD5"] = part.MD5()
129132
}
130133

134+
131135
if part.NumberOfBlocks == 1 {
132136
if err := t.StorageClient.CreateBlockBlobFromReader(t.Container,
133137
part.TargetAlias,

targets/azurepage.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,11 @@ func (t *AzurePage) PreProcessSourceInfo(source *pipeline.SourceInfo, blockSize
5353
}
5454

5555
//if the max retries is exceeded, panic will happen, hence no error is returned.
56+
headers := make(map[string]string)
57+
userAgent, _ := util.GetUserAgentInfo()
58+
headers["User-Agent"] = userAgent
5659
util.RetriableOperation(func(r int) error {
57-
if err := (*t.StorageClient).PutPageBlob(t.Container, (*source).TargetAlias, size, nil); err != nil {
60+
if err := (*t.StorageClient).PutPageBlob(t.Container, (*source).TargetAlias, size, headers); err != nil {
5861
t.resetClient()
5962
return err
6063
}
@@ -95,6 +98,9 @@ func (t *AzurePage) WritePart(part *pipeline.Part) (duration time.Duration, star
9598
if part.IsMD5Computed() {
9699
headers["Content-MD5"] = part.MD5()
97100
}
101+
userAgent, _ := util.GetUserAgentInfo()
102+
headers["User-Agent"] = userAgent
103+
98104
if err := (*t.StorageClient).PutPage(t.Container, part.TargetAlias, offset, endByte, "update", part.Data, headers); err != nil {
99105
util.PrintfIfDebug("WritePart -> |%v|%v|%v|%v", part.Offset, len(part.Data), part.TargetAlias, err)
100106
t.resetClient()

util/util.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net"
88
"os"
99
"regexp"
10+
"runtime"
1011
"strconv"
1112
"strings"
1213
"sync"
@@ -380,7 +381,7 @@ func getStorageHTTPClient() *http.Client {
380381
var c *http.Client
381382
var mtx sync.Mutex
382383

383-
//NewHTTPClient creates a new HTTP client with the configured timeout and MaxIdleConnsPerHost = 50, keep alive dialer.
384+
//NewHTTPClient creates a shared HTTP client with the configured timeout and MaxIdleConnsPerHost = 50, keep alive dialer.
384385
func NewHTTPClient() *http.Client {
385386
mtx.Lock()
386387
defer mtx.Unlock()
@@ -410,7 +411,7 @@ func getSuggestion(err error) string {
410411
case strings.Contains(err.Error(), "Client.Timeout"):
411412
return "Try increasing the timeout using the -s option or reducing the number of workers and readers, options: -r and -g"
412413
case strings.Contains(err.Error(), "too many open files"):
413-
return "Try increasing the number of open files allowed. For debian systems you can try: ulimit -Sn 2048 "
414+
return "Try reducing the number of sources or batch size"
414415
default:
415416
return ""
416417
}
@@ -441,3 +442,21 @@ func GetFileNameFromURL(sourceURI string) (string, error) {
441442

442443
return parts[len(parts)-1], nil
443444
}
445+
446+
//UserAgent TODO
447+
var userAgent string
448+
449+
//GetUserAgentInfo TODO
450+
func GetUserAgentInfo() (string, error) {
451+
if userAgent == "" {
452+
return "", fmt.Errorf("User agent is not set")
453+
}
454+
455+
return userAgent, nil
456+
457+
}
458+
459+
//SetUserAgentInfo TODO
460+
func SetUserAgentInfo(programVersion string) {
461+
userAgent = fmt.Sprintf("%s/%s/(%s %s)/", "BlobPorter", programVersion, runtime.GOOS, runtime.GOARCH)
462+
}

0 commit comments

Comments
 (0)