Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

client: add DoCtx for fasthttp.Client #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,28 @@ func (c *Client) Post(dst []byte, url string, postArgs *Args) (statusCode int, b
return clientPostURL(dst, url, postArgs, c)
}

// DoCtx performs the given request and waits for response until
// the given context is cancelled or deadline is reached.
//
// Request must contain at least non-zero RequestURI with full url (including
// scheme and host) or non-zero Host header + RequestURI.
//
// The function doesn't follow redirects. Use Get* for following redirects.
//
// Response is ignored if resp is nil.
//
// ErrTimeout is returned if the response wasn't returned until
// the deadline provided by the given context.
//
// ErrNoFreeConns is returned if all HostClient.MaxConns connections
// to the host are busy.
//
// It is recommended obtaining req and resp via AcquireRequest
// and AcquireResponse in performance-critical code.
func (c *Client) DoCtx(ctx context.Context, req *Request, resp *Response) error {
return clientDoCtx(ctx, req, resp, c)
}

// DoTimeout performs the given request and waits for response during
// the given timeout duration.
//
Expand Down Expand Up @@ -400,11 +422,15 @@ func (c *Client) Do(req *Request, resp *Response) error {

func (c *Client) mCleaner(m map[string]*HostClient) {
mustStop := false
unusedTimeout := c.MaxIdleConnDuration
if unusedTimeout <= 0 {
unusedTimeout = time.Minute
}
for {
t := time.Now()
c.mLock.Lock()
for k, v := range m {
if t.Sub(v.LastUseTime()) > time.Minute {
if t.Sub(v.LastUseTime()) > unusedTimeout {
delete(m, k)
}
}
Expand Down