Skip to content

Commit

Permalink
Added comments for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
apourchet committed Mar 1, 2019
1 parent bb976e7 commit 4303b79
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ func (c *Client) Send(req *http.Request, v interface{}) error {
// client.Token will be updated when changed
func (c *Client) SendWithAuth(req *http.Request, v interface{}) error {
c.Lock()
// Note: Here we do not want to `defer c.Unlock()` because we need `c.Send(...)`
// to happen outside of the locked section.

if c.Token != nil {
if !c.tokenExpiresAt.IsZero() && c.tokenExpiresAt.Sub(time.Now()) < RequestNewTokenBeforeExpiresIn {
// c.Token will be updated in GetAccessToken call
Expand All @@ -138,6 +141,8 @@ func (c *Client) SendWithAuth(req *http.Request, v interface{}) error {
req.Header.Set("Authorization", "Bearer "+c.Token.Token)
}

// Unlock the client mutex before sending the request, this allows multiple requests
// to be in progress at the same time.
c.Unlock()
return c.Send(req, v)
}
Expand Down

0 comments on commit 4303b79

Please sign in to comment.