no need lock here
Showing
1 changed file
with
3 additions
and
9 deletions
| ... | @@ -47,26 +47,20 @@ import ( | ... | @@ -47,26 +47,20 @@ import ( |
| 47 | "net/url" | 47 | "net/url" |
| 48 | "os" | 48 | "os" |
| 49 | "strings" | 49 | "strings" |
| 50 | "sync" | ||
| 51 | "time" | 50 | "time" |
| 52 | ) | 51 | ) |
| 53 | 52 | ||
| 54 | var defaultSetting = BeegoHttpSettings{UserAgent: "beegoServer", ConnectTimeout: 60 * time.Second, ReadWriteTimeout: 60 * time.Second, Gzip: true} | 53 | var defaultSetting = BeegoHttpSettings{UserAgent: "beegoServer", ConnectTimeout: 60 * time.Second, ReadWriteTimeout: 60 * time.Second, Gzip: true} |
| 55 | var defaultCookieJar http.CookieJar | 54 | var defaultCookieJar http.CookieJar |
| 56 | var settingMutex sync.Mutex | ||
| 57 | 55 | ||
| 58 | // createDefaultCookie creates a global cookiejar to store cookies. | 56 | // createDefaultCookie creates a global cookiejar to store cookies. |
| 59 | func createDefaultCookie() { | 57 | func createDefaultCookie() { |
| 60 | settingMutex.Lock() | ||
| 61 | defaultCookieJar, _ = cookiejar.New(nil) | 58 | defaultCookieJar, _ = cookiejar.New(nil) |
| 62 | settingMutex.Unlock() | ||
| 63 | } | 59 | } |
| 64 | 60 | ||
| 65 | // Overwrite default settings | 61 | // Overwrite default settings |
| 66 | func SetDefaultSetting(setting BeegoHttpSettings) { | 62 | func SetDefaultSetting(setting BeegoHttpSettings) { |
| 67 | settingMutex.Lock() | ||
| 68 | defaultSetting = setting | 63 | defaultSetting = setting |
| 69 | settingMutex.Unlock() | ||
| 70 | } | 64 | } |
| 71 | 65 | ||
| 72 | // return *BeegoHttpRequest with specific method | 66 | // return *BeegoHttpRequest with specific method |
| ... | @@ -82,7 +76,7 @@ func newBeegoRequest(url, method string) *BeegoHttpRequest { | ... | @@ -82,7 +76,7 @@ func newBeegoRequest(url, method string) *BeegoHttpRequest { |
| 82 | return &BeegoHttpRequest{ | 76 | return &BeegoHttpRequest{ |
| 83 | url: url, | 77 | url: url, |
| 84 | req: &req, | 78 | req: &req, |
| 85 | params: map[string]string{}, | 79 | params: map[string]string{}, |
| 86 | files: map[string]string{}, | 80 | files: map[string]string{}, |
| 87 | setting: defaultSetting, | 81 | setting: defaultSetting, |
| 88 | resp: &resp, | 82 | resp: &resp, |
| ... | @@ -256,11 +250,11 @@ func (b *BeegoHttpRequest) PostFile(formname, filename string) *BeegoHttpRequest | ... | @@ -256,11 +250,11 @@ func (b *BeegoHttpRequest) PostFile(formname, filename string) *BeegoHttpRequest |
| 256 | // it supports string and []byte. | 250 | // it supports string and []byte. |
| 257 | func (b *BeegoHttpRequest) Body(data interface{}) *BeegoHttpRequest { | 251 | func (b *BeegoHttpRequest) Body(data interface{}) *BeegoHttpRequest { |
| 258 | switch t := data.(type) { | 252 | switch t := data.(type) { |
| 259 | case string: | 253 | case string: |
| 260 | bf := bytes.NewBufferString(t) | 254 | bf := bytes.NewBufferString(t) |
| 261 | b.req.Body = ioutil.NopCloser(bf) | 255 | b.req.Body = ioutil.NopCloser(bf) |
| 262 | b.req.ContentLength = int64(len(t)) | 256 | b.req.ContentLength = int64(len(t)) |
| 263 | case []byte: | 257 | case []byte: |
| 264 | bf := bytes.NewBuffer(t) | 258 | bf := bytes.NewBuffer(t) |
| 265 | b.req.Body = ioutil.NopCloser(bf) | 259 | b.req.Body = ioutil.NopCloser(bf) |
| 266 | b.req.ContentLength = int64(len(t)) | 260 | b.req.ContentLength = int64(len(t)) | ... | ... |
-
Please register or sign in to post a comment