-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
432 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,9 @@ sudo: false | |
|
||
language: go | ||
|
||
go: 1.8 | ||
- 1.8 | ||
- 1.7 | ||
- 1.6 | ||
|
||
install: | ||
- go get -u github.com/golang/lint/golint | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
// +build go1.7 | ||
|
||
package date | ||
|
||
import ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package autorest | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"io/ioutil" | ||
"net/http" | ||
) | ||
|
||
// NewRetriableRequest returns a wrapper around an HTTP request that support retry logic. | ||
func NewRetriableRequest(req *http.Request) *RetriableRequest { | ||
return &RetriableRequest{req: req} | ||
} | ||
|
||
// Request returns the wrapped HTTP request. | ||
func (rr *RetriableRequest) Request() *http.Request { | ||
return rr.req | ||
} | ||
|
||
func (rr *RetriableRequest) prepareFromByteReader() (err error) { | ||
// fall back to making a copy (only do this once) | ||
b := []byte{} | ||
if rr.req.ContentLength > 0 { | ||
b = make([]byte, rr.req.ContentLength) | ||
_, err = io.ReadFull(rr.req.Body, b) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
b, err = ioutil.ReadAll(rr.req.Body) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
rr.br = bytes.NewReader(b) | ||
rr.req.Body = ioutil.NopCloser(rr.br) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// +build !go1.8 | ||
|
||
package autorest | ||
|
||
import ( | ||
"bytes" | ||
"net/http" | ||
) | ||
|
||
// RetriableRequest provides facilities for retrying an HTTP request. | ||
type RetriableRequest struct { | ||
req *http.Request | ||
br *bytes.Reader | ||
reset bool | ||
} | ||
|
||
// Prepare signals that the request is about to be sent. | ||
func (rr *RetriableRequest) Prepare() (err error) { | ||
// preserve the request body; this is to support retry logic as | ||
// the underlying transport will always close the reqeust body | ||
if rr.req.Body != nil { | ||
if rr.reset { | ||
if rr.br != nil { | ||
_, err = rr.br.Seek(0, 0 /*io.SeekStart*/) | ||
} | ||
rr.reset = false | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
if rr.br == nil { | ||
// fall back to making a copy (only do this once) | ||
err = rr.prepareFromByteReader() | ||
} | ||
// indicates that the request body needs to be reset | ||
rr.reset = true | ||
} | ||
return err | ||
} | ||
|
||
func removeRequestBody(req *http.Request) { | ||
req.Body = nil | ||
req.ContentLength = 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// +build go1.8 | ||
|
||
package autorest | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
// RetriableRequest provides facilities for retrying an HTTP request. | ||
type RetriableRequest struct { | ||
req *http.Request | ||
rc io.ReadCloser | ||
br *bytes.Reader | ||
reset bool | ||
} | ||
|
||
// Prepare signals that the request is about to be sent. | ||
func (rr *RetriableRequest) Prepare() (err error) { | ||
// preserve the request body; this is to support retry logic as | ||
// the underlying transport will always close the reqeust body | ||
if rr.req.Body != nil { | ||
if rr.reset { | ||
if rr.rc != nil { | ||
rr.req.Body = rr.rc | ||
} else if rr.br != nil { | ||
_, err = rr.br.Seek(0, io.SeekStart) | ||
} | ||
rr.reset = false | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
if rr.req.GetBody != nil { | ||
// this will allow us to preserve the body without having to | ||
// make a copy. note we need to do this on each iteration | ||
rr.rc, err = rr.req.GetBody() | ||
if err != nil { | ||
return err | ||
} | ||
} else if rr.br == nil { | ||
// fall back to making a copy (only do this once) | ||
err = rr.prepareFromByteReader() | ||
} | ||
// indicates that the request body needs to be reset | ||
rr.reset = true | ||
} | ||
return err | ||
} | ||
|
||
func removeRequestBody(req *http.Request) { | ||
req.Body = nil | ||
req.GetBody = nil | ||
req.ContentLength = 0 | ||
} |
Oops, something went wrong.