Skip to content

Commit

Permalink
Merge pull request imroc#44 from maddie/progress-interval
Browse files Browse the repository at this point in the history
Allow customization of progress reporting interval
  • Loading branch information
imroc authored Aug 26, 2019
2 parents e3a4bfb + d0ec576 commit 9120b68
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
30 changes: 17 additions & 13 deletions req.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,17 @@ func BodyXML(v interface{}) *bodyXml {

// Req is a convenient client for initiating requests
type Req struct {
client *http.Client
jsonEncOpts *jsonEncOpts
xmlEncOpts *xmlEncOpts
flag int
client *http.Client
jsonEncOpts *jsonEncOpts
xmlEncOpts *xmlEncOpts
flag int
progressInterval time.Duration
}

// New create a new *Req
func New() *Req {
return &Req{flag: LstdFlags}
// default progress reporting interval is 200 milliseconds
return &Req{flag: LstdFlags, progressInterval: 200 * time.Millisecond}
}

type param struct {
Expand Down Expand Up @@ -277,9 +279,10 @@ func (r *Req) Do(method, rawurl string, vs ...interface{}) (resp *Resp, err erro
up = UploadProgress(progress)
}
multipartHelper := &multipartHelper{
form: formParam.Values,
uploads: uploads,
uploadProgress: up,
form: formParam.Values,
uploads: uploads,
uploadProgress: up,
progressInterval: resp.r.progressInterval,
}
multipartHelper.Upload(req)
resp.multipartHelper = multipartHelper
Expand Down Expand Up @@ -484,10 +487,11 @@ func (b *bodyWrapper) Read(p []byte) (n int, err error) {
}

type multipartHelper struct {
form url.Values
uploads []FileUpload
dump []byte
uploadProgress UploadProgress
form url.Values
uploads []FileUpload
dump []byte
uploadProgress UploadProgress
progressInterval time.Duration
}

func (m *multipartHelper) Upload(req *http.Request) {
Expand Down Expand Up @@ -528,7 +532,7 @@ func (m *multipartHelper) Upload(req *http.Request) {
return _err
}
current += int64(n)
if now := time.Now(); now.Sub(lastTime) > 200*time.Millisecond {
if now := time.Now(); now.Sub(lastTime) > m.progressInterval {
lastTime = now
m.uploadProgress(current, total)
}
Expand Down
2 changes: 1 addition & 1 deletion resp.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (r *Resp) download(file *os.File) error {
return _err
}
current += int64(l)
if now := time.Now(); now.Sub(lastTime) > 200*time.Millisecond {
if now := time.Now(); now.Sub(lastTime) > r.r.progressInterval {
lastTime = now
r.downloadProgress(current, total)
}
Expand Down
12 changes: 12 additions & 0 deletions setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,15 @@ func (r *Req) SetXMLIndent(prefix, indent string) {
func SetXMLIndent(prefix, indent string) {
std.SetXMLIndent(prefix, indent)
}

// SetProgressInterval sets the progress reporting interval of both
// UploadProgress and DownloadProgress handler
func (r *Req) SetProgressInterval(interval time.Duration) {
r.progressInterval = interval
}

// SetProgressInterval sets the progress reporting interval of both
// UploadProgress and DownloadProgress handler for the default client
func SetProgressInterval(interval time.Duration) {
std.SetProgressInterval(interval)
}

0 comments on commit 9120b68

Please sign in to comment.