Skip to content

Commit

Permalink
Merge pull request #132 from caozhipan/fix-activeQPS-zero
Browse files Browse the repository at this point in the history
setDeadLine & retry
  • Loading branch information
zhangxu19830126 committed May 8, 2019
2 parents fbfb3ac + d719e31 commit 7d9a447
Showing 1 changed file with 18 additions and 32 deletions.
50 changes: 18 additions & 32 deletions pkg/util/fasthttp_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,23 +202,17 @@ type clientConn struct {

// Do do a http request
func (c *FastHTTPClient) Do(req *fasthttp.Request, addr string, option *HTTPOption) (*fasthttp.Response, error) {
resp, retry, err := c.do(req, addr, option)
if err != nil && retry && isIdempotent(req) {
resp, _, err = c.do(req, addr, option)
}
if err == io.EOF {
err = fasthttp.ErrConnectionClosed
}
resp, err := c.do(req, addr, option)
return resp, err
}

func (c *FastHTTPClient) do(req *fasthttp.Request, addr string, option *HTTPOption) (*fasthttp.Response, bool, error) {
func (c *FastHTTPClient) do(req *fasthttp.Request, addr string, option *HTTPOption) (*fasthttp.Response, error) {
resp := fasthttp.AcquireResponse()
ok, err := c.doNonNilReqResp(req, resp, addr, option)
return resp, ok, err
err := c.doNonNilReqResp(req, resp, addr, option)
return resp, err
}

func (c *FastHTTPClient) doNonNilReqResp(req *fasthttp.Request, resp *fasthttp.Response, addr string, option *HTTPOption) (bool, error) {
func (c *FastHTTPClient) doNonNilReqResp(req *fasthttp.Request, resp *fasthttp.Response, addr string, option *HTTPOption) error {
if req == nil {
panic("BUG: req cannot be nil")
}
Expand Down Expand Up @@ -248,7 +242,7 @@ func (c *FastHTTPClient) doNonNilReqResp(req *fasthttp.Request, resp *fasthttp.R

cc, err := hc.acquireConn(addr)
if err != nil {
return false, err
return err
}
conn := cc.c

Expand All @@ -258,13 +252,11 @@ func (c *FastHTTPClient) doNonNilReqResp(req *fasthttp.Request, resp *fasthttp.R
// of the last write deadline exceeded.
// See https://github.com/golang/go/issues/15133 for details.
currentTime := time.Now()
if currentTime.Sub(cc.lastWriteDeadlineTime) > (opt.WriteTimeout >> 2) {
if err = conn.SetWriteDeadline(currentTime.Add(opt.WriteTimeout)); err != nil {
hc.closeConn(cc)
return true, err
}
cc.lastWriteDeadlineTime = currentTime
if err = conn.SetWriteDeadline(currentTime.Add(opt.WriteTimeout)); err != nil {
hc.closeConn(cc)
return err
}
cc.lastWriteDeadlineTime = currentTime
}

resetConnection := false
Expand All @@ -286,7 +278,7 @@ func (c *FastHTTPClient) doNonNilReqResp(req *fasthttp.Request, resp *fasthttp.R
if err != nil {
c.releaseWriter(bw)
hc.closeConn(cc)
return true, err
return err
}
c.releaseWriter(bw)

Expand All @@ -296,13 +288,11 @@ func (c *FastHTTPClient) doNonNilReqResp(req *fasthttp.Request, resp *fasthttp.R
// of the last read deadline exceeded.
// See https://github.com/golang/go/issues/15133 for details.
currentTime := time.Now()
if currentTime.Sub(cc.lastReadDeadlineTime) > (opt.ReadTimeout >> 2) {
if err = conn.SetReadDeadline(currentTime.Add(opt.ReadTimeout)); err != nil {
hc.closeConn(cc)
return true, err
}
cc.lastReadDeadlineTime = currentTime
if err = conn.SetReadDeadline(currentTime.Add(opt.ReadTimeout)); err != nil {
hc.closeConn(cc)
return err
}
cc.lastReadDeadlineTime = currentTime
}

if !req.Header.IsGet() && req.Header.IsHead() {
Expand All @@ -314,9 +304,9 @@ func (c *FastHTTPClient) doNonNilReqResp(req *fasthttp.Request, resp *fasthttp.R
c.releaseReader(br)
hc.closeConn(cc)
if err == io.EOF {
return true, err
return err
}
return false, err
return err
}
c.releaseReader(br)

Expand All @@ -326,7 +316,7 @@ func (c *FastHTTPClient) doNonNilReqResp(req *fasthttp.Request, resp *fasthttp.R
hc.releaseConn(cc)
}

return false, err
return err
}

func dialAddr(addr string) (net.Conn, error) {
Expand Down Expand Up @@ -369,10 +359,6 @@ func (c *FastHTTPClient) releaseReader(br *bufio.Reader) {
c.readerPool.Put(br)
}

func isIdempotent(req *fasthttp.Request) bool {
return req.Header.IsGet() || req.Header.IsHead() || req.Header.IsPut()
}

func acquireClientConn(conn net.Conn) *clientConn {
v := clientConnPool.Get()
if v == nil {
Expand Down

0 comments on commit 7d9a447

Please sign in to comment.