1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-04 21:20:54 +00:00

no need lock here

This commit is contained in:
JessonChan 2015-04-26 15:24:04 +08:00
parent 973306b28d
commit 0afd04ec6f

View File

@ -47,26 +47,20 @@ import (
"net/url" "net/url"
"os" "os"
"strings" "strings"
"sync"
"time" "time"
) )
var defaultSetting = BeegoHttpSettings{UserAgent: "beegoServer", ConnectTimeout: 60 * time.Second, ReadWriteTimeout: 60 * time.Second, Gzip: true} var defaultSetting = BeegoHttpSettings{UserAgent: "beegoServer", ConnectTimeout: 60 * time.Second, ReadWriteTimeout: 60 * time.Second, Gzip: true}
var defaultCookieJar http.CookieJar var defaultCookieJar http.CookieJar
var settingMutex sync.Mutex
// createDefaultCookie creates a global cookiejar to store cookies. // createDefaultCookie creates a global cookiejar to store cookies.
func createDefaultCookie() { func createDefaultCookie() {
settingMutex.Lock()
defaultCookieJar, _ = cookiejar.New(nil) defaultCookieJar, _ = cookiejar.New(nil)
settingMutex.Unlock()
} }
// Overwrite default settings // Overwrite default settings
func SetDefaultSetting(setting BeegoHttpSettings) { func SetDefaultSetting(setting BeegoHttpSettings) {
settingMutex.Lock()
defaultSetting = setting defaultSetting = setting
settingMutex.Unlock()
} }
// return *BeegoHttpRequest with specific method // return *BeegoHttpRequest with specific method
@ -82,7 +76,7 @@ func newBeegoRequest(url, method string) *BeegoHttpRequest {
return &BeegoHttpRequest{ return &BeegoHttpRequest{
url: url, url: url,
req: &req, req: &req,
params: map[string]string{}, params: map[string]string{},
files: map[string]string{}, files: map[string]string{},
setting: defaultSetting, setting: defaultSetting,
resp: &resp, resp: &resp,
@ -256,11 +250,11 @@ func (b *BeegoHttpRequest) PostFile(formname, filename string) *BeegoHttpRequest
// it supports string and []byte. // it supports string and []byte.
func (b *BeegoHttpRequest) Body(data interface{}) *BeegoHttpRequest { func (b *BeegoHttpRequest) Body(data interface{}) *BeegoHttpRequest {
switch t := data.(type) { switch t := data.(type) {
case string: case string:
bf := bytes.NewBufferString(t) bf := bytes.NewBufferString(t)
b.req.Body = ioutil.NopCloser(bf) b.req.Body = ioutil.NopCloser(bf)
b.req.ContentLength = int64(len(t)) b.req.ContentLength = int64(len(t))
case []byte: case []byte:
bf := bytes.NewBuffer(t) bf := bytes.NewBuffer(t)
b.req.Body = ioutil.NopCloser(bf) b.req.Body = ioutil.NopCloser(bf)
b.req.ContentLength = int64(len(t)) b.req.ContentLength = int64(len(t))