Skip to content

Commit

Permalink
Added option to close connections on HTTPRequest
Browse files Browse the repository at this point in the history
Signed-off-by: André Martins <[email protected]>
  • Loading branch information
aanm committed Jul 8, 2016
1 parent 73f3362 commit 84d1c0e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
8 changes: 8 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ type Client struct {
scheme string
proxyURL *url.URL
mutex *sync.Mutex
closeConnection bool
beforeRequest []func(*Client, *Request) error
afterResponse []func(*Client, *Response) error
}
Expand Down Expand Up @@ -595,6 +596,13 @@ func (c *Client) SetScheme(scheme string) *Client {
return c
}

// SetCloseConnection method sets variable Close in http request struct with the given
// value. More info: https://golang.org/src/net/http/request.go
func (c *Client) SetCloseConnection(close bool) *Client {
c.closeConnection = close
return c
}

// executes the given `Request` object and returns response
func (c *Client) execute(req *Request) (*Response, error) {
// Apply Request middleware
Expand Down
6 changes: 6 additions & 0 deletions default.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,12 @@ func SetScheme(scheme string) *Client {
return DefaultClient.SetScheme(scheme)
}

// SetCloseConnection method sets close connection value in the resty client.
// See `Client.SetCloseConnection` for more information.
func SetCloseConnection(close bool) *Client {
return DefaultClient.SetCloseConnection(close)
}

func init() {
DefaultClient = New()
}
4 changes: 4 additions & 0 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ func createHTTPRequest(c *Client, r *Request) (err error) {
r.RawRequest, err = http.NewRequest(r.Method, r.URL, r.bodyBuf)
}

if err == nil {
r.RawRequest.Close = c.closeConnection
}

// Add headers into http request
r.RawRequest.Header = r.Header

Expand Down
3 changes: 3 additions & 0 deletions resty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1350,6 +1350,9 @@ func TestClientOptions(t *testing.T) {
SetScheme("http")
assertEqual(t, DefaultClient.scheme, "http")

SetCloseConnection(true)
assertEqual(t, DefaultClient.closeConnection, true)

SetLogger(ioutil.Discard)
}

Expand Down

0 comments on commit 84d1c0e

Please sign in to comment.