Skip to content

Commit

Permalink
client: Modify some interfaces to be exportable
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangyongding committed Jan 10, 2025
1 parent 413f9fd commit 817b7fe
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
56 changes: 28 additions & 28 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1519,7 +1519,7 @@ func (c *HostClient) SetMaxConns(newMaxConns int) {
c.connsLock.Unlock()
}

func (c *HostClient) acquireConn(reqTimeout time.Duration, connectionClose bool) (cc *clientConn, err error) {
func (c *HostClient) AcquireConn(reqTimeout time.Duration, connectionClose bool) (cc *clientConn, err error) {
createConn := false
startCleaner := false

Expand Down Expand Up @@ -1642,7 +1642,7 @@ func (c *HostClient) dialConnFor(w *wantConn) {
cc := acquireClientConn(conn)
if !w.tryDeliver(cc, nil) {
// not delivered, return idle connection
c.releaseConn(cc)
c.ReleaseConn(cc)
}
}

Expand All @@ -1660,7 +1660,7 @@ func (c *HostClient) CloseIdleConnections() {
c.connsLock.Unlock()

for _, cc := range scratch {
c.closeConn(cc)
c.CloseConn(cc)
}
}

Expand Down Expand Up @@ -1701,7 +1701,7 @@ func (c *HostClient) connsCleaner() {

// Close idle connections.
for i, cc := range scratch {
c.closeConn(cc)
c.CloseConn(cc)
scratch[i] = nil
}

Expand All @@ -1720,7 +1720,7 @@ func (c *HostClient) connsCleaner() {
}
}

func (c *HostClient) closeConn(cc *clientConn) {
func (c *HostClient) CloseConn(cc *clientConn) {
c.decConnsCount()
cc.c.Close()
releaseClientConn(cc)
Expand Down Expand Up @@ -1780,7 +1780,7 @@ func releaseClientConn(cc *clientConn) {

var clientConnPool sync.Pool

func (c *HostClient) releaseConn(cc *clientConn) {
func (c *HostClient) ReleaseConn(cc *clientConn) {
cc.lastUseTime = time.Now()
if c.MaxConnWaitTimeout <= 0 {
c.connsLock.Lock()
Expand Down Expand Up @@ -1818,7 +1818,7 @@ func (c *HostClient) releaseConn(cc *clientConn) {
}
}

func (c *HostClient) acquireWriter(conn net.Conn) *bufio.Writer {
func (c *HostClient) AcquireWriter(conn net.Conn) *bufio.Writer {
var v any
if c.clientWriterPool != nil {
v = c.clientWriterPool.Get()
Expand All @@ -1845,15 +1845,15 @@ func (c *HostClient) acquireWriter(conn net.Conn) *bufio.Writer {
return bw
}

func (c *HostClient) releaseWriter(bw *bufio.Writer) {
func (c *HostClient) ReleaseWriter(bw *bufio.Writer) {
if c.clientWriterPool != nil {
c.clientWriterPool.Put(bw)
} else {
c.writerPool.Put(bw)
}
}

func (c *HostClient) acquireReader(conn net.Conn) *bufio.Reader {
func (c *HostClient) AcquireReader(conn net.Conn) *bufio.Reader {
var v any
if c.clientReaderPool != nil {
v = c.clientReaderPool.Get()
Expand All @@ -1880,7 +1880,7 @@ func (c *HostClient) acquireReader(conn net.Conn) *bufio.Reader {
return br
}

func (c *HostClient) releaseReader(br *bufio.Reader) {
func (c *HostClient) ReleaseReader(br *bufio.Reader) {
if c.clientReaderPool != nil {
c.clientReaderPool.Put(br)
} else {
Expand Down Expand Up @@ -2145,7 +2145,7 @@ func (w *wantConn) cancel(c *HostClient, err error) {
w.mu.Unlock()

if conn != nil {
c.releaseConn(conn)
c.ReleaseConn(conn)
}
}

Expand Down Expand Up @@ -2826,7 +2826,7 @@ func (c *pipelineConnClient) writer(conn net.Conn, stopCh <-chan struct{}) error
continue
}

w.resp.parseNetConn(conn)
w.resp.ParseNetConn(conn)

if writeTimeout > 0 {
// Set Deadline every time, since golang has fixed the performance issue
Expand Down Expand Up @@ -2973,13 +2973,13 @@ func (t *transport) RoundTrip(hc *HostClient, req *Request, resp *Response) (ret
deadline = time.Now().Add(req.timeout)
}

cc, err := hc.acquireConn(req.timeout, req.ConnectionClose())
cc, err := hc.AcquireConn(req.timeout, req.ConnectionClose())
if err != nil {
return false, err
}
conn := cc.c

resp.parseNetConn(conn)
resp.ParseNetConn(conn)

writeDeadline := deadline
if hc.WriteTimeout > 0 {
Expand All @@ -2990,7 +2990,7 @@ func (t *transport) RoundTrip(hc *HostClient, req *Request, resp *Response) (ret
}

if err = conn.SetWriteDeadline(writeDeadline); err != nil {
hc.closeConn(cc)
hc.CloseConn(cc)
return true, err
}

Expand All @@ -3000,7 +3000,7 @@ func (t *transport) RoundTrip(hc *HostClient, req *Request, resp *Response) (ret
resetConnection = true
}

bw := hc.acquireWriter(conn)
bw := hc.AcquireWriter(conn)
err = req.Write(bw)

if resetConnection {
Expand All @@ -3010,15 +3010,15 @@ func (t *transport) RoundTrip(hc *HostClient, req *Request, resp *Response) (ret
if err == nil {
err = bw.Flush()
}
hc.releaseWriter(bw)
hc.ReleaseWriter(bw)

// Return ErrTimeout on any timeout.
if x, ok := err.(interface{ Timeout() bool }); ok && x.Timeout() {
err = ErrTimeout
}

if err != nil {
hc.closeConn(cc)
hc.CloseConn(cc)
return true, err
}

Expand All @@ -3031,7 +3031,7 @@ func (t *transport) RoundTrip(hc *HostClient, req *Request, resp *Response) (ret
}

if err = conn.SetReadDeadline(readDeadline); err != nil {
hc.closeConn(cc)
hc.CloseConn(cc)
return true, err
}

Expand All @@ -3042,11 +3042,11 @@ func (t *transport) RoundTrip(hc *HostClient, req *Request, resp *Response) (ret
resp.Header.DisableNormalizing()
}

br := hc.acquireReader(conn)
br := hc.AcquireReader(conn)
err = resp.ReadLimitBody(br, hc.MaxResponseBodySize)
if err != nil {
hc.releaseReader(br)
hc.closeConn(cc)
hc.ReleaseReader(br)
hc.CloseConn(cc)
// Don't retry in case of ErrBodyTooLarge since we will just get the same again.
needRetry := err != ErrBodyTooLarge
return needRetry, err
Expand All @@ -3056,25 +3056,25 @@ func (t *transport) RoundTrip(hc *HostClient, req *Request, resp *Response) (ret
if customStreamBody && resp.bodyStream != nil {
rbs := resp.bodyStream
resp.bodyStream = newCloseReaderWithError(rbs, func(wErr error) error {
hc.releaseReader(br)
hc.ReleaseReader(br)
if r, ok := rbs.(*requestStream); ok {
releaseRequestStream(r)
}
if closeConn || resp.ConnectionClose() || wErr != nil {
hc.closeConn(cc)
hc.CloseConn(cc)
} else {
hc.releaseConn(cc)
hc.ReleaseConn(cc)
}
return nil
})
return false, nil
}
hc.releaseReader(br)
hc.ReleaseReader(br)

if closeConn {
hc.closeConn(cc)
hc.CloseConn(cc)
} else {
hc.releaseConn(cc)
hc.ReleaseConn(cc)
}
return false, nil
}
2 changes: 1 addition & 1 deletion http.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ func (w *requestBodyWriter) Write(p []byte) (int, error) {
return len(p), nil
}

func (resp *Response) parseNetConn(conn net.Conn) {
func (resp *Response) ParseNetConn(conn net.Conn) {
resp.raddr = conn.RemoteAddr()
resp.laddr = conn.LocalAddr()
}
Expand Down

0 comments on commit 817b7fe

Please sign in to comment.