From b50fb44950a2687aa55652d0638d74a0e2967a6e Mon Sep 17 00:00:00 2001 From: playHing Date: Sat, 11 Jul 2020 02:00:48 +0800 Subject: [PATCH 1/3] Add bench test on context input query --- context/input_test.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/context/input_test.go b/context/input_test.go index db812a0f..3a6c2e7b 100644 --- a/context/input_test.go +++ b/context/input_test.go @@ -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") + } + }) +} From 55e6298f290345f900bff9443cb8fb9a09e78965 Mon Sep 17 00:00:00 2001 From: playHing Date: Sat, 11 Jul 2020 02:06:09 +0800 Subject: [PATCH 2/3] Fix concurrent form parsing and getting --- context/input.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/context/input.go b/context/input.go index 7b522c36..46a18f03 100644 --- a/context/input.go +++ b/context/input.go @@ -333,7 +333,11 @@ func (input *BeegoInput) Query(key string) string { return val } if input.Context.Request.Form == nil { - input.Context.Request.ParseForm() + input.dataLock.Lock() + defer input.dataLock.Unlock() + if input.Context.Request.Form == nil { + input.Context.Request.ParseForm() + } } return input.Context.Request.Form.Get(key) } From 3e2c795410da9e912f9d4b46bf862a212bec27ca Mon Sep 17 00:00:00 2001 From: playHing Date: Mon, 13 Jul 2020 23:11:23 +0800 Subject: [PATCH 3/3] Rlock for form query --- context/input.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/context/input.go b/context/input.go index 46a18f03..385549c1 100644 --- a/context/input.go +++ b/context/input.go @@ -334,11 +334,13 @@ func (input *BeegoInput) Query(key string) string { } if input.Context.Request.Form == nil { input.dataLock.Lock() - defer input.dataLock.Unlock() 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) }