Skip to content

Commit

Permalink
Merge pull request #172 from hxmhlt/fix_url_concurrentmap_bug
Browse files Browse the repository at this point in the history
Fix:url.Values is not safe map
  • Loading branch information
AlexStocks authored Aug 27, 2019
2 parents 14436e9 + ae028f9 commit dfc90b5
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions common/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"net/url"
"strconv"
"strings"
"sync"
)

import (
Expand Down Expand Up @@ -64,10 +65,12 @@ func (t RoleType) Role() string {
}

type baseUrl struct {
Protocol string
Location string // ip+port
Ip string
Port string
Protocol string
Location string // ip+port
Ip string
Port string
//url.Values is not safe map, add to avoid concurrent map read and map write error
paramsLock sync.RWMutex
Params url.Values
PrimitiveURL string
ctx context.Context
Expand Down Expand Up @@ -287,14 +290,18 @@ func (c URL) Service() string {
}

func (c *URL) AddParam(key string, value string) {
c.paramsLock.Lock()
c.Params.Add(key, value)
c.paramsLock.Unlock()
}

func (c URL) GetParam(s string, d string) string {
var r string
if r = c.Params.Get(s); r == "" {
c.paramsLock.RLock()
if r = c.Params.Get(s); len(r) == 0 {
r = d
}
c.paramsLock.RUnlock()
return r
}
func (c URL) GetParamAndDecoded(key string) (string, error) {
Expand Down

0 comments on commit dfc90b5

Please sign in to comment.