diff options
author | Anthony-Dong <fanhaodong516@gmail.com> | 2023-04-06 00:56:31 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-05 18:56:31 +0200 |
commit | 6b958c2c222bcf715691e3789d7dc09474241121 (patch) | |
tree | 6dba1629204f2ad81a524006b4db0c398f02ecb4 /client.go | |
parent | format : update some codes style (#1533) (diff) | |
download | fasthttp-6b958c2c222bcf715691e3789d7dc09474241121.tar.gz fasthttp-6b958c2c222bcf715691e3789d7dc09474241121.tar.bz2 fasthttp-6b958c2c222bcf715691e3789d7dc09474241121.zip |
support response body stream (#1414)
* feat: support response body stream
* style: add header interface
* Update http.go
Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com>
* feat: support request、response、client stream
* fix: close reader bug
---------
Co-authored-by: fanhaodong.516 <fanhaodong.516@bytedance.com>
Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com>
Diffstat (limited to 'client.go')
-rw-r--r-- | client.go | 29 |
1 files changed, 27 insertions, 2 deletions
@@ -297,6 +297,9 @@ type Client struct { // Connection pool strategy. Can be either LIFO or FIFO (default). ConnPoolStrategy ConnPoolStrategyType + // StreamResponseBody enables response body streaming + StreamResponseBody bool + // ConfigureClient configures the fasthttp.HostClient. ConfigureClient func(hc *HostClient) error @@ -521,6 +524,7 @@ func (c *Client) Do(req *Request, resp *Response) error { MaxConnWaitTimeout: c.MaxConnWaitTimeout, RetryIf: c.RetryIf, ConnPoolStrategy: c.ConnPoolStrategy, + StreamResponseBody: c.StreamResponseBody, clientReaderPool: &c.readerPool, clientWriterPool: &c.writerPool, } @@ -795,6 +799,9 @@ type HostClient struct { // Connection pool strategy. Can be either LIFO or FIFO (default). ConnPoolStrategy ConnPoolStrategyType + // StreamResponseBody enables response body streaming + StreamResponseBody bool + lastUseTime uint32 connsLock sync.Mutex @@ -1331,8 +1338,10 @@ func (c *HostClient) doNonNilReqResp(req *Request, resp *Response) (bool, error) // backing up SkipBody in case it was set explicitly customSkipBody := resp.SkipBody + customStreamBody := resp.StreamBody || c.StreamResponseBody resp.Reset() resp.SkipBody = customSkipBody + resp.StreamBody = customStreamBody req.URI().DisablePathNormalizing = c.DisablePathNormalizing @@ -1442,12 +1451,28 @@ func (c *HostClient) doNonNilReqResp(req *Request, resp *Response) (bool, error) return retry, err } - if resetConnection || req.ConnectionClose() || resp.ConnectionClose() || isConnRST { + closeConn := resetConnection || req.ConnectionClose() || resp.ConnectionClose() || isConnRST + if customStreamBody && resp.bodyStream != nil { + rbs := resp.bodyStream + resp.bodyStream = newCloseReader(rbs, func() error { + if r, ok := rbs.(*requestStream); ok { + releaseRequestStream(r) + } + if closeConn { + c.closeConn(cc) + } else { + c.releaseConn(cc) + } + return nil + }) + return false, nil + } + + if closeConn { c.closeConn(cc) } else { c.releaseConn(cc) } - return false, nil } |