Skip to content

Commit

Permalink
client.go Simplify default UA logic (#1466)
Browse files Browse the repository at this point in the history
The getClientName() checks if !NoDefaultUserAgentHeader then returns the Client.Name field.
But it also saves it to atomic field clientName. This is not needed and logic can be simplified.
Previously the clientName vas a byte slice that was copied from c.Name and cached. See 02e0722

Fix #1458
  • Loading branch information
stokito authored Jan 2, 2023
1 parent e46afc3 commit 51048b0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 38 deletions.
57 changes: 21 additions & 36 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,6 @@ type HostClient struct {
// Connection pool strategy. Can be either LIFO or FIFO (default).
ConnPoolStrategy ConnPoolStrategyType

clientName atomic.Value
lastUseTime uint32

connsLock sync.Mutex
Expand Down Expand Up @@ -1327,9 +1326,14 @@ func (c *HostClient) doNonNilReqResp(req *Request, resp *Response) (bool, error)

userAgentOld := req.Header.UserAgent()
if len(userAgentOld) == 0 {
req.Header.userAgent = append(req.Header.userAgent[:0], c.getClientName()...)
userAgent := c.Name
if userAgent == "" && !c.NoDefaultUserAgentHeader {
userAgent = defaultUserAgent
}
if userAgent != "" {
req.Header.userAgent = append(req.Header.userAgent[:], userAgent...)
}
}

if c.Transport != nil {
err := c.Transport(req, resp)
return err == nil, err
Expand Down Expand Up @@ -1990,21 +1994,6 @@ func dialAddr(addr string, dial DialFunc, dialDualStack, isTLS bool, tlsConfig *
return conn, nil
}

func (c *HostClient) getClientName() []byte {
v := c.clientName.Load()
var clientName []byte
if v == nil {
clientName = []byte(c.Name)
if len(clientName) == 0 && !c.NoDefaultUserAgentHeader {
clientName = defaultUserAgent
}
c.clientName.Store(clientName)
} else {
clientName = v.([]byte)
}
return clientName
}

// AddMissingPort adds a port to a host if it is missing.
// A literal IPv6 address in hostport must be enclosed in square
// brackets, as in "[::1]:80", "[::1%lo0]:80".
Expand Down Expand Up @@ -2318,7 +2307,6 @@ type pipelineConnClient struct {

tlsConfigLock sync.Mutex
tlsConfig *tls.Config
clientName atomic.Value
}

type pipelineWork struct {
Expand Down Expand Up @@ -2389,7 +2377,13 @@ func (c *pipelineConnClient) DoDeadline(req *Request, resp *Response, deadline t

userAgentOld := req.Header.UserAgent()
if len(userAgentOld) == 0 {
req.Header.userAgent = append(req.Header.userAgent[:0], c.getClientName()...)
userAgent := c.Name
if userAgent == "" && !c.NoDefaultUserAgentHeader {
userAgent = defaultUserAgent
}
if userAgent != "" {
req.Header.userAgent = append(req.Header.userAgent[:], userAgent...)
}
}

w := c.acquirePipelineWork(timeout)
Expand Down Expand Up @@ -2490,7 +2484,13 @@ func (c *pipelineConnClient) Do(req *Request, resp *Response) error {

userAgentOld := req.Header.UserAgent()
if len(userAgentOld) == 0 {
req.Header.userAgent = append(req.Header.userAgent[:0], c.getClientName()...)
userAgent := c.Name
if userAgent == "" && !c.NoDefaultUserAgentHeader {
userAgent = defaultUserAgent
}
if userAgent != "" {
req.Header.userAgent = append(req.Header.userAgent[:], userAgent...)
}
}

w := c.acquirePipelineWork(0)
Expand Down Expand Up @@ -2886,19 +2886,4 @@ func (c *pipelineConnClient) PendingRequests() int {
return n
}

func (c *pipelineConnClient) getClientName() []byte {
v := c.clientName.Load()
var clientName []byte
if v == nil {
clientName = []byte(c.Name)
if len(clientName) == 0 && !c.NoDefaultUserAgentHeader {
clientName = defaultUserAgent
}
c.clientName.Store(clientName)
} else {
clientName = v.([]byte)
}
return clientName
}

var errPipelineConnStopped = errors.New("pipeline connection has been stopped")
2 changes: 1 addition & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ func TestClientDefaultUserAgent(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if userAgentSeen != string(defaultUserAgent) {
if userAgentSeen != defaultUserAgent {
t.Fatalf("User-Agent defers %q != %q", userAgentSeen, defaultUserAgent)
}
}
Expand Down
2 changes: 1 addition & 1 deletion strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package fasthttp

var (
defaultServerName = []byte("fasthttp")
defaultUserAgent = []byte("fasthttp")
defaultUserAgent = "fasthttp"
defaultContentType = []byte("text/plain; charset=utf-8")
)

Expand Down

0 comments on commit 51048b0

Please sign in to comment.