Skip to content

Commit 5310eda

Browse files
authored
Properly synchronizing access to the headers, to prevent concurrent map iteration and map write error (#50)
1 parent 3193e88 commit 5310eda

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

presto/client.go

+11
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"pbench/log"
1212
"reflect"
1313
"strings"
14+
"sync"
1415
"time"
1516
)
1617

@@ -42,6 +43,7 @@ type Client struct {
4243
baseHeader http.Header
4344
isTrino bool
4445
forceHttps bool
46+
headerMutex sync.RWMutex
4547
}
4648

4749
func NewClient(serverUrl string, isTrino bool) (*Client, error) {
@@ -105,20 +107,26 @@ func (c *Client) GetHost() string {
105107
}
106108

107109
func (c *Client) setHeader(key, value string) {
110+
c.headerMutex.Lock()
111+
defer c.headerMutex.Unlock()
108112
if c.isTrino {
109113
key = strings.Replace(key, "X-Presto", "X-Trino", 1)
110114
}
111115
c.baseHeader.Set(key, value)
112116
}
113117

114118
func (c *Client) delHeader(key string) {
119+
c.headerMutex.Lock()
120+
defer c.headerMutex.Unlock()
115121
if c.isTrino {
116122
key = strings.Replace(key, "X-Presto", "X-Trino", 1)
117123
}
118124
c.baseHeader.Del(key)
119125
}
120126

121127
func (c *Client) getHeader(key string) string {
128+
c.headerMutex.RLock()
129+
defer c.headerMutex.RUnlock()
122130
if c.isTrino {
123131
key = strings.Replace(key, "X-Presto", "X-Trino", 1)
124132
}
@@ -280,7 +288,10 @@ func (c *Client) NewRequest(method, urlStr string, body interface{}, opts ...Req
280288
return nil, err
281289
}
282290

291+
c.headerMutex.RLock()
283292
req.Header = c.baseHeader.Clone()
293+
c.headerMutex.RUnlock()
294+
284295
if password, ok := c.userInfo.Password(); ok {
285296
req.SetBasicAuth(c.userInfo.Username(), password)
286297
}

0 commit comments

Comments
 (0)