From b17e49e6aae4ef19a649104e654b7dd8acbcd2f0 Mon Sep 17 00:00:00 2001 From: Wusuluren <1634636348@qq.com> Date: Tue, 30 Apr 2019 00:15:24 +0800 Subject: [PATCH] fix concurrent map access problem on BeegoInput.data --- context/input.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/context/input.go b/context/input.go index 81952158..76040616 100644 --- a/context/input.go +++ b/context/input.go @@ -27,6 +27,7 @@ import ( "regexp" "strconv" "strings" + "sync" "github.com/astaxie/beego/session" ) @@ -49,6 +50,7 @@ type BeegoInput struct { pnames []string pvalues []string data map[interface{}]interface{} // store some values in this context when calling context in filter or controller. + dataLock sync.RWMutex RequestBody []byte RunMethod string RunController reflect.Type @@ -204,6 +206,7 @@ func (input *BeegoInput) AcceptsXML() bool { func (input *BeegoInput) AcceptsJSON() bool { return acceptsJSONRegex.MatchString(input.Header("Accept")) } + // AcceptsYAML Checks if request accepts json response func (input *BeegoInput) AcceptsYAML() bool { return acceptsYAMLRegex.MatchString(input.Header("Accept")) @@ -377,6 +380,8 @@ func (input *BeegoInput) CopyBody(MaxMemory int64) []byte { // Data return the implicit data in the input func (input *BeegoInput) Data() map[interface{}]interface{} { + input.dataLock.Lock() + defer input.dataLock.Unlock() if input.data == nil { input.data = make(map[interface{}]interface{}) } @@ -385,6 +390,8 @@ func (input *BeegoInput) Data() map[interface{}]interface{} { // GetData returns the stored data in this context. func (input *BeegoInput) GetData(key interface{}) interface{} { + input.dataLock.Lock() + defer input.dataLock.Unlock() if v, ok := input.data[key]; ok { return v } @@ -394,6 +401,8 @@ func (input *BeegoInput) GetData(key interface{}) interface{} { // SetData stores data with given key in this context. // This data are only available in this context. func (input *BeegoInput) SetData(key, val interface{}) { + input.dataLock.Lock() + defer input.dataLock.Unlock() if input.data == nil { input.data = make(map[interface{}]interface{}) }