Skip to content

Commit

Permalink
Support EnableCloseConnection (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
imroc committed Nov 29, 2022
1 parent a91b8f9 commit 51a6c64
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,7 @@ func (c *Client) roundTrip(r *Request) (resp *Response, err error) {
ContentLength: contentLength,
Body: reqBody,
GetBody: r.GetBody,
Close: r.close,
}
for _, cookie := range r.Cookies {
req.AddCookie(cookie)
Expand Down
12 changes: 12 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type Request struct {
isMultiPart bool
forceChunkedEncoding bool
isSaveResponse bool
close bool
error error
client *Client
uploadCallback UploadCallback
Expand Down Expand Up @@ -1052,3 +1053,14 @@ func (r *Request) SetClient(client *Client) *Request {
func (r *Request) GetClient() *Client {
return r.client
}

// EnableCloseConnection closes the connection after sending this
// request and reading its response if set to true in HTTP/1.1 and
// HTTP/2.
//
// Setting this field prevents re-use of TCP connections between
// requests to the same hosts event if EnableKeepAlives() were called.
func (r *Request) EnableCloseConnection() *Request {
r.close = true
return r
}
6 changes: 6 additions & 0 deletions request_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,3 +481,9 @@ func SetDownloadCallback(callback DownloadCallback) *Request {
func SetDownloadCallbackWithInterval(callback DownloadCallback, minInterval time.Duration) *Request {
return defaultClient.R().SetDownloadCallbackWithInterval(callback, minInterval)
}

// EnableCloseConnection is a global wrapper methods which delegated
// to the default client, create a request and EnableCloseConnection for request.
func EnableCloseConnection() *Request {
return defaultClient.R().EnableCloseConnection()
}

0 comments on commit 51a6c64

Please sign in to comment.