1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-25 20:10:56 +00:00

fix concurrent map access problem on BeegoInput.data

This commit is contained in:
Wusuluren 2019-04-30 00:15:24 +08:00
parent 6a33feee46
commit b17e49e6aa

View File

@ -27,6 +27,7 @@ import (
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"sync"
"github.com/astaxie/beego/session" "github.com/astaxie/beego/session"
) )
@ -49,6 +50,7 @@ type BeegoInput struct {
pnames []string pnames []string
pvalues []string pvalues []string
data map[interface{}]interface{} // store some values in this context when calling context in filter or controller. data map[interface{}]interface{} // store some values in this context when calling context in filter or controller.
dataLock sync.RWMutex
RequestBody []byte RequestBody []byte
RunMethod string RunMethod string
RunController reflect.Type RunController reflect.Type
@ -204,6 +206,7 @@ func (input *BeegoInput) AcceptsXML() bool {
func (input *BeegoInput) AcceptsJSON() bool { func (input *BeegoInput) AcceptsJSON() bool {
return acceptsJSONRegex.MatchString(input.Header("Accept")) return acceptsJSONRegex.MatchString(input.Header("Accept"))
} }
// AcceptsYAML Checks if request accepts json response // AcceptsYAML Checks if request accepts json response
func (input *BeegoInput) AcceptsYAML() bool { func (input *BeegoInput) AcceptsYAML() bool {
return acceptsYAMLRegex.MatchString(input.Header("Accept")) 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 // Data return the implicit data in the input
func (input *BeegoInput) Data() map[interface{}]interface{} { func (input *BeegoInput) Data() map[interface{}]interface{} {
input.dataLock.Lock()
defer input.dataLock.Unlock()
if input.data == nil { if input.data == nil {
input.data = make(map[interface{}]interface{}) 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. // GetData returns the stored data in this context.
func (input *BeegoInput) GetData(key interface{}) interface{} { func (input *BeegoInput) GetData(key interface{}) interface{} {
input.dataLock.Lock()
defer input.dataLock.Unlock()
if v, ok := input.data[key]; ok { if v, ok := input.data[key]; ok {
return v return v
} }
@ -394,6 +401,8 @@ func (input *BeegoInput) GetData(key interface{}) interface{} {
// SetData stores data with given key in this context. // SetData stores data with given key in this context.
// This data are only available in this context. // This data are only available in this context.
func (input *BeegoInput) SetData(key, val interface{}) { func (input *BeegoInput) SetData(key, val interface{}) {
input.dataLock.Lock()
defer input.dataLock.Unlock()
if input.data == nil { if input.data == nil {
input.data = make(map[interface{}]interface{}) input.data = make(map[interface{}]interface{})
} }