Skip to content

Commit

Permalink
fix data race
Browse files Browse the repository at this point in the history
We cannot refer to t.Bearer during t.Generate because we changed it
during t.Generate.
  • Loading branch information
catatsuy committed Jan 21, 2020
1 parent 35735f0 commit dd55128
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
4 changes: 2 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ func (c *Client) CloseIdleConnections() {
}

func (c *Client) setTokenHeader(r *http.Request) {
c.Token.GenerateIfExpired()
r.Header.Set("authorization", fmt.Sprintf("bearer %v", c.Token.Bearer))
bearer := c.Token.GenerateIfExpired()
r.Header.Set("authorization", fmt.Sprintf("bearer %v", bearer))
}

func setHeaders(r *http.Request, n *Notification) {
Expand Down
3 changes: 2 additions & 1 deletion token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,13 @@ func AuthKeyFromBytes(bytes []byte) (*ecdsa.PrivateKey, error) {

// GenerateIfExpired checks to see if the token is about to expire and
// generates a new token.
func (t *Token) GenerateIfExpired() {
func (t *Token) GenerateIfExpired() (bearer string) {
t.Lock()
defer t.Unlock()
if t.Expired() {
t.Generate()
}
return t.Bearer
}

// Expired checks to see if the token has expired.
Expand Down

0 comments on commit dd55128

Please sign in to comment.