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

add concurrency for client's HostClient map #1550

Merged
merged 7 commits into from
May 11, 2023
Merged
Changes from 5 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
110 changes: 61 additions & 49 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,13 +303,24 @@ type Client struct {
// ConfigureClient configures the fasthttp.HostClient.
ConfigureClient func(hc *HostClient) error

mLock sync.Mutex
mLock sync.RWMutex
mOnce sync.Once
m map[string]*HostClient
ms map[string]*HostClient
readerPool sync.Pool
writerPool sync.Pool
}

// GetHostClients return the map of Http HostClient
func (c *Client) GetHostClients() map[string]*HostClient {
return c.m
}

// GetHttpsHostClients return the map of Https HostClient
func (c *Client) GetHttpsHostClients() map[string]*HostClient {
return c.ms
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why add these methods? Since m and ms are protected by mLock it's impossible to use the return value of these functions in a safe way?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is test code. I will delete it


// Get returns the status code and body of url.
//
// The contents of dst will be replaced by the body and returned, if the dst
Expand Down Expand Up @@ -485,68 +496,69 @@ func (c *Client) Do(req *Request, resp *Response) error {
return fmt.Errorf("unsupported protocol %q. http and https are supported", uri.Scheme())
}

c.mOnce.Do(func() {
c.m = make(map[string]*HostClient)
c.ms = make(map[string]*HostClient)
xuxiao415 marked this conversation as resolved.
Show resolved Hide resolved
})

startCleaner := false

c.mLock.Lock()
c.mLock.RLock()
m := c.m
if isTLS {
m = c.ms
}
if m == nil {
m = make(map[string]*HostClient)
if isTLS {
c.ms = m
} else {
c.m = m
}
}
hc := m[string(host)]
c.mLock.RUnlock()
if hc == nil {
hc = &HostClient{
Addr: AddMissingPort(string(host), isTLS),
Name: c.Name,
NoDefaultUserAgentHeader: c.NoDefaultUserAgentHeader,
Dial: c.Dial,
DialDualStack: c.DialDualStack,
IsTLS: isTLS,
TLSConfig: c.TLSConfig,
MaxConns: c.MaxConnsPerHost,
MaxIdleConnDuration: c.MaxIdleConnDuration,
MaxConnDuration: c.MaxConnDuration,
MaxIdemponentCallAttempts: c.MaxIdemponentCallAttempts,
ReadBufferSize: c.ReadBufferSize,
WriteBufferSize: c.WriteBufferSize,
ReadTimeout: c.ReadTimeout,
WriteTimeout: c.WriteTimeout,
MaxResponseBodySize: c.MaxResponseBodySize,
DisableHeaderNamesNormalizing: c.DisableHeaderNamesNormalizing,
DisablePathNormalizing: c.DisablePathNormalizing,
MaxConnWaitTimeout: c.MaxConnWaitTimeout,
RetryIf: c.RetryIf,
ConnPoolStrategy: c.ConnPoolStrategy,
StreamResponseBody: c.StreamResponseBody,
clientReaderPool: &c.readerPool,
clientWriterPool: &c.writerPool,
}

if c.ConfigureClient != nil {
if err := c.ConfigureClient(hc); err != nil {
c.mLock.Unlock()
return err
c.mLock.Lock()
hc = m[string(host)]
if hc == nil {
hc = &HostClient{
Addr: AddMissingPort(string(host), isTLS),
Name: c.Name,
NoDefaultUserAgentHeader: c.NoDefaultUserAgentHeader,
Dial: c.Dial,
DialDualStack: c.DialDualStack,
IsTLS: isTLS,
TLSConfig: c.TLSConfig,
MaxConns: c.MaxConnsPerHost,
MaxIdleConnDuration: c.MaxIdleConnDuration,
MaxConnDuration: c.MaxConnDuration,
MaxIdemponentCallAttempts: c.MaxIdemponentCallAttempts,
ReadBufferSize: c.ReadBufferSize,
WriteBufferSize: c.WriteBufferSize,
ReadTimeout: c.ReadTimeout,
WriteTimeout: c.WriteTimeout,
MaxResponseBodySize: c.MaxResponseBodySize,
DisableHeaderNamesNormalizing: c.DisableHeaderNamesNormalizing,
DisablePathNormalizing: c.DisablePathNormalizing,
MaxConnWaitTimeout: c.MaxConnWaitTimeout,
RetryIf: c.RetryIf,
ConnPoolStrategy: c.ConnPoolStrategy,
StreamResponseBody: c.StreamResponseBody,
clientReaderPool: &c.readerPool,
clientWriterPool: &c.writerPool,
}

if c.ConfigureClient != nil {
if err := c.ConfigureClient(hc); err != nil {
c.mLock.Unlock()
return err
}
}
}

m[string(host)] = hc
if len(m) == 1 {
startCleaner = true
m[string(host)] = hc
if len(m) == 1 {
startCleaner = true
}
}
c.mLock.Unlock()
}

atomic.AddInt32(&hc.pendingClientRequests, 1)
defer atomic.AddInt32(&hc.pendingClientRequests, -1)

c.mLock.Unlock()

if startCleaner {
go c.mCleaner(m)
}
Expand All @@ -559,14 +571,14 @@ func (c *Client) Do(req *Request, resp *Response) error {
// "keep-alive" state. It does not interrupt any connections currently
// in use.
func (c *Client) CloseIdleConnections() {
c.mLock.Lock()
c.mLock.RLock()
for _, v := range c.m {
v.CloseIdleConnections()
}
for _, v := range c.ms {
v.CloseIdleConnections()
}
c.mLock.Unlock()
c.mLock.RUnlock()
}

func (c *Client) mCleaner(m map[string]*HostClient) {
Expand Down