Skip to content

Commit 24a351f

Browse files
authored
Fix data race (#164)
We cannot refer to t.Bearer during t.Generate because we changed it during t.Generate.
1 parent 35735f0 commit 24a351f

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@ func (c *Client) CloseIdleConnections() {
198198
}
199199

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

205205
func setHeaders(r *http.Request, n *Notification) {

token/token.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,13 @@ func AuthKeyFromBytes(bytes []byte) (*ecdsa.PrivateKey, error) {
6868

6969
// GenerateIfExpired checks to see if the token is about to expire and
7070
// generates a new token.
71-
func (t *Token) GenerateIfExpired() {
71+
func (t *Token) GenerateIfExpired() (bearer string) {
7272
t.Lock()
7373
defer t.Unlock()
7474
if t.Expired() {
7575
t.Generate()
7676
}
77+
return t.Bearer
7778
}
7879

7980
// Expired checks to see if the token has expired.

0 commit comments

Comments
 (0)