Skip to content

Commit

Permalink
Merge pull request beego#4066 from playHing/self-dev
Browse files Browse the repository at this point in the history
Fix concurrent issue of context/input Query method
  • Loading branch information
flycash authored Jul 16, 2020
2 parents 1c07144 + 3e2c795 commit 7a48fbb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
8 changes: 7 additions & 1 deletion context/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,14 @@ func (input *BeegoInput) Query(key string) string {
return val
}
if input.Context.Request.Form == nil {
input.Context.Request.ParseForm()
input.dataLock.Lock()
if input.Context.Request.Form == nil {
input.Context.Request.ParseForm()
}
input.dataLock.Unlock()
}
input.dataLock.RLock()
defer input.dataLock.RUnlock()
return input.Context.Request.Form.Get(key)
}

Expand Down
10 changes: 10 additions & 0 deletions context/input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,13 @@ func TestParams(t *testing.T) {
}

}
func BenchmarkQuery(b *testing.B) {
beegoInput := NewInput()
beegoInput.Context = NewContext()
beegoInput.Context.Request, _ = http.NewRequest("POST", "http://www.example.com/?q=foo", nil)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
beegoInput.Query("q")
}
})
}

0 comments on commit 7a48fbb

Please sign in to comment.